1
/* $Header: /cvsroot/osrs/libtiff/libtiff/tif_dir.c,v 1.32 2003/12/26 10:19:41 dron 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)
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
_TIFFsetByteArray(void** vpp, void* vp, long n)
47
_TIFFfree(*vpp), *vpp = 0;
48
if (vp && (*vpp = (void*) _TIFFmalloc(n)))
49
_TIFFmemcpy(*vpp, vp, n);
51
void _TIFFsetString(char** cpp, char* cp)
52
{ _TIFFsetByteArray((void**) cpp, (void*) cp, (long) (strlen(cp)+1)); }
53
void _TIFFsetNString(char** cpp, char* cp, long n)
54
{ _TIFFsetByteArray((void**) cpp, (void*) cp, n); }
55
void _TIFFsetShortArray(uint16** wpp, uint16* wp, long n)
56
{ _TIFFsetByteArray((void**) wpp, (void*) wp, n*sizeof (uint16)); }
57
void _TIFFsetLongArray(uint32** lpp, uint32* lp, long n)
58
{ _TIFFsetByteArray((void**) lpp, (void*) lp, n*sizeof (uint32)); }
59
void _TIFFsetFloatArray(float** fpp, float* fp, long n)
60
{ _TIFFsetByteArray((void**) fpp, (void*) fp, n*sizeof (float)); }
61
void _TIFFsetDoubleArray(double** dpp, double* dp, long n)
62
{ _TIFFsetByteArray((void**) dpp, (void*) dp, n*sizeof (double)); }
65
* Install extra samples information.
68
setExtraSamples(TIFFDirectory* td, va_list ap, int* v)
74
if ((uint16) *v > td->td_samplesperpixel)
76
va = va_arg(ap, uint16*);
77
if (*v > 0 && va == NULL) /* typically missing param */
79
for (i = 0; i < *v; i++)
80
if (va[i] > EXTRASAMPLE_UNASSALPHA)
82
td->td_extrasamples = (uint16) *v;
83
_TIFFsetShortArray(&td->td_sampleinfo, va, td->td_extrasamples);
88
checkInkNamesString(TIFF* tif, int slen, const char* s)
90
TIFFDirectory* td = &tif->tif_dir;
91
int i = td->td_samplesperpixel;
94
const char* ep = s+slen;
97
for (; *cp != '\0'; cp++)
105
TIFFError("TIFFSetField",
106
"%s: Invalid InkNames value; expecting %d names, found %d",
108
td->td_samplesperpixel,
109
td->td_samplesperpixel-i);
114
_TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
116
static const char module[] = "_TIFFVSetField";
118
TIFFDirectory* td = &tif->tif_dir;
126
case TIFFTAG_SUBFILETYPE:
127
td->td_subfiletype = va_arg(ap, uint32);
129
case TIFFTAG_IMAGEWIDTH:
130
td->td_imagewidth = va_arg(ap, uint32);
132
case TIFFTAG_IMAGELENGTH:
133
td->td_imagelength = va_arg(ap, uint32);
135
case TIFFTAG_BITSPERSAMPLE:
136
td->td_bitspersample = (uint16) va_arg(ap, int);
138
* If the data require post-decoding processing
139
* to byte-swap samples, set it up here. Note
140
* that since tags are required to be ordered,
141
* compression code can override this behaviour
142
* in the setup method if it wants to roll the
143
* post decoding work in with its normal work.
145
if (tif->tif_flags & TIFF_SWAB) {
146
if (td->td_bitspersample == 16)
147
tif->tif_postdecode = _TIFFSwab16BitData;
148
else if (td->td_bitspersample == 32)
149
tif->tif_postdecode = _TIFFSwab32BitData;
150
else if (td->td_bitspersample == 64)
151
tif->tif_postdecode = _TIFFSwab64BitData;
154
case TIFFTAG_COMPRESSION:
155
v = va_arg(ap, int) & 0xffff;
157
* If we're changing the compression scheme,
158
* the notify the previous module so that it
159
* can cleanup any state it's setup.
161
if (TIFFFieldSet(tif, FIELD_COMPRESSION)) {
162
if (td->td_compression == v)
164
(*tif->tif_cleanup)(tif);
165
tif->tif_flags &= ~TIFF_CODERSETUP;
168
* Setup new compression routine state.
170
if( (status = TIFFSetCompressionScheme(tif, v)) != 0 )
171
td->td_compression = (uint16) v;
175
case TIFFTAG_PHOTOMETRIC:
176
td->td_photometric = (uint16) va_arg(ap, int);
178
case TIFFTAG_THRESHHOLDING:
179
td->td_threshholding = (uint16) va_arg(ap, int);
181
case TIFFTAG_FILLORDER:
183
if (v != FILLORDER_LSB2MSB && v != FILLORDER_MSB2LSB)
185
td->td_fillorder = (uint16) v;
187
case TIFFTAG_DOCUMENTNAME:
188
_TIFFsetString(&td->td_documentname, va_arg(ap, char*));
191
_TIFFsetString(&td->td_artist, va_arg(ap, char*));
193
case TIFFTAG_DATETIME:
194
_TIFFsetString(&td->td_datetime, va_arg(ap, char*));
196
case TIFFTAG_HOSTCOMPUTER:
197
_TIFFsetString(&td->td_hostcomputer, va_arg(ap, char*));
199
case TIFFTAG_IMAGEDESCRIPTION:
200
_TIFFsetString(&td->td_imagedescription, va_arg(ap, char*));
203
_TIFFsetString(&td->td_make, va_arg(ap, char*));
206
_TIFFsetString(&td->td_model, va_arg(ap, char*));
208
case TIFFTAG_COPYRIGHT:
209
_TIFFsetString(&td->td_copyright, va_arg(ap, char*));
211
case TIFFTAG_ORIENTATION:
213
if (v < ORIENTATION_TOPLEFT || ORIENTATION_LEFTBOT < v) {
214
TIFFWarning(tif->tif_name,
215
"Bad value %ld for \"%s\" tag ignored",
216
v, _TIFFFieldWithTag(tif, tag)->field_name);
218
td->td_orientation = (uint16) v;
220
case TIFFTAG_SAMPLESPERPIXEL:
221
/* XXX should cross check -- e.g. if pallette, then 1 */
225
td->td_samplesperpixel = (uint16) v;
227
case TIFFTAG_ROWSPERSTRIP:
228
v32 = va_arg(ap, uint32);
231
td->td_rowsperstrip = v32;
232
if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
233
td->td_tilelength = v32;
234
td->td_tilewidth = td->td_imagewidth;
237
case TIFFTAG_MINSAMPLEVALUE:
238
td->td_minsamplevalue = (uint16) va_arg(ap, int);
240
case TIFFTAG_MAXSAMPLEVALUE:
241
td->td_maxsamplevalue = (uint16) va_arg(ap, int);
243
case TIFFTAG_SMINSAMPLEVALUE:
244
td->td_sminsamplevalue = (double) va_arg(ap, dblparam_t);
246
case TIFFTAG_SMAXSAMPLEVALUE:
247
td->td_smaxsamplevalue = (double) va_arg(ap, dblparam_t);
249
case TIFFTAG_XRESOLUTION:
250
td->td_xresolution = (float) va_arg(ap, dblparam_t);
252
case TIFFTAG_YRESOLUTION:
253
td->td_yresolution = (float) va_arg(ap, dblparam_t);
255
case TIFFTAG_PLANARCONFIG:
257
if (v != PLANARCONFIG_CONTIG && v != PLANARCONFIG_SEPARATE)
259
td->td_planarconfig = (uint16) v;
261
case TIFFTAG_PAGENAME:
262
_TIFFsetString(&td->td_pagename, va_arg(ap, char*));
264
case TIFFTAG_XPOSITION:
265
td->td_xposition = (float) va_arg(ap, dblparam_t);
267
case TIFFTAG_YPOSITION:
268
td->td_yposition = (float) va_arg(ap, dblparam_t);
270
case TIFFTAG_RESOLUTIONUNIT:
272
if (v < RESUNIT_NONE || RESUNIT_CENTIMETER < v)
274
td->td_resolutionunit = (uint16) v;
276
case TIFFTAG_PAGENUMBER:
277
td->td_pagenumber[0] = (uint16) va_arg(ap, int);
278
td->td_pagenumber[1] = (uint16) va_arg(ap, int);
280
case TIFFTAG_HALFTONEHINTS:
281
td->td_halftonehints[0] = (uint16) va_arg(ap, int);
282
td->td_halftonehints[1] = (uint16) va_arg(ap, int);
284
case TIFFTAG_COLORMAP:
285
v32 = (uint32)(1L<<td->td_bitspersample);
286
_TIFFsetShortArray(&td->td_colormap[0], va_arg(ap, uint16*), v32);
287
_TIFFsetShortArray(&td->td_colormap[1], va_arg(ap, uint16*), v32);
288
_TIFFsetShortArray(&td->td_colormap[2], va_arg(ap, uint16*), v32);
290
case TIFFTAG_EXTRASAMPLES:
291
if (!setExtraSamples(td, ap, &v))
294
case TIFFTAG_MATTEING:
295
td->td_extrasamples = (uint16) (va_arg(ap, int) != 0);
296
if (td->td_extrasamples) {
297
uint16 sv = EXTRASAMPLE_ASSOCALPHA;
298
_TIFFsetShortArray(&td->td_sampleinfo, &sv, 1);
301
case TIFFTAG_TILEWIDTH:
302
v32 = va_arg(ap, uint32);
304
if (tif->tif_mode != O_RDONLY)
306
TIFFWarning(tif->tif_name,
307
"Nonstandard tile width %d, convert file", v32);
309
td->td_tilewidth = v32;
310
tif->tif_flags |= TIFF_ISTILED;
312
case TIFFTAG_TILELENGTH:
313
v32 = va_arg(ap, uint32);
315
if (tif->tif_mode != O_RDONLY)
317
TIFFWarning(tif->tif_name,
318
"Nonstandard tile length %d, convert file", v32);
320
td->td_tilelength = v32;
321
tif->tif_flags |= TIFF_ISTILED;
323
case TIFFTAG_TILEDEPTH:
324
v32 = va_arg(ap, uint32);
327
td->td_tiledepth = v32;
329
case TIFFTAG_DATATYPE:
332
case DATATYPE_VOID: v = SAMPLEFORMAT_VOID; break;
333
case DATATYPE_INT: v = SAMPLEFORMAT_INT; break;
334
case DATATYPE_UINT: v = SAMPLEFORMAT_UINT; break;
335
case DATATYPE_IEEEFP: v = SAMPLEFORMAT_IEEEFP;break;
336
default: goto badvalue;
338
td->td_sampleformat = (uint16) v;
340
case TIFFTAG_SAMPLEFORMAT:
342
if (v < SAMPLEFORMAT_UINT || SAMPLEFORMAT_COMPLEXIEEEFP < v)
344
td->td_sampleformat = (uint16) v;
346
/* Try to fix up the SWAB function for complex data. */
347
if( td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
348
&& td->td_bitspersample == 32
349
&& tif->tif_postdecode == _TIFFSwab32BitData )
350
tif->tif_postdecode = _TIFFSwab16BitData;
351
else if( (td->td_sampleformat == SAMPLEFORMAT_COMPLEXINT
352
|| td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP)
353
&& td->td_bitspersample == 64
354
&& tif->tif_postdecode == _TIFFSwab64BitData )
355
tif->tif_postdecode = _TIFFSwab32BitData;
356
else if( td->td_sampleformat == SAMPLEFORMAT_COMPLEXIEEEFP
357
&& td->td_bitspersample == 128
358
&& tif->tif_postdecode == NULL )
359
tif->tif_postdecode = _TIFFSwab64BitData;
361
case TIFFTAG_IMAGEDEPTH:
362
td->td_imagedepth = va_arg(ap, uint32);
364
case TIFFTAG_STONITS:
365
d = va_arg(ap, dblparam_t);
370
/* Begin Pixar Tags */
371
case TIFFTAG_PIXAR_IMAGEFULLWIDTH:
372
td->td_imagefullwidth = va_arg(ap, uint32);
374
case TIFFTAG_PIXAR_IMAGEFULLLENGTH:
375
td->td_imagefulllength = va_arg(ap, uint32);
377
case TIFFTAG_PIXAR_TEXTUREFORMAT:
378
_TIFFsetString(&td->td_textureformat, va_arg(ap, char*));
380
case TIFFTAG_PIXAR_WRAPMODES:
381
_TIFFsetString(&td->td_wrapmodes, va_arg(ap, char*));
383
case TIFFTAG_PIXAR_FOVCOT:
384
td->td_fovcot = (float) va_arg(ap, dblparam_t);
386
case TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN:
387
_TIFFsetFloatArray(&td->td_matrixWorldToScreen,
388
va_arg(ap, float*), 16);
390
case TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA:
391
_TIFFsetFloatArray(&td->td_matrixWorldToCamera,
392
va_arg(ap, float*), 16);
397
if ((tif->tif_flags & TIFF_INSUBIFD) == 0) {
398
td->td_nsubifd = (uint16) va_arg(ap, int);
399
_TIFFsetLongArray(&td->td_subifd, va_arg(ap, uint32*),
400
(long) td->td_nsubifd);
402
TIFFError(module, "%s: Sorry, cannot nest SubIFDs",
407
case TIFFTAG_YCBCRCOEFFICIENTS:
408
_TIFFsetFloatArray(&td->td_ycbcrcoeffs, va_arg(ap, float*), 3);
410
case TIFFTAG_YCBCRPOSITIONING:
411
td->td_ycbcrpositioning = (uint16) va_arg(ap, int);
413
case TIFFTAG_YCBCRSUBSAMPLING:
414
td->td_ycbcrsubsampling[0] = (uint16) va_arg(ap, int);
415
td->td_ycbcrsubsampling[1] = (uint16) va_arg(ap, int);
417
case TIFFTAG_WHITEPOINT:
418
_TIFFsetFloatArray(&td->td_whitepoint, va_arg(ap, float*), 2);
420
case TIFFTAG_PRIMARYCHROMATICITIES:
421
_TIFFsetFloatArray(&td->td_primarychromas, va_arg(ap, float*), 6);
423
case TIFFTAG_TRANSFERFUNCTION:
424
v = (td->td_samplesperpixel - td->td_extrasamples) > 1 ? 3 : 1;
425
for (i = 0; i < v; i++)
426
_TIFFsetShortArray(&td->td_transferfunction[i],
427
va_arg(ap, uint16*), 1L<<td->td_bitspersample);
429
case TIFFTAG_REFERENCEBLACKWHITE:
430
/* XXX should check for null range */
431
_TIFFsetFloatArray(&td->td_refblackwhite, va_arg(ap, float*), 6);
434
td->td_inkset = (uint16) va_arg(ap, int);
436
case TIFFTAG_DOTRANGE:
437
/* XXX should check for null range */
438
td->td_dotrange[0] = (uint16) va_arg(ap, int);
439
td->td_dotrange[1] = (uint16) va_arg(ap, int);
441
case TIFFTAG_INKNAMES:
443
s = va_arg(ap, char*);
444
i = checkInkNamesString(tif, i, s);
447
_TIFFsetNString(&td->td_inknames, s, i);
448
td->td_inknameslen = i;
451
case TIFFTAG_NUMBEROFINKS:
452
td->td_ninks = (uint16) va_arg(ap, int);
454
case TIFFTAG_TARGETPRINTER:
455
_TIFFsetString(&td->td_targetprinter, va_arg(ap, char*));
457
case TIFFTAG_ICCPROFILE:
458
td->td_profileLength = (uint32) va_arg(ap, uint32);
459
_TIFFsetByteArray(&td->td_profileData, va_arg(ap, void*),
460
td->td_profileLength);
462
case TIFFTAG_PHOTOSHOP:
463
td->td_photoshopLength = (uint32) va_arg(ap, uint32);
464
_TIFFsetByteArray (&td->td_photoshopData, va_arg(ap, void*),
465
td->td_photoshopLength);
467
case TIFFTAG_RICHTIFFIPTC:
468
td->td_richtiffiptcLength = (uint32) va_arg(ap, uint32);
469
_TIFFsetLongArray ((uint32**)&td->td_richtiffiptcData,
471
td->td_richtiffiptcLength);
473
case TIFFTAG_XMLPACKET:
474
td->td_xmlpacketLength = (uint32) va_arg(ap, uint32);
475
_TIFFsetByteArray(&td->td_xmlpacketData, va_arg(ap, void*),
476
td->td_xmlpacketLength);
479
const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
481
int tv_size, iCustom;
484
* This can happen if multiple images are open with
485
* different codecs which have private tags. The
486
* global tag information table may then have tags
487
* that are valid for one file but not the other.
488
* If the client tries to set a tag that is not valid
489
* for the image's codec then we'll arrive here. This
490
* happens, for example, when tiffcp is used to convert
491
* between compression schemes and codec-specific tags
492
* are blindly copied.
494
if( fip == NULL || fip->field_bit != FIELD_CUSTOM )
497
"%s: Invalid %stag \"%s\" (not supported by codec)",
498
tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
499
_TIFFFieldWithTag(tif, tag)->field_name);
505
* Find the existing entry for this custom value.
508
for( iCustom = 0; iCustom < td->td_customValueCount; iCustom++ )
510
if( td->td_customValues[iCustom].info == fip )
512
tv = td->td_customValues + iCustom;
513
if( tv->value != NULL )
514
_TIFFfree( tv->value );
520
* Grow the custom list if the entry was not found.
524
td->td_customValueCount++;
525
if( td->td_customValueCount > 1 )
526
td->td_customValues = (TIFFTagValue *)
527
_TIFFrealloc(td->td_customValues,
528
sizeof(TIFFTagValue) * td->td_customValueCount);
530
td->td_customValues = (TIFFTagValue *)
531
_TIFFmalloc(sizeof(TIFFTagValue));
533
tv = td->td_customValues + (td->td_customValueCount-1);
540
* Set custom value ... save a copy of the custom tag value.
542
tv_size = TIFFDataWidth(fip->field_type);
543
if( fip->field_passcount )
544
tv->count = (int) va_arg(ap, int);
547
if( fip->field_passcount )
549
tv->value = _TIFFmalloc(tv_size * tv->count);
554
_TIFFmemcpy( tv->value, (void *) va_arg(ap,void*),
555
tv->count * tv_size );
557
else if( fip->field_type == TIFF_ASCII )
559
const char *value = (const char *) va_arg(ap,const char *);
560
tv->count = strlen(value)+1;
561
tv->value = _TIFFmalloc(tv->count);
566
strcpy( tv->value, value );
570
/* not supporting "pass by value" types yet */
571
TIFFWarning(module, " ... pass by value not implemented.");
573
tv->value = _TIFFmalloc(tv_size * tv->count);
578
_TIFFmemset( tv->value, 0, tv->count * tv_size );
584
TIFFSetFieldBit(tif, _TIFFFieldWithTag(tif, tag)->field_bit);
585
tif->tif_flags |= TIFF_DIRTYDIRECT;
590
TIFFError(module, "%.1000s: Bad value %d for \"%s\"",
591
tif->tif_name, v, _TIFFFieldWithTag(tif, tag)->field_name);
595
TIFFError(module, "%.1000s: Bad value %ld for \"%s\"",
596
tif->tif_name, v32, _TIFFFieldWithTag(tif, tag)->field_name);
600
TIFFError(module, "%.1000s: Bad value %f for \"%s\"",
601
tif->tif_name, d, _TIFFFieldWithTag(tif, tag)->field_name);
607
* Return 1/0 according to whether or not
608
* it is permissible to set the tag's value.
609
* Note that we allow ImageLength to be changed
610
* so that we can append and extend to images.
611
* Any other tag may not be altered once writing
612
* has commenced, unless its value has no effect
613
* on the format of the data that is written.
616
OkToChangeTag(TIFF* tif, ttag_t tag)
618
const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
619
if (!fip) { /* unknown tag */
620
TIFFError("TIFFSetField", "%s: Unknown %stag %u",
621
tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "", tag);
624
if (tag != TIFFTAG_IMAGELENGTH && (tif->tif_flags & TIFF_BEENWRITING) &&
625
!fip->field_oktochange) {
627
* Consult info table to see if tag can be changed
628
* after we've started writing. We only allow changes
629
* to those tags that don't/shouldn't affect the
630
* compression and/or format of the data.
632
TIFFError("TIFFSetField",
633
"%s: Cannot modify tag \"%s\" while writing",
634
tif->tif_name, fip->field_name);
641
* Record the value of a field in the
642
* internal directory structure. The
643
* field will be written to the file
644
* when/if the directory structure is
648
TIFFSetField(TIFF* tif, ttag_t tag, ...)
654
status = TIFFVSetField(tif, tag, ap);
660
* Like TIFFSetField, but taking a varargs
661
* parameter list. This routine is useful
662
* for building higher-level interfaces on
663
* top of the library.
666
TIFFVSetField(TIFF* tif, ttag_t tag, va_list ap)
668
return OkToChangeTag(tif, tag) ?
669
(*tif->tif_tagmethods.vsetfield)(tif, tag, ap) : 0;
673
_TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap)
675
TIFFDirectory* td = &tif->tif_dir;
679
case TIFFTAG_SUBFILETYPE:
680
*va_arg(ap, uint32*) = td->td_subfiletype;
682
case TIFFTAG_IMAGEWIDTH:
683
*va_arg(ap, uint32*) = td->td_imagewidth;
685
case TIFFTAG_IMAGELENGTH:
686
*va_arg(ap, uint32*) = td->td_imagelength;
688
case TIFFTAG_BITSPERSAMPLE:
689
*va_arg(ap, uint16*) = td->td_bitspersample;
691
case TIFFTAG_COMPRESSION:
692
*va_arg(ap, uint16*) = td->td_compression;
694
case TIFFTAG_PHOTOMETRIC:
695
*va_arg(ap, uint16*) = td->td_photometric;
697
case TIFFTAG_THRESHHOLDING:
698
*va_arg(ap, uint16*) = td->td_threshholding;
700
case TIFFTAG_FILLORDER:
701
*va_arg(ap, uint16*) = td->td_fillorder;
703
case TIFFTAG_DOCUMENTNAME:
704
*va_arg(ap, char**) = td->td_documentname;
707
*va_arg(ap, char**) = td->td_artist;
709
case TIFFTAG_DATETIME:
710
*va_arg(ap, char**) = td->td_datetime;
712
case TIFFTAG_HOSTCOMPUTER:
713
*va_arg(ap, char**) = td->td_hostcomputer;
715
case TIFFTAG_IMAGEDESCRIPTION:
716
*va_arg(ap, char**) = td->td_imagedescription;
719
*va_arg(ap, char**) = td->td_make;
722
*va_arg(ap, char**) = td->td_model;
724
case TIFFTAG_COPYRIGHT:
725
*va_arg(ap, char**) = td->td_copyright;
727
case TIFFTAG_ORIENTATION:
728
*va_arg(ap, uint16*) = td->td_orientation;
730
case TIFFTAG_SAMPLESPERPIXEL:
731
*va_arg(ap, uint16*) = td->td_samplesperpixel;
733
case TIFFTAG_ROWSPERSTRIP:
734
*va_arg(ap, uint32*) = td->td_rowsperstrip;
736
case TIFFTAG_MINSAMPLEVALUE:
737
*va_arg(ap, uint16*) = td->td_minsamplevalue;
739
case TIFFTAG_MAXSAMPLEVALUE:
740
*va_arg(ap, uint16*) = td->td_maxsamplevalue;
742
case TIFFTAG_SMINSAMPLEVALUE:
743
*va_arg(ap, double*) = td->td_sminsamplevalue;
745
case TIFFTAG_SMAXSAMPLEVALUE:
746
*va_arg(ap, double*) = td->td_smaxsamplevalue;
748
case TIFFTAG_XRESOLUTION:
749
*va_arg(ap, float*) = td->td_xresolution;
751
case TIFFTAG_YRESOLUTION:
752
*va_arg(ap, float*) = td->td_yresolution;
754
case TIFFTAG_PLANARCONFIG:
755
*va_arg(ap, uint16*) = td->td_planarconfig;
757
case TIFFTAG_XPOSITION:
758
*va_arg(ap, float*) = td->td_xposition;
760
case TIFFTAG_YPOSITION:
761
*va_arg(ap, float*) = td->td_yposition;
763
case TIFFTAG_PAGENAME:
764
*va_arg(ap, char**) = td->td_pagename;
766
case TIFFTAG_RESOLUTIONUNIT:
767
*va_arg(ap, uint16*) = td->td_resolutionunit;
769
case TIFFTAG_PAGENUMBER:
770
*va_arg(ap, uint16*) = td->td_pagenumber[0];
771
*va_arg(ap, uint16*) = td->td_pagenumber[1];
773
case TIFFTAG_HALFTONEHINTS:
774
*va_arg(ap, uint16*) = td->td_halftonehints[0];
775
*va_arg(ap, uint16*) = td->td_halftonehints[1];
777
case TIFFTAG_COLORMAP:
778
*va_arg(ap, uint16**) = td->td_colormap[0];
779
*va_arg(ap, uint16**) = td->td_colormap[1];
780
*va_arg(ap, uint16**) = td->td_colormap[2];
782
case TIFFTAG_STRIPOFFSETS:
783
case TIFFTAG_TILEOFFSETS:
784
*va_arg(ap, uint32**) = td->td_stripoffset;
786
case TIFFTAG_STRIPBYTECOUNTS:
787
case TIFFTAG_TILEBYTECOUNTS:
788
*va_arg(ap, uint32**) = td->td_stripbytecount;
790
case TIFFTAG_MATTEING:
791
*va_arg(ap, uint16*) =
792
(td->td_extrasamples == 1 &&
793
td->td_sampleinfo[0] == EXTRASAMPLE_ASSOCALPHA);
795
case TIFFTAG_EXTRASAMPLES:
796
*va_arg(ap, uint16*) = td->td_extrasamples;
797
*va_arg(ap, uint16**) = td->td_sampleinfo;
799
case TIFFTAG_TILEWIDTH:
800
*va_arg(ap, uint32*) = td->td_tilewidth;
802
case TIFFTAG_TILELENGTH:
803
*va_arg(ap, uint32*) = td->td_tilelength;
805
case TIFFTAG_TILEDEPTH:
806
*va_arg(ap, uint32*) = td->td_tiledepth;
808
case TIFFTAG_DATATYPE:
809
switch (td->td_sampleformat) {
810
case SAMPLEFORMAT_UINT:
811
*va_arg(ap, uint16*) = DATATYPE_UINT;
813
case SAMPLEFORMAT_INT:
814
*va_arg(ap, uint16*) = DATATYPE_INT;
816
case SAMPLEFORMAT_IEEEFP:
817
*va_arg(ap, uint16*) = DATATYPE_IEEEFP;
819
case SAMPLEFORMAT_VOID:
820
*va_arg(ap, uint16*) = DATATYPE_VOID;
824
case TIFFTAG_SAMPLEFORMAT:
825
*va_arg(ap, uint16*) = td->td_sampleformat;
827
case TIFFTAG_IMAGEDEPTH:
828
*va_arg(ap, uint32*) = td->td_imagedepth;
830
case TIFFTAG_STONITS:
831
*va_arg(ap, double*) = td->td_stonits;
834
*va_arg(ap, uint16*) = td->td_nsubifd;
835
*va_arg(ap, uint32**) = td->td_subifd;
837
case TIFFTAG_YCBCRCOEFFICIENTS:
838
*va_arg(ap, float**) = td->td_ycbcrcoeffs;
840
case TIFFTAG_YCBCRPOSITIONING:
841
*va_arg(ap, uint16*) = td->td_ycbcrpositioning;
843
case TIFFTAG_YCBCRSUBSAMPLING:
844
*va_arg(ap, uint16*) = td->td_ycbcrsubsampling[0];
845
*va_arg(ap, uint16*) = td->td_ycbcrsubsampling[1];
847
case TIFFTAG_WHITEPOINT:
848
*va_arg(ap, float**) = td->td_whitepoint;
850
case TIFFTAG_PRIMARYCHROMATICITIES:
851
*va_arg(ap, float**) = td->td_primarychromas;
853
case TIFFTAG_TRANSFERFUNCTION:
854
*va_arg(ap, uint16**) = td->td_transferfunction[0];
855
if (td->td_samplesperpixel - td->td_extrasamples > 1) {
856
*va_arg(ap, uint16**) = td->td_transferfunction[1];
857
*va_arg(ap, uint16**) = td->td_transferfunction[2];
860
case TIFFTAG_REFERENCEBLACKWHITE:
861
*va_arg(ap, float**) = td->td_refblackwhite;
864
*va_arg(ap, uint16*) = td->td_inkset;
866
case TIFFTAG_DOTRANGE:
867
*va_arg(ap, uint16*) = td->td_dotrange[0];
868
*va_arg(ap, uint16*) = td->td_dotrange[1];
870
case TIFFTAG_INKNAMES:
871
*va_arg(ap, char**) = td->td_inknames;
873
case TIFFTAG_NUMBEROFINKS:
874
*va_arg(ap, uint16*) = td->td_ninks;
876
case TIFFTAG_TARGETPRINTER:
877
*va_arg(ap, char**) = td->td_targetprinter;
879
case TIFFTAG_ICCPROFILE:
880
*va_arg(ap, uint32*) = td->td_profileLength;
881
*va_arg(ap, void**) = td->td_profileData;
883
case TIFFTAG_PHOTOSHOP:
884
*va_arg(ap, uint32*) = td->td_photoshopLength;
885
*va_arg(ap, void**) = td->td_photoshopData;
887
case TIFFTAG_RICHTIFFIPTC:
888
*va_arg(ap, uint32*) = td->td_richtiffiptcLength;
889
*va_arg(ap, void**) = td->td_richtiffiptcData;
891
case TIFFTAG_XMLPACKET:
892
*va_arg(ap, uint32*) = td->td_xmlpacketLength;
893
*va_arg(ap, void**) = td->td_xmlpacketData;
895
/* Begin Pixar Tags */
896
case TIFFTAG_PIXAR_IMAGEFULLWIDTH:
897
*va_arg(ap, uint32*) = td->td_imagefullwidth;
899
case TIFFTAG_PIXAR_IMAGEFULLLENGTH:
900
*va_arg(ap, uint32*) = td->td_imagefulllength;
902
case TIFFTAG_PIXAR_TEXTUREFORMAT:
903
*va_arg(ap, char**) = td->td_textureformat;
905
case TIFFTAG_PIXAR_WRAPMODES:
906
*va_arg(ap, char**) = td->td_wrapmodes;
908
case TIFFTAG_PIXAR_FOVCOT:
909
*va_arg(ap, float*) = td->td_fovcot;
911
case TIFFTAG_PIXAR_MATRIX_WORLDTOSCREEN:
912
*va_arg(ap, float**) = td->td_matrixWorldToScreen;
914
case TIFFTAG_PIXAR_MATRIX_WORLDTOCAMERA:
915
*va_arg(ap, float**) = td->td_matrixWorldToCamera;
921
const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
925
* This can happen if multiple images are open with
926
* different codecs which have private tags. The
927
* global tag information table may then have tags
928
* that are valid for one file but not the other.
929
* If the client tries to get a tag that is not valid
930
* for the image's codec then we'll arrive here.
932
if( fip == NULL || fip->field_bit != FIELD_CUSTOM )
934
TIFFError("_TIFFVGetField",
935
"%s: Invalid %stag \"%s\" (not supported by codec)",
936
tif->tif_name, isPseudoTag(tag) ? "pseudo-" : "",
937
_TIFFFieldWithTag(tif, tag)->field_name);
943
** Do we have a custom value?
946
for( i = 0; i < td->td_customValueCount; i++ )
948
TIFFTagValue *tv = td->td_customValues + i;
950
if( tv->info->field_tag != tag )
953
if( fip->field_passcount )
955
*va_arg(ap, u_short *) = (u_short) tv->count;
956
*va_arg(ap, void **) = tv->value;
960
else if( fip->field_type == TIFF_ASCII )
962
*va_arg(ap, void **) = tv->value;
968
printf( "TIFFVGetField ... pass by value not imp.\n" );
978
* Return the value of a field in the
979
* internal directory structure.
982
TIFFGetField(TIFF* tif, ttag_t tag, ...)
988
status = TIFFVGetField(tif, tag, ap);
994
* Like TIFFGetField, but taking a varargs
995
* parameter list. This routine is useful
996
* for building higher-level interfaces on
997
* top of the library.
1000
TIFFVGetField(TIFF* tif, ttag_t tag, va_list ap)
1002
const TIFFFieldInfo* fip = _TIFFFindFieldInfo(tif, tag, TIFF_ANY);
1003
return (fip && (isPseudoTag(tag) || TIFFFieldSet(tif, fip->field_bit)) ?
1004
(*tif->tif_tagmethods.vgetfield)(tif, tag, ap) : 0);
1007
#define CleanupField(member) { \
1009
_TIFFfree(td->member); \
1015
* Release storage associated with a directory.
1018
TIFFFreeDirectory(TIFF* tif)
1020
TIFFDirectory *td = &tif->tif_dir;
1023
CleanupField(td_colormap[0]);
1024
CleanupField(td_colormap[1]);
1025
CleanupField(td_colormap[2]);
1026
CleanupField(td_documentname);
1027
CleanupField(td_artist);
1028
CleanupField(td_datetime);
1029
CleanupField(td_hostcomputer);
1030
CleanupField(td_imagedescription);
1031
CleanupField(td_make);
1032
CleanupField(td_model);
1033
CleanupField(td_copyright);
1034
CleanupField(td_pagename);
1035
CleanupField(td_sampleinfo);
1036
CleanupField(td_subifd);
1037
CleanupField(td_ycbcrcoeffs);
1038
CleanupField(td_inknames);
1039
CleanupField(td_targetprinter);
1040
CleanupField(td_whitepoint);
1041
CleanupField(td_primarychromas);
1042
CleanupField(td_refblackwhite);
1043
CleanupField(td_transferfunction[0]);
1044
CleanupField(td_transferfunction[1]);
1045
CleanupField(td_transferfunction[2]);
1046
CleanupField(td_profileData);
1047
CleanupField(td_photoshopData);
1048
CleanupField(td_richtiffiptcData);
1049
CleanupField(td_xmlpacketData);
1050
CleanupField(td_stripoffset);
1051
CleanupField(td_stripbytecount);
1052
/* Begin Pixar Tags */
1053
CleanupField(td_textureformat);
1054
CleanupField(td_wrapmodes);
1055
CleanupField(td_matrixWorldToScreen);
1056
CleanupField(td_matrixWorldToCamera);
1057
/* End Pixar Tags */
1059
/* Cleanup custom tag values */
1060
for( i = 0; i < td->td_customValueCount; i++ )
1061
_TIFFfree( td->td_customValues[i].value );
1063
if( td->td_customValues != NULL )
1064
_TIFFfree( td->td_customValues );
1070
* Client Tag extension support (from Niles Ritter).
1072
static TIFFExtendProc _TIFFextender = (TIFFExtendProc) NULL;
1075
TIFFSetTagExtender(TIFFExtendProc extender)
1077
TIFFExtendProc prev = _TIFFextender;
1078
_TIFFextender = extender;
1083
* Setup for a new directory. Should we automatically call
1084
* TIFFWriteDirectory() if the current one is dirty?
1086
* The newly created directory will not exist on the file till
1087
* TIFFWriteDirectory(), TIFFFlush() or TIFFClose() is called.
1090
TIFFCreateDirectory(TIFF* tif)
1092
TIFFDefaultDirectory(tif);
1093
tif->tif_diroff = 0;
1094
tif->tif_nextdiroff = 0;
1095
tif->tif_curoff = 0;
1096
tif->tif_row = (uint32) -1;
1097
tif->tif_curstrip = (tstrip_t) -1;
1103
* Setup a default directory structure.
1106
TIFFDefaultDirectory(TIFF* tif)
1108
register TIFFDirectory* td = &tif->tif_dir;
1110
_TIFFSetupFieldInfo(tif);
1111
_TIFFmemset(td, 0, sizeof (*td));
1112
td->td_fillorder = FILLORDER_MSB2LSB;
1113
td->td_bitspersample = 1;
1114
td->td_threshholding = THRESHHOLD_BILEVEL;
1115
td->td_orientation = ORIENTATION_TOPLEFT;
1116
td->td_samplesperpixel = 1;
1117
td->td_rowsperstrip = (uint32) -1;
1118
td->td_tilewidth = (uint32) -1;
1119
td->td_tilelength = (uint32) -1;
1120
td->td_tiledepth = 1;
1121
td->td_resolutionunit = RESUNIT_INCH;
1122
td->td_sampleformat = SAMPLEFORMAT_UINT;
1123
td->td_imagedepth = 1;
1124
td->td_ycbcrsubsampling[0] = 2;
1125
td->td_ycbcrsubsampling[1] = 2;
1126
td->td_ycbcrpositioning = YCBCRPOSITION_CENTERED;
1127
td->td_inkset = INKSET_CMYK;
1129
tif->tif_postdecode = _TIFFNoPostDecode;
1130
tif->tif_tagmethods.vsetfield = _TIFFVSetField;
1131
tif->tif_tagmethods.vgetfield = _TIFFVGetField;
1132
tif->tif_tagmethods.printdir = NULL;
1134
* Give client code a chance to install their own
1135
* tag extensions & methods, prior to compression overloads.
1138
(*_TIFFextender)(tif);
1139
(void) TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
1141
* NB: The directory is marked dirty as a result of setting
1142
* up the default compression scheme. However, this really
1143
* isn't correct -- we want TIFF_DIRTYDIRECT to be set only
1144
* if the user does something. We could just do the setup
1145
* by hand, but it seems better to use the normal mechanism
1146
* (i.e. TIFFSetField).
1148
tif->tif_flags &= ~TIFF_DIRTYDIRECT;
1151
* As per http://bugzilla.remotesensing.org/show_bug.cgi?id=19
1152
* we clear the ISTILED flag when setting up a new directory.
1153
* Should we also be clearing stuff like INSUBIFD?
1155
tif->tif_flags &= ~TIFF_ISTILED;
1161
TIFFAdvanceDirectory(TIFF* tif, uint32* nextdir, toff_t* off)
1163
static const char module[] = "TIFFAdvanceDirectory";
1167
toff_t poff=*nextdir;
1168
if (poff+sizeof(uint16) > tif->tif_size)
1170
TIFFError(module, "%s: Error fetching directory count",
1174
_TIFFmemcpy(&dircount, tif->tif_base+poff, sizeof (uint16));
1175
if (tif->tif_flags & TIFF_SWAB)
1176
TIFFSwabShort(&dircount);
1177
poff+=sizeof (uint16)+dircount*sizeof (TIFFDirEntry);
1180
if (((toff_t) (poff+sizeof (uint32))) > tif->tif_size)
1182
TIFFError(module, "%s: Error fetching directory link",
1186
_TIFFmemcpy(nextdir, tif->tif_base+poff, sizeof (uint32));
1187
if (tif->tif_flags & TIFF_SWAB)
1188
TIFFSwabLong(nextdir);
1193
if (!SeekOK(tif, *nextdir) ||
1194
!ReadOK(tif, &dircount, sizeof (uint16))) {
1195
TIFFError(module, "%s: Error fetching directory count",
1199
if (tif->tif_flags & TIFF_SWAB)
1200
TIFFSwabShort(&dircount);
1202
*off = TIFFSeekFile(tif,
1203
dircount*sizeof (TIFFDirEntry), SEEK_CUR);
1205
(void) TIFFSeekFile(tif,
1206
dircount*sizeof (TIFFDirEntry), SEEK_CUR);
1207
if (!ReadOK(tif, nextdir, sizeof (uint32))) {
1208
TIFFError(module, "%s: Error fetching directory link",
1212
if (tif->tif_flags & TIFF_SWAB)
1213
TIFFSwabLong(nextdir);
1219
* Count the number of directories in a file.
1222
TIFFNumberOfDirectories(TIFF* tif)
1224
toff_t nextdir = tif->tif_header.tiff_diroff;
1227
while (nextdir != 0 && TIFFAdvanceDirectory(tif, &nextdir, NULL))
1233
* Set the n-th directory as the current directory.
1234
* NB: Directories are numbered starting at 0.
1237
TIFFSetDirectory(TIFF* tif, tdir_t dirn)
1242
nextdir = tif->tif_header.tiff_diroff;
1243
for (n = dirn; n > 0 && nextdir != 0; n--)
1244
if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
1246
tif->tif_nextdiroff = nextdir;
1248
* Set curdir to the actual directory index. The
1249
* -1 is because TIFFReadDirectory will increment
1250
* tif_curdir after successfully reading the directory.
1252
tif->tif_curdir = (dirn - n) - 1;
1254
* Reset tif_dirnumber counter nad start new list of seen directories.
1255
* We need this in order to prevent IFD loops.
1257
tif->tif_dirnumber = 0;
1258
return (TIFFReadDirectory(tif));
1262
* Set the current directory to be the directory
1263
* located at the specified file offset. This interface
1264
* is used mainly to access directories linked with
1265
* the SubIFD tag (e.g. thumbnail images).
1268
TIFFSetSubDirectory(TIFF* tif, uint32 diroff)
1270
tif->tif_nextdiroff = diroff;
1272
* Reset tif_dirnumber counter nad start new list of seen directories.
1273
* We need this in order to prevent IFD loops.
1275
tif->tif_dirnumber = 0;
1276
return (TIFFReadDirectory(tif));
1280
* Return file offset of the current directory.
1283
TIFFCurrentDirOffset(TIFF* tif)
1285
return (tif->tif_diroff);
1289
* Return an indication of whether or not we are
1290
* at the last directory in the file.
1293
TIFFLastDirectory(TIFF* tif)
1295
return (tif->tif_nextdiroff == 0);
1299
* Unlink the specified directory from the directory chain.
1302
TIFFUnlinkDirectory(TIFF* tif, tdir_t dirn)
1304
static const char module[] = "TIFFUnlinkDirectory";
1309
if (tif->tif_mode == O_RDONLY) {
1310
TIFFError(module, "Can not unlink directory in read-only file");
1314
* Go to the directory before the one we want
1315
* to unlink and nab the offset of the link
1316
* field we'll need to patch.
1318
nextdir = tif->tif_header.tiff_diroff;
1319
off = sizeof (uint16) + sizeof (uint16);
1320
for (n = dirn-1; n > 0; n--) {
1322
TIFFError(module, "Directory %d does not exist", dirn);
1325
if (!TIFFAdvanceDirectory(tif, &nextdir, &off))
1329
* Advance to the directory to be unlinked and fetch
1330
* the offset of the directory that follows.
1332
if (!TIFFAdvanceDirectory(tif, &nextdir, NULL))
1335
* Go back and patch the link field of the preceding
1336
* directory to point to the offset of the directory
1339
(void) TIFFSeekFile(tif, off, SEEK_SET);
1340
if (tif->tif_flags & TIFF_SWAB)
1341
TIFFSwabLong(&nextdir);
1342
if (!WriteOK(tif, &nextdir, sizeof (uint32))) {
1343
TIFFError(module, "Error writing directory link");
1347
* Leave directory state setup safely. We don't have
1348
* facilities for doing inserting and removing directories,
1349
* so it's safest to just invalidate everything. This
1350
* means that the caller can only append to the directory
1353
(*tif->tif_cleanup)(tif);
1354
if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {
1355
_TIFFfree(tif->tif_rawdata);
1356
tif->tif_rawdata = NULL;
1359
tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP|TIFF_POSTENCODE);
1360
TIFFFreeDirectory(tif);
1361
TIFFDefaultDirectory(tif);
1362
tif->tif_diroff = 0; /* force link on next write */
1363
tif->tif_nextdiroff = 0; /* next write must be at end */
1364
tif->tif_curoff = 0;
1365
tif->tif_row = (uint32) -1;
1366
tif->tif_curstrip = (tstrip_t) -1;
1372
* Author: Bruce Cameron <cameron@petris.com>
1374
* Set a table of tags that are to be replaced during directory process by the
1375
* 'IGNORE' state - or return TRUE/FALSE for the requested tag such that
1376
* 'ReadDirectory' can use the stored information.
1379
TIFFReassignTagToIgnore (enum TIFFIgnoreSense task, int TIFFtagID)
1381
static int TIFFignoretags [FIELD_LAST];
1382
static int tagcount = 0 ;
1383
int i; /* Loop index */
1384
int j; /* Loop index */
1389
if ( tagcount < (FIELD_LAST - 1) )
1391
for ( j = 0 ; j < tagcount ; ++j )
1392
{ /* Do not add duplicate tag */
1393
if ( TIFFignoretags [j] == TIFFtagID )
1396
TIFFignoretags [tagcount++] = TIFFtagID ;
1402
for ( i = 0 ; i < tagcount ; ++i )
1404
if ( TIFFignoretags [i] == TIFFtagID )
1410
tagcount = 0 ; /* Clear the list */