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
; 
 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 
= v
; 
 173         case TIFFTAG_PHOTOMETRIC
: 
 174                 td
->td_photometric 
= (uint16
) va_arg(ap
, int); 
 176         case TIFFTAG_THRESHHOLDING
: 
 177                 td
->td_threshholding 
= (uint16
) va_arg(ap
, int); 
 179         case TIFFTAG_FILLORDER
: 
 181                 if (v 
!= FILLORDER_LSB2MSB 
&& v 
!= FILLORDER_MSB2LSB
) 
 183                 td
->td_fillorder 
= (uint16
) v
; 
 185         case TIFFTAG_DOCUMENTNAME
: 
 186                 _TIFFsetString(&td
->td_documentname
, va_arg(ap
, char*)); 
 189                 _TIFFsetString(&td
->td_artist
, va_arg(ap
, char*)); 
 191         case TIFFTAG_DATETIME
: 
 192                 _TIFFsetString(&td
->td_datetime
, va_arg(ap
, char*)); 
 194         case TIFFTAG_HOSTCOMPUTER
: 
 195                 _TIFFsetString(&td
->td_hostcomputer
, va_arg(ap
, char*)); 
 197         case TIFFTAG_IMAGEDESCRIPTION
: 
 198                 _TIFFsetString(&td
->td_imagedescription
, va_arg(ap
, char*)); 
 201                 _TIFFsetString(&td
->td_make
, va_arg(ap
, char*)); 
 204                 _TIFFsetString(&td
->td_model
, va_arg(ap
, char*)); 
 206         case TIFFTAG_SOFTWARE
: 
 207                 _TIFFsetString(&td
->td_software
, va_arg(ap
, char*)); 
 209         case TIFFTAG_ORIENTATION
