1 /* $Id: tif_dir.c,v 1.130 2017-05-17 21:54:05 erouault Exp $ */
4 * Copyright (c) 1988-1997 Sam Leffler
5 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
7 * Permission to use, copy, modify, distribute, and sell this software and
8 * its documentation for any purpose is hereby granted without fee, provided
9 * that (i) the above copyright notices and this permission notice appear in
10 * all copies of the software and related documentation, and (ii) the names of
11 * Sam Leffler and Silicon Graphics may not be used in any advertising or
12 * publicity relating to the software without the specific, prior written
13 * permission of Sam Leffler and Silicon Graphics.
15 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
19 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
30 * Directory Tag Get & Set Routines.
31 * (and also some miscellaneous stuff)
38 * These are used in the backwards compatibility code...
40 #define DATATYPE_VOID 0 /* !untyped data */
41 #define DATATYPE_INT 1 /* !signed integer data */
42 #define DATATYPE_UINT 2 /* !unsigned integer data */
43 #define DATATYPE_IEEEFP 3 /* !IEEE floating point data */
46 setByteArray(void** vpp
, void* vp
, size_t nmemb
, size_t elem_size
)
53 tmsize_t bytes
= (tmsize_t
)(nmemb
* elem_size
);
54 if (elem_size
&& bytes
/ elem_size
== nmemb
)
55 *vpp
= (void*) _TIFFmalloc(bytes
);
57 _TIFFmemcpy(*vpp
, vp
, bytes
);
60 void _TIFFsetByteArray(void** vpp
, void* vp
, uint32 n
)
61 { setByteArray(vpp
, vp
, n
, 1); }
62 void _TIFFsetString(char** cpp
, char* cp
)
63 { setByteArray((void**) cpp
, (void*) cp
, strlen(cp
)+1, 1); }
64 static void _TIFFsetNString(char** cpp
, char* cp
, uint32 n
)
65 { setByteArray((void**) cpp
, (void*) cp
, n
, 1); }
66 void _TIFFsetShortArray(uint16
** wpp
, uint16
* wp
, uint32 n
)
67 { setByteArray((void**) wpp
, (void*) wp
, n
, sizeof (uint16
)); }
68 void _TIFFsetLongArray(uint32
** lpp
, uint32
* lp
, uint32 n
)
69 { setByteArray((void**) lpp
, (void*) lp
, n
, sizeof (uint32
)); }
70 static void _TIFFsetLong8Array(uint64
** lpp
, uint64
* lp
, uint32 n
)
71 { setByteArray((void**) lpp
, (void*) lp
, n
, sizeof (uint64
)); }
72 void _TIFFsetFloatArray(float** fpp
, float* fp
, uint32 n
)
73 { setByteArray((void**) fpp
, (void*) fp
, n
, sizeof (float)); }
74 void _TIFFsetDoubleArray(double** dpp
, double* dp
, uint32 n
)
75 { setByteArray((void**) dpp
, (void*) dp
, n
, sizeof (double)); }
78 setDoubleArrayOneValue(double** vpp
, double value
, size_t nmemb
)
82 *vpp
= _TIFFmalloc(nmemb
*sizeof(double));
86 ((double*)*vpp
)[nmemb
] = value
;
91 * Install extra samples information.
94 setExtraSamples(TIFFDirectory
* td
, va_list ap
, uint32
* v
)
96 /* XXX: Unassociated alpha data == 999 is a known Corel Draw bug, see below */
97 #define EXTRASAMPLE_COREL_UNASSALPHA 999
102 *v
= (uint16
) va_arg(ap
, uint16_vap
);
103 if ((uint16
) *v
> td
->td_samplesperpixel
)
105 va
= va_arg(ap
, uint16
*);
106 if (*v
> 0 && va
== NULL
) /* typically missing param */
108 for (i
= 0; i
< *v
; i
++) {
109 if (va
[i
] > EXTRASAMPLE_UNASSALPHA
) {
111 * XXX: Corel Draw is known to produce incorrect
112 * ExtraSamples tags which must be patched here if we
113 * want to be able to open some of the damaged TIFF
116 if (va
[i
] == EXTRASAMPLE_COREL_UNASSALPHA
)
117 va
[i
] = EXTRASAMPLE_UNASSALPHA
;
122 td
->td_extrasamples
= (uint16
) *v
;
123 _TIFFsetShortArray(&td
->td_sampleinfo
, va
, td
->td_extrasamples
);
126 #undef EXTRASAMPLE_COREL_UNASSALPHA
130 * Confirm we have "samplesperpixel" ink names separated by \0. Returns
131 * zero if the ink names are not as expected.
134 checkInkNamesString(TIFF
* tif
, uint32 slen
, const char* s
)
136 TIFFDirectory
* td
= &tif
->tif_dir
;
137 uint16 i
= td
->td_samplesperpixel
;
140 const char* ep
= s
+slen
;
143 for (; cp
< ep
&& *cp
!= '\0'; cp
++) {}
148 return ((uint32
)(cp
-s
));
151 TIFFErrorExt(tif
->tif_clientdata
, "TIFFSetField",
152 "%s: Invalid InkNames value; expecting %d names, found %d",
154 td
->td_samplesperpixel
,
155 td
->td_samplesperpixel
-i
);
159 static float TIFFClampDoubleToFloat( double val
)
169 _TIFFVSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
171 static const char module
[] = "_TIFFVSetField";
173 TIFFDirectory
* td
= &tif
->tif_dir
;
178 const TIFFField
*fip
= TIFFFindField(tif
, tag
, TIFF_ANY
);
179 uint32 standard_tag
= tag
;
180 if( fip
== NULL
) /* cannot happen since OkToChangeTag() already checks it */
183 * We want to force the custom code to be used for custom
184 * fields even if the tag happens to match a well known
185 * one - important for reinterpreted handling of standard
186 * tag values in custom directories (i.e. EXIF)
188 if (fip
->field_bit
== FIELD_CUSTOM
) {
192 switch (standard_tag
) {
193 case TIFFTAG_SUBFILETYPE
:
194 td
->td_subfiletype
= (uint32
) va_arg(ap
, uint32
);
196 case TIFFTAG_IMAGEWIDTH
:
197 td
->td_imagewidth
= (uint32
) va_arg(ap
, uint32
);
199 case TIFFTAG_IMAGELENGTH
:
200 td
->td_imagelength
= (uint32
) va_arg(ap
, uint32
);
202 case TIFFTAG_BITSPERSAMPLE
:
203 td
->td_bitspersample
= (uint16
) va_arg(ap
, uint16_vap
);
205 * If the data require post-decoding processing to byte-swap
206 * samples, set it up here. Note that since tags are required
207 * to be ordered, compression code can override this behaviour
208 * in the setup method if it wants to roll the post decoding
209 * work in with its normal work.
211 if (tif
->tif_flags
& TIFF_SWAB
) {
212 if (td
->td_bitspersample
== 8)
213 tif
->tif_postdecode
= _TIFFNoPostDecode
;
214 else if (td
->td_bitspersample
== 16)
215 tif
->tif_postdecode
= _TIFFSwab16BitData
;
216 else if (td
->td_bitspersample
== 24)
217 tif
->tif_postdecode
= _TIFFSwab24BitData
;
218 else if (td
->td_bitspersample
== 32)
219 tif
->tif_postdecode
= _TIFFSwab32BitData
;
220 else if (td
->td_bitspersample
== 64)
221 tif
->tif_postdecode
= _TIFFSwab64BitData
;
222 else if (td
->td_bitspersample
== 128) /* two 64's */
223 tif
->tif_postdecode
= _TIFFSwab64BitData
;
226 case TIFFTAG_COMPRESSION
:
227 v
= (uint16
) va_arg(ap
, uint16_vap
);
229 * If we're changing the compression scheme, the notify the
230 * previous module so that it can cleanup any state it's
233 if (TIFFFieldSet(tif
, FIELD_COMPRESSION
)) {
234 if ((uint32
)td
->td_compression
== v
)
236 (*tif
->tif_cleanup
)(tif
);
237 tif
->tif_flags
&= ~TIFF_CODERSETUP
;
240 * Setup new compression routine state.
242 if( (status
= TIFFSetCompressionScheme(tif
, v
)) != 0 )
243 td
->td_compression
= (uint16
) v
;
247 case TIFFTAG_PHOTOMETRIC
:
248 td
->td_photometric
= (uint16
) va_arg(ap
, uint16_vap
);
250 case TIFFTAG_THRESHHOLDING
:
251 td
->td_threshholding
= (uint16
) va_arg(ap
, uint16_vap
);
253 case TIFFTAG_FILLORDER
:
254 v
= (uint16
) va_arg(ap
, uint16_vap
);
255 if (v
!= FILLORDER_LSB2MSB
&& v
!= FILLORDER_MSB2LSB
)
257 td
->td_fillorder
= (uint16
) v
;
259 case TIFFTAG_ORIENTATION
:
260 v
= (uint16
) va_arg(ap
, uint16_vap
);
261 if (v
< ORIENTATION_TOPLEFT
|| ORIENTATION_LEFTBOT
< v
)
264 td
->td_orientation
= (uint16
) v
;
266 case TIFFTAG_SAMPLESPERPIXEL
:
267 v
= (uint16
) va_arg(ap
, uint16_vap
);
270 if( v
!= td
->td_samplesperpixel
)
272 /* See http://bugzilla.maptools.org/show_bug.cgi?id=2500 */
273 if( td
->td_sminsamplevalue
!= NULL
)
275 TIFFWarningExt(tif
->tif_clientdata
,module
,
276 "SamplesPerPixel tag value is changing, "
277 "but SMinSampleValue tag was read with a different value. Cancelling it");
278 TIFFClrFieldBit(tif
,FIELD_SMINSAMPLEVALUE
);
279 _TIFFfree(td
->td_sminsamplevalue
);
280 td
->td_sminsamplevalue
= NULL
;
282 if( td
->td_smaxsamplevalue
!= NULL
)
284 TIFFWarningExt(tif
->tif_clientdata
,module
,
285 "SamplesPerPixel tag value is changing, "
286 "but SMaxSampleValue tag was read with a different value. Cancelling it");
287 TIFFClrFieldBit(tif
,FIELD_SMAXSAMPLEVALUE
);
288 _TIFFfree(td
->td_smaxsamplevalue
);
289 td
->td_smaxsamplevalue
= NULL
;
292 td
->td_samplesperpixel
= (uint16
) v
;
294 case TIFFTAG_ROWSPERSTRIP
:
295 v32
= (uint32
) va_arg(ap
, uint32
);
298 td
->td_rowsperstrip
= v32
;
299 if (!TIFFFieldSet(tif
, FIELD_TILEDIMENSIONS
)) {
300 td
->td_tilelength
= v32
;
301 td
->td_tilewidth
= td
->td_imagewidth
;
304 case TIFFTAG_MINSAMPLEVALUE
:
305 td
->td_minsamplevalue
= (uint16
) va_arg(ap
, uint16_vap
);
307 case TIFFTAG_MAXSAMPLEVALUE
:
308 td
->td_maxsamplevalue
= (uint16
) va_arg(ap
, uint16_vap
);
310 case TIFFTAG_SMINSAMPLEVALUE
:
311 if (tif
->tif_flags
& TIFF_PERSAMPLE
)
312 _TIFFsetDoubleArray(&td
->td_sminsamplevalue
, va_arg(ap
, double*), td
->td_samplesperpixel
);
314 setDoubleArrayOneValue(&td
->td_sminsamplevalue
, va_arg(ap
, double), td
->td_samplesperpixel
);
316 case TIFFTAG_SMAXSAMPLEVALUE
:
317 if (tif
->tif_flags
& TIFF_PERSAMPLE
)
318 _TIFFsetDoubleArray(&td
->td_smaxsamplevalue
, va_arg(ap
, double*), td
->td_samplesperpixel
);
320 setDoubleArrayOneValue(&td
->td_smaxsamplevalue
, va_arg(ap
, double), td
->td_samplesperpixel
);
322 case TIFFTAG_XRESOLUTION
:
323 dblval
= va_arg(ap
, double);
326 td
->td_xresolution
= TIFFClampDoubleToFloat( dblval
);
328 case TIFFTAG_YRESOLUTION
:
329 dblval
= va_arg(ap
, double);
332 td
->td_yresolution
= TIFFClampDoubleToFloat( dblval
);
334 case TIFFTAG_PLANARCONFIG
:
335 v
= (uint16
) va_arg(ap
, uint16_vap
);
336 if (v
!= PLANARCONFIG_CONTIG
&& v
!= PLANARCONFIG_SEPARATE
)
338 td
->td_planarconfig
= (uint16
) v
;
340 case TIFFTAG_XPOSITION
:
341 td
->td_xposition
= TIFFClampDoubleToFloat( va_arg(ap
, double) );
343 case TIFFTAG_YPOSITION
:
344 td
->td_yposition
= TIFFClampDoubleToFloat( va_arg(ap
, double) );
346 case TIFFTAG_RESOLUTIONUNIT
:
347 v
= (uint16
) va_arg(ap
, uint16_vap
);
348 if (v
< RESUNIT_NONE
|| RESUNIT_CENTIMETER
< v
)
350 td
->td_resolutionunit
= (uint16
) v
;
352 case TIFFTAG_PAGENUMBER
:
353 td
->td_pagenumber
[0] = (uint16
) va_arg(ap
, uint16_vap
);
354 td
->td_pagenumber
[1] = (uint16
) va_arg(ap
, uint16_vap
);
356 case TIFFTAG_HALFTONEHINTS
:
357 td
->td_halftonehints
[0] = (uint16
) va_arg(ap
, uint16_vap
);
358 td
->td_halftonehints
[1] = (uint16
) va_arg(ap
, uint16_vap
);
360 case TIFFTAG_COLORMAP
:
361 v32
= (uint32
)(1L<<td
->td_bitspersample
);
362 _TIFFsetShortArray(&td
->td_colormap
[0], va_arg(ap
, uint16
*), v32
);
363 _TIFFsetShortArray(&td
->td_colormap
[1], va_arg(ap
, uint16
*), v32
);
364 _TIFFsetShortArray(&td
->td_colormap
[2], va_arg(ap
, uint16
*), v32
);
366 case TIFFTAG_EXTRASAMPLES
:
367 if (!setExtraSamples(td
, ap
, &v
))
370 case TIFFTAG_MATTEING
:
371 td
->td_extrasamples
= (((uint16
) va_arg(ap
, uint16_vap
)) != 0);
372 if (td
->td_extrasamples
) {
373 uint16 sv
= EXTRASAMPLE_ASSOCALPHA
;
374 _TIFFsetShortArray(&td
->td_sampleinfo
, &sv
, 1);
377 case TIFFTAG_TILEWIDTH
:
378 v32
= (uint32
) va_arg(ap
, uint32
);
380 if (tif
->tif_mode
!= O_RDONLY
)
382 TIFFWarningExt(tif
->tif_clientdata
, tif
->tif_name
,
383 "Nonstandard tile width %d, convert file", v32
);
385 td
->td_tilewidth
= v32
;
386 tif
->tif_flags
|= TIFF_ISTILED
;
388 case TIFFTAG_TILELENGTH
:
389 v32
= (uint32
) va_arg(ap
, uint32
);
391 if (tif
->tif_mode
!= O_RDONLY
)
393 TIFFWarningExt(tif
->tif_clientdata
, tif
->tif_name
,
394 "Nonstandard tile length %d, convert file", v32
);
396 td
->td_tilelength
= v32
;
397 tif
->tif_flags
|= TIFF_ISTILED
;
399 case TIFFTAG_TILEDEPTH
:
400 v32
= (uint32
) va_arg(ap
, uint32
);
403 td
->td_tiledepth
= v32
;
405 case TIFFTAG_DATATYPE
:
406 v
= (uint16
) va_arg(ap
, uint16_vap
);
408 case DATATYPE_VOID
: v
= SAMPLEFORMAT_VOID
; break;
409 case DATATYPE_INT
: v
= SAMPLEFORMAT_INT
; break;
410 case DATATYPE_UINT
: v
= SAMPLEFORMAT_UINT
; break;
411 case DATATYPE_IEEEFP
: v
= SAMPLEFORMAT_IEEEFP
;break;
412 default: goto badvalue
;
414 td
->td_sampleformat
= (uint16
) v
;
416 case TIFFTAG_SAMPLEFORMAT
:
417 v
= (uint16
) va_arg(ap
, uint16_vap
);
418 if (v
< SAMPLEFORMAT_UINT
|| SAMPLEFORMAT_COMPLEXIEEEFP
< v
)
420 td
->td_sampleformat
= (uint16
) v
;
422 /* Try to fix up the SWAB function for complex data. */
423 if( td
->td_sampleformat
== SAMPLEFORMAT_COMPLEXINT
424 && td
->td_bitspersample
== 32
425 && tif
->tif_postdecode
== _TIFFSwab32BitData
)
426 tif
->tif_postdecode
= _TIFFSwab16BitData
;
427 else if( (td
->td_sampleformat
== SAMPLEFORMAT_COMPLEXINT
428 || td
->td_sampleformat
== SAMPLEFORMAT_COMPLEXIEEEFP
)
429 && td
->td_bitspersample
== 64
430 && tif
->tif_postdecode
== _TIFFSwab64BitData
)
431 tif
->tif_postdecode
= _TIFFSwab32BitData
;
433 case TIFFTAG_IMAGEDEPTH
:
434 td
->td_imagedepth
= (uint32
) va_arg(ap
, uint32
);
437 if ((tif
->tif_flags
& TIFF_INSUBIFD
) == 0) {
438 td
->td_nsubifd
= (uint16
) va_arg(ap
, uint16_vap
);
439 _TIFFsetLong8Array(&td
->td_subifd
, (uint64
*) va_arg(ap
, uint64
*),
440 (uint32
) td
->td_nsubifd
);
442 TIFFErrorExt(tif
->tif_clientdata
, module
,
443 "%s: Sorry, cannot nest SubIFDs",
448 case TIFFTAG_YCBCRPOSITIONING
:
449 td
->td_ycbcrpositioning
= (uint16
) va_arg(ap
, uint16_vap
);
451 case TIFFTAG_YCBCRSUBSAMPLING
:
452 td
->td_ycbcrsubsampling
[0] = (uint16
) va_arg(ap
, uint16_vap
);
453 td
->td_ycbcrsubsampling
[1] = (uint16
) va_arg(ap
, uint16_vap
);
455 case TIFFTAG_TRANSFERFUNCTION
:
456 v
= (td
->td_samplesperpixel
- td
->td_extrasamples
) > 1 ? 3 : 1;
457 for (i
= 0; i
< v
; i
++)
458 _TIFFsetShortArray(&td
->td_transferfunction
[i
],
459 va_arg(ap
, uint16
*), 1U<<td
->td_bitspersample
);
461 case TIFFTAG_REFERENCEBLACKWHITE
:
462 /* XXX should check for null range */
463 _TIFFsetFloatArray(&td
->td_refblackwhite
, va_arg(ap
, float*), 6);
465 case TIFFTAG_INKNAMES
:
466 v
= (uint16
) va_arg(ap
, uint16_vap
);
467 s
= va_arg(ap
, char*);
468 v
= checkInkNamesString(tif
, v
, s
);
471 _TIFFsetNString(&td
->td_inknames
, s
, v
);
472 td
->td_inknameslen
= v
;
475 case TIFFTAG_PERSAMPLE
:
476 v
= (uint16
) va_arg(ap
, uint16_vap
);
477 if( v
== PERSAMPLE_MULTI
)
478 tif
->tif_flags
|= TIFF_PERSAMPLE
;
480 tif
->tif_flags
&= ~TIFF_PERSAMPLE
;
484 int tv_size
, iCustom
;
487 * This can happen if multiple images are open with different
488 * codecs which have private tags. The global tag information
489 * table may then have tags that are valid for one file but not
490 * the other. If the client tries to set a tag that is not valid
491 * for the image's codec then we'll arrive here. This
492 * happens, for example, when tiffcp is used to convert between
493 * compression schemes and codec-specific tags are blindly copied.
495 if(fip
->field_bit
!= FIELD_CUSTOM
) {
496 TIFFErrorExt(tif
->tif_clientdata
, module
,
497 "%s: Invalid %stag \"%s\" (not supported by codec)",
498 tif
->tif_name
, isPseudoTag(tag
) ? "pseudo-" : "",
505 * Find the existing entry for this custom value.
508 for (iCustom
= 0; iCustom
< td
->td_customValueCount
; iCustom
++) {
509 if (td
->td_customValues
[iCustom
].info
->field_tag
== tag
) {
510 tv
= td
->td_customValues
+ iCustom
;
511 if (tv
->value
!= NULL
) {
512 _TIFFfree(tv
->value
);
520 * Grow the custom list if the entry was not found.
523 TIFFTagValue
*new_customValues
;
525 td
->td_customValueCount
++;
526 new_customValues
= (TIFFTagValue
*)
527 _TIFFrealloc(td
->td_customValues
,
528 sizeof(TIFFTagValue
) * td
->td_customValueCount
);
529 if (!new_customValues
) {
530 TIFFErrorExt(tif
->tif_clientdata
, module
,
531 "%s: Failed to allocate space for list of custom values",
537 td
->td_customValues
= new_customValues
;
539 tv
= td
->td_customValues
+ (td
->td_customValueCount
- 1);
546 * Set custom value ... save a copy of the custom tag value.
548 tv_size
= _TIFFDataSize(fip
->field_type
);
551 TIFFErrorExt(tif
->tif_clientdata
, module
,
552 "%s: Bad field type %d for \"%s\"",
553 tif
->tif_name
, fip
->field_type
,
558 if (fip
->field_type
== TIFF_ASCII
)
562 if (fip
->field_passcount
)
564 assert(fip
->field_writecount
==TIFF_VARIABLE2
);
565 ma
=(uint32
)va_arg(ap
,uint32
);
566 mb
=(char*)va_arg(ap
,char*);
570 mb
=(char*)va_arg(ap
,char*);
571 ma
=(uint32
)(strlen(mb
)+1);
574 setByteArray(&tv
->value
,mb
,ma
,1);
578 if (fip
->field_passcount
) {
579 if (fip
->field_writecount
== TIFF_VARIABLE2
)
580 tv
->count
= (uint32
) va_arg(ap
, uint32
);
582 tv
->count
= (int) va_arg(ap
, int);
583 } else if (fip
->field_writecount
== TIFF_VARIABLE
584 || fip
->field_writecount
== TIFF_VARIABLE2
)
586 else if (fip
->field_writecount
== TIFF_SPP
)
587 tv
->count
= td
->td_samplesperpixel
;
589 tv
->count
= fip
->field_writecount
;
591 if (tv
->count
== 0) {
593 TIFFErrorExt(tif
->tif_clientdata
, module
,
594 "%s: Null count for \"%s\" (type "
595 "%d, writecount %d, passcount %d)",
599 fip
->field_writecount
,
600 fip
->field_passcount
);
604 tv
->value
= _TIFFCheckMalloc(tif
, tv
->count
, tv_size
,
605 "custom tag binary object");
611 if (fip
->field_tag
== TIFFTAG_DOTRANGE
612 && strcmp(fip
->field_name
,"DotRange") == 0) {
613 /* TODO: This is an evil exception and should not have been
614 handled this way ... likely best if we move it into
615 the directory structure with an explicit field in
616 libtiff 4.1 and assign it a FIELD_ value */
618 v2
[0] = (uint16
)va_arg(ap
, int);
619 v2
[1] = (uint16
)va_arg(ap
, int);
620 _TIFFmemcpy(tv
->value
, &v2
, 4);
623 else if (fip
->field_passcount
624 || fip
->field_writecount
== TIFF_VARIABLE
625 || fip
->field_writecount
== TIFF_VARIABLE2
626 || fip
->field_writecount
== TIFF_SPP
628 _TIFFmemcpy(tv
->value
, va_arg(ap
, void *),
629 tv
->count
* tv_size
);
631 char *val
= (char *)tv
->value
;
632 assert( tv
->count
== 1 );
634 switch (fip
->field_type
) {
638 uint8 v2
= (uint8
)va_arg(ap
, int);
639 _TIFFmemcpy(val
, &v2
, tv_size
);
644 int8 v2
= (int8
)va_arg(ap
, int);
645 _TIFFmemcpy(val
, &v2
, tv_size
);
650 uint16 v2
= (uint16
)va_arg(ap
, int);
651 _TIFFmemcpy(val
, &v2
, tv_size
);
656 int16 v2
= (int16
)va_arg(ap
, int);
657 _TIFFmemcpy(val
, &v2
, tv_size
);
663 uint32 v2
= va_arg(ap
, uint32
);
664 _TIFFmemcpy(val
, &v2
, tv_size
);
669 int32 v2
= va_arg(ap
, int32
);
670 _TIFFmemcpy(val
, &v2
, tv_size
);
676 uint64 v2
= va_arg(ap
, uint64
);
677 _TIFFmemcpy(val
, &v2
, tv_size
);
682 int64 v2
= va_arg(ap
, int64
);
683 _TIFFmemcpy(val
, &v2
, tv_size
);
690 float v2
= TIFFClampDoubleToFloat(va_arg(ap
, double));
691 _TIFFmemcpy(val
, &v2
, tv_size
);
696 double v2
= va_arg(ap
, double);
697 _TIFFmemcpy(val
, &v2
, tv_size
);
701 _TIFFmemset(val
, 0, tv_size
);
710 const TIFFField
* fip2
=TIFFFieldWithTag(tif
,tag
);
712 TIFFSetFieldBit(tif
, fip2
->field_bit
);
713 tif
->tif_flags
|= TIFF_DIRTYDIRECT
;
721 const TIFFField
* fip2
=TIFFFieldWithTag(tif
,tag
);
722 TIFFErrorExt(tif
->tif_clientdata
, module
,
723 "%s: Bad value %u for \"%s\" tag",
725 fip2
? fip2
->field_name
: "Unknown");
731 const TIFFField
* fip2
=TIFFFieldWithTag(tif
,tag
);
732 TIFFErrorExt(tif
->tif_clientdata
, module
,
733 "%s: Bad value %u for \"%s\" tag",
735 fip2
? fip2
->field_name
: "Unknown");
741 const TIFFField
* fip2
=TIFFFieldWithTag(tif
,tag
);
742 TIFFErrorExt(tif
->tif_clientdata
, module
,
743 "%s: Bad value %f for \"%s\" tag",
744 tif
->tif_name
, dblval
,
745 fip2
? fip2
->field_name
: "Unknown");
752 * Return 1/0 according to whether or not
753 * it is permissible to set the tag's value.
754 * Note that we allow ImageLength to be changed
755 * so that we can append and extend to images.
756 * Any other tag may not be altered once writing
757 * has commenced, unless its value has no effect
758 * on the format of the data that is written.
761 OkToChangeTag(TIFF
* tif
, uint32 tag
)
763 const TIFFField
* fip
= TIFFFindField(tif
, tag
, TIFF_ANY
);
764 if (!fip
) { /* unknown tag */
765 TIFFErrorExt(tif
->tif_clientdata
, "TIFFSetField", "%s: Unknown %stag %u",
766 tif
->tif_name
, isPseudoTag(tag
) ? "pseudo-" : "", tag
);
769 if (tag
!= TIFFTAG_IMAGELENGTH
&& (tif
->tif_flags
& TIFF_BEENWRITING
) &&
770 !fip
->field_oktochange
) {
772 * Consult info table to see if tag can be changed
773 * after we've started writing. We only allow changes
774 * to those tags that don't/shouldn't affect the
775 * compression and/or format of the data.
777 TIFFErrorExt(tif
->tif_clientdata
, "TIFFSetField",
778 "%s: Cannot modify tag \"%s\" while writing",
779 tif
->tif_name
, fip
->field_name
);
786 * Record the value of a field in the
787 * internal directory structure. The
788 * field will be written to the file
789 * when/if the directory structure is
793 TIFFSetField(TIFF
* tif
, uint32 tag
, ...)
799 status
= TIFFVSetField(tif
, tag
, ap
);
805 * Clear the contents of the field in the internal structure.
808 TIFFUnsetField(TIFF
* tif
, uint32 tag
)
810 const TIFFField
*fip
= TIFFFieldWithTag(tif
, tag
);
811 TIFFDirectory
* td
= &tif
->tif_dir
;
816 if( fip
->field_bit
!= FIELD_CUSTOM
)
817 TIFFClrFieldBit(tif
, fip
->field_bit
);
820 TIFFTagValue
*tv
= NULL
;
823 for (i
= 0; i
< td
->td_customValueCount
; i
++) {
825 tv
= td
->td_customValues
+ i
;
826 if( tv
->info
->field_tag
== tag
)
830 if( i
< td
->td_customValueCount
)
832 _TIFFfree(tv
->value
);
833 for( ; i
< td
->td_customValueCount
-1; i
++) {
834 td
->td_customValues
[i
] = td
->td_customValues
[i
+1];
836 td
->td_customValueCount
--;
840 tif
->tif_flags
|= TIFF_DIRTYDIRECT
;
846 * Like TIFFSetField, but taking a varargs
847 * parameter list. This routine is useful
848 * for building higher-level interfaces on
849 * top of the library.
852 TIFFVSetField(TIFF
* tif
, uint32 tag
, va_list ap
)
854 return OkToChangeTag(tif
, tag
) ?
855 (*tif
->tif_tagmethods
.vsetfield
)(tif
, tag
, ap
) : 0;
859 _TIFFVGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
861 TIFFDirectory
* td
= &tif
->tif_dir
;
863 uint32 standard_tag
= tag
;
864 const TIFFField
* fip
= TIFFFindField(tif
, tag
, TIFF_ANY
);
865 if( fip
== NULL
) /* cannot happen since TIFFGetField() already checks it */
868 if( tag
== TIFFTAG_NUMBEROFINKS
)
871 for (i
= 0; i
< td
->td_customValueCount
; i
++) {
873 TIFFTagValue
*tv
= td
->td_customValues
+ i
;
874 if (tv
->info
->field_tag
!= tag
)
876 val
= *(uint16
*)tv
->value
;
877 /* Truncate to SamplesPerPixel, since the */
878 /* setting code for INKNAMES assume that there are SamplesPerPixel */
880 /* Fixes http://bugzilla.maptools.org/show_bug.cgi?id=2599 */
881 if( val
> td
->td_samplesperpixel
)
883 TIFFWarningExt(tif
->tif_clientdata
,"_TIFFVGetField",
884 "Truncating NumberOfInks from %u to %u",
885 val
, td
->td_samplesperpixel
);
886 val
= td
->td_samplesperpixel
;
888 *va_arg(ap
, uint16
*) = val
;
895 * We want to force the custom code to be used for custom
896 * fields even if the tag happens to match a well known
897 * one - important for reinterpreted handling of standard
898 * tag values in custom directories (i.e. EXIF)
900 if (fip
->field_bit
== FIELD_CUSTOM
) {
904 switch (standard_tag
) {
905 case TIFFTAG_SUBFILETYPE
:
906 *va_arg(ap
, uint32
*) = td
->td_subfiletype
;
908 case TIFFTAG_IMAGEWIDTH
:
909 *va_arg(ap
, uint32
*) = td
->td_imagewidth
;
911 case TIFFTAG_IMAGELENGTH
:
912 *va_arg(ap
, uint32
*) = td
->td_imagelength
;
914 case TIFFTAG_BITSPERSAMPLE
:
915 *va_arg(ap
, uint16
*) = td
->td_bitspersample
;
917 case TIFFTAG_COMPRESSION
:
918 *va_arg(ap
, uint16
*) = td
->td_compression
;
920 case TIFFTAG_PHOTOMETRIC
:
921 *va_arg(ap
, uint16
*) = td
->td_photometric
;
923 case TIFFTAG_THRESHHOLDING
:
924 *va_arg(ap
, uint16
*) = td
->td_threshholding
;
926 case TIFFTAG_FILLORDER
:
927 *va_arg(ap
, uint16
*) = td
->td_fillorder
;
929 case TIFFTAG_ORIENTATION
:
930 *va_arg(ap
, uint16
*) = td
->td_orientation
;
932 case TIFFTAG_SAMPLESPERPIXEL
:
933 *va_arg(ap
, uint16
*) = td
->td_samplesperpixel
;
935 case TIFFTAG_ROWSPERSTRIP
:
936 *va_arg(ap
, uint32
*) = td
->td_rowsperstrip
;
938 case TIFFTAG_MINSAMPLEVALUE
:
939 *va_arg(ap
, uint16
*) = td
->td_minsamplevalue
;
941 case TIFFTAG_MAXSAMPLEVALUE
:
942 *va_arg(ap
, uint16
*) = td
->td_maxsamplevalue
;
944 case TIFFTAG_SMINSAMPLEVALUE
:
945 if (tif
->tif_flags
& TIFF_PERSAMPLE
)
946 *va_arg(ap
, double**) = td
->td_sminsamplevalue
;
949 /* libtiff historically treats this as a single value. */
951 double v
= td
->td_sminsamplevalue
[0];
952 for (i
=1; i
< td
->td_samplesperpixel
; ++i
)
953 if( td
->td_sminsamplevalue
[i
] < v
)
954 v
= td
->td_sminsamplevalue
[i
];
955 *va_arg(ap
, double*) = v
;
958 case TIFFTAG_SMAXSAMPLEVALUE
:
959 if (tif
->tif_flags
& TIFF_PERSAMPLE
)
960 *va_arg(ap
, double**) = td
->td_smaxsamplevalue
;
963 /* libtiff historically treats this as a single value. */
965 double v
= td
->td_smaxsamplevalue
[0];
966 for (i
=1; i
< td
->td_samplesperpixel
; ++i
)
967 if( td
->td_smaxsamplevalue
[i
] > v
)
968 v
= td
->td_smaxsamplevalue
[i
];
969 *va_arg(ap
, double*) = v
;
972 case TIFFTAG_XRESOLUTION
:
973 *va_arg(ap
, float*) = td
->td_xresolution
;
975 case TIFFTAG_YRESOLUTION
:
976 *va_arg(ap
, float*) = td
->td_yresolution
;
978 case TIFFTAG_PLANARCONFIG
:
979 *va_arg(ap
, uint16
*) = td
->td_planarconfig
;
981 case TIFFTAG_XPOSITION
:
982 *va_arg(ap
, float*) = td
->td_xposition
;
984 case TIFFTAG_YPOSITION
:
985 *va_arg(ap
, float*) = td
->td_yposition
;
987 case TIFFTAG_RESOLUTIONUNIT
:
988 *va_arg(ap
, uint16
*) = td
->td_resolutionunit
;
990 case TIFFTAG_PAGENUMBER
:
991 *va_arg(ap
, uint16
*) = td
->td_pagenumber
[0];
992 *va_arg(ap
, uint16
*) = td
->td_pagenumber
[1];
994 case TIFFTAG_HALFTONEHINTS
:
995 *va_arg(ap
, uint16
*) = td
->td_halftonehints
[0];
996 *va_arg(ap
, uint16
*) = td
->td_halftonehints
[1];
998 case TIFFTAG_COLORMAP
:
999 *va_arg(ap
, uint16
**) = td
->td_colormap
[0];
1000 *va_arg(ap
, uint16
**) = td
->td_colormap
[1];
1001 *va_arg(ap
, uint16
**) = td
->td_colormap
[2];
1003 case TIFFTAG_STRIPOFFSETS
:
1004 case TIFFTAG_TILEOFFSETS
:
1005 _TIFFFillStriles( tif
);
1006 *va_arg(ap
, uint64
**) = td
->td_stripoffset
;
1008 case TIFFTAG_STRIPBYTECOUNTS
:
1009 case TIFFTAG_TILEBYTECOUNTS
:
1010 _TIFFFillStriles( tif
);
1011 *va_arg(ap
, uint64
**) = td
->td_stripbytecount
;
1013 case TIFFTAG_MATTEING
:
1014 *va_arg(ap
, uint16
*) =
1015 (td
->td_extrasamples
== 1 &&
1016 td
->td_sampleinfo
[0] == EXTRASAMPLE_ASSOCALPHA
);
1018 case TIFFTAG_EXTRASAMPLES
:
1019 *va_arg(ap
, uint16
*) = td
->td_extrasamples
;
1020 *va_arg(ap
, uint16
**) = td
->td_sampleinfo
;
1022 case TIFFTAG_TILEWIDTH
:
1023 *va_arg(ap
, uint32
*) = td
->td_tilewidth
;
1025 case TIFFTAG_TILELENGTH
:
1026 *va_arg(ap
, uint32
*) = td
->td_tilelength
;
1028 case TIFFTAG_TILEDEPTH
:
1029 *va_arg(ap
, uint32
*) = td
->td_tiledepth
;
1031 case TIFFTAG_DATATYPE
:
1032 switch (td
->td_sampleformat
) {
1033 case SAMPLEFORMAT_UINT
:
1034 *va_arg(ap
, uint16
*) = DATATYPE_UINT
;
1036 case SAMPLEFORMAT_INT
:
1037 *va_arg(ap
, uint16
*) = DATATYPE_INT
;
1039 case SAMPLEFORMAT_IEEEFP
:
1040 *va_arg(ap
, uint16
*) = DATATYPE_IEEEFP
;
1042 case SAMPLEFORMAT_VOID
:
1043 *va_arg(ap
, uint16
*) = DATATYPE_VOID
;
1047 case TIFFTAG_SAMPLEFORMAT
:
1048 *va_arg(ap
, uint16
*) = td
->td_sampleformat
;
1050 case TIFFTAG_IMAGEDEPTH
:
1051 *va_arg(ap
, uint32
*) = td
->td_imagedepth
;
1053 case TIFFTAG_SUBIFD
:
1054 *va_arg(ap
, uint16
*) = td
->td_nsubifd
;
1055 *va_arg(ap
, uint64
**) = td
->td_subifd
;
1057 case TIFFTAG_YCBCRPOSITIONING
:
1058 *va_arg(ap
, uint16
*) = td
->td_ycbcrpositioning
;
1060 case TIFFTAG_YCBCRSUBSAMPLING
:
1061 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[0];
1062 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[1];
1064 case TIFFTAG_TRANSFERFUNCTION
:
1065 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[0];
1066 if (td
->td_samplesperpixel
- td
->td_extrasamples
> 1) {
1067 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[1];
1068 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[2];
1071 case TIFFTAG_REFERENCEBLACKWHITE
:
1072 *va_arg(ap
, float**) = td
->td_refblackwhite
;
1074 case TIFFTAG_INKNAMES
:
1075 *va_arg(ap
, char**) = td
->td_inknames
;
1082 * This can happen if multiple images are open
1083 * with different codecs which have private
1084 * tags. The global tag information table may
1085 * then have tags that are valid for one file
1086 * but not the other. If the client tries to
1087 * get a tag that is not valid for the image's
1088 * codec then we'll arrive here.
1090 if( fip
->field_bit
!= FIELD_CUSTOM
)
1092 TIFFErrorExt(tif
->tif_clientdata
, "_TIFFVGetField",
1093 "%s: Invalid %stag \"%s\" "
1094 "(not supported by codec)",
1096 isPseudoTag(tag
) ? "pseudo-" : "",
1103 * Do we have a custom value?
1106 for (i
= 0; i
< td
->td_customValueCount
; i
++) {
1107 TIFFTagValue
*tv
= td
->td_customValues
+ i
;
1109 if (tv
->info
->field_tag
!= tag
)
1112 if (fip
->field_passcount
) {
1113 if (fip
->field_readcount
== TIFF_VARIABLE2
)
1114 *va_arg(ap
, uint32
*) = (uint32
)tv
->count
;
1115 else /* Assume TIFF_VARIABLE */
1116 *va_arg(ap
, uint16
*) = (uint16
)tv
->count
;
1117 *va_arg(ap
, void **) = tv
->value
;
1119 } else if (fip
->field_tag
== TIFFTAG_DOTRANGE
1120 && strcmp(fip
->field_name
,"DotRange") == 0) {
1121 /* TODO: This is an evil exception and should not have been
1122 handled this way ... likely best if we move it into
1123 the directory structure with an explicit field in
1124 libtiff 4.1 and assign it a FIELD_ value */
1125 *va_arg(ap
, uint16
*) = ((uint16
*)tv
->value
)[0];
1126 *va_arg(ap
, uint16
*) = ((uint16
*)tv
->value
)[1];
1129 if (fip
->field_type
== TIFF_ASCII
1130 || fip
->field_readcount
== TIFF_VARIABLE
1131 || fip
->field_readcount
== TIFF_VARIABLE2
1132 || fip
->field_readcount
== TIFF_SPP
1134 *va_arg(ap
, void **) = tv
->value
;
1137 char *val
= (char *)tv
->value
;
1138 assert( tv
->count
== 1 );
1139 switch (fip
->field_type
) {
1141 case TIFF_UNDEFINED
:
1142 *va_arg(ap
, uint8
*) =
1147 *va_arg(ap
, int8
*) =
1152 *va_arg(ap
, uint16
*) =
1157 *va_arg(ap
, int16
*) =
1163 *va_arg(ap
, uint32
*) =
1168 *va_arg(ap
, int32
*) =
1174 *va_arg(ap
, uint64
*) =
1179 *va_arg(ap
, int64
*) =
1184 case TIFF_SRATIONAL
:
1186 *va_arg(ap
, float*) =
1191 *va_arg(ap
, double*) =
1209 * Return the value of a field in the
1210 * internal directory structure.
1213 TIFFGetField(TIFF
* tif
, uint32 tag
, ...)
1219 status
= TIFFVGetField(tif
, tag
, ap
);
1225 * Like TIFFGetField, but taking a varargs
1226 * parameter list. This routine is useful
1227 * for building higher-level interfaces on
1228 * top of the library.
1231 TIFFVGetField(TIFF
* tif
, uint32 tag
, va_list ap
)
1233 const TIFFField
* fip
= TIFFFindField(tif
, tag
, TIFF_ANY
);
1234 return (fip
&& (isPseudoTag(tag
) || TIFFFieldSet(tif
, fip
->field_bit
)) ?
1235 (*tif
->tif_tagmethods
.vgetfield
)(tif
, tag
, ap
) : 0);
1238 #define CleanupField(member) { \
1240 _TIFFfree(td->member); \
1246 * Release storage associated with a directory.
1249 TIFFFreeDirectory(TIFF
* tif
)
1251 TIFFDirectory
*td
= &tif
->tif_dir
;
1254 _TIFFmemset(td
->td_fieldsset
, 0, FIELD_SETLONGS
);
1255 CleanupField(td_sminsamplevalue
);
1256 CleanupField(td_smaxsamplevalue
);
1257 CleanupField(td_colormap
[0]);
1258 CleanupField(td_colormap
[1]);
1259 CleanupField(td_colormap
[2]);
1260 CleanupField(td_sampleinfo
);
1261 CleanupField(td_subifd
);
1262 CleanupField(td_inknames
);
1263 CleanupField(td_refblackwhite
);
1264 CleanupField(td_transferfunction
[0]);
1265 CleanupField(td_transferfunction
[1]);
1266 CleanupField(td_transferfunction
[2]);
1267 CleanupField(td_stripoffset
);
1268 CleanupField(td_stripbytecount
);
1269 TIFFClrFieldBit(tif
, FIELD_YCBCRSUBSAMPLING
);
1270 TIFFClrFieldBit(tif
, FIELD_YCBCRPOSITIONING
);
1272 /* Cleanup custom tag values */
1273 for( i
= 0; i
< td
->td_customValueCount
; i
++ ) {
1274 if (td
->td_customValues
[i
].value
)
1275 _TIFFfree(td
->td_customValues
[i
].value
);
1278 td
->td_customValueCount
= 0;
1279 CleanupField(td_customValues
);
1281 #if defined(DEFER_STRILE_LOAD)
1282 _TIFFmemset( &(td
->td_stripoffset_entry
), 0, sizeof(TIFFDirEntry
));
1283 _TIFFmemset( &(td
->td_stripbytecount_entry
), 0, sizeof(TIFFDirEntry
));
1289 * Client Tag extension support (from Niles Ritter).
1291 static TIFFExtendProc _TIFFextender
= (TIFFExtendProc
) NULL
;
1294 TIFFSetTagExtender(TIFFExtendProc extender
)
1296 TIFFExtendProc prev
= _TIFFextender
;
1297 _TIFFextender
= extender
;
1302 * Setup for a new directory. Should we automatically call
1303 * TIFFWriteDirectory() if the current one is dirty?
1305 * The newly created directory will not exist on the file till
1306 * TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
1309 TIFFCreateDirectory(TIFF
* tif
)
1311 TIFFDefaultDirectory(tif
);
1312 tif
->tif_diroff
= 0;
1313 tif
->tif_nextdiroff
= 0;
1314 tif
->tif_curoff
= 0;
1315 tif
->tif_row
= (uint32
) -1;
1316 tif
->tif_curstrip
= (uint32
) -1;
1322 TIFFCreateCustomDirectory(TIFF
* tif
, const TIFFFieldArray
* infoarray
)
1324 TIFFDefaultDirectory(tif
);
1327 * Reset the field definitions to match the application provided list.
1328 * Hopefully TIFFDefaultDirectory() won't have done anything irreversable
1329 * based on it's assumption this is an image directory.
1331 _TIFFSetupFields(tif
, infoarray
);
1333 tif
->tif_diroff
= 0;
1334 tif
->tif_nextdiroff
= 0;
1335 tif
->tif_curoff
= 0;
1336 tif
->tif_row
= (uint32
) -1;
1337 tif
->tif_curstrip
= (uint32
) -1;
1343 TIFFCreateEXIFDirectory(TIFF
* tif
)
1345 const TIFFFieldArray
* exifFieldArray
;
1346 exifFieldArray
= _TIFFGetExifFields();
1347 return TIFFCreateCustomDirectory(tif
, exifFieldArray
);
1351 * Setup a default directory structure.
1354 TIFFDefaultDirectory(TIFF
* tif
)
1356 register TIFFDirectory
* td
= &tif
->tif_dir
;
1357 const TIFFFieldArray
* tiffFieldArray
;
1359 tiffFieldArray
= _TIFFGetFields();
1360 _TIFFSetupFields(tif
, tiffFieldArray
);
1362 _TIFFmemset(td
, 0, sizeof (*td
));
1363 td
->td_fillorder
= FILLORDER_MSB2LSB
;
1364 td
->td_bitspersample
= 1;
1365 td
->td_threshholding
= THRESHHOLD_BILEVEL
;
1366 td
->td_orientation
= ORIENTATION_TOPLEFT
;
1367 td
->td_samplesperpixel
= 1;
1368 td
->td_rowsperstrip
= (uint32
) -1;
1369 td
->td_tilewidth
= 0;
1370 td
->td_tilelength
= 0;
1371 td
->td_tiledepth
= 1;
1372 td
->td_stripbytecountsorted
= 1; /* Our own arrays always sorted. */
1373 td
->td_resolutionunit
= RESUNIT_INCH
;
1374 td
->td_sampleformat
= SAMPLEFORMAT_UINT
;
1375 td
->td_imagedepth
= 1;
1376 td
->td_ycbcrsubsampling
[0] = 2;
1377 td
->td_ycbcrsubsampling
[1] = 2;
1378 td
->td_ycbcrpositioning
= YCBCRPOSITION_CENTERED
;
1379 tif
->tif_postdecode
= _TIFFNoPostDecode
;
1380 tif
->tif_foundfield
= NULL
;
1381 tif
->tif_tagmethods
.vsetfield
= _TIFFVSetField
;
1382 tif
->tif_tagmethods
.vgetfield
= _TIFFVGetField
;
1383 tif
->tif_tagmethods
.printdir
= NULL
;
1385 * Give client code a chance to install their own
1386 * tag extensions & methods, prior to compression overloads,
1387 * but do some prior cleanup first. (http://trac.osgeo.org/gdal/ticket/5054)
1389 if (tif
->tif_nfieldscompat
> 0) {
1392 for (i
= 0; i
< tif
->tif_nfieldscompat
; i
++) {
1393 if (tif
->tif_fieldscompat
[i
].allocated_size
)
1394 _TIFFfree(tif
->tif_fieldscompat
[i
].fields
);
1396 _TIFFfree(tif
->tif_fieldscompat
);
1397 tif
->tif_nfieldscompat
= 0;
1398 tif
->tif_fieldscompat
= NULL
;
1401 (*_TIFFextender
)(tif
);
1402 (void) TIFFSetField(tif
, TIFFTAG_COMPRESSION
, COMPRESSION_NONE
);
1404 * NB: The directory is marked dirty as a result of setting
1405 * up the default compression scheme. However, this really
1406 * isn't correct -- we want TIFF_DIRTYDIRECT to be set only
1407 * if the user does something. We could just do the setup
1408 * by hand, but it seems better to use the normal mechanism
1409 * (i.e. TIFFSetField).
1411 tif
->tif_flags
&= ~TIFF_DIRTYDIRECT
;
1414 * As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
1415 * we clear the ISTILED flag when setting up a new directory.
1416 * Should we also be clearing stuff like INSUBIFD?
1418 tif
->tif_flags
&= ~TIFF_ISTILED
;
1424 TIFFAdvanceDirectory(TIFF
* tif
, uint64
* nextdir
, uint64
* off
)
1426 static const char module
[] = "TIFFAdvanceDirectory";
1429 uint64 poff
=*nextdir
;
1430 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1432 tmsize_t poffa
,poffb
,poffc
,poffd
;
1435 poffa
=(tmsize_t
)poff
;
1436 poffb
=poffa
+sizeof(uint16
);
1437 if (((uint64
)poffa
!=poff
)||(poffb
<poffa
)||(poffb
<(tmsize_t
)sizeof(uint16
))||(poffb
>tif
->tif_size
))
1439 TIFFErrorExt(tif
->tif_clientdata
,module
,"Error fetching directory count");
1443 _TIFFmemcpy(&dircount
,tif
->tif_base
+poffa
,sizeof(uint16
));
1444 if (tif
->tif_flags
&TIFF_SWAB
)
1445 TIFFSwabShort(&dircount
);
1446 poffc
=poffb
+dircount
*12;
1447 poffd
=poffc
+sizeof(uint32
);
1448 if ((poffc
<poffb
)||(poffc
<dircount
*12)||(poffd
<poffc
)||(poffd
<(tmsize_t
)sizeof(uint32
))||(poffd
>tif
->tif_size
))
1450 TIFFErrorExt(tif
->tif_clientdata
,module
,"Error fetching directory link");
1455 _TIFFmemcpy(&nextdir32
,tif
->tif_base
+poffc
,sizeof(uint32
));
1456 if (tif
->tif_flags
&TIFF_SWAB
)
1457 TIFFSwabLong(&nextdir32
);
1462 tmsize_t poffa
,poffb
,poffc
,poffd
;
1465 poffa
=(tmsize_t
)poff
;
1466 poffb
=poffa
+sizeof(uint64
);
1467 if (((uint64
)poffa
!=poff
)||(poffb
<poffa
)||(poffb
<(tmsize_t
)sizeof(uint64
))||(poffb
>tif
->tif_size
))
1469 TIFFErrorExt(tif
->tif_clientdata
,module
,"Error fetching directory count");
1472 _TIFFmemcpy(&dircount64
,tif
->tif_base
+poffa
,sizeof(uint64
));
1473 if (tif
->tif_flags
&TIFF_SWAB
)
1474 TIFFSwabLong8(&dircount64
);
1475 if (dircount64
>0xFFFF)
1477 TIFFErrorExt(tif
->tif_clientdata
,module
,"Sanity check on directory count failed");
1480 dircount16
=(uint16
)dircount64
;
1481 poffc
=poffb
+dircount16
*20;
1482 poffd
=poffc
+sizeof(uint64
);
1483 if ((poffc
<poffb
)||(poffc
<dircount16
*20)||(poffd
<poffc
)||(poffd
<(tmsize_t
)sizeof(uint64
))||(poffd
>tif
->tif_size
))
1485 TIFFErrorExt(tif
->tif_clientdata
,module
,"Error fetching directory link");
1490 _TIFFmemcpy(nextdir
,tif
->tif_base
+poffc
,sizeof(uint64
));
1491 if (tif
->tif_flags
&TIFF_SWAB
)
1492 TIFFSwabLong8(nextdir
);
1498 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1502 if (!SeekOK(tif
, *nextdir
) ||
1503 !ReadOK(tif
, &dircount
, sizeof (uint16
))) {
1504 TIFFErrorExt(tif
->tif_clientdata
, module
, "%s: Error fetching directory count",
1508 if (tif
->tif_flags
& TIFF_SWAB
)
1509 TIFFSwabShort(&dircount
);
1511 *off
= TIFFSeekFile(tif
,
1512 dircount
*12, SEEK_CUR
);
1514 (void) TIFFSeekFile(tif
,
1515 dircount
*12, SEEK_CUR
);
1516 if (!ReadOK(tif
, &nextdir32
, sizeof (uint32
))) {
1517 TIFFErrorExt(tif
->tif_clientdata
, module
, "%s: Error fetching directory link",
1521 if (tif
->tif_flags
& TIFF_SWAB
)
1522 TIFFSwabLong(&nextdir32
);
1529 if (!SeekOK(tif
, *nextdir
) ||
1530 !ReadOK(tif
, &dircount64
, sizeof (uint64
))) {
1531 TIFFErrorExt(tif
->tif_clientdata
, module
, "%s: Error fetching directory count",
1535 if (tif
->tif_flags
& TIFF_SWAB
)
1536 TIFFSwabLong8(&dircount64
);
1537 if (dircount64
>0xFFFF)
1539 TIFFErrorExt(tif
->tif_clientdata
, module
, "Error fetching directory count");
1542 dircount16
= (uint16
)dircount64
;
1544 *off
= TIFFSeekFile(tif
,
1545 dircount16
*20, SEEK_CUR
);
1547 (void) TIFFSeekFile(tif
,
1548 dircount16
*20, SEEK_CUR
);
1549 if (!ReadOK(tif
, nextdir
, sizeof (uint64
))) {
1550 TIFFErrorExt(tif
->tif_clientdata
, module
,
1551 "%s: Error fetching directory link",
1555 if (tif
->tif_flags
& TIFF_SWAB
)
1556 TIFFSwabLong8(nextdir
);
1563 * Count the number of directories in a file.
1566 TIFFNumberOfDirectories(TIFF
* tif
)
1568 static const char module
[] = "TIFFNumberOfDirectories";
1571 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1572 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
1574 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
1576 while (nextdir
!= 0 && TIFFAdvanceDirectory(tif
, &nextdir
, NULL
))
1583 TIFFErrorExt(tif
->tif_clientdata
, module
,
1584 "Directory count exceeded 65535 limit,"
1585 " giving up on counting.");
1593 * Set the n-th directory as the current directory.
1594 * NB: Directories are numbered starting at 0.
1597 TIFFSetDirectory(TIFF
* tif
, uint16 dirn
)
1602 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1603 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
1605 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
1606 for (n
= dirn
; n
> 0 && nextdir
!= 0; n
--)
1607 if (!TIFFAdvanceDirectory(tif
, &nextdir
, NULL
))
1609 tif
->tif_nextdiroff
= nextdir
;
1611 * Set curdir to the actual directory index. The
1612 * -1 is because TIFFReadDirectory will increment
1613 * tif_curdir after successfully reading the directory.
1615 tif
->tif_curdir
= (dirn
- n
) - 1;
1617 * Reset tif_dirnumber counter and start new list of seen directories.
1618 * We need this to prevent IFD loops.
1620 tif
->tif_dirnumber
= 0;
1621 return (TIFFReadDirectory(tif
));
1625 * Set the current directory to be the directory
1626 * located at the specified file offset. This interface
1627 * is used mainly to access directories linked with
1628 * the SubIFD tag (e.g. thumbnail images).
1631 TIFFSetSubDirectory(TIFF
* tif
, uint64 diroff
)
1633 tif
->tif_nextdiroff
= diroff
;
1635 * Reset tif_dirnumber counter and start new list of seen directories.
1636 * We need this to prevent IFD loops.
1638 tif
->tif_dirnumber
= 0;
1639 return (TIFFReadDirectory(tif
));
1643 * Return file offset of the current directory.
1646 TIFFCurrentDirOffset(TIFF
* tif
)
1648 return (tif
->tif_diroff
);
1652 * Return an indication of whether or not we are
1653 * at the last directory in the file.
1656 TIFFLastDirectory(TIFF
* tif
)
1658 return (tif
->tif_nextdiroff
== 0);
1662 * Unlink the specified directory from the directory chain.
1665 TIFFUnlinkDirectory(TIFF
* tif
, uint16 dirn
)
1667 static const char module
[] = "TIFFUnlinkDirectory";
1672 if (tif
->tif_mode
== O_RDONLY
) {
1673 TIFFErrorExt(tif
->tif_clientdata
, module
,
1674 "Can not unlink directory in read-only file");
1678 * Go to the directory before the one we want
1679 * to unlink and nab the offset of the link
1680 * field we'll need to patch.
1682 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1684 nextdir
= tif
->tif_header
.classic
.tiff_diroff
;
1689 nextdir
= tif
->tif_header
.big
.tiff_diroff
;
1692 for (n
= dirn
-1; n
> 0; n
--) {
1694 TIFFErrorExt(tif
->tif_clientdata
, module
, "Directory %d does not exist", dirn
);
1697 if (!TIFFAdvanceDirectory(tif
, &nextdir
, &off
))
1701 * Advance to the directory to be unlinked and fetch
1702 * the offset of the directory that follows.
1704 if (!TIFFAdvanceDirectory(tif
, &nextdir
, NULL
))
1707 * Go back and patch the link field of the preceding
1708 * directory to point to the offset of the directory
1711 (void) TIFFSeekFile(tif
, off
, SEEK_SET
);
1712 if (!(tif
->tif_flags
&TIFF_BIGTIFF
))
1715 nextdir32
=(uint32
)nextdir
;
1716 assert((uint64
)nextdir32
==nextdir
);
1717 if (tif
->tif_flags
& TIFF_SWAB
)
1718 TIFFSwabLong(&nextdir32
);
1719 if (!WriteOK(tif
, &nextdir32
, sizeof (uint32
))) {
1720 TIFFErrorExt(tif
->tif_clientdata
, module
, "Error writing directory link");
1726 if (tif
->tif_flags
& TIFF_SWAB
)
1727 TIFFSwabLong8(&nextdir
);
1728 if (!WriteOK(tif
, &nextdir
, sizeof (uint64
))) {
1729 TIFFErrorExt(tif
->tif_clientdata
, module
, "Error writing directory link");
1734 * Leave directory state setup safely. We don't have
1735 * facilities for doing inserting and removing directories,
1736 * so it's safest to just invalidate everything. This
1737 * means that the caller can only append to the directory
1740 (*tif
->tif_cleanup
)(tif
);
1741 if ((tif
->tif_flags
& TIFF_MYBUFFER
) && tif
->tif_rawdata
) {
1742 _TIFFfree(tif
->tif_rawdata
);
1743 tif
->tif_rawdata
= NULL
;
1745 tif
->tif_rawdataoff
= 0;
1746 tif
->tif_rawdataloaded
= 0;
1748 tif
->tif_flags
&= ~(TIFF_BEENWRITING
|TIFF_BUFFERSETUP
|TIFF_POSTENCODE
|TIFF_BUF4WRITE
);
1749 TIFFFreeDirectory(tif
);
1750 TIFFDefaultDirectory(tif
);
1751 tif
->tif_diroff
= 0; /* force link on next write */
1752 tif
->tif_nextdiroff
= 0; /* next write must be at end */
1753 tif
->tif_curoff
= 0;
1754 tif
->tif_row
= (uint32
) -1;
1755 tif
->tif_curstrip
= (uint32
) -1;
1759 /* vim: set ts=8 sts=8 sw=8 noet: */