4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
30 * Directory Tag Get & Set Routines.
31 * (and also some miscellaneous stuff)
36 * These are used in the backwards compatibility code...
38 #define DATATYPE_VOID 0 /* !untyped data */
39 #define DATATYPE_INT 1 /* !signed integer data */
40 #define DATATYPE_UINT 2 /* !unsigned integer data */
41 #define DATATYPE_IEEEFP 3 /* !IEEE floating point data */
44 setByteArray(void** vpp
, void* vp
, size_t nmemb
, size_t elem_size
)
47 _TIFFfree(*vpp
), *vpp
= 0;
49 tmsize_t bytes
= (tmsize_t
)(nmemb
* elem_size
);
50 if (elem_size
&& bytes
/ elem_size
== nmemb
)
51 *vpp
= (void*) _TIFFmalloc(bytes
);
53 _TIFFmemcpy(*vpp
, vp
, bytes
);
56 void _TIFFsetByteArray(void** vpp
, void* vp
, uint32 n
)
57 { setByteArray(vpp
, vp
, n
, 1); }
58 void _TIFFsetString(char** cpp
, char* cp
)
59 { setByteArray((void**) cpp
, (void*) cp
, strlen(cp
)+1, 1); }
60 void _TIFFsetNString(char** cpp
, char* cp
, uint32 n
)
61 { setByteArray((void**) cpp
, (void*) cp
, n
, 1); }
62 void _TIFFsetShortArray(uint16
** wpp
, uint16
* wp
, uint32 n
)
63 { setByteArray((void**) wpp
, (void*) wp
, n
, sizeof (uint16
)); }
64 void _TIFFsetLongArray(uint32
** lpp
, uint32
* lp
, uint32 n
)
65 { setByteArray((void**) lpp
, (void*) lp
, n
, sizeof (uint32
)); }
66 void _TIFFsetLong8Array(uint64
** lpp
, uint64
* lp
, uint32 n
)
67 { setByteArray((void**) lpp
, (void*) lp
, n
, sizeof (uint64
)); }
68 void _TIFFsetFloatArray(float** fpp
, float* fp
, uint32 n
)
69 { setByteArray((void**) fpp
, (void*) fp
, n
, sizeof (float)); }
70 void _TIFFsetDoubleArray(double** dpp
, double* dp
, uint32 n
)
71 { setByteArray((void**) dpp
, (void*) dp
, n
, sizeof (double)); }
74 setDoubleArrayOneValue(double** vpp
, double value
, size_t nmemb
)
78 *vpp
= _TIFFmalloc(nmemb
*sizeof(double));
82 ((double*)*vpp
)[nmemb
] = value
;
87 * Install extra samples information.
90 setExtraSamples(TIFFDirectory
* td
, va_list ap
, uint32
* v
)
92 /* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
93 #define EXTRASAMPLE_COREL_UNASSALPHA 999
98 *v
= (uint16
) va_arg(ap
, uint16_vap
);
99 if ((uint16
) *v
> td
->td_samplesperpixel
)
101 va
= va_arg(ap
, uint16
*);
102 if (*v
> 0 && va
== NULL
) /* typically missing param */
104 for (i
= 0; i
< *v
; i
++) {
105 if (va
[i
] > EXTRASAMPLE_UNASSALPHA
) {
107 * XXX: Corel Draw is known to produce incorrect
108 * ExtraSamples tags which must be patched here if we
109 * want to be able to open some of the damaged TIFF
112 if (va
[i
] == EXTRASAMPLE_COREL_UNASSALPHA
)
113 va
[i
] = EXTRASAMPLE_UNASSALPHA
;
118 td
->td_extrasamples
= (uint16
) *v
;
119 _TIFFsetShortArray(&td
->td_sampleinfo
, va
, td
->td_extrasamples
);
122 #undef EXTRASAMPLE_COREL_UNASSALPHA
126 * Confirm we have "samplesperpixel" ink names separated by \0. Returns
127 * zero if the ink names are not as expected.
130 checkInkNamesString(TIFF
* tif
, uint32 slen
, const char* s
)
132 TIFFDirectory
* td
= &tif
->tif_dir
;
133 uint16 i
= td
->td_samplesperpixel
;
136 const char* ep
= s
+slen
;
139 for (; cp
< ep
&& *cp
!= '\0'; cp
++) {}
144 return ((uint32
)(cp
-s
));
147 TIFFErrorExt(tif
->tif_clientdata
, "TIFFSetField",
148 "%s: Invalid InkNames value; expecting %d names, found %d",
150 td
->td_samplesperpixel
,
151 td
->td_samplesperpixel
-i
);
156 _TIFFVSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
158 static const char module[] = "_TIFFVSetField";
160 TIFFDirectory
* td
= &tif
->tif_dir
;
164 const TIFFField
*fip
= TIFFFindField(tif
, tag
, TIFF_ANY
);
165 uint32 standard_tag
= tag
;
168 * We want to force the custom code to be used for custom
169 * fields even if the tag happens to match a well known
170 * one - important for reinterpreted handling of standard
171 * tag values in custom directories (ie. EXIF)
173 if (fip
->field_bit
== FIELD_CUSTOM
) {
177 switch (standard_tag
) {
178 case TIFFTAG_SUBFILETYPE
:
179 td
->td_subfiletype
= (uint32
) va_arg(ap
, uint32
);
181 case TIFFTAG_IMAGEWIDTH
:
182 td
->td_imagewidth
= (uint32
) va_arg(ap
, uint32
);
184 case TIFFTAG_IMAGELENGTH
:
185 td
->td_imagelength
= (uint32
) va_arg(ap
, uint32
);
187 case TIFFTAG_BITSPERSAMPLE
:
188 td
->td_bitspersample
= (uint16
) va_arg(ap
, uint16_vap
);
190 * If the data require post-decoding processing to byte-swap
191 * samples, set it up here. Note that since tags are required
192 * to be ordered, compression code can override this behaviour
193 * in the setup method if it wants to roll the post decoding
194 * work in with its normal work.
196 if (tif
->tif_flags
& TIFF_SWAB
) {
197 if (td
->td_bitspersample
== 8)
198 tif
->tif_postdecode
= _TIFFNoPostDecode
;
199 else if (td
->td_bitspersample
== 16)
200 tif
->tif_postdecode
= _TIFFSwab16BitData
;
201 else if (td
->td_bitspersample
== 24)
202 tif
->tif_postdecode
= _TIFFSwab24BitData
;
203 else if (td
->td_bitspersample
== 32)
204 tif
->tif_postdecode
= _TIFFSwab32BitData
;
205 else if (td
->td_bitspersample
== 64)
206 tif
->tif_postdecode
= _TIFFSwab64BitData
;
207 else if (td
->td_bitspersample
== 128) /* two 64's */
208 tif
->tif_postdecode
= _TIFFSwab64BitData
;
211 case TIFFTAG_COMPRESSION
:
212 v
= (uint16
) va_arg(ap
, uint16_vap
);
214 * If we're changing the compression scheme, the notify the
215 * previous module so that it can cleanup any state it's
218 if (TIFFFieldSet(tif
, FIELD_COMPRESSION
)) {
219 if ((uint32
)td
->td_compression
== v
)
221 (*tif
->tif_cleanup
)(tif
);
222 tif
->tif_flags
&= ~TIFF_CODERSETUP
;
225 * Setup new compression routine state.
227 if( (status
= TIFFSetCompressionScheme(tif
, v
)) != 0 )
228 td
->td_compression
= (uint16
) v
;
232 case TIFFTAG_PHOTOMETRIC
:
233 td
->td_photometric
= (uint16
) va_arg(ap
, uint16_vap
);
235 case TIFFTAG_THRESHHOLDING
:
236 td
->td_threshholding
= (uint16
) va_arg(ap
, uint16_vap
);
238 case TIFFTAG_FILLORDER
:
239 v
= (uint16
) va_arg(ap
, uint16_vap
);
240 if (v
!= FILLORDER_LSB2MSB
&& v
!= FILLORDER_MSB2LSB
)
242 td
->td_fillorder
= (uint16
) v
;
244 case TIFFTAG_ORIENTATION
:
245 v
= (uint16
) va_arg(ap
, uint16_vap
);
246 if (v
< ORIENTATION_TOPLEFT
|| ORIENTATION_LEFTBOT
< v
)
249 td
->td_orientation
= (uint16
) v
;
251 case TIFFTAG_SAMPLESPERPIXEL
:
252 v
= (uint16
) va_arg(ap
, uint16_vap
);
255 td
->td_samplesperpixel
= (uint16
) v
;
257 case TIFFTAG_ROWSPERSTRIP
:
258 v32
= (uint32
) va_arg(ap
, uint32
);
261 td
->td_rowsperstrip
= v32
;
262 if (!TIFFFieldSet(tif
, FIELD_TILEDIMENSIONS
)) {
263 td
->td_tilelength
= v32
;
264 td
->td_tilewidth
= td
->td_imagewidth
;
267 case TIFFTAG_MINSAMPLEVALUE
:
268 td
->td_minsamplevalue
= (uint16
) va_arg(ap
, uint16_vap
);
270 case TIFFTAG_MAXSAMPLEVALUE
:
271 td
->td_maxsamplevalue
= (uint16
) va_arg(ap
, uint16_vap
);
273 case TIFFTAG_SMINSAMPLEVALUE
:
274 if (tif
->tif_flags
& TIFF_PERSAMPLE
)
275 _TIFFsetDoubleArray(&td
->td_sminsamplevalue
, va_arg(ap
, double*), td
->td_samplesperpixel
);
277 setDoubleArrayOneValue(&td
->td_sminsamplevalue
, va_arg(ap
, double), td
->td_samplesperpixel
);
279 case TIFFTAG_SMAXSAMPLEVALUE
:
280 if (tif
->tif_flags
& TIFF_PERSAMPLE
)
281 _TIFFsetDoubleArray(&td
->td_smaxsamplevalue
, va_arg(ap
, double*), td
->td_samplesperpixel
);
283 setDoubleArrayOneValue(&td
->td_smaxsamplevalue
, va_arg(ap
, double), td
->td_samplesperpixel
);
285 case TIFFTAG_XRESOLUTION
:
286 td
->td_xresolution
= (float) va_arg(ap
, double);
288 case TIFFTAG_YRESOLUTION
:
289 td
->td_yresolution
= (float) va_arg(ap
, double);
291 case TIFFTAG_PLANARCONFIG
:
292 v
= (uint16
) va_arg(ap
, uint16_vap
);
293 if (v
!= PLANARCONFIG_CONTIG
&& v
!= PLANARCONFIG_SEPARATE
)
295 td
->td_planarconfig
= (uint16
) v
;
297 case TIFFTAG_XPOSITION
:
298 td
->td_xposition
= (float) va_arg(ap
, double);
300 case TIFFTAG_YPOSITION
:
301 td
->td_yposition
= (float) va_arg(ap
, double);
303 case TIFFTAG_RESOLUTIONUNIT
:
304 v
= (uint16
) va_arg(ap
, uint16_vap
);
305 if (v
< RESUNIT_NONE
|| RESUNIT_CENTIMETER
< v
)
307 td
->td_resolutionunit
= (uint16
) v
;
309 case TIFFTAG_PAGENUMBER
:
310 td
->td_pagenumber
[0] = (uint16
) va_arg(ap
, uint16_vap
);
311 td
->td_pagenumber
[1] = (uint16
) va_arg(ap
, uint16_vap
);
313 case TIFFTAG_HALFTONEHINTS
:
314 td
->td_halftonehints
[0] = (uint16
) va_arg(ap
, uint16_vap
);
315 td
->td_halftonehints
[1] = (uint16
) va_arg(ap
, uint16_vap
);
317 case TIFFTAG_COLORMAP
:
318 v32
= (uint32
)(1L<<td
->td_bitspersample
);
319 _TIFFsetShortArray(&td
->td_colormap
[0], va_arg(ap
, uint16
*), v32
);
320 _TIFFsetShortArray(&td
->td_colormap
[1], va_arg(ap
, uint16
*), v32
);
321 _TIFFsetShortArray(&td
->td_colormap
[2], va_arg(ap
, uint16
*), v32
);
323 case TIFFTAG_EXTRASAMPLES
:
324 if (!setExtraSamples(td
, ap
, &v
))
327 case TIFFTAG_MATTEING
:
328 td
->td_extrasamples
= (((uint16
) va_arg(ap
, uint16_vap
)) != 0);
329 if (td
->td_extrasamples
) {
330 uint16 sv
= EXTRASAMPLE_ASSOCALPHA
;
331 _TIFFsetShortArray(&td
->td_sampleinfo
, &sv
, 1);
334 case TIFFTAG_TILEWIDTH
:
335 v32
= (uint32
) va_arg(ap
, uint32
);
337 if (tif
->tif_mode
!= O_RDONLY
)
339 TIFFWarningExt(tif
->tif_clientdata
, tif
->tif_name
,
340 "Nonstandard tile width %d, convert file", v32
);
342 td
->td_tilewidth
= v32
;
343 tif
->tif_flags
|= TIFF_ISTILED
;
345 case TIFFTAG_TILELENGTH
:
346 v32
= (uint32
) va_arg(ap
, uint32
);
348 if (tif
->tif_mode
!= O_RDONLY
)
350 TIFFWarningExt(tif
->tif_clientdata
, tif
->tif_name
,
351 "Nonstandard tile length %d, convert file", v32
);
353 td
->td_tilelength
= v32
;
354 tif
->tif_flags
|= TIFF_ISTILED
;
356 case TIFFTAG_TILEDEPTH
:
357 v32
= (uint32
) va_arg(ap
, uint32
);
360 td
->td_tiledepth
= v32
;
362 case TIFFTAG_DATATYPE
:
363 v
= (uint16
) va_arg(ap
, uint16_vap
);
365 case DATATYPE_VOID
: v
= SAMPLEFORMAT_VOID
; break;
366 case DATATYPE_INT
: v
= SAMPLEFORMAT_INT
; break;
367 case DATATYPE_UINT
: v
= SAMPLEFORMAT_UINT
; break;
368 case DATATYPE_IEEEFP
: v
= SAMPLEFORMAT_IEEEFP
;break;
369 default: goto badvalue
;
371 td
->td_sampleformat
= (uint16
) v
;
373 case TIFFTAG_SAMPLEFORMAT
:
374 v
= (uint16
) va_arg(ap
, uint16_vap
);
375 if (v
< SAMPLEFORMAT_UINT
|| SAMPLEFORMAT_COMPLEXIEEEFP
< v
)
377 td
->td_sampleformat
= (uint16
) v
;
379 /* Try to fix up the SWAB function for complex data. */
380 if( td
->td_sampleformat
== SAMPLEFORMAT_COMPLEXINT
381 && td
->td_bitspersample
== 32
382 && tif
->tif_postdecode
== _TIFFSwab32BitData
)
383 tif
->tif_postdecode
= _TIFFSwab16BitData
;
384 else if( (td
->td_sampleformat
== SAMPLEFORMAT_COMPLEXINT
385 || td
->td_sampleformat
== SAMPLEFORMAT_COMPLEXIEEEFP
)
386 && td
->td_bitspersample
== 64
387 && tif
->tif_postdecode
== _TIFFSwab64BitData
)
388 tif
->tif_postdecode
= _TIFFSwab32BitData
;
390 case TIFFTAG_IMAGEDEPTH
:
391 td
->td_imagedepth
= (uint32
) va_arg(ap
, uint32
);
394 if ((tif
->tif_flags
& TIFF_INSUBIFD
) == 0) {
395 td
->td_nsubifd
= (uint16
) va_arg(ap
, uint16_vap
);
396 _TIFFsetLong8Array(&td
->td_subifd
, (uint64
*) va_arg(ap
, uint64
*),
397 (long) td
->td_nsubifd
);
399 TIFFErrorExt(tif
->tif_clientdata
, module,
400 "%s: Sorry, cannot nest SubIFDs",
405 case TIFFTAG_YCBCRPOSITIONING
:
406 td
->td_ycbcrpositioning
= (uint16
) va_arg(ap
, uint16_vap
);
408 case TIFFTAG_YCBCRSUBSAMPLING
:
409 td
->td_ycbcrsubsampling
[0] = (uint16
) va_arg(ap
, uint16_vap
);
410 td
->td_ycbcrsubsampling
[1] = (uint16
) va_arg(ap
, uint16_vap
);
412 case TIFFTAG_TRANSFERFUNCTION
:
413 v
= (td
->td_samplesperpixel
- td
->td_extrasamples
) > 1 ? 3 : 1;
414 for (i
= 0; i
< v
; i
++)
415 _TIFFsetShortArray(&td
->td_transferfunction
[i
],
416 va_arg(ap
, uint16
*), 1L<<td
->td_bitspersample
);
418 case TIFFTAG_REFERENCEBLACKWHITE
:
419 /* XXX should check for null range */
420 _TIFFsetFloatArray(&td
->td_refblackwhite
, va_arg(ap
, float*), 6);
422 case TIFFTAG_INKNAMES
:
423 v
= (uint16
) va_arg(ap
, uint16_vap
);
424 s
= va_arg(ap
, char*);
425 v
= checkInkNamesString(tif
, v
, s
);
428 _TIFFsetNString(&td
->td_inknames
, s
, v
);
429 td
->td_inknameslen
= v
;
432 case TIFFTAG_PERSAMPLE
:
433 v
= (uint16
) va_arg(ap
, uint16_vap
);
434 if( v
== PERSAMPLE_MULTI
)
435 tif
->tif_flags
|= TIFF_PERSAMPLE
;
437 tif
->tif_flags
&= ~TIFF_PERSAMPLE
;
441 int tv_size
, iCustom
;
444 * This can happen if multiple images are open with different
445 * codecs which have private tags. The global tag information
446 * table may then have tags that are valid for one file but not
447 * the other. If the client tries to set a tag that is not valid
448 * for the image's codec then we'll arrive here. This
449 * happens, for example, when tiffcp is used to convert between
450 * compression schemes and codec-specific tags are blindly copied.
452 if(fip
== NULL
|| fip
->field_bit
!= FIELD_CUSTOM
) {
453 TIFFErrorExt(tif
->tif_clientdata
, module,
454 "%s: Invalid %stag \"%s\" (not supported by codec)",
455 tif
->tif_name
, isPseudoTag(tag
) ? "pseudo-" : "",
456 fip
? fip
->field_name
: "Unknown");
462 * Find the existing entry for this custom value.
465 for (iCustom
= 0; iCustom
< td
->td_customValueCount
; iCustom
++) {
466 if (td
->td_customValues
[iCustom
].info
->field_tag
== tag
) {
467 tv
= td
->td_customValues
+ iCustom
;
468 if (tv
->value
!= NULL
) {
469 _TIFFfree(tv
->value
);
477 * Grow the custom list if the entry was not found.
480 TIFFTagValue
*new_customValues
;
482 td
->td_customValueCount
++;
483 new_customValues
= (TIFFTagValue
*)
484 _TIFFrealloc(td
->td_customValues
,
485 sizeof(TIFFTagValue
) * td
->td_customValueCount
);
486 if (!new_customValues
) {
487 TIFFErrorExt(tif
->tif_clientdata
, module,
488 "%s: Failed to allocate space for list of custom values",
494 td
->td_customValues
= new_customValues
;
496 tv
= td
->td_customValues
+ (td
->td_customValueCount
- 1);
503 * Set custom value ... save a copy of the custom tag value.
505 tv_size
= _TIFFDataSize(fip
->field_type
);
508 TIFFErrorExt(tif
->tif_clientdata
, module,
509 "%s: Bad field type %d for \"%s\"",
510 tif
->tif_name
, fip
->field_type
,
515 if (fip
->field_type
== TIFF_ASCII
)
519 if (fip
->field_passcount
)
521 assert(fip
->field_writecount
==TIFF_VARIABLE2
);
522 ma
=(uint32
)va_arg(ap
,uint32
);
523 mb
=(char*)va_arg(ap
,char*);
527 mb
=(char*)va_arg(ap
,char*);
528 ma
=(uint32
)(strlen(mb
)+1);
531 setByteArray(&tv
->value
,mb
,ma
,1);
535 if (fip
->field_passcount
) {
536 if (fip
->field_writecount
== TIFF_VARIABLE2
)
537 tv
->count
= (uint32
) va_arg(ap
, uint32
);
539 tv
->count
= (int) va_arg(ap
, int);
540 } else if (fip
->field_writecount
== TIFF_VARIABLE
541 || fip
->field_writecount
== TIFF_VARIABLE2
)
543 else if (fip
->field_writecount
== TIFF_SPP
)
544 tv
->count
= td
->td_samplesperpixel
;
546 tv
->count
= fip
->field_writecount
;
548 if (tv
->count
== 0) {
550 TIFFErrorExt(tif
->tif_clientdata
, module,
551 "%s: Null count for \"%s\" (type "
552 "%d, writecount %d, passcount %d)",
556 fip
->field_writecount
,
557 fip
->field_passcount
);
561 tv
->value
= _TIFFCheckMalloc(tif
, tv
->count
, tv_size
,
562 "custom tag binary object");
568 if (fip
->field_tag
== TIFFTAG_DOTRANGE
569 && strcmp(fip
->field_name
,"DotRange") == 0) {
570 /* TODO: This is an evil exception and should not have been
571 handled this way ... likely best if we move it into
572 the directory structure with an explicit field in
573 libtiff 4.1 and assign it a FIELD_ value */
575 v
[0] = (uint16
)va_arg(ap
, int);
576 v
[1] = (uint16
)va_arg(ap
, int);
577 _TIFFmemcpy(tv
->value
, &v
, 4);
580 else if (fip
->field_passcount
581 || fip
->field_writecount
== TIFF_VARIABLE
582 || fip
->field_writecount
== TIFF_VARIABLE2
583 || fip
->field_writecount
== TIFF_SPP
585 _TIFFmemcpy(tv
->value
, va_arg(ap
, void *),
586 tv
->count
* tv_size
);
588 char *val
= (char *)tv
->value
;
589 assert( tv
->count
== 1 );
591 switch (fip
->field_type
) {
595 uint8 v
= (uint8
)va_arg(ap
, int);
596 _TIFFmemcpy(val
, &v
, tv_size
);
601 int8 v
= (int8
)va_arg(ap
, int);
602 _TIFFmemcpy(val
, &v
, tv_size
);
607 uint16 v
= (uint16
)va_arg(ap
, int);
608 _TIFFmemcpy(val
, &v
, tv_size
);
613 int16 v
= (int16
)va_arg(ap
, int);
614 _TIFFmemcpy(val
, &v
, tv_size
);
620 uint32 v
= va_arg(ap
, uint32
);
621 _TIFFmemcpy(val
, &v
, tv_size
);
626 int32 v
= va_arg(ap
, int32
);
627 _TIFFmemcpy(val
, &v
, tv_size
);
633 uint64 v
= va_arg(ap
, uint64
);
634 _TIFFmemcpy(val
, &v
, tv_size
);
639 int64 v
= va_arg(ap
, int64
);
640 _TIFFmemcpy(val
, &v
, tv_size
);
647 float v
= (float)va_arg(ap
, double);
648 _TIFFmemcpy(val
, &v
, tv_size
);
653 double v
= va_arg(ap
, double);
654 _TIFFmemcpy(val
, &v
, tv_size
);
658 _TIFFmemset(val
, 0, tv_size
);
667 const TIFFField
* fip
=TIFFFieldWithTag(tif
,tag
);
669 TIFFSetFieldBit(tif
, fip
->field_bit
);
670 tif
->tif_flags
|= TIFF_DIRTYDIRECT
;
678 const TIFFField
* fip
=TIFFFieldWithTag(tif
,tag
);
679 TIFFErrorExt(tif
->tif_clientdata
, module,
680 "%s: Bad value %u for \"%s\" tag",
682 fip
? fip
->field_name
: "Unknown");
688 const TIFFField
* fip
=TIFFFieldWithTag(tif
,tag
);
689 TIFFErrorExt(tif
->tif_clientdata
, module,
690 "%s: Bad value %u for \"%s\" tag",
692 fip
? fip
->field_name
: "Unknown");
699 * Return 1/0 according to whether or not
700 * it is permissible to set the tag's value.
701 * Note that we allow ImageLength to be changed
702 * so that we can append and extend to images.
703 * Any other tag may not be altered once writing
704 * has commenced, unless its value has no effect
705 * on the format of the data that is written.
708 OkToChangeTag(TIFF
* tif
, uint32 tag
)
710 const TIFFField
* fip
= TIFFFindField(tif
, tag
, TIFF_ANY
);
711 if (!fip
) { /* unknown tag */
712 TIFFErrorExt(tif
->tif_clientdata
, "TIFFSetField", "%s: Unknown %stag %u",
713 tif
->tif_name
, isPseudoTag(tag
) ? "pseudo-" : "", tag
);
716 if (tag
!= TIFFTAG_IMAGELENGTH
&& (tif
->tif_flags
& TIFF_BEENWRITING
) &&
717 !fip
->field_oktochange
) {
719 * Consult info table to see if tag can be changed
720 * after we've started writing. We only allow changes
721 * to those tags that don't/shouldn't affect the
722 * compression and/or format of the data.
724 TIFFErrorExt(tif
->tif_clientdata
, "TIFFSetField",
725 "%s: Cannot modify tag \"%s\" while writing",
726 tif
->tif_name
, fip
->field_name
);
733 * Record the value of a field in the
734 * internal directory structure. The
735 * field will be written to the file
736 * when/if the directory structure is
740 TIFFSetField(TIFF
* tif
, uint32 tag
, ...)
746 status
= TIFFVSetField(tif
, tag
, ap
);
752 * Clear the contents of the field in the internal structure.
755 TIFFUnsetField(TIFF
* tif
, uint32 tag
)
757 const TIFFField
*fip
= TIFFFieldWithTag(tif
, tag
);
758 TIFFDirectory
* td
= &tif
->tif_dir
;
763 if( fip
->field_bit
!= FIELD_CUSTOM
)
764 TIFFClrFieldBit(tif
, fip
->field_bit
);
767 TIFFTagValue
*tv
= NULL
;
770 for (i
= 0; i
< td
->td_customValueCount
; i
++) {
772 tv
= td
->td_customValues
+ i
;
773 if( tv
->info
->field_tag
== tag
)
777 if( i
< td
->td_customValueCount
)
779 _TIFFfree(tv
->value
);
780 for( ; i
< td
->td_customValueCount
-1; i
++) {
781 td
->td_customValues
[i
] = td
->td_customValues
[i
+1];
783 td
->td_customValueCount
--;
787 tif
->tif_flags
|= TIFF_DIRTYDIRECT
;
793 * Like TIFFSetField, but taking a varargs
794 * parameter list. This routine is useful
795 * for building higher-level interfaces on
796 * top of the library.
799 TIFFVSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
801 return OkToChangeTag(tif
, tag
) ?
802 (*tif
->tif_tagmethods
.vsetfield
)(tif
, tag
, ap
) : 0;
806 _TIFFVGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
808 TIFFDirectory
* td
= &tif
->tif_dir
;
810 uint32 standard_tag
= tag
;
811 const TIFFField
* fip
= TIFFFindField(tif
, tag
, TIFF_ANY
);
814 * We want to force the custom code to be used for custom
815 * fields even if the tag happens to match a well known
816 * one - important for reinterpreted handling of standard
817 * tag values in custom directories (ie. EXIF)
819 if (fip
->field_bit
== FIELD_CUSTOM
) {
823 switch (standard_tag
) {
824 case TIFFTAG_SUBFILETYPE
:
825 *va_arg(ap
, uint32
*) = td
->td_subfiletype
;
827 case TIFFTAG_IMAGEWIDTH
:
828 *va_arg(ap
, uint32
*) = td
->td_imagewidth
;
830 case TIFFTAG_IMAGELENGTH
:
831 *va_arg(ap
, uint32
*) = td
->td_imagelength
;
833 case TIFFTAG_BITSPERSAMPLE
:
834 *va_arg(ap
, uint16
*) = td
->td_bitspersample
;
836 case TIFFTAG_COMPRESSION
:
837 *va_arg(ap
, uint16
*) = td
->td_compression
;
839 case TIFFTAG_PHOTOMETRIC
:
840 *va_arg(ap
, uint16
*) = td
->td_photometric
;
842 case TIFFTAG_THRESHHOLDING
:
843 *va_arg(ap
, uint16
*) = td
->td_threshholding
;
845 case TIFFTAG_FILLORDER
:
846 *va_arg(ap
, uint16
*) = td
->td_fillorder
;
848 case TIFFTAG_ORIENTATION
:
849 *va_arg(ap
, uint16
*) = td
->td_orientation
;
851 case TIFFTAG_SAMPLESPERPIXEL
:
852 *va_arg(ap
, uint16
*) = td
->td_samplesperpixel
;
854 case TIFFTAG_ROWSPERSTRIP
:
855 *va_arg(ap
, uint32
*) = td
->td_rowsperstrip
;
857 case TIFFTAG_MINSAMPLEVALUE
:
858 *va_arg(ap
, uint16
*) = td
->td_minsamplevalue
;
860 case TIFFTAG_MAXSAMPLEVALUE
:
861 *va_arg(ap
, uint16
*) = td
->td_maxsamplevalue
;
863 case TIFFTAG_SMINSAMPLEVALUE
:
864 if (tif
->tif_flags
& TIFF_PERSAMPLE
)
865 *va_arg(ap
, double**) = td
->td_sminsamplevalue
;
868 /* libtiff historially treats this as a single value. */
870 double v
= td
->td_sminsamplevalue
[0];
871 for (i
=1; i
< td
->td_samplesperpixel
; ++i
)
872 if( td
->td_sminsamplevalue
[i
] < v
)
873 v
= td
->td_sminsamplevalue
[i
];
874 *va_arg(ap
, double*) = v
;
877 case TIFFTAG_SMAXSAMPLEVALUE
:
878 if (tif
->tif_flags
& TIFF_PERSAMPLE
)
879 *va_arg(ap
, double**) = td
->td_smaxsamplevalue
;
882 /* libtiff historially treats this as a single value. */
884 double v
= td
->td_smaxsamplevalue
[0];
885 for (i
=1; i
< td
->td_samplesperpixel
; ++i
)
886 if( td
->td_smaxsamplevalue
[i
] > v
)
887 v
= td
->td_smaxsamplevalue
[i
];
888 *va_arg(ap
, double*) = v
;
891 case TIFFTAG_XRESOLUTION
:
892 *va_arg(ap
, float*) = td
->td_xresolution
;
894 case TIFFTAG_YRESOLUTION
:
895 *va_arg(ap
, float*) = td
->td_yresolution
;
897 case TIFFTAG_PLANARCONFIG
:
898 *va_arg(ap
, uint16
*) = td
->td_planarconfig
;
900 case TIFFTAG_XPOSITION
:
901 *va_arg(ap
, float*) = td
->td_xposition
;
903 case TIFFTAG_YPOSITION
:
904 *va_arg(ap
, float*) = td
->td_yposition
;
906 case TIFFTAG_RESOLUTIONUNIT
:
907 *va_arg(ap
, uint16
*) = td
->td_resolutionunit
;
909 case TIFFTAG_PAGENUMBER
:
910 *va_arg(ap
, uint16
*) = td
->td_pagenumber
[0];
911 *va_arg(ap
, uint16
*) = td
->td_pagenumber
[1];
913 case TIFFTAG_HALFTONEHINTS
:
914 *va_arg(ap
, uint16
*) = td
->td_halftonehints
[0];
915 *va_arg(ap
, uint16
*) = td
->td_halftonehints
[1];
917 case TIFFTAG_COLORMAP
:
918 *va_arg(ap
, uint16
**) = td
->td_colormap
[0];
919 *va_arg(ap
, uint16
**) = td
->td_colormap
[1];
920 *va_arg(ap
, uint16
**) = td
->td_colormap
[2];
922 case TIFFTAG_STRIPOFFSETS
:
923 case TIFFTAG_TILEOFFSETS
:
924 _TIFFFillStriles( tif
);
925 *va_arg(ap
, uint64
**) = td
->td_stripoffset
;
927 case TIFFTAG_STRIPBYTECOUNTS
:
928 case TIFFTAG_TILEBYTECOUNTS
:
929 _TIFFFillStriles( tif
);
930 *va_arg(ap
, uint64
**) = td
->td_stripbytecount
;
932 case TIFFTAG_MATTEING
:
933 *va_arg(ap
, uint16
*) =
934 (td
->td_extrasamples
== 1 &&
935 td
->td_sampleinfo
[0] == EXTRASAMPLE_ASSOCALPHA
);
937 case TIFFTAG_EXTRASAMPLES
:
938 *va_arg(ap
, uint16
*) = td
->td_extrasamples
;
939 *va_arg(ap
, uint16
**) = td
->td_sampleinfo
;
941 case TIFFTAG_TILEWIDTH
:
942 *va_arg(ap
, uint32
*) = td
->td_tilewidth
;
944 case TIFFTAG_TILELENGTH
:
945 *va_arg(ap
, uint32
*) = td
->td_tilelength
;
947 case TIFFTAG_TILEDEPTH
:
948 *va_arg(ap
, uint32
*) = td
->td_tiledepth
;
950 case TIFFTAG_DATATYPE
:
951 switch (td
->td_sampleformat
) {
952 case SAMPLEFORMAT_UINT
:
953 *va_arg(ap
, uint16
*) = DATATYPE_UINT
;
955 case SAMPLEFORMAT_INT
:
956 *va_arg(ap
, uint16
*) = DATATYPE_INT
;
958 case SAMPLEFORMAT_IEEEFP
:
959 *va_arg(ap
, uint16
*) = DATATYPE_IEEEFP
;
961 case SAMPLEFORMAT_VOID
:
962 *va_arg(ap
, uint16
*) = DATATYPE_VOID
;
966 case TIFFTAG_SAMPLEFORMAT
:
967 *va_arg(ap
, uint16
*) = td
->td_sampleformat
;
969 case TIFFTAG_IMAGEDEPTH
:
970 *va_arg(ap
, uint32
*) = td
->td_imagedepth
;
973 *va_arg(ap
, uint16
*) = td
->td_nsubifd
;
974 *va_arg(ap
, uint64
**) = td
->td_subifd
;
976 case TIFFTAG_YCBCRPOSITIONING
:
977 *va_arg(ap
, uint16
*) = td
->td_ycbcrpositioning
;
979 case TIFFTAG_YCBCRSUBSAMPLING
:
980 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[0];
981 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[1];
983 case TIFFTAG_TRANSFERFUNCTION
:
984 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[0];
985 if (td
->td_samplesperpixel
- td
->td_extrasamples
> 1) {
986 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[1];
987 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[2];
990 case TIFFTAG_REFERENCEBLACKWHITE
:
991 *va_arg(ap
, float**) = td
->td_refblackwhite
;
993 case TIFFTAG_INKNAMES
:
994 *va_arg(ap
, char**) = td
->td_inknames
;
1001 * This can happen if multiple images are open
1002 * with different codecs which have private
1003 * tags. The global tag information table may
1004 * then have tags that are valid for one file
1005 * but not the other. If the client tries to
1006 * get a tag that is not valid for the image's
1007 * codec then we'll arrive here.
1009 if( fip
== NULL
|| fip
->field_bit
!= FIELD_CUSTOM
)
1011 TIFFErrorExt(tif
->tif_clientdata
, "_TIFFVGetField",
1012 "%s: Invalid %stag \"%s\" "
1013 "(not supported by codec)",
1015 isPseudoTag(tag
) ? "pseudo-" : "",
1016 fip
? fip
->field_name
: "Unknown");
1022 * Do we have a custom value?
1025 for (i
= 0; i
< td
->td_customValueCount
; i
++) {
1026 TIFFTagValue
*tv
= td
->td_customValues
+ i
;
1028 if (tv
->info
->field_tag
!= tag
)
1031 if (fip
->field_passcount
) {
1032 if (fip
->field_readcount
== TIFF_VARIABLE2
)
1033 *va_arg(ap
, uint32
*) = (uint32
)tv
->count
;
1034 else /* Assume TIFF_VARIABLE */
1035 *va_arg(ap
, uint16
*) = (uint16
)tv
->count
;
1036 *va_arg(ap
, void **) = tv
->value
;
1038 } else if (fip
->field_tag
== TIFFTAG_DOTRANGE
1039 && strcmp(fip
->field_name
,"DotRange") == 0) {
1040 /* TODO: This is an evil exception and should not have been
1041 handled this way ... likely best if we move it into
1042 the directory structure with an explicit field in
1043 libtiff 4.1 and assign it a FIELD_ value */
1044 *va_arg(ap
, uint16
*) = ((uint16
*)tv
->value
)[0];
1045 *va_arg(ap
, uint16
*) = ((uint16
*)tv
->value
)[1];
1048 if (fip
->field_type
== TIFF_ASCII
1049 || fip
->field_readcount
== TIFF_VARIABLE
1050 || fip
->field_readcount
== TIFF_VARIABLE2
1051 || fip
->field_readcount
== TIFF_SPP
1053 *va_arg(ap
, void **) = tv
->value
;
1056 char *val
= (char *)tv
->value
;
1057 assert( tv
->count
== 1 );
1058 switch (fip
->field_type
) {
1060 case TIFF_UNDEFINED
:
1061 *va_arg(ap
, uint8
*) =
1066 *va_arg(ap
, int8
*) =
1071 *va_arg(ap
, uint16
*) =
1076 *va_arg(ap
, int16
*) =
1082 *va_arg(ap
, uint32
*) =
1087 *va_arg(ap
, int32
*) =
1093 *va_arg(ap
, uint64
*) =
1098 *va_arg(ap
, int64
*) =
1103 case TIFF_SRATIONAL
:
1105 *va_arg(ap
, float*) =
1110 *va_arg(ap
, double*) =
1128 * Return the value of a field in the
1129 * internal directory structure.
1132 TIFFGetField(TIFF
* tif
, uint32 tag
, ...)
1138 status
= TIFFVGetField(tif
, tag
, ap
);
1144 * Like TIFFGetField, but taking a varargs
1145 * parameter list. This routine is useful
1146 * for building higher-level interfaces on
1147 * top of the library.
1150 TIFFVGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
1152 const TIFFField
* fip
= TIFFFindField(tif
, tag
, TIFF_ANY
);
1153 return (fip
&& (isPseudoTag(tag
) || TIFFFieldSet(tif
, fip
->field_bit
)) ?
1154 (*tif
->tif_tagmethods
.vgetfield
)(tif
, tag
, ap
) : 0);
1157 #define CleanupField(member) { \
1159 _TIFFfree(td->member); \
1165 * Release storage associated with a directory.
1168 TIFFFreeDirectory(TIFF
* tif
)
1170 TIFFDirectory
*td
= &tif
->tif_dir
;
1173 _TIFFmemset(td
->td_fieldsset
, 0, FIELD_SETLONGS
);
1174 CleanupField(td_sminsamplevalue
);
1175 CleanupField(td_smaxsamplevalue
);
1176 CleanupField(td_colormap
[0]);
1177 CleanupField(td_colormap
[1]);
1178 CleanupField(td_colormap
[2]);
1179 CleanupField(td_sampleinfo
);
1180 CleanupField(td_subifd
);
1181 CleanupField(td_inknames
);
1182 CleanupField(td_refblackwhite
);
1183 CleanupField(td_transferfunction
[0]);
1184 CleanupField(td_transferfunction
[1]);
1185 CleanupField(td_transferfunction
[2]);
1186 CleanupField(td_stripoffset
);
1187 CleanupField(td_stripbytecount
);
1188 TIFFClrFieldBit(tif
, FIELD_YCBCRSUBSAMPLING
);
1189 TIFFClrFieldBit(tif
, FIELD_YCBCRPOSITIONING
);
1191 /* Cleanup custom tag values */
1192 for( i
= 0; i
< td
->td_customValueCount
; i
++ ) {
1193 if (td
->td_customValues
[i
].value
)
1194 _TIFFfree(td
->td_customValues
[i
].value
);
1197 td
->td_customValueCount
= 0;
1198 CleanupField(td_customValues
);
1200 #if defined(DEFER_STRILE_LOAD)
1201 _TIFFmemset( &(td
->td_stripoffset_entry
), 0, sizeof(TIFFDirEntry
));
1202 _TIFFmemset( &(td
->td_stripbytecount_entry
), 0, sizeof(TIFFDirEntry
));
1208 * Client Tag extension support (from Niles Ritter).
1210 static TIFFExtendProc _TIFFextender
= (TIFFExtendProc
) NULL
;
1213 TIFFSetTagExtender(TIFFExtendProc extender
)
1215 TIFFExtendProc prev
= _TIFFextender
;
1216 _TIFFextender
= extender
;
1221 * Setup for a new directory. Should we automatically call
1222 * TIFFWriteDirectory() if the current one is dirty?
1224 * The newly created directory will not exist on the file till
1225 * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
1228 TIFFCreateDirectory(TIFF
* tif
)
1230 TIFFDefaultDirectory(tif
);
1231 tif
->tif_diroff
= 0;
1232 tif
->tif_nextdiroff
= 0;
1233 tif
->tif_curoff
= 0;
1234 tif
->tif_row
= (uint32
) -1;
1235 tif
->tif_curstrip
= (uint32
) -1;
1241 TIFFCreateCustomDirectory(TIFF
* tif
, const TIFFFieldArray
* infoarray
)
1243 TIFFDefaultDirectory(tif
);
1246 * Reset the field definitions to match the application provided list.
1247 * Hopefully TIFFDefaultDirectory() won't have done anything irreversable
1248 * based on it's assumption this is an image directory.
1250 _TIFFSetupFields(tif
, infoarray
);
1252 tif
->tif_diroff
= 0;
1253 tif
->tif_nextdiroff
= 0;
1254 tif
->tif_curoff
= 0;
1255 tif
->tif_row
= (uint32
) -1;
1256 tif
->tif_curstrip
= (uint32
) -1;
1262 TIFFCreateEXIFDirectory(TIFF
* tif
)
1264 const TIFFFieldArray
* exifFieldArray
;
1265 exifFieldArray
= _TIFFGetExifFields();
1266 return TIFFCreateCustomDirectory(tif
, exifFieldArray
);
1270 * Setup a default directory structure.
1273 TIFFDefaultDirectory(TIFF
* tif
)
1275 register TIFFDirectory
* td
= &tif
->tif_dir
;
1276 const TIFFFieldArray
* tiffFieldArray
;
1278 tiffFieldArray
= _TIFFGetFields();
1279 _TIFFSetupFields(tif
, tiffFieldArray
);
1281 _TIFFmemset(td
, 0, sizeof (*td
));
1282 td
->td_fillorder
= FILLORDER_MSB2LSB
;
1283 td
->td_bitspersample
= 1;
1284 td
->td_threshholding
= THRESHHOLD_BILEVEL
;
1285 td
->td_orientation
= ORIENTATION_TOPLEFT
;
1286 td
->td_samplesperpixel
= 1;
1287 td
->td_rowsperstrip
= (uint32
) -1;
1288 td
->td_tilewidth
= 0;
1289 td
->td_tilelength
= 0;
1290 td
->td_tiledepth
= 1;
1291 td
->td_stripbytecountsorted
= 1; /* Our own arrays always sorted. */
1292 td
->td_resolutionunit
= RESUNIT_INCH
;
1293 td
->td_sampleformat
= SAMPLEFORMAT_UINT
;
1294 td
->td_imagedepth
= 1;
1295 td
->td_ycbcrsubsampling
[0] = 2;
1296 td
->td_ycbcrsubsampling
[1] = 2;
1297 td
->td_ycbcrpositioning
= YCBCRPOSITION_CENTERED
;
1298 tif
->tif_postdecode
= _TIFFNoPostDecode
;
1299 tif
->tif_foundfield
= NULL
;
1300 tif
->tif_tagmethods
.vsetfield
= _TIFFVSetField
;
1301 tif
->tif_tagmethods
.vgetfield
= _TIFFVGetField
;
1302 tif
->tif_tagmethods
.printdir
= NULL
;
1304 * Give client code a chance to install their own
1305 * tag extensions & methods, prior to compression overloads.
1308 (*_TIFFextender
)(tif
);
1309 (void) TIFFSetField(tif
, TIFFTAG_COMPRESSION
, COMPRESSION_NONE
);
1311 * NB: The directory is marked dirty as a result of setting
1312 * up the default compression scheme. However, this really
1313 * isn't correct -- we want TIFF_DIRTYDIRECT to be set only
1314 * if the user does something. We could just do the setup
1315 * by hand, but it seems better to use the normal mechanism
1316 * (i.e. TIFFSetField).
1318 tif
->tif_flags
&= ~TIFF_DIRTYDIRECT
;
1321 * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
1322 * we clear the ISTILED flag when setting up a new directory.
1323 * Should we also be clearing stuff like INSUBIFD?
1325 tif
->tif_flags
&= ~TIFF_ISTILED
;
1331 TIFFAdvanceDirectory(TIFF
* tif
, uint64
* nextdir
, uint64
* off
)
1333 static const char module[] = "TIFFAdvanceDirectory";
1336 uint64 poff
=*nextdir
;
1337 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1339 tmsize_t poffa
,poffb
,poffc
,poffd
;
1342 poffa
=(tmsize_t
)poff
;
1343 poffb
=poffa
+sizeof(uint16
);
1344 if (((uint64
)poffa
!=poff
)||(poffb
<poffa
)||(poffb
<(tmsize_t
)sizeof(uint16
))||(poffb
>tif
->tif_size
))
1346 TIFFErrorExt(tif
->tif_clientdata
,module,"Error fetching directory count");
1349 _TIFFmemcpy(&dircount
,tif
->tif_base
+poffa
,sizeof(uint16
));
1350 if (tif
->tif_flags
&TIFF_SWAB
)
1351 TIFFSwabShort(&dircount
);
1352 poffc
=poffb
+dircount
*12;
1353 poffd
=poffc
+sizeof(uint32
);
1354 if ((poffc
<poffb
)||(poffc
<dircount
*12)||(poffd
<poffc
)||(poffd
<(tmsize_t
)sizeof(uint32
))||(poffd
>tif
->tif_size
))
1356 TIFFErrorExt(tif
->tif_clientdata
,module,"Error fetching directory link");
1361 _TIFFmemcpy(&nextdir32
,tif
->tif_base
+poffc
,sizeof(uint32
));
1362 if (tif
->tif_flags
&TIFF_SWAB
)
1363 TIFFSwabLong(&nextdir32
);
1368 tmsize_t poffa
,poffb
,poffc
,poffd
;
1371 poffa
=(tmsize_t
)poff
;
1372 poffb
=poffa
+sizeof(uint64
);
1373 if (((uint64
)poffa
!=poff
)||(poffb
<poffa
)||(poffb
<(tmsize_t
)sizeof(uint64
))||(poffb
>tif
->tif_size
))
1375 TIFFErrorExt(tif
->tif_clientdata
,module,"Error fetching directory count");
1378 _TIFFmemcpy(&dircount64
,tif
->tif_base
+poffa
,sizeof(uint64
));
1379 if (tif
->tif_flags
&TIFF_SWAB
)
1380 TIFFSwabLong8(&dircount64
);
1381 if (dircount64
>0xFFFF)
1383 TIFFErrorExt(tif
->tif_clientdata
,module,"Sanity check on directory count failed");
1386 dircount16
=(uint16
)dircount64
;
1387 poffc
=poffb
+dircount16
*20;
1388 poffd
=poffc
+sizeof(uint64
);
1389 if ((poffc
<poffb
)||(poffc
<dircount16
*20)||(poffd
<poffc
)||(poffd
<(tmsize_t
)sizeof(uint64
))||(poffd
>tif
->tif_size
))
1391 TIFFErrorExt(tif
->tif_clientdata
,module,"Error fetching directory link");
1396 _TIFFmemcpy(nextdir
,tif
->tif_base
+poffc
,sizeof(uint64
));
1397 if (tif
->tif_flags
&TIFF_SWAB
)
1398 TIFFSwabLong8(nextdir
);
1404 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1408 if (!SeekOK(tif
, *nextdir
) ||
1409 !ReadOK(tif
, &dircount
, sizeof (uint16
))) {
1410 TIFFErrorExt(tif
->tif_clientdata
, module, "%s: Error fetching directory count",
1414 if (tif
->tif_flags
& TIFF_SWAB
)
1415 TIFFSwabShort(&dircount
);
1417 *off
= TIFFSeekFile(tif
,
1418 dircount
*12, SEEK_CUR
);
1420 (void) TIFFSeekFile(tif
,
1421 dircount
*12, SEEK_CUR
);
1422 if (!ReadOK(tif
, &nextdir32
, sizeof (uint32
))) {
1423 TIFFErrorExt(tif
->tif_clientdata
, module, "%s: Error fetching directory link",
1427 if (tif
->tif_flags
& TIFF_SWAB
)
1428 TIFFSwabLong(&nextdir32
);
1435 if (!SeekOK(tif
, *nextdir
) ||
1436 !ReadOK(tif
, &dircount64
, sizeof (uint64
))) {
1437 TIFFErrorExt(tif
->tif_clientdata
, module, "%s: Error fetching directory count",
1441 if (tif
->tif_flags
& TIFF_SWAB
)
1442 TIFFSwabLong8(&dircount64
);
1443 if (dircount64
>0xFFFF)
1445 TIFFErrorExt(tif
->tif_clientdata
, module, "Error fetching directory count");
1448 dircount16
= (uint16
)dircount64
;
1450 *off
= TIFFSeekFile(tif
,
1451 dircount16
*20, SEEK_CUR
);
1453 (void) TIFFSeekFile(tif
,
1454 dircount16
*20, SEEK_CUR
);
1455 if (!ReadOK(tif
, nextdir
, sizeof (uint64
))) {
1456 TIFFErrorExt(tif
->tif_clientdata
, module, "%s: Error fetching directory link",
1460 if (tif
->tif_flags
& TIFF_SWAB
)
1461 TIFFSwabLong8(nextdir
);
1468 * Count the number of directories in a file.
1471 TIFFNumberOfDirectories(TIFF
* tif
)
1475 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1476 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
1478 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
1480 while (nextdir
!= 0 && TIFFAdvanceDirectory(tif
, &nextdir
, NULL
))
1486 * Set the n-th directory as the current directory.
1487 * NB: Directories are numbered starting at 0.
1490 TIFFSetDirectory(TIFF
* tif
, uint16 dirn
)
1495 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1496 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
1498 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
1499 for (n
= dirn
; n
> 0 && nextdir
!= 0; n
--)
1500 if (!TIFFAdvanceDirectory(tif
, &nextdir
, NULL
))
1502 tif
->tif_nextdiroff
= nextdir
;
1504 * Set curdir to the actual directory index. The
1505 * -1 is because TIFFReadDirectory will increment
1506 * tif_curdir after successfully reading the directory.
1508 tif
->tif_curdir
= (dirn
- n
) - 1;
1510 * Reset tif_dirnumber counter and start new list of seen directories.
1511 * We need this to prevent IFD loops.
1513 tif
->tif_dirnumber
= 0;
1514 return (TIFFReadDirectory(tif
));
1518 * Set the current directory to be the directory
1519 * located at the specified file offset. This interface
1520 * is used mainly to access directories linked with
1521 * the SubIFD tag (e.g. thumbnail images).
1524 TIFFSetSubDirectory(TIFF
* tif
, uint64 diroff
)
1526 tif
->tif_nextdiroff
= diroff
;
1528 * Reset tif_dirnumber counter and start new list of seen directories.
1529 * We need this to prevent IFD loops.
1531 tif
->tif_dirnumber
= 0;
1532 return (TIFFReadDirectory(tif
));
1536 * Return file offset of the current directory.
1539 TIFFCurrentDirOffset(TIFF
* tif
)
1541 return (tif
->tif_diroff
);
1545 * Return an indication of whether or not we are
1546 * at the last directory in the file.
1549 TIFFLastDirectory(TIFF
* tif
)
1551 return (tif
->tif_nextdiroff
== 0);
1555 * Unlink the specified directory from the directory chain.
1558 TIFFUnlinkDirectory(TIFF
* tif
, uint16 dirn
)
1560 static const char module[] = "TIFFUnlinkDirectory";
1565 if (tif
->tif_mode
== O_RDONLY
) {
1566 TIFFErrorExt(tif
->tif_clientdata
, module,
1567 "Can not unlink directory in read-only file");
1571 * Go to the directory before the one we want
1572 * to unlink and nab the offset of the link
1573 * field we'll need to patch.
1575 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1577 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
1582 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
1585 for (n
= dirn
-1; n
> 0; n
--) {
1587 TIFFErrorExt(tif
->tif_clientdata
, module, "Directory %d does not exist", dirn
);
1590 if (!TIFFAdvanceDirectory(tif
, &nextdir
, &off
))
1594 * Advance to the directory to be unlinked and fetch
1595 * the offset of the directory that follows.
1597 if (!TIFFAdvanceDirectory(tif
, &nextdir
, NULL
))
1600 * Go back and patch the link field of the preceding
1601 * directory to point to the offset of the directory
1604 (void) TIFFSeekFile(tif
, off
, SEEK_SET
);
1605 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1608 nextdir32
=(uint32
)nextdir
;
1609 assert((uint64
)nextdir32
==nextdir
);
1610 if (tif
->tif_flags
& TIFF_SWAB
)
1611 TIFFSwabLong(&nextdir32
);
1612 if (!WriteOK(tif
, &nextdir32
, sizeof (uint32
))) {
1613 TIFFErrorExt(tif
->tif_clientdata
, module, "Error writing directory link");
1619 if (tif
->tif_flags
& TIFF_SWAB
)
1620 TIFFSwabLong8(&nextdir
);
1621 if (!WriteOK(tif
, &nextdir
, sizeof (uint64
))) {
1622 TIFFErrorExt(tif
->tif_clientdata
, module, "Error writing directory link");
1627 * Leave directory state setup safely. We don't have
1628 * facilities for doing inserting and removing directories,
1629 * so it's safest to just invalidate everything. This
1630 * means that the caller can only append to the directory
1633 (*tif
->tif_cleanup
)(tif
);
1634 if ((tif
->tif_flags
& TIFF_MYBUFFER
) && tif
->tif_rawdata
) {
1635 _TIFFfree(tif
->tif_rawdata
);
1636 tif
->tif_rawdata
= NULL
;
1638 tif
->tif_rawdataoff
= 0;
1639 tif
->tif_rawdataloaded
= 0;
1641 tif
->tif_flags
&= ~(TIFF_BEENWRITING
|TIFF_BUFFERSETUP
|TIFF_POSTENCODE
|TIFF_BUF4WRITE
);
1642 TIFFFreeDirectory(tif
);
1643 TIFFDefaultDirectory(tif
);
1644 tif
->tif_diroff
= 0; /* force link on next write */
1645 tif
->tif_nextdiroff
= 0; /* next write must be at end */
1646 tif
->tif_curoff
= 0;
1647 tif
->tif_row
= (uint32
) -1;
1648 tif
->tif_curstrip
= (uint32
) -1;
1652 /* vim: set ts=8 sts=8 sw=8 noet: */