: 
 211                 if (v 
< ORIENTATION_TOPLEFT 
|| ORIENTATION_LEFTBOT 
< v
) { 
 212                         TIFFWarning(tif
->tif_name
, 
 213                             "Bad value %ld for \"%s\" tag ignored", 
 214                             v
, _TIFFFieldWithTag(tif
, tag
)->field_name
); 
 216                         td
->td_orientation 
= (uint16
) v
; 
 218         case TIFFTAG_SAMPLESPERPIXEL
: 
 219                 /* XXX should cross check -- e.g. if pallette, then 1 */ 
 223                 td
->td_samplesperpixel 
= (uint16
) v
; 
 225         case TIFFTAG_ROWSPERSTRIP
: 
 226                 v32 
= va_arg(ap
, uint32
); 
 229                 td
->td_rowsperstrip 
= v32
; 
 230                 if (!TIFFFieldSet(tif
, FIELD_TILEDIMENSIONS
)) { 
 231                         td
->td_tilelength 
= v32
; 
 232                         td
->td_tilewidth 
= td
->td_imagewidth
; 
 235         case TIFFTAG_MINSAMPLEVALUE
: 
 236                 td
->td_minsamplevalue 
= (uint16
) va_arg(ap
, int); 
 238         case TIFFTAG_MAXSAMPLEVALUE
: 
 239                 td
->td_maxsamplevalue 
= (uint16
) va_arg(ap
, int); 
 241         case TIFFTAG_SMINSAMPLEVALUE
: 
 242                 td
->td_sminsamplevalue 
= (double) va_arg(ap
, dblparam_t
); 
 244         case TIFFTAG_SMAXSAMPLEVALUE
: 
 245                 td
->td_smaxsamplevalue 
= (double) va_arg(ap
, dblparam_t
); 
 247         case TIFFTAG_XRESOLUTION
: 
 248                 td
->td_xresolution 
= (float) va_arg(ap
, dblparam_t
); 
 250         case TIFFTAG_YRESOLUTION
: 
 251                 td
->td_yresolution 
= (float) va_arg(ap
, dblparam_t
); 
 253         case TIFFTAG_PLANARCONFIG
: 
 255                 if (v 
!= PLANARCONFIG_CONTIG 
&& v 
!= PLANARCONFIG_SEPARATE
) 
 257                 td
->td_planarconfig 
= (uint16
) v
; 
 259         case TIFFTAG_PAGENAME
: 
 260                 _TIFFsetString(&td
->td_pagename
, va_arg(ap
, char*)); 
 262         case TIFFTAG_XPOSITION
: 
 263                 td
->td_xposition 
= (float) va_arg(ap
, dblparam_t
); 
 265         case TIFFTAG_YPOSITION
: 
 266                 td
->td_yposition 
= (float) va_arg(ap
, dblparam_t
); 
 268         case TIFFTAG_RESOLUTIONUNIT
: 
 270                 if (v 
< RESUNIT_NONE 
|| RESUNIT_CENTIMETER 
< v
) 
 272                 td
->td_resolutionunit 
= (uint16
) v
; 
 274         case TIFFTAG_PAGENUMBER
: 
 275                 td
->td_pagenumber
[0] = (uint16
) va_arg(ap
, int); 
 276                 td
->td_pagenumber
[1] = (uint16
) va_arg(ap
, int); 
 278         case TIFFTAG_HALFTONEHINTS
: 
 279                 td
->td_halftonehints
[0] = (uint16
) va_arg(ap
, int); 
 280                 td
->td_halftonehints
[1] = (uint16
) va_arg(ap
, int); 
 282         case TIFFTAG_COLORMAP
: 
 283                 v32 
= (uint32
)(1L<<td
->td_bitspersample
); 
 284                 _TIFFsetShortArray(&td
->td_colormap
[0], va_arg(ap
, uint16
*), v32
); 
 285                 _TIFFsetShortArray(&td
->td_colormap
[1], va_arg(ap
, uint16
*), v32
); 
 286                 _TIFFsetShortArray(&td
->td_colormap
[2], va_arg(ap
, uint16
*), v32
); 
 288         case TIFFTAG_EXTRASAMPLES
: 
 289                 if (!setExtraSamples(td
, ap
, &v
)) 
 292         case TIFFTAG_MATTEING
: 
 293                 td
->td_extrasamples 
= (uint16
) (va_arg(ap
, int) != 0); 
 294                 if (td
->td_extrasamples
) { 
 295                         uint16 sv 
= EXTRASAMPLE_ASSOCALPHA
; 
 296                         _TIFFsetShortArray(&td
->td_sampleinfo
, &sv
, 1); 
 299         case TIFFTAG_TILEWIDTH
: 
 300                 v32 
= va_arg(ap
, uint32
); 
 302                         if (tif
->tif_mode 
!= O_RDONLY
) 
 304                         TIFFWarning(tif
->tif_name
, 
 305                             "Nonstandard tile width %d, convert file", v32
); 
 307                 td
->td_tilewidth 
= v32
; 
 308                 tif
->tif_flags 
|= TIFF_ISTILED
; 
 310         case TIFFTAG_TILELENGTH
: 
 311                 v32 
= va_arg(ap
, uint32
); 
 313                         if (tif
->tif_mode 
!= O_RDONLY
) 
 315                         TIFFWarning(tif
->tif_name
, 
 316                             "Nonstandard tile length %d, convert file", v32
); 
 318                 td
->td_tilelength 
= v32
; 
 319                 tif
->tif_flags 
|= TIFF_ISTILED
; 
 321         case TIFFTAG_TILEDEPTH
: 
 322                 v32 
= va_arg(ap
, uint32
); 
 325                 td
->td_tiledepth 
= v32
; 
 327         case TIFFTAG_DATATYPE
: 
 330                 case DATATYPE_VOID
:     v 
= SAMPLEFORMAT_VOID
;  break; 
 331                 case DATATYPE_INT
:      v 
= SAMPLEFORMAT_INT
;   break; 
 332                 case DATATYPE_UINT
:     v 
= SAMPLEFORMAT_UINT
;  break; 
 333                 case DATATYPE_IEEEFP
:   v 
= SAMPLEFORMAT_IEEEFP
;break; 
 334                 default:                goto badvalue
; 
 336                 td
->td_sampleformat 
= (uint16
) v
; 
 338         case TIFFTAG_SAMPLEFORMAT
: 
 340                 if (v 
< SAMPLEFORMAT_UINT 
|| SAMPLEFORMAT_VOID 
< v
) 
 342                 td
->td_sampleformat 
= (uint16
) v
; 
 344         case TIFFTAG_IMAGEDEPTH
: 
 345                 td
->td_imagedepth 
= va_arg(ap
, uint32
); 
 347         case TIFFTAG_STONITS
: 
 348                 d 
= va_arg(ap
, dblparam_t
); 
 355                 if ((tif
->tif_flags 
& TIFF_INSUBIFD
) == 0) { 
 356                         td
->td_nsubifd 
= (uint16
) va_arg(ap
, int); 
 357                         _TIFFsetLongArray(&td
->td_subifd
, va_arg(ap
, uint32
*), 
 358                             (long) td
->td_nsubifd
); 
 360                         TIFFError(tif
->tif_name
, "Sorry, cannot nest SubIFDs"); 
 366         case TIFFTAG_YCBCRCOEFFICIENTS
: 
 367                 _TIFFsetFloatArray(&td
->td_ycbcrcoeffs
, va_arg(ap
, float*), 3); 
 369         case TIFFTAG_YCBCRPOSITIONING
: 
 370                 td
->td_ycbcrpositioning 
= (uint16
) va_arg(ap
, int); 
 372         case TIFFTAG_YCBCRSUBSAMPLING
: 
 373                 td
->td_ycbcrsubsampling
[0] = (uint16
) va_arg(ap
, int); 
 374                 td
->td_ycbcrsubsampling
[1] = (uint16
) va_arg(ap
, int); 
 377 #ifdef COLORIMETRY_SUPPORT 
 378         case TIFFTAG_WHITEPOINT
: 
 379                 _TIFFsetFloatArray(&td
->td_whitepoint
, va_arg(ap
, float*), 2); 
 381         case TIFFTAG_PRIMARYCHROMATICITIES
: 
 382                 _TIFFsetFloatArray(&td
->td_primarychromas
, va_arg(ap
, float*), 6); 
 384         case TIFFTAG_TRANSFERFUNCTION
: 
 385                 v 
= (td
->td_samplesperpixel 
- td
->td_extrasamples
) > 1 ? 3 : 1; 
 386                 for (i 
= 0; i 
< v
; i
++) 
 387                         _TIFFsetShortArray(&td
->td_transferfunction
[i
], 
 388                             va_arg(ap
, uint16
*), 1L<<td
->td_bitspersample
); 
 390         case TIFFTAG_REFERENCEBLACKWHITE
: 
 391                 /* XXX should check for null range */ 
 392                 _TIFFsetFloatArray(&td
->td_refblackwhite
, va_arg(ap
, float*), 6); 
 397                 td
->td_inkset 
= (uint16
) va_arg(ap
, int); 
 399         case TIFFTAG_DOTRANGE
: 
 400                 /* XXX should check for null range */ 
 401                 td
->td_dotrange
[0] = (uint16
) va_arg(ap
, int); 
 402                 td
->td_dotrange
[1] = (uint16
) va_arg(ap
, int); 
 404         case TIFFTAG_INKNAMES
: 
 406                 s 
= va_arg(ap
, char*); 
 407                 i 
= checkInkNamesString(tif
, i
, s
); 
 410                         _TIFFsetNString(&td
->td_inknames
, s
, i
); 
 411                         td
->td_inknameslen 
= i
; 
 414         case TIFFTAG_NUMBEROFINKS
: 
 415                 td
->td_ninks 
= (uint16
) va_arg(ap
, int); 
 417         case TIFFTAG_TARGETPRINTER
: 
 418                 _TIFFsetString(&td
->td_targetprinter
, va_arg(ap
, char*)); 
 422         case TIFFTAG_ICCPROFILE
: 
 423                 td
->td_profileLength 
= (uint32
) va_arg(ap
, uint32
); 
 424                 _TIFFsetByteArray(&td
->td_profileData
, va_arg(ap
, void*), 
 425                     td
->td_profileLength
); 
 428 #ifdef PHOTOSHOP_SUPPORT 
 429         case TIFFTAG_PHOTOSHOP
: 
 430                 td
->td_photoshopLength 
= (uint32
) va_arg(ap
, uint32
); 
 431                 _TIFFsetByteArray (&td
->td_photoshopData
, va_arg(ap
, void*), 
 432                         td
->td_photoshopLength
); 
 436     case TIFFTAG_RICHTIFFIPTC
:  
 437                 td
->td_richtiffiptcLength 
= (uint32
) va_arg(ap
, uint32
); 
 438 #ifdef PHOTOSHOP_SUPPORT 
 439                 _TIFFsetLongArray ((uint32
**)&td
->td_richtiffiptcData
, va_arg(ap
, uint32
*), 
 440                         td
->td_richtiffiptcLength
); 
 442                 _TIFFsetByteArray (&td
->td_photoshopData
, va_arg(ap
, void*), 
 443                         td
->td_photoshopLength
); 
 449                  * This can happen if multiple images are open with 
 450                  * different codecs which have private tags.  The 
 451                  * global tag information table may then have tags 
 452                  * that are valid for one file but not the other.  
 453                  * If the client tries to set a tag that is not valid 
 454                  * for the image's codec then we'll arrive here.  This 
 455                  * happens, for example, when tiffcp is used to convert 
 456                  * between compression schemes and codec-specific tags 
 457                  * are blindly copied. 
 459                 TIFFError("TIFFSetField", 
 460                     "%s: Invalid %stag \"%s\" (not supported by codec)", 
 461                     tif
->tif_name
, isPseudoTag(tag
) ? "pseduo-" : "", 
 462                     _TIFFFieldWithTag(tif
, tag
)->field_name
); 
 467                 TIFFSetFieldBit(tif
, _TIFFFieldWithTag(tif
, tag
)->field_bit
); 
 468                 tif
->tif_flags 
|= TIFF_DIRTYDIRECT
; 
 473         TIFFError(tif
->tif_name
, "%d: Bad value for \"%s\"", v
, 
 474             _TIFFFieldWithTag(tif
, tag
)->field_name
); 
 478         TIFFError(tif
->tif_name
, "%ld: Bad value for \"%s\"", v32
, 
 479             _TIFFFieldWithTag(tif
, tag
)->field_name
); 
 483         TIFFError(tif
->tif_name
, "%f: Bad value for \"%s\"", d
, 
 484             _TIFFFieldWithTag(tif
, tag
)->field_name
); 
 490  * Return 1/0 according to whether or not 
 491  * it is permissible to set the tag's value. 
 492  * Note that we allow ImageLength to be changed 
 493  * so that we can append and extend to images. 
 494  * Any other tag may not be altered once writing 
 495  * has commenced, unless its value has no effect 
 496  * on the format of the data that is written. 
 499 OkToChangeTag(TIFF
* tif
, ttag_t tag
) 
 501         const TIFFFieldInfo
* fip 
= _TIFFFindFieldInfo(tif
, tag
, TIFF_ANY
); 
 502         if (!fip
) {                     /* unknown tag */ 
 503                 TIFFError("TIFFSetField", "%s: Unknown %stag %u", 
 504                     tif
->tif_name
, isPseudoTag(tag
) ? "pseudo-" : "", tag
); 
 507         if (tag 
!= TIFFTAG_IMAGELENGTH 
&& (tif
->tif_flags 
& TIFF_BEENWRITING
) && 
 508             !fip
->field_oktochange
) { 
 510                  * Consult info table to see if tag can be changed 
 511                  * after we've started writing.  We only allow changes 
 512                  * to those tags that don't/shouldn't affect the 
 513                  * compression and/or format of the data. 
 515                 TIFFError("TIFFSetField", 
 516                     "%s: Cannot modify tag \"%s\" while writing", 
 517                     tif
->tif_name
, fip
->field_name
); 
 524  * Record the value of a field in the 
 525  * internal directory structure.  The 
 526  * field will be written to the file 
 527  * when/if the directory structure is 
 531 TIFFSetField(TIFF
* tif
, ttag_t tag
, ...) 
 537         status 
= TIFFVSetField(tif
, tag
, ap
); 
 543  * Like TIFFSetField, but taking a varargs 
 544  * parameter list.  This routine is useful 
 545  * for building higher-level interfaces on 
 546  * top of the library. 
 549 TIFFVSetField(TIFF
* tif
, ttag_t tag
, va_list ap
) 
 551         return OkToChangeTag(tif
, tag
) ? 
 552             (*tif
->tif_vsetfield
)(tif
, tag
, ap
) : 0; 
 556 _TIFFVGetField(TIFF
* tif
, ttag_t tag
, va_list ap
) 
 558         TIFFDirectory
* td 
= &tif
->tif_dir
; 
 561         case TIFFTAG_SUBFILETYPE
: 
 562                 *va_arg(ap
, uint32
*) = td
->td_subfiletype
; 
 564         case TIFFTAG_IMAGEWIDTH
: 
 565                 *va_arg(ap
, uint32
*) = td
->td_imagewidth
; 
 567         case TIFFTAG_IMAGELENGTH
: 
 568                 *va_arg(ap
, uint32
*) = td
->td_imagelength
; 
 570         case TIFFTAG_BITSPERSAMPLE
: 
 571                 *va_arg(ap
, uint16
*) = td
->td_bitspersample
; 
 573         case TIFFTAG_COMPRESSION
: 
 574                 *va_arg(ap
, uint16
*) = td
->td_compression
; 
 576         case TIFFTAG_PHOTOMETRIC
: 
 577                 *va_arg(ap
, uint16
*) = td
->td_photometric
; 
 579         case TIFFTAG_THRESHHOLDING
: 
 580                 *va_arg(ap
, uint16
*) = td
->td_threshholding
; 
 582         case TIFFTAG_FILLORDER
: 
 583                 *va_arg(ap
, uint16
*) = td
->td_fillorder
; 
 585         case TIFFTAG_DOCUMENTNAME
: 
 586                 *va_arg(ap
, char**) = td
->td_documentname
; 
 589                 *va_arg(ap
, char**) = td
->td_artist
; 
 591         case TIFFTAG_DATETIME
: 
 592                 *va_arg(ap
, char**) = td
->td_datetime
; 
 594         case TIFFTAG_HOSTCOMPUTER
: 
 595                 *va_arg(ap
, char**) = td
->td_hostcomputer
; 
 597         case TIFFTAG_IMAGEDESCRIPTION
: 
 598                 *va_arg(ap
, char**) = td
->td_imagedescription
; 
 601                 *va_arg(ap
, char**) = td
->td_make
; 
 604                 *va_arg(ap
, char**) = td
->td_model
; 
 606         case TIFFTAG_SOFTWARE
: 
 607                 *va_arg(ap
, char**) = td
->td_software
; 
 609         case TIFFTAG_ORIENTATION
: 
 610                 *va_arg(ap
, uint16
*) = td
->td_orientation
; 
 612         case TIFFTAG_SAMPLESPERPIXEL
: 
 613                 *va_arg(ap
, uint16
*) = td
->td_samplesperpixel
; 
 615         case TIFFTAG_ROWSPERSTRIP
: 
 616                 *va_arg(ap
, uint32
*) = td
->td_rowsperstrip
; 
 618         case TIFFTAG_MINSAMPLEVALUE
: 
 619                 *va_arg(ap
, uint16
*) = td
->td_minsamplevalue
; 
 621         case TIFFTAG_MAXSAMPLEVALUE
: 
 622                 *va_arg(ap
, uint16
*) = td
->td_maxsamplevalue
; 
 624         case TIFFTAG_SMINSAMPLEVALUE
: 
 625                 *va_arg(ap
, double*) = td
->td_sminsamplevalue
; 
 627         case TIFFTAG_SMAXSAMPLEVALUE
: 
 628                 *va_arg(ap
, double*) = td
->td_smaxsamplevalue
; 
 630         case TIFFTAG_XRESOLUTION
: 
 631                 *va_arg(ap
, float*) = td
->td_xresolution
; 
 633         case TIFFTAG_YRESOLUTION
: 
 634                 *va_arg(ap
, float*) = td
->td_yresolution
; 
 636         case TIFFTAG_PLANARCONFIG
: 
 637                 *va_arg(ap
, uint16
*) = td
->td_planarconfig
; 
 639         case TIFFTAG_XPOSITION
: 
 640                 *va_arg(ap
, float*) = td
->td_xposition
; 
 642         case TIFFTAG_YPOSITION
: 
 643                 *va_arg(ap
, float*) = td
->td_yposition
; 
 645         case TIFFTAG_PAGENAME
: 
 646                 *va_arg(ap
, char**) = td
->td_pagename
; 
 648         case TIFFTAG_RESOLUTIONUNIT
: 
 649                 *va_arg(ap
, uint16
*) = td
->td_resolutionunit
; 
 651         case TIFFTAG_PAGENUMBER
: 
 652                 *va_arg(ap
, uint16
*) = td
->td_pagenumber
[0]; 
 653                 *va_arg(ap
, uint16
*) = td
->td_pagenumber
[1]; 
 655         case TIFFTAG_HALFTONEHINTS
: 
 656                 *va_arg(ap
, uint16
*) = td
->td_halftonehints
[0]; 
 657                 *va_arg(ap
, uint16
*) = td
->td_halftonehints
[1]; 
 659         case TIFFTAG_COLORMAP
: 
 660                 *va_arg(ap
, uint16
**) = td
->td_colormap
[0]; 
 661                 *va_arg(ap
, uint16
**) = td
->td_colormap
[1]; 
 662                 *va_arg(ap
, uint16
**) = td
->td_colormap
[2]; 
 664         case TIFFTAG_STRIPOFFSETS
: 
 665         case TIFFTAG_TILEOFFSETS
: 
 666                 *va_arg(ap
, uint32
**) = td
->td_stripoffset
; 
 668         case TIFFTAG_STRIPBYTECOUNTS
: 
 669         case TIFFTAG_TILEBYTECOUNTS
: 
 670                 *va_arg(ap
, uint32
**) = td
->td_stripbytecount
; 
 672         case TIFFTAG_MATTEING
: 
 673                 *va_arg(ap
, uint16
*) = 
 674                     (td
->td_extrasamples 
== 1 && 
 675                      td
->td_sampleinfo
[0] == EXTRASAMPLE_ASSOCALPHA
); 
 677         case TIFFTAG_EXTRASAMPLES
: 
 678                 *va_arg(ap
, uint16
*) = td
->td_extrasamples
; 
 679                 *va_arg(ap
, uint16
**) = td
->td_sampleinfo
; 
 681         case TIFFTAG_TILEWIDTH
: 
 682                 *va_arg(ap
, uint32
*) = td
->td_tilewidth
; 
 684         case TIFFTAG_TILELENGTH
: 
 685                 *va_arg(ap
, uint32
*) = td
->td_tilelength
; 
 687         case TIFFTAG_TILEDEPTH
: 
 688                 *va_arg(ap
, uint32
*) = td
->td_tiledepth
; 
 690         case TIFFTAG_DATATYPE
: 
 691                 switch (td
->td_sampleformat
) { 
 692                 case SAMPLEFORMAT_UINT
: 
 693                         *va_arg(ap
, uint16
*) = DATATYPE_UINT
; 
 695                 case SAMPLEFORMAT_INT
: 
 696                         *va_arg(ap
, uint16
*) = DATATYPE_INT
; 
 698                 case SAMPLEFORMAT_IEEEFP
: 
 699                         *va_arg(ap
, uint16
*) = DATATYPE_IEEEFP
; 
 701                 case SAMPLEFORMAT_VOID
: 
 702                         *va_arg(ap
, uint16
*) = DATATYPE_VOID
; 
 706         case TIFFTAG_SAMPLEFORMAT
: 
 707                 *va_arg(ap
, uint16
*) = td
->td_sampleformat
; 
 709         case TIFFTAG_IMAGEDEPTH
: 
 710                 *va_arg(ap
, uint32
*) = td
->td_imagedepth
; 
 712         case TIFFTAG_STONITS
: 
 713                 *va_arg(ap
, double*) = td
->td_stonits
; 
 717                 *va_arg(ap
, uint16
*) = td
->td_nsubifd
; 
 718                 *va_arg(ap
, uint32
**) = td
->td_subifd
; 
 722         case TIFFTAG_YCBCRCOEFFICIENTS
: 
 723                 *va_arg(ap
, float**) = td
->td_ycbcrcoeffs
; 
 725         case TIFFTAG_YCBCRPOSITIONING
: 
 726                 *va_arg(ap
, uint16
*) = td
->td_ycbcrpositioning
; 
 728         case TIFFTAG_YCBCRSUBSAMPLING
: 
 729                 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[0]; 
 730                 *va_arg(ap
, uint16
*) = td
->td_ycbcrsubsampling
[1]; 
 733 #ifdef COLORIMETRY_SUPPORT 
 734         case TIFFTAG_WHITEPOINT
: 
 735                 *va_arg(ap
, float**) = td
->td_whitepoint
; 
 737         case TIFFTAG_PRIMARYCHROMATICITIES
: 
 738                 *va_arg(ap
, float**) = td
->td_primarychromas
; 
 740         case TIFFTAG_TRANSFERFUNCTION
: 
 741                 *va_arg(ap
, uint16
**) = td
->td_transferfunction
[0]; 
 742                 if (td
->td_samplesperpixel 
- td
->td_extrasamples 
> 1) { 
 743                         *va_arg(ap
, uint16
**) = td
->td_transferfunction
[1]; 
 744                         *va_arg(ap
, uint16
**) = td
->td_transferfunction
[2]; 
 747         case TIFFTAG_REFERENCEBLACKWHITE
: 
 748                 *va_arg(ap
, float**) = td
->td_refblackwhite
; 
 753                 *va_arg(ap
, uint16
*) = td
->td_inkset
; 
 755         case TIFFTAG_DOTRANGE
: 
 756                 *va_arg(ap
, uint16
*) = td
->td_dotrange
[0]; 
 757                 *va_arg(ap
, uint16
*) = td
->td_dotrange
[1]; 
 759         case TIFFTAG_INKNAMES
: 
 760                 *va_arg(ap
, char**) = td
->td_inknames
; 
 762         case TIFFTAG_NUMBEROFINKS
: 
 763                 *va_arg(ap
, uint16
*) = td
->td_ninks
; 
 765         case TIFFTAG_TARGETPRINTER
: 
 766                 *va_arg(ap
, char**) = td
->td_targetprinter
; 
 770         case TIFFTAG_ICCPROFILE
: 
 771                 *va_arg(ap
, uint32
*) = td
->td_profileLength
; 
 772                 *va_arg(ap
, void**) = td
->td_profileData
; 
 775 #ifdef PHOTOSHOP_SUPPORT 
 776         case TIFFTAG_PHOTOSHOP
: 
 777                 *va_arg(ap
, uint32
*) = td
->td_photoshopLength
; 
 778                 *va_arg(ap
, void**) = td
->td_photoshopData
; 
 782         case TIFFTAG_RICHTIFFIPTC
: 
 783                 *va_arg(ap
, uint32
*) = td
->td_richtiffiptcLength
; 
 784                 *va_arg(ap
, void**) = td
->td_richtiffiptcData
; 
 789                  * This can happen if multiple images are open with 
 790                  * different codecs which have private tags.  The 
 791                  * global tag information table may then have tags 
 792                  * that are valid for one file but not the other.  
 793                  * If the client tries to get a tag that is not valid 
 794                  * for the image's codec then we'll arrive here. 
 796                 TIFFError("TIFFGetField", 
 797                     "%s: Invalid %stag \"%s\" (not supported by codec)", 
 798                     tif
->tif_name
, isPseudoTag(tag
) ? "pseudo-" : "", 
 799                     _TIFFFieldWithTag(tif
, tag
)->field_name
); 
 806  * Return the value of a field in the 
 807  * internal directory structure. 
 810 TIFFGetField(TIFF
* tif
, ttag_t tag
, ...) 
 816         status 
= TIFFVGetField(tif
, tag
, ap
); 
 822  * Like TIFFGetField, but taking a varargs 
 823  * parameter list.  This routine is useful 
 824  * for building higher-level interfaces on 
 825  * top of the library. 
 828 TIFFVGetField(TIFF
* tif
, ttag_t tag
, va_list ap
) 
 830         const TIFFFieldInfo
* fip 
= _TIFFFindFieldInfo(tif
, tag
, TIFF_ANY
); 
 831         return (fip 
&& (isPseudoTag(tag
) || TIFFFieldSet(tif
, fip
->field_bit
)) ? 
 832             (*tif
->tif_vgetfield
)(tif
, tag
, ap
) : 0); 
 835 #define CleanupField(member) {          \ 
 837         _TIFFfree(td->member);          \ 
 843  * Release storage associated with a directory. 
 846 TIFFFreeDirectory(TIFF
* tif
) 
 848         register TIFFDirectory 
*td 
= &tif
->tif_dir
; 
 850         CleanupField(td_colormap
[0]); 
 851         CleanupField(td_colormap
[1]); 
 852         CleanupField(td_colormap
[2]); 
 853         CleanupField(td_documentname
); 
 854         CleanupField(td_artist
); 
 855         CleanupField(td_datetime
); 
 856         CleanupField(td_hostcomputer
); 
 857         CleanupField(td_imagedescription
); 
 858         CleanupField(td_make
); 
 859         CleanupField(td_model
); 
 860         CleanupField(td_software
); 
 861         CleanupField(td_pagename
); 
 862         CleanupField(td_sampleinfo
); 
 864         CleanupField(td_subifd
); 
 867         CleanupField(td_ycbcrcoeffs
); 
 870         CleanupField(td_inknames
); 
 871         CleanupField(td_targetprinter
); 
 873 #ifdef COLORIMETRY_SUPPORT 
 874         CleanupField(td_whitepoint
); 
 875         CleanupField(td_primarychromas
); 
 876         CleanupField(td_refblackwhite
); 
 877         CleanupField(td_transferfunction
[0]); 
 878         CleanupField(td_transferfunction
[1]); 
 879         CleanupField(td_transferfunction
[2]); 
 882         CleanupField(td_profileData
); 
 884 #ifdef PHOTOSHOP_SUPPORT 
 885         CleanupField(td_photoshopData
); 
 888         CleanupField(td_richtiffiptcData
); 
 890         CleanupField(td_stripoffset
); 
 891         CleanupField(td_stripbytecount
); 
 896  * Client Tag extension support (from Niles Ritter). 
 898 static TIFFExtendProc _TIFFextender 
= (TIFFExtendProc
) NULL
; 
 901 TIFFSetTagExtender(TIFFExtendProc extender
) 
 903         TIFFExtendProc prev 
= _TIFFextender
; 
 904         _TIFFextender 
= extender
; 
 909  * Setup a default directory structure. 
 912 TIFFDefaultDirectory(TIFF
* tif
) 
 914         register TIFFDirectory
* td 
= &tif
->tif_dir
; 
 916         _TIFFSetupFieldInfo(tif
); 
 917         _TIFFmemset(td
, 0, sizeof (*td
)); 
 918         td
->td_fillorder 
= FILLORDER_MSB2LSB
; 
 919         td
->td_bitspersample 
= 1; 
 920         td
->td_threshholding 
= THRESHHOLD_BILEVEL
; 
 921         td
->td_orientation 
= ORIENTATION_TOPLEFT
; 
 922         td
->td_samplesperpixel 
= 1; 
 923         td
->td_rowsperstrip 
= (uint32
) -1; 
 924         td
->td_tilewidth 
= (uint32
) -1; 
 925         td
->td_tilelength 
= (uint32
) -1; 
 926         td
->td_tiledepth 
= 1; 
 927         td
->td_resolutionunit 
= RESUNIT_INCH
; 
 928         td
->td_sampleformat 
= SAMPLEFORMAT_VOID
; 
 929         td
->td_imagedepth 
= 1; 
 931         td
->td_ycbcrsubsampling
[0] = 2; 
 932         td
->td_ycbcrsubsampling
[1] = 2; 
 933         td
->td_ycbcrpositioning 
= YCBCRPOSITION_CENTERED
; 
 936         td
->td_inkset 
= INKSET_CMYK
; 
 939         tif
->tif_postdecode 
= _TIFFNoPostDecode
; 
 940         tif
->tif_vsetfield 
= _TIFFVSetField
; 
 941         tif
->tif_vgetfield 
= _TIFFVGetField
; 
 942         tif
->tif_printdir 
= NULL
; 
 944          *  Give client code a chance to install their own 
 945          *  tag extensions & methods, prior to compression overloads. 
 948                 (*_TIFFextender
)(tif
); 
 949         (void) TIFFSetField(tif
, TIFFTAG_COMPRESSION
, COMPRESSION_NONE
); 
 951          * NB: The directory is marked dirty as a result of setting 
 952          * up the default compression scheme.  However, this really 
 953          * isn't correct -- we want TIFF_DIRTYDIRECT to be set only 
 954          * if the user does something.  We could just do the setup 
 955          * by hand, but it seems better to use the normal mechanism 
 956          * (i.e. TIFFSetField). 
 958         tif
->tif_flags 
&= ~TIFF_DIRTYDIRECT
; 
 963 TIFFAdvanceDirectory(TIFF
* tif
, uint32
* nextdir
, toff_t
* off
) 
 965         static const char module[] = "TIFFAdvanceDirectory"; 
 968         if (!SeekOK(tif
, *nextdir
) || 
 969             !ReadOK(tif
, &dircount
, sizeof (uint16
))) { 
 970                 TIFFError(module, "%s: Error fetching directory count", 
 974         if (tif
->tif_flags 
& TIFF_SWAB
) 
 975                 TIFFSwabShort(&dircount
); 
 977                 *off 
= TIFFSeekFile(tif
, 
 978                     dircount
*sizeof (TIFFDirEntry
), SEEK_CUR
); 
 980                 (void) TIFFSeekFile(tif
, 
 981                     dircount
*sizeof (TIFFDirEntry
), SEEK_CUR
); 
 982         if (!ReadOK(tif
, nextdir
, sizeof (uint32
))) { 
 983                 TIFFError(module, "%s: Error fetching directory link", 
 987         if (tif
->tif_flags 
& TIFF_SWAB
) 
 988                 TIFFSwabLong(nextdir
); 
 993  * Count the number of directories in a file. 
 996 TIFFNumberOfDirectories(TIFF
* tif
) 
 998         uint32 nextdir 
= tif
->tif_header
.tiff_diroff
; 
1001         while (nextdir 
!= 0 && TIFFAdvanceDirectory(tif
, &nextdir
, NULL
)) 
1007  * Set the n-th directory as the current directory. 
1008  * NB: Directories are numbered starting at 0. 
1011 TIFFSetDirectory(TIFF
* tif
, tdir_t dirn
) 
1016         nextdir 
= tif
->tif_header
.tiff_diroff
; 
1017         for (n 
= dirn
; n 
> 0 && nextdir 
!= 0; n
--) 
1018                 if (!TIFFAdvanceDirectory(tif
, &nextdir
, NULL
)) 
1020         tif
->tif_nextdiroff 
= nextdir
; 
1022          * Set curdir to the actual directory index.  The 
1023          * -1 is because TIFFReadDirectory will increment 
1024          * tif_curdir after successfully reading the directory. 
1026         tif
->tif_curdir 
= (dirn 
- n
) - 1; 
1027         return (TIFFReadDirectory(tif
)); 
1031  * Set the current directory to be the directory 
1032  * located at the specified file offset.  This interface 
1033  * is used mainly to access directories linked with 
1034  * the SubIFD tag (e.g. thumbnail images). 
1037 TIFFSetSubDirectory(TIFF
* tif
, uint32 diroff
) 
1039         tif
->tif_nextdiroff 
= diroff
; 
1040         return (TIFFReadDirectory(tif
)); 
1044  * Return file offset of the current directory. 
1047 TIFFCurrentDirOffset(TIFF
* tif
) 
1049         return (tif
->tif_diroff
); 
1053  * Return an indication of whether or not we are 
1054  * at the last directory in the file. 
1057 TIFFLastDirectory(TIFF
* tif
) 
1059         return (tif
->tif_nextdiroff 
== 0); 
1063  * Unlink the specified directory from the directory chain. 
1066 TIFFUnlinkDirectory(TIFF
* tif
, tdir_t dirn
) 
1068         static const char module[] = "TIFFUnlinkDirectory"; 
1073         if (tif
->tif_mode 
== O_RDONLY
) { 
1074                 TIFFError(module, "Can not unlink directory in read-only file"); 
1078          * Go to the directory before the one we want 
1079          * to unlink and nab the offset of the link 
1080          * field we'll need to patch. 
1082         nextdir 
= tif
->tif_header
.tiff_diroff
; 
1083         off 
= sizeof (uint16
) + sizeof (uint16
); 
1084         for (n 
= dirn
-1; n 
> 0; n
--) { 
1086                         TIFFError(module, "Directory %d does not exist", dirn
); 
1089                 if (!TIFFAdvanceDirectory(tif
, &nextdir
, &off
)) 
1093          * Advance to the directory to be unlinked and fetch 
1094          * the offset of the directory that follows. 
1096         if (!TIFFAdvanceDirectory(tif
, &nextdir
, NULL
)) 
1099          * Go back and patch the link field of the preceding 
1100          * directory to point to the offset of the directory 
1103         (void) TIFFSeekFile(tif
, off
, SEEK_SET
); 
1104         if (tif
->tif_flags 
& TIFF_SWAB
) 
1105                 TIFFSwabLong(&nextdir
); 
1106         if (!WriteOK(tif
, &nextdir
, sizeof (uint32
))) { 
1107                 TIFFError(module, "Error writing directory link"); 
1111          * Leave directory state setup safely.  We don't have 
1112          * facilities for doing inserting and removing directories, 
1113          * so it's safest to just invalidate everything.  This 
1114          * means that the caller can only append to the directory 
1117         (*tif
->tif_cleanup
)(tif
); 
1118         if ((tif
->tif_flags 
& TIFF_MYBUFFER
) && tif
->tif_rawdata
) { 
1119                 _TIFFfree(tif
->tif_rawdata
); 
1120                 tif
->tif_rawdata 
= NULL
; 
1123         tif
->tif_flags 
&= ~(TIFF_BEENWRITING
|TIFF_BUFFERSETUP
|TIFF_POSTENCODE
); 
1124         TIFFFreeDirectory(tif
); 
1125         TIFFDefaultDirectory(tif
); 
1126         tif
->tif_diroff 
= 0;                    /* force link on next write */ 
1127         tif
->tif_nextdiroff 
= 0;                /* next write must be at end */ 
1128         tif
->tif_curoff 
= 0; 
1129         tif
->tif_row 
= (uint32
) -1; 
1130         tif
->tif_curstrip 
= (tstrip_t
) -1; 
1136  * Author: Bruce Cameron <cameron@petris.com> 
1138  * Set a table of tags that are to be replaced during directory process by the 
1139  * 'IGNORE' state - or return TRUE/FALSE for the requested tag such that 
1140  * 'ReadDirectory' can use the stored information. 
1143 TIFFReassignTagToIgnore (enum TIFFIgnoreSense task
, int TIFFtagID
) 
1145     static int TIFFignoretags 
[FIELD_LAST
]; 
1146     static int tagcount 
= 0 ; 
1147     int         i
;                                      /* Loop index */ 
1148     int         j
;                                      /* Loop index */ 
1153         if ( tagcount 
< (FIELD_LAST 
- 1) ) 
1155             for ( j 
= 0 ; j 
< tagcount 
; ++j 
) 
1156             {                                   /* Do not add duplicate tag */ 
1157                 if ( TIFFignoretags 
[j
] == TIFFtagID 
) 
1160             TIFFignoretags 
[tagcount
++] = TIFFtagID 
; 
1166         for ( i 
= 0 ; i 
< tagcount 
; ++i 
) 
1168             if ( TIFFignoretags 
[i
] == TIFFtagID 
) 
1174         tagcount 
= 0 ;                  /* Clear the list */