1 /* $Id: tif_dir.c,v 1.113 2012-06-14 20:32:53 fwarmerdam 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
)
48 _TIFFfree(*vpp
), *vpp
= 0;
50 tmsize_t bytes
= (tmsize_t
)(nmemb
* elem_size
);
51 if (elem_size
&& bytes
/ elem_size
== nmemb
)
52 *vpp
= (void*) _TIFFmalloc(bytes
);
54 _TIFFmemcpy(*vpp
, vp
, bytes
);
57 void _TIFFsetByteArray(void** vpp
, void* vp
, uint32 n
)
58 { setByteArray(vpp
, vp
, n
, 1); }
59 void _TIFFsetString(char** cpp
, char* cp
)
60 { setByteArray((void**) cpp
, (void*) cp
, strlen(cp
)+1, 1); }
61 void _TIFFsetNString(char** cpp
, char* cp
, uint32 n
)
62 { setByteArray((void**) cpp
, (void*) cp
, n
, 1); }
63 void _TIFFsetShortArray(uint16
** wpp
, uint16
* wp
, uint32 n
)
64 { setByteArray((void**) wpp
, (void*) wp
, n
, sizeof (uint16
)); }
65 void _TIFFsetLongArray(uint32
** lpp
, uint32
* lp
, uint32 n
)
66 { setByteArray((void**) lpp
, (void*) lp
, n
, sizeof (uint32
)); }
67 void _TIFFsetLong8Array(uint64
** lpp
, uint64
* lp
, uint32 n
)
68 { setByteArray((void**) lpp
, (void*) lp
, n
, sizeof (uint64
)); }
69 void _TIFFsetFloatArray(float** fpp
, float* fp
, uint32 n
)
70 { setByteArray((void**) fpp
, (void*) fp
, n
, sizeof (float)); }
71 void _TIFFsetDoubleArray(double** dpp
, double* dp
, uint32 n
)
72 { setByteArray((void**) dpp
, (void*) dp
, n
, sizeof (double)); }
75 setDoubleArrayOneValue(double** vpp
, double value
, size_t nmemb
)
79 *vpp
= _TIFFmalloc(nmemb
*sizeof(double));
83 ((double*)*vpp
)[nmemb
] = value
;
88 * Install extra samples information.
91 setExtraSamples(TIFFDirectory
* td
, va_list ap
, uint32
* v
)
93 /* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
94 #define EXTRASAMPLE_COREL_UNASSALPHA 999
99 *v
= (uint16
) va_arg(ap
, uint16_vap
);
100 if ((uint16
) *v
> td
->td_samplesperpixel
)
102 va
= va_arg(ap
, uint16
*);
103 if (*v
> 0 && va
== NULL
) /* typically missing param */
105 for (i
= 0; i
< *v
; i
++) {
106 if (va
[i
] > EXTRASAMPLE_UNASSALPHA
) {
108 * XXX: Corel Draw is known to produce incorrect
109 * ExtraSamples tags which must be patched here if we
110 * want to be able to open some of the damaged TIFF
113 if (va
[i
] == EXTRASAMPLE_COREL_UNASSALPHA
)
114 va
[i
] = EXTRASAMPLE_UNASSALPHA
;
119 td
->td_extrasamples
= (uint16
) *v
;
120 _TIFFsetShortArray(&td
->td_sampleinfo
, va
, td
->td_extrasamples
);
123 #undef EXTRASAMPLE_COREL_UNASSALPHA
127 * Confirm we have "samplesperpixel" ink names separated by \0. Returns
128 * zero if the ink names are not as expected.
131 checkInkNamesString(TIFF
* tif
, uint32 slen
, const char* s
)
133 TIFFDirectory
* td
= &tif
->tif_dir
;
134 uint16 i
= td
->td_samplesperpixel
;
137 const char* ep
= s
+slen
;
140 for (; cp
< ep
&& *cp
!= '\0'; cp
++) {}
145 return ((uint32
)(cp
-s
));
148 TIFFErrorExt(tif
->tif_clientdata
, "TIFFSetField",
149 "%s: Invalid InkNames value; expecting %d names, found %d",
151 td
->td_samplesperpixel
,
152 td
->td_samplesperpixel
-i
);
157 _TIFFVSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
159 static const char module
[] = "_TIFFVSetField";
161 TIFFDirectory
* td
= &tif
->tif_dir
;
165 const TIFFField
*fip
= TIFFFindField(tif
, tag
, TIFF_ANY
);
166 uint32 standard_tag
= tag
;
169 * We want to force the custom code to be used for custom
170 * fields even if the tag happens to match a well known
171 * one - important for reinterpreted handling of standard
172 * tag values in custom directories (ie. EXIF)
174 if (fip
->field_bit
== FIELD_CUSTOM
) {
178 switch (standard_tag
) {
179 case TIFFTAG_SUBFILETYPE
:
180 td
->td_subfiletype
= (uint32
) va_arg(ap
, uint32
);
182 case TIFFTAG_IMAGEWIDTH
:
183 td
->td_imagewidth
= (uint32
) va_arg(ap
, uint32
);
185 case TIFFTAG_IMAGELENGTH
:
186 td
->td_imagelength
= (uint32
) va_arg(ap
, uint32
);
188 case TIFFTAG_BITSPERSAMPLE
:
189 td
->td_bitspersample
= (uint16
) va_arg(ap
, uint16_vap
);
191 * If the data require post-decoding processing to byte-swap
192 * samples, set it up here. Note that since tags are required
193 * to be ordered, compression code can override this behaviour
194 * in the setup method if it wants to roll the post decoding
195 * work in with its normal work.
197 if (tif
->tif_flags
& TIFF_SWAB
) {
198 if (td
->td_bitspersample
== 8)
199 tif
->tif_postdecode
= _TIFFNoPostDecode
;
200 else if (td
->td_bitspersample
== 16)
201 tif
->tif_postdecode
= _TIFFSwab16BitData
;
202 else if (td
->td_bitspersample
== 24)
203 tif
->tif_postdecode
= _TIFFSwab24BitData
;
204 else if (td
->td_bitspersample
== 32)
205 tif
->tif_postdecode
= _TIFFSwab32BitData
;
206 else if (td
->td_bitspersample
== 64)
207 tif
->tif_postdecode
= _TIFFSwab64BitData
;
208 else if (td
->td_bitspersample
== 128) /* two 64's */
209 tif
->tif_postdecode
= _TIFFSwab64BitData
;
212 case TIFFTAG_COMPRESSION
:
213 v
= (uint16
) va_arg(ap
, uint16_vap
);
215 * If we're changing the compression scheme, the notify the
216 * previous module so that it can cleanup any state it's
219 if (TIFFFieldSet(tif
, FIELD_COMPRESSION
)) {
220 if ((uint32
)td
->td_compression
== v
)
222 (*tif
->tif_cleanup
)(tif
);
223 tif
->tif_flags
&= ~TIFF_CODERSETUP
;
226 * Setup new compression routine state.
228 if( (status
= TIFFSetCompressionScheme(tif
, v
)) != 0 )
229 td
->td_compression
= (uint16
) v
;
233 case TIFFTAG_PHOTOMETRIC
:
234 td
->td_photometric
= (uint16
) va_arg(ap
, uint16_vap
);
236 case TIFFTAG_THRESHHOLDING
:
237 td
->td_threshholding
= (uint16
) va_arg(ap
, uint16_vap
);
239 case TIFFTAG_FILLORDER
:
240 v
= (uint16
) va_arg(ap
, uint16_vap
);
241 if (v
!= FILLORDER_LSB2MSB
&& v
!= FILLORDER_MSB2LSB
)
243 td
->td_fillorder
= (uint16
) v
;
245 case TIFFTAG_ORIENTATION
:
246 v
= (uint16
) va_arg(ap
, uint16_vap
);
247 if (v
< ORIENTATION_TOPLEFT
|| ORIENTATION_LEFTBOT
< v
)
250 td
->td_orientation
= (uint16
) v
;
252 case TIFFTAG_SAMPLESPERPIXEL
:
253 v
= (uint16
) va_arg(ap
, uint16_vap
);
256 td
->td_samplesperpixel
= (uint16
) v
;
258 case TIFFTAG_ROWSPERSTRIP
:
259 v32
= (uint32
) va_arg(ap
, uint32
);
262 td
->td_rowsperstrip
= v32
;
263 if (!TIFFFieldSet(tif
, FIELD_TILEDIMENSIONS
)) {
264 td
->td_tilelength
= v32
;
265 td
->td_tilewidth
= td
->td_imagewidth
;
268 case TIFFTAG_MINSAMPLEVALUE
:
269 td
->td_minsamplevalue
= (uint16
) va_arg(ap
, uint16_vap
);
271 case TIFFTAG_MAXSAMPLEVALUE
:
272 td
->td_maxsamplevalue
= (uint16
) va_arg(ap
, uint16_vap
);
274 case TIFFTAG_SMINSAMPLEVALUE
:
275 if (tif
->tif_flags
& TIFF_PERSAMPLE
)
276 _TIFFsetDoubleArray(&td
->td_sminsamplevalue
, va_arg(ap
, double*), td
->td_samplesperpixel
);
278 setDoubleArrayOneValue(&td
->td_sminsamplevalue
, va_arg(ap
, double), td
->td_samplesperpixel
);
280 case TIFFTAG_SMAXSAMPLEVALUE
:
281 if (tif
->tif_flags
& TIFF_PERSAMPLE
)
282 _TIFFsetDoubleArray(&td
->td_smaxsamplevalue
, va_arg(ap
, double*), td
->td_samplesperpixel
);
284 setDoubleArrayOneValue(&td
->td_smaxsamplevalue
, va_arg(ap
, double), td
->td_samplesperpixel
);
286 case TIFFTAG_XRESOLUTION
:
287 td
->td_xresolution
= (float) va_arg(ap
, double);
289 case TIFFTAG_YRESOLUTION
:
290 td
->td_yresolution
= (float) va_arg(ap
, double);
292 case TIFFTAG_PLANARCONFIG
:
293 v
= (uint16
) va_arg(ap
, uint16_vap
);
294 if (v
!= PLANARCONFIG_CONTIG
&& v
!= PLANARCONFIG_SEPARATE
)
296 td
->td_planarconfig
= (uint16
) v
;
298 case TIFFTAG_XPOSITION
:
299 td
->td_xposition
= (float) va_arg(ap
, double);
301 case TIFFTAG_YPOSITION
:
302 td
->td_yposition
= (float) va_arg(ap
, double);
304 case TIFFTAG_RESOLUTIONUNIT
:
305 v
= (uint16
) va_arg(ap
, uint16_vap
);
306 if (v
< RESUNIT_NONE
|| RESUNIT_CENTIMETER
< v
)
308 td
->td_resolutionunit
= (uint16
) v
;
310 case TIFFTAG_PAGENUMBER
:
311 td
->td_pagenumber
[0] = (uint16
) va_arg(ap
, uint16_vap
);
312 td
->td_pagenumber
[1] = (uint16
) va_arg(ap
, uint16_vap
);
314 case TIFFTAG_HALFTONEHINTS
:
315 td
->td_halftonehints
[0] = (uint16
) va_arg(ap
, uint16_vap
);
316 td
->td_halftonehints
[1] = (uint16
) va_arg(ap
, uint16_vap
);
318 case TIFFTAG_COLORMAP
:
319 v32
= (uint32
)(1L<<td
->td_bitspersample
);
320 _TIFFsetShortArray(&td
->td_colormap
[0], va_arg(ap
, uint16
*), v32
);
321 _TIFFsetShortArray(&td
->td_colormap
[1], va_arg(ap
, uint16
*), v32
);
322 _TIFFsetShortArray(&td
->td_colormap
[2], va_arg(ap
, uint16
*), v32
);
324 case TIFFTAG_EXTRASAMPLES
:
325 if (!setExtraSamples(td
, ap
, &v
))
328 case TIFFTAG_MATTEING
:
329 td
->td_extrasamples
= (((uint16
) va_arg(ap
, uint16_vap
)) != 0);
330 if (td
->td_extrasamples
) {
331 uint16 sv
= EXTRASAMPLE_ASSOCALPHA
;
332 _TIFFsetShortArray(&td
->td_sampleinfo
, &sv
, 1);
335 case TIFFTAG_TILEWIDTH
:
336 v32
= (uint32
) va_arg(ap
, uint32
);
338 if (tif
->tif_mode
!= O_RDONLY
)
340 TIFFWarningExt(tif
->tif_clientdata
, tif
->tif_name
,
341 "Nonstandard tile width %d, convert file", v32
);
343 td
->td_tilewidth
= v32
;
344 tif
->tif_flags
|= TIFF_ISTILED
;
346 case TIFFTAG_TILELENGTH
:
347 v32
= (uint32
) va_arg(ap
, uint32
);
349 if (tif
->tif_mode
!= O_RDONLY
)
351 TIFFWarningExt(tif
->tif_clientdata
, tif
->tif_name
,
352 "Nonstandard tile length %d, convert file", v32
);
354 td
->td_tilelength
= v32
;
355 tif
->tif_flags
|= TIFF_ISTILED
;
357 case TIFFTAG_TILEDEPTH
:
358 v32
= (uint32
) va_arg(ap
, uint32
);
361 td
->td_tiledepth
= v32
;
363 case TIFFTAG_DATATYPE
:
364 v
= (uint16
) va_arg(ap
, uint16_vap
);
366 case DATATYPE_VOID
: v
= SAMPLEFORMAT_VOID
; break;
367 case DATATYPE_INT
: v
= SAMPLEFORMAT_INT
; break;
368 case DATATYPE_UINT
: v
= SAMPLEFORMAT_UINT
; break;
369 case DATATYPE_IEEEFP
: v
= SAMPLEFORMAT_IEEEFP
;break;
370 default: goto badvalue
;
372 td
->td_sampleformat
= (uint16
) v
;
374 case TIFFTAG_SAMPLEFORMAT
:
375 v
= (uint16
) va_arg(ap
, uint16_vap
);
376 if (v
< SAMPLEFORMAT_UINT
|| SAMPLEFORMAT_COMPLEXIEEEFP
< v
)
378 td
->td_sampleformat
= (uint16
) v
;
380 /* Try to fix up the SWAB function for complex data. */
381 if( td
->td_sampleformat
== SAMPLEFORMAT_COMPLEXINT
382 && td
->td_bitspersample
== 32
383 && tif
->tif_postdecode
== _TIFFSwab32BitData
)
384 tif
->tif_postdecode
= _TIFFSwab16BitData
;
385 else if( (td
->td_sampleformat
== SAMPLEFORMAT_COMPLEXINT
386 || td
->td_sampleformat
== SAMPLEFORMAT_COMPLEXIEEEFP
)
387 && td
->td_bitspersample
== 64
388 && tif
->tif_postdecode
== _TIFFSwab64BitData
)
389 tif
->tif_postdecode
= _TIFFSwab32BitData
;
391 case TIFFTAG_IMAGEDEPTH
:
392 td
->td_imagedepth
= (uint32
) va_arg(ap
, uint32
);
395 if ((tif
->tif_flags
& TIFF_INSUBIFD
) == 0) {
396 td
->td_nsubifd
= (uint16
) va_arg(ap
, uint16_vap
);
397 _TIFFsetLong8Array(&td
->td_subifd
, (uint64
*) va_arg(ap
, uint64
*),
398 (long) td
->td_nsubifd
);
400 TIFFErrorExt(tif
->tif_clientdata
, module
,
401 "%s: Sorry, cannot nest SubIFDs",
406 case TIFFTAG_YCBCRPOSITIONING
:
407 td
->td_ycbcrpositioning
= (uint16
) va_arg(ap
, uint16_vap
);
409 case TIFFTAG_YCBCRSUBSAMPLING
:
410 td
->td_ycbcrsubsampling
[0] = (uint16
) va_arg(ap
, uint16_vap
);
411 td
->td_ycbcrsubsampling
[1] = (uint16
) va_arg(ap
, uint16_vap
);
413 case TIFFTAG_TRANSFERFUNCTION
:
414 v
= (td
->td_samplesperpixel
- td
->td_extrasamples
) > 1 ? 3 : 1;
415 for (i
= 0; i
< v
; i
++)
416 _TIFFsetShortArray(&td
->td_transferfunction
[i
],
417 va_arg(ap
, uint16
*), 1L<<td
->td_bitspersample
);
419 case TIFFTAG_REFERENCEBLACKWHITE
:
420 /* XXX should check for null range */
421 _TIFFsetFloatArray(&td
->td_refblackwhite
, va_arg(ap
, float*), 6);
423 case TIFFTAG_INKNAMES
:
424 v
= (uint16
) va_arg(ap
, uint16_vap
);
425 s
= va_arg(ap
, char*);
426 v
= checkInkNamesString(tif
, v
, s
);
429 _TIFFsetNString(&td
->td_inknames
, s
, v
);
430 td
->td_inknameslen
= v
;
433 case TIFFTAG_PERSAMPLE
:
434 v
= (uint16
) va_arg(ap
, uint16_vap
);
435 if( v
== PERSAMPLE_MULTI
)
436 tif
->tif_flags
|= TIFF_PERSAMPLE
;
438 tif
->tif_flags
&= ~TIFF_PERSAMPLE
;
442 int tv_size
, iCustom
;
445 * This can happen if multiple images are open with different
446 * codecs which have private tags. The global tag information
447 * table may then have tags that are valid for one file but not
448 * the other. If the client tries to set a tag that is not valid
449 * for the image's codec then we'll arrive here. This
450 * happens, for example, when tiffcp is used to convert between
451 * compression schemes and codec-specific tags are blindly copied.
453 if(fip
== NULL
|| fip
->field_bit
!= FIELD_CUSTOM
) {
454 TIFFErrorExt(tif
->tif_clientdata
, module
,
455 "%s: Invalid %stag \"%s\" (not supported by codec)",
456 tif
->tif_name
, isPseudoTag(tag
) ? "pseudo-" : "",
457 fip
? fip
->field_name
: "Unknown");
463 * Find the existing entry for this custom value.
466 for (iCustom
= 0; iCustom
< td
->td_customValueCount
; iCustom
++) {
467 if (td
->td_customValues
[iCustom
].info
->field_tag
== tag
) {
468 tv
= td
->td_customValues
+ iCustom
;
469 if (tv
->value
!= NULL
) {
470 _TIFFfree(tv
->value
);
478 * Grow the custom list if the entry was not found.
481 TIFFTagValue
*new_customValues
;
483 td
->td_customValueCount
++;
484 new_customValues
= (TIFFTagValue
*)
485 _TIFFrealloc(td
->td_customValues
,
486 sizeof(TIFFTagValue
) * td
->td_customValueCount
);
487 if (!new_customValues
) {
488 TIFFErrorExt(tif
->tif_clientdata
, module
,
489 "%s: Failed to allocate space for list of custom values",
495 td
->td_customValues
= new_customValues
;
497 tv
= td
->td_customValues
+ (td
->td_customValueCount
- 1);
504 * Set custom value ... save a copy of the custom tag value.
506 tv_size
= _TIFFDataSize(fip
->field_type
);
509 TIFFErrorExt(tif
->tif_clientdata
, module
,
510 "%s: Bad field type %d for \"%s\"",
511 tif
->tif_name
, fip
->field_type
,
516 if (fip
->field_type
== TIFF_ASCII
)
520 if (fip
->field_passcount
)
522 assert(fip
->field_writecount
==TIFF_VARIABLE2
);
523 ma
=(uint32
)va_arg(ap
,uint32
);
524 mb
=(char*)va_arg(ap
,char*);
528 mb
=(char*)va_arg(ap
,char*);
529 ma
=(uint32
)(strlen(mb
)+1);
532 setByteArray(&tv
->value
,mb
,ma
,1);
536 if (fip
->field_passcount
) {
537 if (fip
->field_writecount
== TIFF_VARIABLE2
)
538 tv
->count
= (uint32
) va_arg(ap
, uint32
);
540 tv
->count
= (int) va_arg(ap
, int);
541 } else if (fip
->field_writecount
== TIFF_VARIABLE
542 || fip
->field_writecount
== TIFF_VARIABLE2
)
544 else if (fip
->field_writecount
== TIFF_SPP
)
545 tv
->count
= td
->td_samplesperpixel
;
547 tv
->count
= fip
->field_writecount
;
549 if (tv
->count
== 0) {
551 TIFFErrorExt(tif
->tif_clientdata
, module
,
552 "%s: Null count for \"%s\" (type "
553 "%d, writecount %d, passcount %d)",
557 fip
->field_writecount
,
558 fip
->field_passcount
);
562 tv
->value
= _TIFFCheckMalloc(tif
, tv
->count
, tv_size
,
563 "custom tag binary object");
569 if (fip
->field_tag
== TIFFTAG_DOTRANGE
570 && strcmp(fip
->field_name
,"DotRange") == 0) {
571 /* TODO: This is an evil exception and should not have been
572 handled this way ... likely best if we move it into
573 the directory structure with an explicit field in
574 libtiff 4.1 and assign it a FIELD_ value */
576 v
[0] = (uint16
)va_arg(ap
, int);
577 v
[1] = (uint16
)va_arg(ap
, int);
578 _TIFFmemcpy(tv
->value
, &v
, 4);
581 else if (fip
->field_passcount
582 || fip
->field_writecount
== TIFF_VARIABLE
583 || fip
->field_writecount
== TIFF_VARIABLE2
584 || fip
->field_writecount
== TIFF_SPP
586 _TIFFmemcpy(tv
->value
, va_arg(ap
, void *),
587 tv
->count
* tv_size
);
589 char *val
= (char *)tv
->value
;
590 assert( tv
->count
== 1 );
592 switch (fip
->field_type
) {
596 uint8 v
= (uint8
)va_arg(ap
, int);
597 _TIFFmemcpy(val
, &v
, tv_size
);
602 int8 v
= (int8
)va_arg(ap
, int);
603 _TIFFmemcpy(val
, &v
, tv_size
);
608 uint16 v
= (uint16
)va_arg(ap
, int);
609 _TIFFmemcpy(val
, &v
, tv_size
);
614 int16 v
= (int16
)va_arg(ap
, int);
615 _TIFFmemcpy(val
, &v
, tv_size
);
621 uint32 v
= va_arg(ap
, uint32
);
622 _TIFFmemcpy(val
, &v
, tv_size
);
627 int32 v
= va_arg(ap
, int32
);
628 _TIFFmemcpy(val
, &v
, tv_size
);
634 uint64 v
= va_arg(ap
, uint64
);
635 _TIFFmemcpy(val
, &v
, tv_size
);
640 int64 v
= va_arg(ap
, int64
);
641 _TIFFmemcpy(val
, &v
, tv_size
);
648 float v
= (float)va_arg(ap
, double);
649 _TIFFmemcpy(val
, &v
, tv_size
);
654 double v
= va_arg(ap
, double);
655 _TIFFmemcpy(val
, &v
, tv_size
);
659 _TIFFmemset(val
, 0, tv_size
);
668 const TIFFField
* fip
=TIFFFieldWithTag(tif
,tag
);
670 TIFFSetFieldBit(tif
, fip
->field_bit
);
671 tif
->tif_flags
|= TIFF_DIRTYDIRECT
;
679 const TIFFField
* fip
=TIFFFieldWithTag(tif
,tag
);
680 TIFFErrorExt(tif
->tif_clientdata
, module
,
681 "%s: Bad value %u for \"%s\" tag",
683 fip
? fip
->field_name
: "Unknown");
689 const TIFFField
* fip
=TIFFFieldWithTag(tif
,tag
);
690 TIFFErrorExt(tif
->tif_clientdata
, module
,
691 "%s: Bad value %u for \"%s\" tag",
693 fip
? fip
->field_name
: "Unknown");
700 * Return 1/0 according to whether or not
701 * it is permissible to set the tag's value.
702 * Note that we allow ImageLength to be changed
703 * so that we can append and extend to images.
704 * Any other tag may not be altered once writing
705 * has commenced, unless its value has no effect
706 * on the format of the data that is written.
709 OkToChangeTag(TIFF
* tif
, uint32 tag
)
711 const TIFFField
* fip
= TIFFFindField(tif
, tag
, TIFF_ANY
);
712 if (!fip
) { /* unknown tag */
713 TIFFErrorExt(tif
->tif_clientdata
, "TIFFSetField", "%s: Unknown %stag %u",
714 tif
->tif_name
, isPseudoTag(tag
) ? "pseudo-" : "", tag
);
717 if (tag
!= TIFFTAG_IMAGELENGTH
&& (tif
->tif_flags
& TIFF_BEENWRITING
) &&
718 !fip
->field_oktochange
) {
720 * Consult info table to see if tag can be changed
721 * after we've started writing. We only allow changes
722 * to those tags that don't/shouldn't affect the
723 * compression and/or format of the data.
725 TIFFErrorExt(tif
->tif_clientdata
, "TIFFSetField",
726 "%s: Cannot modify tag \"%s\" while writing",
727 tif
->tif_name
, fip
->field_name
);
734 * Record the value of a field in the
735 * internal directory structure. The
736 * field will be written to the file
737 * when/if the directory structure is
741 TIFFSetField(TIFF
* tif
, uint32 tag
, ...)
747 status
= TIFFVSetField(tif
, tag
, ap
);
753 * Clear the contents of the field in the internal structure.
756 TIFFUnsetField(TIFF
* tif
, uint32 tag
)
758 const TIFFField
*fip
= TIFFFieldWithTag(tif
, tag
);
759 TIFFDirectory
* td
= &tif
->tif_dir
;
764 if( fip
->field_bit
!= FIELD_CUSTOM
)
765 TIFFClrFieldBit(tif
, fip
->field_bit
);
768 TIFFTagValue
*tv
= NULL
;
771 for (i
= 0; i
< td
->td_customValueCount
; i
++) {
773 tv
= td
->td_customValues
+ i
;
774 if( tv
->info
->field_tag
== tag
)
778 if( i
< td
->td_customValueCount
)
780 _TIFFfree(tv
->value
);
781 for( ; i
< td
->td_customValueCount
-1; i
++) {
782 td
->td_customValues
[i
] = td
->td_customValues
[i
+1];
784 td
->td_customValueCount
--;
788 tif
->tif_flags
|= TIFF_DIRTYDIRECT
;
794 * Like TIFFSetField, but taking a varargs
795 * parameter list. This routine is useful
796 * for building higher-level interfaces on
797 * top of the library.
800 TIFFVSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
802 return OkToChangeTag(tif
, tag
) ?
803 (*tif
->tif_tagmethods
.vsetfield
)(tif
, tag
, ap
) : 0;
807 _TIFFVGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
809 TIFFDirectory
* td
= &tif
->tif_dir
;
811 uint32 standard_tag
= tag
;
812 const TIFFField
* fip
= TIFFFindField(tif
, tag
, TIFF_ANY
);
815 * We want to force the custom code to be used for custom
816 * fields even if the tag happens to match a well known
817 * one - important for reinterpreted handling of standard
818 * tag values in custom directories (ie. EXIF)
820 if (fip
->field_bit
== FIELD_CUSTOM
) {
824 switch (standard_tag
) {
825 case TIFFTAG_SUBFILETYPE
:
826 *va_arg(ap
, uint32
*) = td
->td_subfiletype
;
828 case TIFFTAG_IMAGEWIDTH
:
829 *va_arg(ap
, uint32
*) = td
->td_imagewidth
;
831 case TIFFTAG_IMAGELENGTH
:
832 *va_arg(ap
, uint32
*) = td
->td_imagelength
;
834 case TIFFTAG_BITSPERSAMPLE
:
835 *va_arg(ap
, uint16
*) = td
->td_bitspersample
;
837 case TIFFTAG_COMPRESSION
:
838 *va_arg(ap
, uint16
*) = td
->td_compression
;
840 case TIFFTAG_PHOTOMETRIC
:
841 *va_arg(ap
, uint16
*) = td
->td_photometric
;
843 case TIFFTAG_THRESHHOLDING
:
844 *va_arg(ap
, uint16
*) = td
->td_threshholding
;
846 case TIFFTAG_FILLORDER
:
847 *va_arg(ap
, uint16
*) = td
->td_fillorder
;
849 case TIFFTAG_ORIENTATION
:
850 *va_arg(ap
, uint16
*) = td
->td_orientation
;
852 case TIFFTAG_SAMPLESPERPIXEL
:
853 *va_arg(ap
, uint16
*) = td
->td_samplesperpixel
;
855 case TIFFTAG_ROWSPERSTRIP
:
856 *va_arg(ap
, uint32
*) = td
->td_rowsperstrip
;
858 case TIFFTAG_MINSAMPLEVALUE
:
859 *va_arg(ap
, uint16
*) = td
->td_minsamplevalue
;
861 case TIFFTAG_MAXSAMPLEVALUE
:
862 *va_arg(ap
, uint16
*) = td
->td_maxsamplevalue
;
864 case TIFFTAG_SMINSAMPLEVALUE
:
865 if (tif
->tif_flags
& TIFF_PERSAMPLE
)
866 *va_arg(ap
, double**) = td
->td_sminsamplevalue
;
869 /* libtiff historially treats this as a single value. */
871 double v
= td
->td_sminsamplevalue
[0];
872 for (i
=1; i
< td
->td_samplesperpixel
; ++i
)
873 if( td
->td_sminsamplevalue
[i
] < v
)
874 v
= td
->td_sminsamplevalue
[i
];
875 *va_arg(ap
, double*) = v
;
878 case TIFFTAG_SMAXSAMPLEVALUE
:
879 if (tif
->tif_flags
& TIFF_PERSAMPLE
)
880 *va_arg(ap
, double**) = td
->td_smaxsamplevalue
;
883 /* libtiff historially treats this as a single value. */
885 double v
= td
->td_smaxsamplevalue
[0];
886 for (i
=1; i
< td
->td_samplesperpixel
; ++i
)
887 if( td
->td_smaxsamplevalue
[i
] > v
)
888 v
= td
->td_smaxsamplevalue
[i
];
889 *va_arg(ap
, double*) = v
;
892 case TIFFTAG_XRESOLUTION
:
893 *va_arg(ap
, float*) = td
->td_xresolution
;
895 case TIFFTAG_YRESOLUTION
:
896 *va_arg(ap
, float*) = td
->td_yresolution
;
898 case TIFFTAG_PLANARCONFIG
:
899 *va_arg(ap
, uint16
*) = td
->td_planarconfig
;
901 case TIFFTAG_XPOSITION
:
902 *va_arg(ap
, float*) = td
->td_xposition
;
904 case TIFFTAG_YPOSITION
:
905 *va_arg(ap
, float*) = td
->td_yposition
;
907 case TIFFTAG_RESOLUTIONUNIT
:
908 *va_arg(ap
, uint16
*) = td
->td_resolutionunit
;
910 case TIFFTAG_PAGENUMBER
:
911 *va_arg(ap
, uint16
*) = td
->td_pagenumber
[0];
912 *va_arg(ap
, uint16
*) = td
->td_pagenumber
[1];
914 case TIFFTAG_HALFTONEHINTS
:
915 *va_arg(ap
, uint16
*) = td
->td_halftonehints
[0];
916 *va_arg(ap
, uint16
*) = td
->td_halftonehints
[1];
918 case TIFFTAG_COLORMAP
:
919 *va_arg(ap
, uint16
**) = td
->td_colormap
[0];
920 *va_arg(ap
, uint16
**) = td
->td_colormap
[1];
921 *va_arg(ap
, uint16
**) = td
->td_colormap
[2];
923 case TIFFTAG_STRIPOFFSETS
:
924 case TIFFTAG_TILEOFFSETS
:
925 _TIFFFillStriles( tif
);
926 *va_arg(ap
, uint64
**) = td
->td_stripoffset
;
928 case TIFFTAG_STRIPBYTECOUNTS
:
929 case TIFFTAG_TILEBYTECOUNTS
:
930 _TIFFFillStriles( tif
);
931 *va_arg(ap
, uint64
**) = td
->td_stripbytecount
;
933 case TIFFTAG_MATTEING
:
934 *va_arg(ap
, uint16
*) =
935 (td
->td_extrasamples
== 1 &&
936 td
->td_sampleinfo
[0] == EXTRASAMPLE_ASSOCALPHA
);
938 case TIFFTAG_EXTRASAMPLES
:
939 *va_arg(ap
, uint16
*) = td
->td_extrasamples
;
940 *va_arg(ap
, uint16
**) = td
->td_sampleinfo
;
942 case TIFFTAG_TILEWIDTH
:
943 *va_arg(ap
, uint32
*) = td
->td_tilewidth
;
945 case TIFFTAG_TILELENGTH
:
946 *va_arg(ap
, uint32
*) = td
->td_tilelength
;
948 case TIFFTAG_TILEDEPTH
:
949 *va_arg(ap
, uint32
*) = td
->td_tiledepth
;
951 case TIFFTAG_DATATYPE
:
952 switch (td
->td_sampleformat
) {
953 case SAMPLEFORMAT_UINT
:
954 *va_arg(ap
, uint16
*) = DATATYPE_UINT
;
956 case SAMPLEFORMAT_INT
:
957 *va_arg(ap
, uint16
*) = DATATYPE_INT
;
959 case SAMPLEFORMAT_IEEEFP
:
960 *va_arg(ap
, uint16
*) = DATATYPE_IEEEFP
;
962 case SAMPLEFORMAT_VOID
:
963 *va_arg(ap
, uint16
*) = DATATYPE_VOID
;
967 case TIFFTAG_SAMPLEFORMAT
:
968 *va_arg(ap
, uint16
*) = td
->td_sampleformat
;
970 case TIFFTAG_IMAGEDEPTH
:
971 *va_arg(ap
, uint32
*) = td
->td_imagedepth
;
974 *va_arg(ap
, uint16
*) = td
->td_nsubifd
;
975 *va_arg(ap
, uint64
**) = td
->td_subifd
;
977 case TIFFTAG_YCBCRPOSITIONING
:
978 *va_arg(ap
, uint16
*) = td
->td_ycbcrpositioning
;
980 case TIFFTAG_YCBCRSUBSAMPLING
:
981 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[0];
982 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[1];
984 case TIFFTAG_TRANSFERFUNCTION
:
985 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[0];
986 if (td
->td_samplesperpixel
- td
->td_extrasamples
> 1) {
987 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[1];
988 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[2];
991 case TIFFTAG_REFERENCEBLACKWHITE
:
992 *va_arg(ap
, float**) = td
->td_refblackwhite
;
994 case TIFFTAG_INKNAMES
:
995 *va_arg(ap
, char**) = td
->td_inknames
;
1002 * This can happen if multiple images are open
1003 * with different codecs which have private
1004 * tags. The global tag information table may
1005 * then have tags that are valid for one file
1006 * but not the other. If the client tries to
1007 * get a tag that is not valid for the image's
1008 * codec then we'll arrive here.
1010 if( fip
== NULL
|| fip
->field_bit
!= FIELD_CUSTOM
)
1012 TIFFErrorExt(tif
->tif_clientdata
, "_TIFFVGetField",
1013 "%s: Invalid %stag \"%s\" "
1014 "(not supported by codec)",
1016 isPseudoTag(tag
) ? "pseudo-" : "",
1017 fip
? fip
->field_name
: "Unknown");
1023 * Do we have a custom value?
1026 for (i
= 0; i
< td
->td_customValueCount
; i
++) {
1027 TIFFTagValue
*tv
= td
->td_customValues
+ i
;
1029 if (tv
->info
->field_tag
!= tag
)
1032 if (fip
->field_passcount
) {
1033 if (fip
->field_readcount
== TIFF_VARIABLE2
)
1034 *va_arg(ap
, uint32
*) = (uint32
)tv
->count
;
1035 else /* Assume TIFF_VARIABLE */
1036 *va_arg(ap
, uint16
*) = (uint16
)tv
->count
;
1037 *va_arg(ap
, void **) = tv
->value
;
1039 } else if (fip
->field_tag
== TIFFTAG_DOTRANGE
1040 && strcmp(fip
->field_name
,"DotRange") == 0) {
1041 /* TODO: This is an evil exception and should not have been
1042 handled this way ... likely best if we move it into
1043 the directory structure with an explicit field in
1044 libtiff 4.1 and assign it a FIELD_ value */
1045 *va_arg(ap
, uint16
*) = ((uint16
*)tv
->value
)[0];
1046 *va_arg(ap
, uint16
*) = ((uint16
*)tv
->value
)[1];
1049 if (fip
->field_type
== TIFF_ASCII
1050 || fip
->field_readcount
== TIFF_VARIABLE
1051 || fip
->field_readcount
== TIFF_VARIABLE2
1052 || fip
->field_readcount
== TIFF_SPP
1054 *va_arg(ap
, void **) = tv
->value
;
1057 char *val
= (char *)tv
->value
;
1058 assert( tv
->count
== 1 );
1059 switch (fip
->field_type
) {
1061 case TIFF_UNDEFINED
:
1062 *va_arg(ap
, uint8
*) =
1067 *va_arg(ap
, int8
*) =
1072 *va_arg(ap
, uint16
*) =
1077 *va_arg(ap
, int16
*) =
1083 *va_arg(ap
, uint32
*) =
1088 *va_arg(ap
, int32
*) =
1094 *va_arg(ap
, uint64
*) =
1099 *va_arg(ap
, int64
*) =
1104 case TIFF_SRATIONAL
:
1106 *va_arg(ap
, float*) =
1111 *va_arg(ap
, double*) =
1129 * Return the value of a field in the
1130 * internal directory structure.
1133 TIFFGetField(TIFF
* tif
, uint32 tag
, ...)
1139 status
= TIFFVGetField(tif
, tag
, ap
);
1145 * Like TIFFGetField, but taking a varargs
1146 * parameter list. This routine is useful
1147 * for building higher-level interfaces on
1148 * top of the library.
1151 TIFFVGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
1153 const TIFFField
* fip
= TIFFFindField(tif
, tag
, TIFF_ANY
);
1154 return (fip
&& (isPseudoTag(tag
) || TIFFFieldSet(tif
, fip
->field_bit
)) ?
1155 (*tif
->tif_tagmethods
.vgetfield
)(tif
, tag
, ap
) : 0);
1158 #define CleanupField(member) { \
1160 _TIFFfree(td->member); \
1166 * Release storage associated with a directory.
1169 TIFFFreeDirectory(TIFF
* tif
)
1171 TIFFDirectory
*td
= &tif
->tif_dir
;
1174 _TIFFmemset(td
->td_fieldsset
, 0, FIELD_SETLONGS
);
1175 CleanupField(td_sminsamplevalue
);
1176 CleanupField(td_smaxsamplevalue
);
1177 CleanupField(td_colormap
[0]);
1178 CleanupField(td_colormap
[1]);
1179 CleanupField(td_colormap
[2]);
1180 CleanupField(td_sampleinfo
);
1181 CleanupField(td_subifd
);
1182 CleanupField(td_inknames
);
1183 CleanupField(td_refblackwhite
);
1184 CleanupField(td_transferfunction
[0]);
1185 CleanupField(td_transferfunction
[1]);
1186 CleanupField(td_transferfunction
[2]);
1187 CleanupField(td_stripoffset
);
1188 CleanupField(td_stripbytecount
);
1189 TIFFClrFieldBit(tif
, FIELD_YCBCRSUBSAMPLING
);
1190 TIFFClrFieldBit(tif
, FIELD_YCBCRPOSITIONING
);
1192 /* Cleanup custom tag values */
1193 for( i
= 0; i
< td
->td_customValueCount
; i
++ ) {
1194 if (td
->td_customValues
[i
].value
)
1195 _TIFFfree(td
->td_customValues
[i
].value
);
1198 td
->td_customValueCount
= 0;
1199 CleanupField(td_customValues
);
1201 #if defined(DEFER_STRILE_LOAD)
1202 _TIFFmemset( &(td
->td_stripoffset_entry
), 0, sizeof(TIFFDirEntry
));
1203 _TIFFmemset( &(td
->td_stripbytecount_entry
), 0, sizeof(TIFFDirEntry
));
1209 * Client Tag extension support (from Niles Ritter).
1211 static TIFFExtendProc _TIFFextender
= (TIFFExtendProc
) NULL
;
1214 TIFFSetTagExtender(TIFFExtendProc extender
)
1216 TIFFExtendProc prev
= _TIFFextender
;
1217 _TIFFextender
= extender
;
1222 * Setup for a new directory. Should we automatically call
1223 * TIFFWriteDirectory() if the current one is dirty?
1225 * The newly created directory will not exist on the file till
1226 * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
1229 TIFFCreateDirectory(TIFF
* tif
)
1231 TIFFDefaultDirectory(tif
);
1232 tif
->tif_diroff
= 0;
1233 tif
->tif_nextdiroff
= 0;
1234 tif
->tif_curoff
= 0;
1235 tif
->tif_row
= (uint32
) -1;
1236 tif
->tif_curstrip
= (uint32
) -1;
1242 TIFFCreateCustomDirectory(TIFF
* tif
, const TIFFFieldArray
* infoarray
)
1244 TIFFDefaultDirectory(tif
);
1247 * Reset the field definitions to match the application provided list.
1248 * Hopefully TIFFDefaultDirectory() won't have done anything irreversable
1249 * based on it's assumption this is an image directory.
1251 _TIFFSetupFields(tif
, infoarray
);
1253 tif
->tif_diroff
= 0;
1254 tif
->tif_nextdiroff
= 0;
1255 tif
->tif_curoff
= 0;
1256 tif
->tif_row
= (uint32
) -1;
1257 tif
->tif_curstrip
= (uint32
) -1;
1263 TIFFCreateEXIFDirectory(TIFF
* tif
)
1265 const TIFFFieldArray
* exifFieldArray
;
1266 exifFieldArray
= _TIFFGetExifFields();
1267 return TIFFCreateCustomDirectory(tif
, exifFieldArray
);
1271 * Setup a default directory structure.
1274 TIFFDefaultDirectory(TIFF
* tif
)
1276 register TIFFDirectory
* td
= &tif
->tif_dir
;
1277 const TIFFFieldArray
* tiffFieldArray
;
1279 tiffFieldArray
= _TIFFGetFields();
1280 _TIFFSetupFields(tif
, tiffFieldArray
);
1282 _TIFFmemset(td
, 0, sizeof (*td
));
1283 td
->td_fillorder
= FILLORDER_MSB2LSB
;
1284 td
->td_bitspersample
= 1;
1285 td
->td_threshholding
= THRESHHOLD_BILEVEL
;
1286 td
->td_orientation
= ORIENTATION_TOPLEFT
;
1287 td
->td_samplesperpixel
= 1;
1288 td
->td_rowsperstrip
= (uint32
) -1;
1289 td
->td_tilewidth
= 0;
1290 td
->td_tilelength
= 0;
1291 td
->td_tiledepth
= 1;
1292 td
->td_stripbytecountsorted
= 1; /* Our own arrays always sorted. */
1293 td
->td_resolutionunit
= RESUNIT_INCH
;
1294 td
->td_sampleformat
= SAMPLEFORMAT_UINT
;
1295 td
->td_imagedepth
= 1;
1296 td
->td_ycbcrsubsampling
[0] = 2;
1297 td
->td_ycbcrsubsampling
[1] = 2;
1298 td
->td_ycbcrpositioning
= YCBCRPOSITION_CENTERED
;
1299 tif
->tif_postdecode
= _TIFFNoPostDecode
;
1300 tif
->tif_foundfield
= NULL
;
1301 tif
->tif_tagmethods
.vsetfield
= _TIFFVSetField
;
1302 tif
->tif_tagmethods
.vgetfield
= _TIFFVGetField
;
1303 tif
->tif_tagmethods
.printdir
= NULL
;
1305 * Give client code a chance to install their own
1306 * tag extensions & methods, prior to compression overloads.
1309 (*_TIFFextender
)(tif
);
1310 (void) TIFFSetField(tif
, TIFFTAG_COMPRESSION
, COMPRESSION_NONE
);
1312 * NB: The directory is marked dirty as a result of setting
1313 * up the default compression scheme. However, this really
1314 * isn't correct -- we want TIFF_DIRTYDIRECT to be set only
1315 * if the user does something. We could just do the setup
1316 * by hand, but it seems better to use the normal mechanism
1317 * (i.e. TIFFSetField).
1319 tif
->tif_flags
&= ~TIFF_DIRTYDIRECT
;
1322 * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
1323 * we clear the ISTILED flag when setting up a new directory.
1324 * Should we also be clearing stuff like INSUBIFD?
1326 tif
->tif_flags
&= ~TIFF_ISTILED
;
1332 TIFFAdvanceDirectory(TIFF
* tif
, uint64
* nextdir
, uint64
* off
)
1334 static const char module
[] = "TIFFAdvanceDirectory";
1337 uint64 poff
=*nextdir
;
1338 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1340 tmsize_t poffa
,poffb
,poffc
,poffd
;
1343 poffa
=(tmsize_t
)poff
;
1344 poffb
=poffa
+sizeof(uint16
);
1345 if (((uint64
)poffa
!=poff
)||(poffb
<poffa
)||(poffb
<(tmsize_t
)sizeof(uint16
))||(poffb
>tif
->tif_size
))
1347 TIFFErrorExt(tif
->tif_clientdata
,module
,"Error fetching directory count");
1350 _TIFFmemcpy(&dircount
,tif
->tif_base
+poffa
,sizeof(uint16
));
1351 if (tif
->tif_flags
&TIFF_SWAB
)
1352 TIFFSwabShort(&dircount
);
1353 poffc
=poffb
+dircount
*12;
1354 poffd
=poffc
+sizeof(uint32
);
1355 if ((poffc
<poffb
)||(poffc
<dircount
*12)||(poffd
<poffc
)||(poffd
<(tmsize_t
)sizeof(uint32
))||(poffd
>tif
->tif_size
))
1357 TIFFErrorExt(tif
->tif_clientdata
,module
,"Error fetching directory link");
1362 _TIFFmemcpy(&nextdir32
,tif
->tif_base
+poffc
,sizeof(uint32
));
1363 if (tif
->tif_flags
&TIFF_SWAB
)
1364 TIFFSwabLong(&nextdir32
);
1369 tmsize_t poffa
,poffb
,poffc
,poffd
;
1372 poffa
=(tmsize_t
)poff
;
1373 poffb
=poffa
+sizeof(uint64
);
1374 if (((uint64
)poffa
!=poff
)||(poffb
<poffa
)||(poffb
<(tmsize_t
)sizeof(uint64
))||(poffb
>tif
->tif_size
))
1376 TIFFErrorExt(tif
->tif_clientdata
,module
,"Error fetching directory count");
1379 _TIFFmemcpy(&dircount64
,tif
->tif_base
+poffa
,sizeof(uint64
));
1380 if (tif
->tif_flags
&TIFF_SWAB
)
1381 TIFFSwabLong8(&dircount64
);
1382 if (dircount64
>0xFFFF)
1384 TIFFErrorExt(tif
->tif_clientdata
,module
,"Sanity check on directory count failed");
1387 dircount16
=(uint16
)dircount64
;
1388 poffc
=poffb
+dircount16
*20;
1389 poffd
=poffc
+sizeof(uint64
);
1390 if ((poffc
<poffb
)||(poffc
<dircount16
*20)||(poffd
<poffc
)||(poffd
<(tmsize_t
)sizeof(uint64
))||(poffd
>tif
->tif_size
))
1392 TIFFErrorExt(tif
->tif_clientdata
,module
,"Error fetching directory link");
1397 _TIFFmemcpy(nextdir
,tif
->tif_base
+poffc
,sizeof(uint64
));
1398 if (tif
->tif_flags
&TIFF_SWAB
)
1399 TIFFSwabLong8(nextdir
);
1405 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1409 if (!SeekOK(tif
, *nextdir
) ||
1410 !ReadOK(tif
, &dircount
, sizeof (uint16
))) {
1411 TIFFErrorExt(tif
->tif_clientdata
, module
, "%s: Error fetching directory count",
1415 if (tif
->tif_flags
& TIFF_SWAB
)
1416 TIFFSwabShort(&dircount
);
1418 *off
= TIFFSeekFile(tif
,
1419 dircount
*12, SEEK_CUR
);
1421 (void) TIFFSeekFile(tif
,
1422 dircount
*12, SEEK_CUR
);
1423 if (!ReadOK(tif
, &nextdir32
, sizeof (uint32
))) {
1424 TIFFErrorExt(tif
->tif_clientdata
, module
, "%s: Error fetching directory link",
1428 if (tif
->tif_flags
& TIFF_SWAB
)
1429 TIFFSwabLong(&nextdir32
);
1436 if (!SeekOK(tif
, *nextdir
) ||
1437 !ReadOK(tif
, &dircount64
, sizeof (uint64
))) {
1438 TIFFErrorExt(tif
->tif_clientdata
, module
, "%s: Error fetching directory count",
1442 if (tif
->tif_flags
& TIFF_SWAB
)
1443 TIFFSwabLong8(&dircount64
);
1444 if (dircount64
>0xFFFF)
1446 TIFFErrorExt(tif
->tif_clientdata
, module
, "Error fetching directory count");
1449 dircount16
= (uint16
)dircount64
;
1451 *off
= TIFFSeekFile(tif
,
1452 dircount16
*20, SEEK_CUR
);
1454 (void) TIFFSeekFile(tif
,
1455 dircount16
*20, SEEK_CUR
);
1456 if (!ReadOK(tif
, nextdir
, sizeof (uint64
))) {
1457 TIFFErrorExt(tif
->tif_clientdata
, module
, "%s: Error fetching directory link",
1461 if (tif
->tif_flags
& TIFF_SWAB
)
1462 TIFFSwabLong8(nextdir
);
1469 * Count the number of directories in a file.
1472 TIFFNumberOfDirectories(TIFF
* tif
)
1476 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1477 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
1479 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
1481 while (nextdir
!= 0 && TIFFAdvanceDirectory(tif
, &nextdir
, NULL
))
1487 * Set the n-th directory as the current directory.
1488 * NB: Directories are numbered starting at 0.
1491 TIFFSetDirectory(TIFF
* tif
, uint16 dirn
)
1496 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1497 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
1499 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
1500 for (n
= dirn
; n
> 0 && nextdir
!= 0; n
--)
1501 if (!TIFFAdvanceDirectory(tif
, &nextdir
, NULL
))
1503 tif
->tif_nextdiroff
= nextdir
;
1505 * Set curdir to the actual directory index. The
1506 * -1 is because TIFFReadDirectory will increment
1507 * tif_curdir after successfully reading the directory.
1509 tif
->tif_curdir
= (dirn
- n
) - 1;
1511 * Reset tif_dirnumber counter and start new list of seen directories.
1512 * We need this to prevent IFD loops.
1514 tif
->tif_dirnumber
= 0;
1515 return (TIFFReadDirectory(tif
));
1519 * Set the current directory to be the directory
1520 * located at the specified file offset. This interface
1521 * is used mainly to access directories linked with
1522 * the SubIFD tag (e.g. thumbnail images).
1525 TIFFSetSubDirectory(TIFF
* tif
, uint64 diroff
)
1527 tif
->tif_nextdiroff
= diroff
;
1529 * Reset tif_dirnumber counter and start new list of seen directories.
1530 * We need this to prevent IFD loops.
1532 tif
->tif_dirnumber
= 0;
1533 return (TIFFReadDirectory(tif
));
1537 * Return file offset of the current directory.
1540 TIFFCurrentDirOffset(TIFF
* tif
)
1542 return (tif
->tif_diroff
);
1546 * Return an indication of whether or not we are
1547 * at the last directory in the file.
1550 TIFFLastDirectory(TIFF
* tif
)
1552 return (tif
->tif_nextdiroff
== 0);
1556 * Unlink the specified directory from the directory chain.
1559 TIFFUnlinkDirectory(TIFF
* tif
, uint16 dirn
)
1561 static const char module
[] = "TIFFUnlinkDirectory";
1566 if (tif
->tif_mode
== O_RDONLY
) {
1567 TIFFErrorExt(tif
->tif_clientdata
, module
,
1568 "Can not unlink directory in read-only file");
1572 * Go to the directory before the one we want
1573 * to unlink and nab the offset of the link
1574 * field we'll need to patch.
1576 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1578 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
1583 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
1586 for (n
= dirn
-1; n
> 0; n
--) {
1588 TIFFErrorExt(tif
->tif_clientdata
, module
, "Directory %d does not exist", dirn
);
1591 if (!TIFFAdvanceDirectory(tif
, &nextdir
, &off
))
1595 * Advance to the directory to be unlinked and fetch
1596 * the offset of the directory that follows.
1598 if (!TIFFAdvanceDirectory(tif
, &nextdir
, NULL
))
1601 * Go back and patch the link field of the preceding
1602 * directory to point to the offset of the directory
1605 (void) TIFFSeekFile(tif
, off
, SEEK_SET
);
1606 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1609 nextdir32
=(uint32
)nextdir
;
1610 assert((uint64
)nextdir32
==nextdir
);
1611 if (tif
->tif_flags
& TIFF_SWAB
)
1612 TIFFSwabLong(&nextdir32
);
1613 if (!WriteOK(tif
, &nextdir32
, sizeof (uint32
))) {
1614 TIFFErrorExt(tif
->tif_clientdata
, module
, "Error writing directory link");
1620 if (tif
->tif_flags
& TIFF_SWAB
)
1621 TIFFSwabLong8(&nextdir
);
1622 if (!WriteOK(tif
, &nextdir
, sizeof (uint64
))) {
1623 TIFFErrorExt(tif
->tif_clientdata
, module
, "Error writing directory link");
1628 * Leave directory state setup safely. We don't have
1629 * facilities for doing inserting and removing directories,
1630 * so it's safest to just invalidate everything. This
1631 * means that the caller can only append to the directory
1634 (*tif
->tif_cleanup
)(tif
);
1635 if ((tif
->tif_flags
& TIFF_MYBUFFER
) && tif
->tif_rawdata
) {
1636 _TIFFfree(tif
->tif_rawdata
);
1637 tif
->tif_rawdata
= NULL
;
1639 tif
->tif_rawdataoff
= 0;
1640 tif
->tif_rawdataloaded
= 0;
1642 tif
->tif_flags
&= ~(TIFF_BEENWRITING
|TIFF_BUFFERSETUP
|TIFF_POSTENCODE
|TIFF_BUF4WRITE
);
1643 TIFFFreeDirectory(tif
);
1644 TIFFDefaultDirectory(tif
);
1645 tif
->tif_diroff
= 0; /* force link on next write */
1646 tif
->tif_nextdiroff
= 0; /* next write must be at end */
1647 tif
->tif_curoff
= 0;
1648 tif
->tif_row
= (uint32
) -1;
1649 tif
->tif_curstrip
= (uint32
) -1;
1653 /* vim: set ts=8 sts=8 sw=8 noet: */