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
); 
  89 checkInkNamesString(TIFF
* tif
, int slen
, const char* s
) 
  91         TIFFDirectory
* td 
= &tif
->tif_dir
; 
  92         int i 
= td
->td_samplesperpixel
; 
  95                 const char* ep 
= s
+slen
; 
  98                         for (; *cp 
!= '\0'; cp
++) 
 106         TIFFError("TIFFSetField", 
 107             "%s: Invalid InkNames value; expecting %d names, found %d", 
 109             td
->td_samplesperpixel
, 
 110             td
->td_samplesperpixel
-i
); 
 116 _TIFFVSetField(TIFF
* tif
, ttag_t tag
, va_list ap
) 
 118         TIFFDirectory
* td 
= &tif
->tif_dir
; 
 127         case TIFFTAG_SUBFILETYPE
: 
 128                 td
->td_subfiletype 
= va_arg(ap
, uint32
); 
 130         case TIFFTAG_IMAGEWIDTH
: 
 131                 td
->td_imagewidth 
= va_arg(ap
, uint32
); 
 133         case TIFFTAG_IMAGELENGTH
: 
 134                 td
->td_imagelength 
= va_arg(ap
, uint32
); 
 136         case TIFFTAG_BITSPERSAMPLE
: 
 137                 td
->td_bitspersample 
= (uint16
) va_arg(ap
, int); 
 139                  * If the data require post-decoding processing 
 140                  * to byte-swap samples, set it up here.  Note 
 141                  * that since tags are required to be ordered, 
 142                  * compression code can override this behaviour 
 143                  * in the setup method if it wants to roll the 
 144                  * post decoding work in with its normal work. 
 146                 if (tif
->tif_flags 
& TIFF_SWAB
) { 
 147                         if (td
->td_bitspersample 
== 16) 
 148                                 tif
->tif_postdecode 
= _TIFFSwab16BitData
; 
 149                         else if (td
->td_bitspersample 
== 32) 
 150                                 tif
->tif_postdecode 
= _TIFFSwab32BitData
; 
 151                         else if (td
->td_bitspersample 
== 64) 
 152                                 tif
->tif_postdecode 
= _TIFFSwab64BitData
; 
 155         case TIFFTAG_COMPRESSION
: 
 156                 v16 
= va_arg(ap
, int) & 0xffff; 
 158                  * If we're changing the compression scheme, 
 159                  * the notify the previous module so that it 
 160                  * can cleanup any state it's setup. 
 162                 if (TIFFFieldSet(tif
, FIELD_COMPRESSION
)) { 
 163                         if (td
->td_compression 
== v16
) 
 165                         (*tif
->tif_cleanup
)(tif
); 
 166                         tif
->tif_flags 
&= ~TIFF_CODERSETUP
; 
 169                  * Setup new compression routine state. 
 171                 if( (status 
= TIFFSetCompressionScheme(tif
, v16
)) != 0 ) 
 172                         td
->td_compression 
= v16
; 
 174         case TIFFTAG_PHOTOMETRIC
: 
 175                 td
->td_photometric 
= (uint16
) va_arg(ap
, int); 
 177         case TIFFTAG_THRESHHOLDING
: 
 178                 td
->td_threshholding 
= (uint16
) va_arg(ap
, int); 
 180         case TIFFTAG_FILLORDER
: 
 182                 if (v 
!= FILLORDER_LSB2MSB 
&& v 
!= FILLORDER_MSB2LSB
) 
 184                 td
->td_fillorder 
= (uint16
) v
; 
 186         case TIFFTAG_DOCUMENTNAME
: 
 187                 _TIFFsetString(&td
->td_documentname
, va_arg(ap
, char*)); 
 190                 _TIFFsetString(&td
->td_artist
, va_arg(ap
, char*)); 
 192         case TIFFTAG_DATETIME
: 
 193                 _TIFFsetString(&td
->td_datetime
, va_arg(ap
, char*)); 
 195         case TIFFTAG_HOSTCOMPUTER
: 
 196                 _TIFFsetString(&td
->td_hostcomputer
, va_arg(ap
, char*)); 
 198         case TIFFTAG_IMAGEDESCRIPTION
: 
 199                 _TIFFsetString(&td
->td_imagedescription
, va_arg(ap
, char*)); 
 202                 _TIFFsetString(&td
->td_make
, va_arg(ap
, char*)); 
 205                 _TIFFsetString(&td
->td_model
, va_arg(ap
, char*)); 
 207         case TIFFTAG_SOFTWARE
: 
 208                 _TIFFsetString(&td
->td_software
, va_arg(ap
, char*)); 
 210         case TIFFTAG_ORIENTATION
: 
 212                 if (v 
< ORIENTATION_TOPLEFT 
|| ORIENTATION_LEFTBOT 
< v
) { 
 213                         TIFFWarning(tif
->tif_name
, 
 214                             "Bad value %ld for \"%s\" tag ignored", 
 215                             v
, _TIFFFieldWithTag(tif
, tag
)->field_name
); 
 217                         td
->td_orientation 
= (uint16
) v
; 
 219         case TIFFTAG_SAMPLESPERPIXEL
: 
 220                 /* XXX should cross check -- e.g. if pallette, then 1 */ 
 224                 td
->td_samplesperpixel 
= (uint16
) v
; 
 226         case TIFFTAG_ROWSPERSTRIP
: 
 227                 v32 
= va_arg(ap
, uint32
); 
 230                 td
->td_rowsperstrip 
= v32
; 
 231                 if (!TIFFFieldSet(tif
, FIELD_TILEDIMENSIONS
)) { 
 232                         td
->td_tilelength 
= v32
; 
 233                         td
->td_tilewidth 
= td
->td_imagewidth
; 
 236         case TIFFTAG_MINSAMPLEVALUE
: 
 237                 td
->td_minsamplevalue 
= (uint16
) va_arg(ap
, int); 
 239         case TIFFTAG_MAXSAMPLEVALUE
: 
 240                 td
->td_maxsamplevalue 
= (uint16
) va_arg(ap
, int); 
 242         case TIFFTAG_SMINSAMPLEVALUE
: 
 243                 td
->td_sminsamplevalue 
= (double) va_arg(ap
, dblparam_t
); 
 245         case TIFFTAG_SMAXSAMPLEVALUE
: 
 246                 td
->td_smaxsamplevalue 
= (double) va_arg(ap
, dblparam_t
); 
 248         case TIFFTAG_XRESOLUTION
: 
 249                 td
->td_xresolution 
= (float) va_arg(ap
, dblparam_t
); 
 251         case TIFFTAG_YRESOLUTION
: 
 252                 td
->td_yresolution 
= (float) va_arg(ap
, dblparam_t
); 
 254         case TIFFTAG_PLANARCONFIG
: 
 256                 if (v 
!= PLANARCONFIG_CONTIG 
&& v 
!= PLANARCONFIG_SEPARATE
) 
 258                 td
->td_planarconfig 
= (uint16
) v
; 
 260         case TIFFTAG_PAGENAME
: 
 261                 _TIFFsetString(&td
->td_pagename
, va_arg(ap
, char*)); 
 263         case TIFFTAG_XPOSITION
: 
 264                 td
->td_xposition 
= (float) va_arg(ap
, dblparam_t
); 
 266         case TIFFTAG_YPOSITION
: 
 267                 td
->td_yposition 
= (float) va_arg(ap
, dblparam_t
); 
 269         case TIFFTAG_RESOLUTIONUNIT
: 
 271                 if (v 
< RESUNIT_NONE 
|| RESUNIT_CENTIMETER 
< v
) 
 273                 td
->td_resolutionunit 
= (uint16
) v
; 
 275         case TIFFTAG_PAGENUMBER
: 
 276                 td
->td_pagenumber
[0] = (uint16
) va_arg(ap
, int); 
 277                 td
->td_pagenumber
[1] = (uint16
) va_arg(ap
, int); 
 279         case TIFFTAG_HALFTONEHINTS
: 
 280                 td
->td_halftonehints
[0] = (uint16
) va_arg(ap
, int); 
 281                 td
->td_halftonehints
[1] = (uint16
) va_arg(ap
, int); 
 283         case TIFFTAG_COLORMAP
: 
 284                 v32 
= (uint32
)(1L<<td
->td_bitspersample
); 
 285                 _TIFFsetShortArray(&td
->td_colormap
[0], va_arg(ap
, uint16
*), v32
); 
 286                 _TIFFsetShortArray(&td
->td_colormap
[1], va_arg(ap
, uint16
*), v32
); 
 287                 _TIFFsetShortArray(&td
->td_colormap
[2], va_arg(ap
, uint16
*), v32
); 
 289         case TIFFTAG_EXTRASAMPLES
: 
 290                 if (!setExtraSamples(td
, ap
, &v
)) 
 293         case TIFFTAG_MATTEING
: 
 294                 td
->td_extrasamples 
= (uint16
) (va_arg(ap
, int) != 0); 
 295                 if (td
->td_extrasamples
) { 
 296                         uint16 sv 
= EXTRASAMPLE_ASSOCALPHA
; 
 297                         _TIFFsetShortArray(&td
->td_sampleinfo
, &sv
, 1); 
 300         case TIFFTAG_TILEWIDTH
: 
 301                 v32 
= va_arg(ap
, uint32
); 
 303                         if (tif
->tif_mode 
!= O_RDONLY
) 
 305                         TIFFWarning(tif
->tif_name
, 
 306                             "Nonstandard tile width %d, convert file", v32
); 
 308                 td
->td_tilewidth 
= v32
; 
 309                 tif
->tif_flags 
|= TIFF_ISTILED
; 
 311         case TIFFTAG_TILELENGTH
: 
 312                 v32 
= va_arg(ap
, uint32
); 
 314                         if (tif
->tif_mode 
!= O_RDONLY
) 
 316                         TIFFWarning(tif
->tif_name
, 
 317                             "Nonstandard tile length %d, convert file", v32
); 
 319                 td
->td_tilelength 
= v32
; 
 320                 tif
->tif_flags 
|= TIFF_ISTILED
; 
 322         case TIFFTAG_TILEDEPTH
: 
 323                 v32 
= va_arg(ap
, uint32
); 
 326                 td
->td_tiledepth 
= v32
; 
 328         case TIFFTAG_DATATYPE
: 
 331                 case DATATYPE_VOID
:     v 
= SAMPLEFORMAT_VOID
;  break; 
 332                 case DATATYPE_INT
:      v 
= SAMPLEFORMAT_INT
;   break; 
 333                 case DATATYPE_UINT
:     v 
= SAMPLEFORMAT_UINT
;  break; 
 334                 case DATATYPE_IEEEFP
:   v 
= SAMPLEFORMAT_IEEEFP
;break; 
 335                 default:                goto badvalue
; 
 337                 td
->td_sampleformat 
= (uint16
) v
; 
 339         case TIFFTAG_SAMPLEFORMAT
: 
 341                 if (v 
< SAMPLEFORMAT_UINT 
|| SAMPLEFORMAT_VOID 
< v
) 
 343                 td
->td_sampleformat 
= (uint16
) v
; 
 345         case TIFFTAG_IMAGEDEPTH
: 
 346                 td
->td_imagedepth 
= va_arg(ap
, uint32
); 
 348         case TIFFTAG_STONITS
: 
 349                 d 
= va_arg(ap
, dblparam_t
); 
 356                 if ((tif
->tif_flags 
& TIFF_INSUBIFD
) == 0) { 
 357                         td
->td_nsubifd 
= (uint16
) va_arg(ap
, int); 
 358                         _TIFFsetLongArray(&td
->td_subifd
, va_arg(ap
, uint32
*), 
 359                             (long) td
->td_nsubifd
); 
 361                         TIFFError(tif
->tif_name
, "Sorry, cannot nest SubIFDs"); 
 367         case TIFFTAG_YCBCRCOEFFICIENTS
: 
 368                 _TIFFsetFloatArray(&td
->td_ycbcrcoeffs
, va_arg(ap
, float*), 3); 
 370         case TIFFTAG_YCBCRPOSITIONING
: 
 371                 td
->td_ycbcrpositioning 
= (uint16
) va_arg(ap
, int); 
 373         case TIFFTAG_YCBCRSUBSAMPLING
: 
 374                 td
->td_ycbcrsubsampling
[0] = (uint16
) va_arg(ap
, int); 
 375                 td
->td_ycbcrsubsampling
[1] = (uint16
) va_arg(ap
, int); 
 378 #ifdef COLORIMETRY_SUPPORT 
 379         case TIFFTAG_WHITEPOINT
: 
 380                 _TIFFsetFloatArray(&td
->td_whitepoint
, va_arg(ap
, float*), 2); 
 382         case TIFFTAG_PRIMARYCHROMATICITIES
: 
 383                 _TIFFsetFloatArray(&td
->td_primarychromas
, va_arg(ap
, float*), 6); 
 385         case TIFFTAG_TRANSFERFUNCTION
: 
 386                 v 
= (td
->td_samplesperpixel 
- td
->td_extrasamples
) > 1 ? 3 : 1; 
 387                 for (i 
= 0; i 
< v
; i
++) 
 388                         _TIFFsetShortArray(&td
->td_transferfunction
[i
], 
 389                             va_arg(ap
, uint16
*), 1L<<td
->td_bitspersample
); 
 391         case TIFFTAG_REFERENCEBLACKWHITE
: 
 392                 /* XXX should check for null range */ 
 393                 _TIFFsetFloatArray(&td
->td_refblackwhite
, va_arg(ap
, float*), 6); 
 398                 td
->td_inkset 
= (uint16
) va_arg(ap
, int); 
 400         case TIFFTAG_DOTRANGE
: 
 401                 /* XXX should check for null range */ 
 402                 td
->td_dotrange
[0] = (uint16
) va_arg(ap
, int); 
 403                 td
->td_dotrange
[1] = (uint16
) va_arg(ap
, int); 
 405         case TIFFTAG_INKNAMES
: 
 407                 s 
= va_arg(ap
, char*); 
 408                 i 
= checkInkNamesString(tif
, i
, s
); 
 411                         _TIFFsetNString(&td
->td_inknames
, s
, i
); 
 412                         td
->td_inknameslen 
= i
; 
 415         case TIFFTAG_NUMBEROFINKS
: 
 416                 td
->td_ninks 
= (uint16
) va_arg(ap
, int); 
 418         case TIFFTAG_TARGETPRINTER
: 
 419                 _TIFFsetString(&td
->td_targetprinter
, va_arg(ap
, char*)); 
 423         case TIFFTAG_ICCPROFILE
: 
 424                 td
->td_profileLength 
= (uint32
) va_arg(ap
, uint32
); 
 425                 _TIFFsetByteArray(&td
->td_profileData
, va_arg(ap
, void*), 
 426                     td
->td_profileLength
); 
 429 #ifdef PHOTOSHOP_SUPPORT 
 430         case TIFFTAG_PHOTOSHOP
: 
 431                 td
->td_photoshopLength 
= (uint32
) va_arg(ap
, uint32
); 
 432                 _TIFFsetByteArray (&td
->td_photoshopData
, va_arg(ap
, void*), 
 433                         td
->td_photoshopLength
); 
 437     case TIFFTAG_RICHTIFFIPTC
:  
 438                 td
->td_richtiffiptcLength 
= (uint32
) va_arg(ap
, uint32
); 
 439 #ifdef PHOTOSHOP_SUPPORT 
 440                 _TIFFsetLongArray ((uint32
**)&td
->td_richtiffiptcData
, va_arg(ap
, uint32
*), 
 441                         td
->td_richtiffiptcLength
); 
 443                 _TIFFsetByteArray (&td
->td_photoshopData
, va_arg(ap
, void*), 
 444                         td
->td_photoshopLength
); 
 450                  * This can happen if multiple images are open with 
 451                  * different codecs which have private tags.  The 
 452                  * global tag information table may then have tags 
 453                  * that are valid for one file but not the other.  
 454                  * If the client tries to set a tag that is not valid 
 455                  * for the image's codec then we'll arrive here.  This 
 456                  * happens, for example, when tiffcp is used to convert 
 457                  * between compression schemes and codec-specific tags 
 458                  * are blindly copied. 
 460                 TIFFError("TIFFSetField", 
 461                     "%s: Invalid %stag \"%s\" (not supported by codec)", 
 462                     tif
->tif_name
, isPseudoTag(tag
) ? "pseduo-" : "", 
 463                     _TIFFFieldWithTag(tif
, tag
)->field_name
); 
 468                 TIFFSetFieldBit(tif
, _TIFFFieldWithTag(tif
, tag
)->field_bit
); 
 469                 tif
->tif_flags 
|= TIFF_DIRTYDIRECT
; 
 474         TIFFError(tif
->tif_name
, "%d: Bad value for \"%s\"", v
, 
 475             _TIFFFieldWithTag(tif
, tag
)->field_name
); 
 479         TIFFError(tif
->tif_name
, "%ld: Bad value for \"%s\"", v32
, 
 480             _TIFFFieldWithTag(tif
, tag
)->field_name
); 
 484         TIFFError(tif
->tif_name
, "%f: Bad value for \"%s\"", d
, 
 485             _TIFFFieldWithTag(tif
, tag
)->field_name
); 
 491  * Return 1/0 according to whether or not 
 492  * it is permissible to set the tag's value. 
 493  * Note that we allow ImageLength to be changed 
 494  * so that we can append and extend to images. 
 495  * Any other tag may not be altered once writing 
 496  * has commenced, unless its value has no effect 
 497  * on the format of the data that is written. 
 500 OkToChangeTag(TIFF
* tif
, ttag_t tag
) 
 502         const TIFFFieldInfo
* fip 
= _TIFFFindFieldInfo(tif
, tag
, TIFF_ANY
); 
 503         if (!fip
) {                     /* unknown tag */ 
 504                 TIFFError("TIFFSetField", "%s: Unknown %stag %u", 
 505                     tif
->tif_name
, isPseudoTag(tag
) ? "pseudo-" : "", tag
); 
 508         if (tag 
!= TIFFTAG_IMAGELENGTH 
&& (tif
->tif_flags 
& TIFF_BEENWRITING
) && 
 509             !fip
->field_oktochange
) { 
 511                  * Consult info table to see if tag can be changed 
 512                  * after we've started writing.  We only allow changes 
 513                  * to those tags that don't/shouldn't affect the 
 514                  * compression and/or format of the data. 
 516                 TIFFError("TIFFSetField", 
 517                     "%s: Cannot modify tag \"%s\" while writing", 
 518                     tif
->tif_name
, fip
->field_name
); 
 525  * Record the value of a field in the 
 526  * internal directory structure.  The 
 527  * field will be written to the file 
 528  * when/if the directory structure is 
 532 TIFFSetField(TIFF
* tif
, ttag_t tag
, ...) 
 538         status 
= TIFFVSetField(tif
, tag
, ap
); 
 544  * Like TIFFSetField, but taking a varargs 
 545  * parameter list.  This routine is useful 
 546  * for building higher-level interfaces on 
 547  * top of the library. 
 550 TIFFVSetField(TIFF
* tif
, ttag_t tag
, va_list ap
) 
 552         return OkToChangeTag(tif
, tag
) ? 
 553             (*tif
->tif_vsetfield
)(tif
, tag
, ap
) : 0; 
 557 _TIFFVGetField(TIFF
* tif
, ttag_t tag
, va_list ap
) 
 559         TIFFDirectory
* td 
= &tif
->tif_dir
; 
 562         case TIFFTAG_SUBFILETYPE
: 
 563                 *va_arg(ap
, uint32
*) = td
->td_subfiletype
; 
 565         case TIFFTAG_IMAGEWIDTH
: 
 566                 *va_arg(ap
, uint32
*) = td
->td_imagewidth
; 
 568         case TIFFTAG_IMAGELENGTH
: 
 569                 *va_arg(ap
, uint32
*) = td
->td_imagelength
; 
 571         case TIFFTAG_BITSPERSAMPLE
: 
 572                 *va_arg(ap
, uint16
*) = td
->td_bitspersample
; 
 574         case TIFFTAG_COMPRESSION
: 
 575                 *va_arg(ap
, uint16
*) = td
->td_compression
; 
 577         case TIFFTAG_PHOTOMETRIC
: 
 578                 *va_arg(ap
, uint16
*) = td
->td_photometric
; 
 580         case TIFFTAG_THRESHHOLDING
: 
 581                 *va_arg(ap
, uint16
*) = td
->td_threshholding
; 
 583         case TIFFTAG_FILLORDER
: 
 584                 *va_arg(ap
, uint16
*) = td
->td_fillorder
; 
 586         case TIFFTAG_DOCUMENTNAME
: 
 587                 *va_arg(ap
, char**) = td
->td_documentname
; 
 590                 *va_arg(ap
, char**) = td
->td_artist
; 
 592         case TIFFTAG_DATETIME
: 
 593                 *va_arg(ap
, char**) = td
->td_datetime
; 
 595         case TIFFTAG_HOSTCOMPUTER
: 
 596                 *va_arg(ap
, char**) = td
->td_hostcomputer
; 
 598         case TIFFTAG_IMAGEDESCRIPTION
: 
 599                 *va_arg(ap
, char**) = td
->td_imagedescription
; 
 602                 *va_arg(ap
, char**) = td
->td_make
; 
 605                 *va_arg(ap
, char**) = td
->td_model
; 
 607         case TIFFTAG_SOFTWARE
: 
 608                 *va_arg(ap
, char**) = td
->td_software
; 
 610         case TIFFTAG_ORIENTATION
: 
 611                 *va_arg(ap
, uint16
*) = td
->td_orientation
; 
 613         case TIFFTAG_SAMPLESPERPIXEL
: 
 614                 *va_arg(ap
, uint16
*) = td
->td_samplesperpixel
; 
 616         case TIFFTAG_ROWSPERSTRIP
: 
 617                 *va_arg(ap
, uint32
*) = td
->td_rowsperstrip
; 
 619         case TIFFTAG_MINSAMPLEVALUE
: 
 620                 *va_arg(ap
, uint16
*) = td
->td_minsamplevalue
; 
 622         case TIFFTAG_MAXSAMPLEVALUE
: 
 623                 *va_arg(ap
, uint16
*) = td
->td_maxsamplevalue
; 
 625         case TIFFTAG_SMINSAMPLEVALUE
: 
 626                 *va_arg(ap
, double*) = td
->td_sminsamplevalue
; 
 628         case TIFFTAG_SMAXSAMPLEVALUE
: 
 629                 *va_arg(ap
, double*) = td
->td_smaxsamplevalue
; 
 631         case TIFFTAG_XRESOLUTION
: 
 632                 *va_arg(ap
, float*) = td
->td_xresolution
; 
 634         case TIFFTAG_YRESOLUTION
: 
 635                 *va_arg(ap
, float*) = td
->td_yresolution
; 
 637         case TIFFTAG_PLANARCONFIG
: 
 638                 *va_arg(ap
, uint16
*) = td
->td_planarconfig
; 
 640         case TIFFTAG_XPOSITION
: 
 641                 *va_arg(ap
, float*) = td
->td_xposition
; 
 643         case TIFFTAG_YPOSITION
: 
 644                 *va_arg(ap
, float*) = td
->td_yposition
; 
 646         case TIFFTAG_PAGENAME
: 
 647                 *va_arg(ap
, char**) = td
->td_pagename
; 
 649         case TIFFTAG_RESOLUTIONUNIT
: 
 650                 *va_arg(ap
, uint16
*) = td
->td_resolutionunit
; 
 652         case TIFFTAG_PAGENUMBER
: 
 653                 *va_arg(ap
, uint16
*) = td
->td_pagenumber
[0]; 
 654                 *va_arg(ap
, uint16
*) = td
->td_pagenumber
[1]; 
 656         case TIFFTAG_HALFTONEHINTS
: 
 657                 *va_arg(ap
, uint16
*) = td
->td_halftonehints
[0]; 
 658                 *va_arg(ap
, uint16
*) = td
->td_halftonehints
[1]; 
 660         case TIFFTAG_COLORMAP
: 
 661                 *va_arg(ap
, uint16
**) = td
->td_colormap
[0]; 
 662                 *va_arg(ap
, uint16
**) = td
->td_colormap
[1]; 
 663                 *va_arg(ap
, uint16
**) = td
->td_colormap
[2]; 
 665         case TIFFTAG_STRIPOFFSETS
: 
 666         case TIFFTAG_TILEOFFSETS
: 
 667                 *va_arg(ap
, uint32
**) = td
->td_stripoffset
; 
 669         case TIFFTAG_STRIPBYTECOUNTS
: 
 670         case TIFFTAG_TILEBYTECOUNTS
: 
 671                 *va_arg(ap
, uint32
**) = td
->td_stripbytecount
; 
 673         case TIFFTAG_MATTEING
: 
 674                 *va_arg(ap
, uint16
*) = 
 675                     (td
->td_extrasamples 
== 1 && 
 676                      td
->td_sampleinfo
[0] == EXTRASAMPLE_ASSOCALPHA
); 
 678         case TIFFTAG_EXTRASAMPLES
: 
 679                 *va_arg(ap
, uint16
*) = td
->td_extrasamples
; 
 680                 *va_arg(ap
, uint16
**) = td
->td_sampleinfo
; 
 682         case TIFFTAG_TILEWIDTH
: 
 683                 *va_arg(ap
, uint32
*) = td
->td_tilewidth
; 
 685         case TIFFTAG_TILELENGTH
: 
 686                 *va_arg(ap
, uint32
*) = td
->td_tilelength
; 
 688         case TIFFTAG_TILEDEPTH
: 
 689                 *va_arg(ap
, uint32
*) = td
->td_tiledepth
; 
 691         case TIFFTAG_DATATYPE
: 
 692                 switch (td
->td_sampleformat
) { 
 693                 case SAMPLEFORMAT_UINT
: 
 694                         *va_arg(ap
, uint16
*) = DATATYPE_UINT
; 
 696                 case SAMPLEFORMAT_INT
: 
 697                         *va_arg(ap
, uint16
*) = DATATYPE_INT
; 
 699                 case SAMPLEFORMAT_IEEEFP
: 
 700                         *va_arg(ap
, uint16
*) = DATATYPE_IEEEFP
; 
 702                 case SAMPLEFORMAT_VOID
: 
 703                         *va_arg(ap
, uint16
*) = DATATYPE_VOID
; 
 707         case TIFFTAG_SAMPLEFORMAT
: 
 708                 *va_arg(ap
, uint16
*) = td
->td_sampleformat
; 
 710         case TIFFTAG_IMAGEDEPTH
: 
 711                 *va_arg(ap
, uint32
*) = td
->td_imagedepth
; 
 713         case TIFFTAG_STONITS
: 
 714                 *va_arg(ap
, double*) = td
->td_stonits
; 
 718                 *va_arg(ap
, uint16
*) = td
->td_nsubifd
; 
 719                 *va_arg(ap
, uint32
**) = td
->td_subifd
; 
 723         case TIFFTAG_YCBCRCOEFFICIENTS
: 
 724                 *va_arg(ap
, float**) = td
->td_ycbcrcoeffs
; 
 726         case TIFFTAG_YCBCRPOSITIONING
: 
 727                 *va_arg(ap
, uint16
*) = td
->td_ycbcrpositioning
; 
 729         case TIFFTAG_YCBCRSUBSAMPLING
: 
 730                 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[0]; 
 731                 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[1]; 
 734 #ifdef COLORIMETRY_SUPPORT 
 735         case TIFFTAG_WHITEPOINT
: 
 736                 *va_arg(ap
, float**) = td
->td_whitepoint
; 
 738         case TIFFTAG_PRIMARYCHROMATICITIES
: 
 739                 *va_arg(ap
, float**) = td
->td_primarychromas
; 
 741         case TIFFTAG_TRANSFERFUNCTION
: 
 742                 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[0]; 
 743                 if (td
->td_samplesperpixel 
- td
->td_extrasamples 
> 1) { 
 744                         *va_arg(ap
, uint16
**) = td
->td_transferfunction
[1]; 
 745                         *va_arg(ap
, uint16
**) = td
->td_transferfunction
[2]; 
 748         case TIFFTAG_REFERENCEBLACKWHITE
: 
 749                 *va_arg(ap
, float**) = td
->td_refblackwhite
; 
 754                 *va_arg(ap
, uint16
*) = td
->td_inkset
; 
 756         case TIFFTAG_DOTRANGE
: 
 757                 *va_arg(ap
, uint16
*) = td
->td_dotrange
[0]; 
 758                 *va_arg(ap
, uint16
*) = td
->td_dotrange
[1]; 
 760         case TIFFTAG_INKNAMES
: 
 761                 *va_arg(ap
, char**) = td
->td_inknames
; 
 763         case TIFFTAG_NUMBEROFINKS
: 
 764                 *va_arg(ap
, uint16
*) = td
->td_ninks
; 
 766         case TIFFTAG_TARGETPRINTER
: 
 767                 *va_arg(ap
, char**) = td
->td_targetprinter
; 
 771         case TIFFTAG_ICCPROFILE
: 
 772                 *va_arg(ap
, uint32
*) = td
->td_profileLength
; 
 773                 *va_arg(ap
, void**) = td
->td_profileData
; 
 776 #ifdef PHOTOSHOP_SUPPORT 
 777         case TIFFTAG_PHOTOSHOP
: 
 778                 *va_arg(ap
, uint32
*) = td
->td_photoshopLength
; 
 779                 *va_arg(ap
, void**) = td
->td_photoshopData
; 
 783         case TIFFTAG_RICHTIFFIPTC
: 
 784                 *va_arg(ap
, uint32
*) = td
->td_richtiffiptcLength
; 
 785                 *va_arg(ap
, void**) = td
->td_richtiffiptcData
; 
 790                  * This can happen if multiple images are open with 
 791                  * different codecs which have private tags.  The 
 792                  * global tag information table may then have tags 
 793                  * that are valid for one file but not the other.  
 794                  * If the client tries to get a tag that is not valid 
 795                  * for the image's codec then we'll arrive here. 
 797                 TIFFError("TIFFGetField", 
 798                     "%s: Invalid %stag \"%s\" (not supported by codec)", 
 799                     tif
->tif_name
, isPseudoTag(tag
) ? "pseudo-" : "", 
 800                     _TIFFFieldWithTag(tif
, tag
)->field_name
); 
 807  * Return the value of a field in the 
 808  * internal directory structure. 
 811 TIFFGetField(TIFF
* tif
, ttag_t tag
, ...) 
 817         status 
= TIFFVGetField(tif
, tag
, ap
); 
 823  * Like TIFFGetField, but taking a varargs 
 824  * parameter list.  This routine is useful 
 825  * for building higher-level interfaces on 
 826  * top of the library. 
 829 TIFFVGetField(TIFF
* tif
, ttag_t tag
, va_list ap
) 
 831         const TIFFFieldInfo
* fip 
= _TIFFFindFieldInfo(tif
, tag
, TIFF_ANY
); 
 832         return (fip 
&& (isPseudoTag(tag
) || TIFFFieldSet(tif
, fip
->field_bit
)) ? 
 833             (*tif
->tif_vgetfield
)(tif
, tag
, ap
) : 0); 
 836 #define CleanupField(member) {          \ 
 838         _TIFFfree(td->member);          \ 
 844  * Release storage associated with a directory. 
 847 TIFFFreeDirectory(TIFF
* tif
) 
 849         register TIFFDirectory 
*td 
= &tif
->tif_dir
; 
 851         CleanupField(td_colormap
[0]); 
 852         CleanupField(td_colormap
[1]); 
 853         CleanupField(td_colormap
[2]); 
 854         CleanupField(td_documentname
); 
 855         CleanupField(td_artist
); 
 856         CleanupField(td_datetime
); 
 857         CleanupField(td_hostcomputer
); 
 858         CleanupField(td_imagedescription
); 
 859         CleanupField(td_make
); 
 860         CleanupField(td_model
); 
 861         CleanupField(td_software
); 
 862         CleanupField(td_pagename
); 
 863         CleanupField(td_sampleinfo
); 
 865         CleanupField(td_subifd
); 
 868         CleanupField(td_ycbcrcoeffs
); 
 871         CleanupField(td_inknames
); 
 872         CleanupField(td_targetprinter
); 
 874 #ifdef COLORIMETRY_SUPPORT 
 875         CleanupField(td_whitepoint
); 
 876         CleanupField(td_primarychromas
); 
 877         CleanupField(td_refblackwhite
); 
 878         CleanupField(td_transferfunction
[0]); 
 879         CleanupField(td_transferfunction
[1]); 
 880         CleanupField(td_transferfunction
[2]); 
 883         CleanupField(td_profileData
); 
 885 #ifdef PHOTOSHOP_SUPPORT 
 886         CleanupField(td_photoshopData
); 
 889         CleanupField(td_richtiffiptcData
); 
 891         CleanupField(td_stripoffset
); 
 892         CleanupField(td_stripbytecount
); 
 897  * Client Tag extension support (from Niles Ritter). 
 899 static TIFFExtendProc _TIFFextender 
= (TIFFExtendProc
) NULL
; 
 902 TIFFSetTagExtender(TIFFExtendProc extender
) 
 904         TIFFExtendProc prev 
= _TIFFextender
; 
 905         _TIFFextender 
= extender
; 
 910  * Setup a default directory structure. 
 913 TIFFDefaultDirectory(TIFF
* tif
) 
 915         register TIFFDirectory
* td 
= &tif
->tif_dir
; 
 917         _TIFFSetupFieldInfo(tif
); 
 918         _TIFFmemset(td
, 0, sizeof (*td
)); 
 919         td
->td_fillorder 
= FILLORDER_MSB2LSB
; 
 920         td
->td_bitspersample 
= 1; 
 921         td
->td_threshholding 
= THRESHHOLD_BILEVEL
; 
 922         td
->td_orientation 
= ORIENTATION_TOPLEFT
; 
 923         td
->td_samplesperpixel 
= 1; 
 924         td
->td_rowsperstrip 
= (uint32
) -1; 
 925         td
->td_tilewidth 
= (uint32
) -1; 
 926         td
->td_tilelength 
= (uint32
) -1; 
 927         td
->td_tiledepth 
= 1; 
 928         td
->td_resolutionunit 
= RESUNIT_INCH
; 
 929         td
->td_sampleformat 
= SAMPLEFORMAT_VOID
; 
 930         td
->td_imagedepth 
= 1; 
 932         td
->td_ycbcrsubsampling
[0] = 2; 
 933         td
->td_ycbcrsubsampling
[1] = 2; 
 934         td
->td_ycbcrpositioning 
= YCBCRPOSITION_CENTERED
; 
 937         td
->td_inkset 
= INKSET_CMYK
; 
 940         tif
->tif_postdecode 
= _TIFFNoPostDecode
; 
 941         tif
->tif_vsetfield 
= _TIFFVSetField
; 
 942         tif
->tif_vgetfield 
= _TIFFVGetField
; 
 943         tif
->tif_printdir 
= NULL
; 
 945          *  Give client code a chance to install their own 
 946          *  tag extensions & methods, prior to compression overloads. 
 949                 (*_TIFFextender
)(tif
); 
 950         (void) TIFFSetField(tif
, TIFFTAG_COMPRESSION
, COMPRESSION_NONE
); 
 952          * NB: The directory is marked dirty as a result of setting 
 953          * up the default compression scheme.  However, this really 
 954          * isn't correct -- we want TIFF_DIRTYDIRECT to be set only 
 955          * if the user does something.  We could just do the setup 
 956          * by hand, but it seems better to use the normal mechanism 
 957          * (i.e. TIFFSetField). 
 959         tif
->tif_flags 
&= ~TIFF_DIRTYDIRECT
; 
 964 TIFFAdvanceDirectory(TIFF
* tif
, uint32
* nextdir
, toff_t
* off
) 
 966         static const char module[] = "TIFFAdvanceDirectory"; 
 969         if (!SeekOK(tif
, *nextdir
) || 
 970             !ReadOK(tif
, &dircount
, sizeof (uint16
))) { 
 971                 TIFFError(module, "%s: Error fetching directory count", 
 975         if (tif
->tif_flags 
& TIFF_SWAB
) 
 976                 TIFFSwabShort(&dircount
); 
 978                 *off 
= TIFFSeekFile(tif
, 
 979                     dircount
*sizeof (TIFFDirEntry
), SEEK_CUR
); 
 981                 (void) TIFFSeekFile(tif
, 
 982                     dircount
*sizeof (TIFFDirEntry
), SEEK_CUR
); 
 983         if (!ReadOK(tif
, nextdir
, sizeof (uint32
))) { 
 984                 TIFFError(module, "%s: Error fetching directory link", 
 988         if (tif
->tif_flags 
& TIFF_SWAB
) 
 989                 TIFFSwabLong(nextdir
); 
 994  * Count the number of directories in a file. 
 997 TIFFNumberOfDirectories(TIFF
* tif
) 
 999         uint32 nextdir 
= tif
->tif_header
.tiff_diroff
; 
1002         while (nextdir 
!= 0 && TIFFAdvanceDirectory(tif
, &nextdir
, NULL
)) 
1008  * Set the n-th directory as the current directory. 
1009  * NB: Directories are numbered starting at 0. 
1012 TIFFSetDirectory(TIFF
* tif
, tdir_t dirn
) 
1017         nextdir 
= tif
->tif_header
.tiff_diroff
; 
1018         for (n 
= dirn
; n 
> 0 && nextdir 
!= 0; n
--) 
1019                 if (!TIFFAdvanceDirectory(tif
, &nextdir
, NULL
)) 
1021         tif
->tif_nextdiroff 
= nextdir
; 
1023          * Set curdir to the actual directory index.  The 
1024          * -1 is because TIFFReadDirectory will increment 
1025          * tif_curdir after successfully reading the directory. 
1027         tif
->tif_curdir 
= (dirn 
- n
) - 1; 
1028         return (TIFFReadDirectory(tif
)); 
1032  * Set the current directory to be the directory 
1033  * located at the specified file offset.  This interface 
1034  * is used mainly to access directories linked with 
1035  * the SubIFD tag (e.g. thumbnail images). 
1038 TIFFSetSubDirectory(TIFF
* tif
, uint32 diroff
) 
1040         tif
->tif_nextdiroff 
= diroff
; 
1041         return (TIFFReadDirectory(tif
)); 
1045  * Return file offset of the current directory. 
1048 TIFFCurrentDirOffset(TIFF
* tif
) 
1050         return (tif
->tif_diroff
); 
1054  * Return an indication of whether or not we are 
1055  * at the last directory in the file. 
1058 TIFFLastDirectory(TIFF
* tif
) 
1060         return (tif
->tif_nextdiroff 
== 0); 
1064  * Unlink the specified directory from the directory chain. 
1067 TIFFUnlinkDirectory(TIFF
* tif
, tdir_t dirn
) 
1069         static const char module[] = "TIFFUnlinkDirectory"; 
1074         if (tif
->tif_mode 
== O_RDONLY
) { 
1075                 TIFFError(module, "Can not unlink directory in read-only file"); 
1079          * Go to the directory before the one we want 
1080          * to unlink and nab the offset of the link 
1081          * field we'll need to patch. 
1083         nextdir 
= tif
->tif_header
.tiff_diroff
; 
1084         off 
= sizeof (uint16
) + sizeof (uint16
); 
1085         for (n 
= dirn
-1; n 
> 0; n
--) { 
1087                         TIFFError(module, "Directory %d does not exist", dirn
); 
1090                 if (!TIFFAdvanceDirectory(tif
, &nextdir
, &off
)) 
1094          * Advance to the directory to be unlinked and fetch 
1095          * the offset of the directory that follows. 
1097         if (!TIFFAdvanceDirectory(tif
, &nextdir
, NULL
)) 
1100          * Go back and patch the link field of the preceding 
1101          * directory to point to the offset of the directory 
1104         (void) TIFFSeekFile(tif
, off
, SEEK_SET
); 
1105         if (tif
->tif_flags 
& TIFF_SWAB
) 
1106                 TIFFSwabLong(&nextdir
); 
1107         if (!WriteOK(tif
, &nextdir
, sizeof (uint32
))) { 
1108                 TIFFError(module, "Error writing directory link"); 
1112          * Leave directory state setup safely.  We don't have 
1113          * facilities for doing inserting and removing directories, 
1114          * so it's safest to just invalidate everything.  This 
1115          * means that the caller can only append to the directory 
1118         (*tif
->tif_cleanup
)(tif
); 
1119         if ((tif
->tif_flags 
& TIFF_MYBUFFER
) && tif
->tif_rawdata
) { 
1120                 _TIFFfree(tif
->tif_rawdata
); 
1121                 tif
->tif_rawdata 
= NULL
; 
1124         tif
->tif_flags 
&= ~(TIFF_BEENWRITING
|TIFF_BUFFERSETUP
|TIFF_POSTENCODE
); 
1125         TIFFFreeDirectory(tif
); 
1126         TIFFDefaultDirectory(tif
); 
1127         tif
->tif_diroff 
= 0;                    /* force link on next write */ 
1128         tif
->tif_nextdiroff 
= 0;                /* next write must be at end */ 
1129         tif
->tif_curoff 
= 0; 
1130         tif
->tif_row 
= (uint32
) -1; 
1131         tif
->tif_curstrip 
= (tstrip_t
) -1; 
1137  * Author: Bruce Cameron <cameron@petris.com> 
1139  * Set a table of tags that are to be replaced during directory process by the 
1140  * 'IGNORE' state - or return TRUE/FALSE for the requested tag such that 
1141  * 'ReadDirectory' can use the stored information. 
1144 TIFFReassignTagToIgnore (enum TIFFIgnoreSense task
, int TIFFtagID
) 
1146     static int TIFFignoretags 
[FIELD_LAST
]; 
1147     static int tagcount 
= 0 ; 
1148     int         i
;                                      /* Loop index */ 
1149     int         j
;                                      /* Loop index */ 
1154         if ( tagcount 
< (FIELD_LAST 
- 1) ) 
1156             for ( j 
= 0 ; j 
< tagcount 
; ++j 
) 
1157             {                                   /* Do not add duplicate tag */ 
1158                 if ( TIFFignoretags 
[j
] == TIFFtagID 
) 
1161             TIFFignoretags 
[tagcount
++] = TIFFtagID 
; 
1167         for ( i 
= 0 ; i 
< tagcount 
; ++i 
) 
1169             if ( TIFFignoretags 
[i
] == TIFFtagID 
) 
1175         tagcount 
= 0 ;                  /* Clear the list */