]>
git.saurik.com Git - wxWidgets.git/blob - src/tiff/tif_dirwrite.c
   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 Write Support Routines. 
  35 #define TIFFCvtNativeToIEEEFloat(tif, n, fp) 
  36 #define TIFFCvtNativeToIEEEDouble(tif, n, dp) 
  38 extern  void TIFFCvtNativeToIEEEFloat(TIFF
*, uint32
, float*); 
  39 extern  void TIFFCvtNativeToIEEEDouble(TIFF
*, uint32
, double*); 
  42 static  int TIFFWriteNormalTag(TIFF
*, TIFFDirEntry
*, const TIFFFieldInfo
*); 
  43 static  void TIFFSetupShortLong(TIFF
*, ttag_t
, TIFFDirEntry
*, uint32
); 
  44 static  int TIFFSetupShortPair(TIFF
*, ttag_t
, TIFFDirEntry
*); 
  45 static  int TIFFWritePerSampleShorts(TIFF
*, ttag_t
, TIFFDirEntry
*); 
  46 static  int TIFFWritePerSampleAnys(TIFF
*, TIFFDataType
, ttag_t
, TIFFDirEntry
*); 
  47 static  int TIFFWriteShortTable(TIFF
*, ttag_t
, TIFFDirEntry
*, uint32
, uint16
**); 
  48 static  int TIFFWriteShortArray(TIFF
*, 
  49             TIFFDataType
, ttag_t
, TIFFDirEntry
*, uint32
, uint16
*); 
  50 static  int TIFFWriteLongArray(TIFF 
*, 
  51             TIFFDataType
, ttag_t
, TIFFDirEntry
*, uint32
, uint32
*); 
  52 static  int TIFFWriteRationalArray(TIFF 
*, 
  53             TIFFDataType
, ttag_t
, TIFFDirEntry
*, uint32
, float*); 
  54 static  int TIFFWriteFloatArray(TIFF 
*, 
  55             TIFFDataType
, ttag_t
, TIFFDirEntry
*, uint32
, float*); 
  56 static  int TIFFWriteDoubleArray(TIFF 
*, 
  57             TIFFDataType
, ttag_t
, TIFFDirEntry
*, uint32
, double*); 
  58 static  int TIFFWriteByteArray(TIFF
*, TIFFDirEntry
*, char*); 
  59 static  int TIFFWriteAnyArray(TIFF
*, 
  60             TIFFDataType
, ttag_t
, TIFFDirEntry
*, uint32
, double*); 
  61 #ifdef COLORIMETRY_SUPPORT 
  62 static  int TIFFWriteTransferFunction(TIFF
*, TIFFDirEntry
*); 
  65 static  int TIFFWriteInkNames(TIFF
*, TIFFDirEntry
*); 
  67 static  int TIFFWriteData(TIFF
*, TIFFDirEntry
*, char*); 
  68 static  int TIFFLinkDirectory(TIFF
*); 
  70 #define WriteRationalPair(type, tag1, v1, tag2, v2) {           \ 
  71         if (!TIFFWriteRational(tif, type, tag1, dir, v1))       \ 
  73         if (!TIFFWriteRational(tif, type, tag2, dir+1, v2))     \ 
  77 #define TIFFWriteRational(tif, type, tag, dir, v) \ 
  78         TIFFWriteRationalArray((tif), (type), (tag), (dir), 1, &(v)) 
  79 #ifndef TIFFWriteRational 
  80 static  int TIFFWriteRational(TIFF
*, 
  81             TIFFDataType
, ttag_t
, TIFFDirEntry
*, float); 
  85  * Write the contents of the current directory 
  86  * to the specified file.  This routine doesn't 
  87  * handle overwriting a directory with auxiliary 
  88  * storage that's been changed. 
  91 TIFFWriteDirectory(TIFF
* tif
) 
 101         u_long b
, fields
[FIELD_SETLONGS
]; 
 104         if (tif
->tif_mode 
== O_RDONLY
) 
 107          * Clear write state so that subsequent images with 
 108          * different characteristics get the right buffers 
 111         if (tif
->tif_flags 
& TIFF_POSTENCODE
) { 
 112                 tif
->tif_flags 
&= ~TIFF_POSTENCODE
; 
 113                 if (!(*tif
->tif_postencode
)(tif
)) { 
 114                         TIFFError(tif
->tif_name
, 
 115                             "Error post-encoding before directory write"); 
 119         (*tif
->tif_close
)(tif
);                 /* shutdown encoder */ 
 121          * Flush any data that might have been written 
 122          * by the compression close+cleanup routines. 
 124         if (tif
->tif_rawcc 
> 0 && !TIFFFlushData1(tif
)) { 
 125                 TIFFError(tif
->tif_name
, 
 126                     "Error flushing data before directory write"); 
 129         if ((tif
->tif_flags 
& TIFF_MYBUFFER
) && tif
->tif_rawdata
) { 
 130                 _TIFFfree(tif
->tif_rawdata
); 
 131                 tif
->tif_rawdata 
= NULL
; 
 134         tif
->tif_flags 
&= ~(TIFF_BEENWRITING
|TIFF_BUFFERSETUP
); 
 138          * Size the directory so that we can calculate 
 139          * offsets for the data items that aren't kept 
 140          * in-place in each field. 
 143         for (b 
= 0; b 
<= FIELD_LAST
; b
++) 
 144                 if (TIFFFieldSet(tif
, b
)) 
 145                         nfields 
+= (b 
< FIELD_SUBFILETYPE 
? 2 : 1); 
 146         dirsize 
= nfields 
* sizeof (TIFFDirEntry
); 
 147         data 
= (char*) _TIFFmalloc(dirsize
); 
 149                 TIFFError(tif
->tif_name
, 
 150                     "Cannot write directory, out of space"); 
 154          * Directory hasn't been placed yet, put 
 155          * it at the end of the file and link it 
 156          * into the existing directory structure. 
 158         if (tif
->tif_diroff 
== 0 && !TIFFLinkDirectory(tif
)) 
 160         tif
->tif_dataoff 
= (toff_t
)( 
 161             tif
->tif_diroff 
+ sizeof (uint16
) + dirsize 
+ sizeof (toff_t
)); 
 162         if (tif
->tif_dataoff 
& 1) 
 164         (void) TIFFSeekFile(tif
, tif
->tif_dataoff
, SEEK_SET
); 
 166         dir 
= (TIFFDirEntry
*) data
; 
 168          * Setup external form of directory 
 169          * entries and write data items. 
 171         _TIFFmemcpy(fields
, td
->td_fieldsset
, sizeof (fields
)); 
 173          * Write out ExtraSamples tag only if 
 174          * extra samples are present in the data. 
 176         if (FieldSet(fields
, FIELD_EXTRASAMPLES
) && !td
->td_extrasamples
) { 
 177                 ResetFieldBit(fields
, FIELD_EXTRASAMPLES
); 
 179                 dirsize 
-= sizeof (TIFFDirEntry
); 
 181         for (fi 
= 0, nfi 
= tif
->tif_nfields
; nfi 
> 0; nfi
--, fi
++) { 
 182                 const TIFFFieldInfo
* fip 
= tif
->tif_fieldinfo
[fi
]; 
 183                 if (!FieldSet(fields
, fip
->field_bit
)) 
 185                 switch (fip
->field_bit
) { 
 186                 case FIELD_STRIPOFFSETS
: 
 188                          * We use one field bit for both strip and tile 
 189                          * offsets, and so must be careful in selecting 
 190                          * the appropriate field descriptor (so that tags 
 191                          * are written in sorted order). 
 194                             TIFFTAG_TILEOFFSETS 
: TIFFTAG_STRIPOFFSETS
; 
 195                         if (tag 
!= fip
->field_tag
) 
 197                         if (!TIFFWriteLongArray(tif
, TIFF_LONG
, tag
, dir
, 
 198                             (uint32
) td
->td_nstrips
, td
->td_stripoffset
)) 
 201                 case FIELD_STRIPBYTECOUNTS
: 
 203                          * We use one field bit for both strip and tile 
 204                          * byte counts, and so must be careful in selecting 
 205                          * the appropriate field descriptor (so that tags 
 206                          * are written in sorted order). 
 209                             TIFFTAG_TILEBYTECOUNTS 
: TIFFTAG_STRIPBYTECOUNTS
; 
 210                         if (tag 
!= fip
->field_tag
) 
 212                         if (!TIFFWriteLongArray(tif
, TIFF_LONG
, tag
, dir
, 
 213                             (uint32
) td
->td_nstrips
, td
->td_stripbytecount
)) 
 216                 case FIELD_ROWSPERSTRIP
: 
 217                         TIFFSetupShortLong(tif
, TIFFTAG_ROWSPERSTRIP
, 
 218                             dir
, td
->td_rowsperstrip
); 
 221                         if (!TIFFWriteShortTable(tif
, TIFFTAG_COLORMAP
, dir
, 
 225                 case FIELD_IMAGEDIMENSIONS
: 
 226                         TIFFSetupShortLong(tif
, TIFFTAG_IMAGEWIDTH
, 
 227                             dir
++, td
->td_imagewidth
); 
 228                         TIFFSetupShortLong(tif
, TIFFTAG_IMAGELENGTH
, 
 229                             dir
, td
->td_imagelength
); 
 231                 case FIELD_TILEDIMENSIONS
: 
 232                         TIFFSetupShortLong(tif
, TIFFTAG_TILEWIDTH
, 
 233                             dir
++, td
->td_tilewidth
); 
 234                         TIFFSetupShortLong(tif
, TIFFTAG_TILELENGTH
, 
 235                             dir
, td
->td_tilelength
); 
 238                         WriteRationalPair(TIFF_RATIONAL
, 
 239                             TIFFTAG_XPOSITION
, td
->td_xposition
, 
 240                             TIFFTAG_YPOSITION
, td
->td_yposition
); 
 242                 case FIELD_RESOLUTION
: 
 243                         WriteRationalPair(TIFF_RATIONAL
, 
 244                             TIFFTAG_XRESOLUTION
, td
->td_xresolution
, 
 245                             TIFFTAG_YRESOLUTION
, td
->td_yresolution
); 
 247                 case FIELD_BITSPERSAMPLE
: 
 248                 case FIELD_MINSAMPLEVALUE
: 
 249                 case FIELD_MAXSAMPLEVALUE
: 
 250                 case FIELD_SAMPLEFORMAT
: 
 251                         if (!TIFFWritePerSampleShorts(tif
, fip
->field_tag
, dir
)) 
 254                 case FIELD_SMINSAMPLEVALUE
: 
 255                 case FIELD_SMAXSAMPLEVALUE
: 
 256                         if (!TIFFWritePerSampleAnys(tif
, 
 257                             _TIFFSampleToTagType(tif
), fip
->field_tag
, dir
)) 
 260                 case FIELD_PAGENUMBER
: 
 261                 case FIELD_HALFTONEHINTS
: 
 263                 case FIELD_YCBCRSUBSAMPLING
: 
 268                         if (!TIFFSetupShortPair(tif
, fip
->field_tag
, dir
)) 
 273                         if (!TIFFWriteInkNames(tif
, dir
)) 
 277 #ifdef COLORIMETRY_SUPPORT 
 278                 case FIELD_TRANSFERFUNCTION
: 
 279                         if (!TIFFWriteTransferFunction(tif
, dir
)) 
 285                         if (!TIFFWriteNormalTag(tif
, dir
, fip
)) 
 288                          * Total hack: if this directory includes a SubIFD 
 289                          * tag then force the next <n> directories to be 
 290                          * written as ``sub directories'' of this one.  This 
 291                          * is used to write things like thumbnails and 
 292                          * image masks that one wants to keep out of the 
 293                          * normal directory linkage access mechanism. 
 295                         if (dir
->tdir_count 
> 0) { 
 296                                 tif
->tif_flags 
|= TIFF_INSUBIFD
; 
 297                                 tif
->tif_nsubifd 
= dir
->tdir_count
; 
 298                                 if (dir
->tdir_count 
> 1) 
 299                                         tif
->tif_subifdoff 
= dir
->tdir_offset
; 
 301                                         tif
->tif_subifdoff 
= (uint32
)( 
 304                                             + ((char*)&dir
->tdir_offset
-data
)); 
 309                         if (!TIFFWriteNormalTag(tif
, dir
, fip
)) 
 314                 ResetFieldBit(fields
, fip
->field_bit
); 
 319         dircount 
= (uint16
) nfields
; 
 320         diroff 
= (uint32
) tif
->tif_nextdiroff
; 
 321         if (tif
->tif_flags 
& TIFF_SWAB
) { 
 323                  * The file's byte order is opposite to the 
 324                  * native machine architecture.  We overwrite 
 325                  * the directory information with impunity 
 326                  * because it'll be released below after we 
 327                  * write it to the file.  Note that all the 
 328                  * other tag construction routines assume that 
 329                  * we do this byte-swapping; i.e. they only 
 330                  * byte-swap indirect data. 
 332                 for (dir 
= (TIFFDirEntry
*) data
; dircount
; dir
++, dircount
--) { 
 333                         TIFFSwabArrayOfShort(&dir
->tdir_tag
, 2); 
 334                         TIFFSwabArrayOfLong(&dir
->tdir_count
, 2); 
 336                 dircount 
= (uint16
) nfields
; 
 337                 TIFFSwabShort(&dircount
); 
 338                 TIFFSwabLong(&diroff
); 
 340         (void) TIFFSeekFile(tif
, tif
->tif_diroff
, SEEK_SET
); 
 341         if (!WriteOK(tif
, &dircount
, sizeof (dircount
))) { 
 342                 TIFFError(tif
->tif_name
, "Error writing directory count"); 
 345         if (!WriteOK(tif
, data
, dirsize
)) { 
 346                 TIFFError(tif
->tif_name
, "Error writing directory contents"); 
 349         if (!WriteOK(tif
, &diroff
, sizeof (diroff
))) { 
 350                 TIFFError(tif
->tif_name
, "Error writing directory link"); 
 353         TIFFFreeDirectory(tif
); 
 355         tif
->tif_flags 
&= ~TIFF_DIRTYDIRECT
; 
 356         (*tif
->tif_cleanup
)(tif
); 
 359          * Reset directory-related state for subsequent 
 362         TIFFDefaultDirectory(tif
); 
 365         tif
->tif_row 
= (uint32
) -1; 
 366         tif
->tif_curstrip 
= (tstrip_t
) -1; 
 372 #undef WriteRationalPair 
 375  * Process tags that are not special cased. 
 378 TIFFWriteNormalTag(TIFF
* tif
, TIFFDirEntry
* dir
, const TIFFFieldInfo
* fip
) 
 380         u_short wc 
= (u_short
) fip
->field_writecount
; 
 383         dir
->tdir_tag 
= fip
->field_tag
; 
 384         dir
->tdir_type 
= (u_short
) fip
->field_type
; 
 385         dir
->tdir_count 
= wc
; 
 386 #define WRITEF(x,y)     x(tif, fip->field_type, fip->field_tag, dir, wc, y) 
 387         switch (fip
->field_type
) { 
 392                         if (wc 
== (u_short
) TIFF_VARIABLE
) 
 393                                 TIFFGetField(tif
, fip
->field_tag
, &wc
, &wp
); 
 395                                 TIFFGetField(tif
, fip
->field_tag
, &wp
); 
 396                         if (!WRITEF(TIFFWriteShortArray
, wp
)) 
 400                         TIFFGetField(tif
, fip
->field_tag
, &sv
); 
 402                             TIFFInsertData(tif
, dir
->tdir_type
, sv
); 
 409                         if (wc 
== (u_short
) TIFF_VARIABLE
) 
 410                                 TIFFGetField(tif
, fip
->field_tag
, &wc
, &lp
); 
 412                                 TIFFGetField(tif
, fip
->field_tag
, &lp
); 
 413                         if (!WRITEF(TIFFWriteLongArray
, lp
)) 
 416                         /* XXX handle LONG->SHORT conversion */ 
 417                         TIFFGetField(tif
, fip
->field_tag
, &dir
->tdir_offset
); 
 424                         if (wc 
== (u_short
) TIFF_VARIABLE
) 
 425                                 TIFFGetField(tif
, fip
->field_tag
, &wc
, &fp
); 
 427                                 TIFFGetField(tif
, fip
->field_tag
, &fp
); 
 428                         if (!WRITEF(TIFFWriteRationalArray
, fp
)) 
 432                         TIFFGetField(tif
, fip
->field_tag
, &fv
); 
 433                         if (!WRITEF(TIFFWriteRationalArray
, &fv
)) 
 440                         if (wc 
== (u_short
) TIFF_VARIABLE
) 
 441                                 TIFFGetField(tif
, fip
->field_tag
, &wc
, &fp
); 
 443                                 TIFFGetField(tif
, fip
->field_tag
, &fp
); 
 444                         if (!WRITEF(TIFFWriteFloatArray
, fp
)) 
 448                         TIFFGetField(tif
, fip
->field_tag
, &fv
); 
 449                         if (!WRITEF(TIFFWriteFloatArray
, &fv
)) 
 456                         if (wc 
== (u_short
) TIFF_VARIABLE
) 
 457                                 TIFFGetField(tif
, fip
->field_tag
, &wc
, &dp
); 
 459                                 TIFFGetField(tif
, fip
->field_tag
, &dp
); 
 460                         if (!WRITEF(TIFFWriteDoubleArray
, dp
)) 
 464                         TIFFGetField(tif
, fip
->field_tag
, &dv
); 
 465                         if (!WRITEF(TIFFWriteDoubleArray
, &dv
)) 
 471                   TIFFGetField(tif
, fip
->field_tag
, &cp
); 
 472                   dir
->tdir_count 
= (uint32
) (strlen(cp
) + 1); 
 473                   if (!TIFFWriteByteArray(tif
, dir
, cp
)) 
 478         /* added based on patch request from MARTIN.MCBRIDE.MM@agfa.co.uk, 
 479            correctness not verified (FW, 99/08) */ 
 484                     if (wc 
== (u_short
) TIFF_VARIABLE
) { 
 485                         TIFFGetField(tif
, fip
->field_tag
, &wc
, &cp
); 
 486                         dir
->tdir_count 
= wc
; 
 488                         TIFFGetField(tif
, fip
->field_tag
, &cp
); 
 489                     if (!TIFFWriteByteArray(tif
, dir
, cp
)) 
 493                     TIFFGetField(tif
, fip
->field_tag
, &cv
); 
 494                     if (!TIFFWriteByteArray(tif
, dir
, &cv
)) 
 501                   if (wc 
== (u_short
) TIFF_VARIABLE
) { 
 502                         TIFFGetField(tif
, fip
->field_tag
, &wc
, &cp
); 
 503                         dir
->tdir_count 
= wc
; 
 504                   } else if (wc 
== (u_short
) TIFF_VARIABLE2
) { 
 505                         TIFFGetField(tif
, fip
->field_tag
, &wc2
, &cp
); 
 506                         dir
->tdir_count 
= wc2
; 
 508                         TIFFGetField(tif
, fip
->field_tag
, &cp
); 
 509                   if (!TIFFWriteByteArray(tif
, dir
, cp
)) 
 522  * Setup a directory entry with either a SHORT 
 523  * or LONG type according to the value. 
 526 TIFFSetupShortLong(TIFF
* tif
, ttag_t tag
, TIFFDirEntry
* dir
, uint32 v
) 
 531                 dir
->tdir_type 
= (short) TIFF_LONG
; 
 532                 dir
->tdir_offset 
= v
; 
 534                 dir
->tdir_type 
= (short) TIFF_SHORT
; 
 535                 dir
->tdir_offset 
= TIFFInsertData(tif
, (int) TIFF_SHORT
, v
); 
 538 #undef MakeShortDirent 
 540 #ifndef TIFFWriteRational 
 542  * Setup a RATIONAL directory entry and 
 543  * write the associated indirect value. 
 546 TIFFWriteRational(TIFF
* tif
, 
 547     TIFFDataType type
, ttag_t tag
, TIFFDirEntry
* dir
, float v
) 
 549         return (TIFFWriteRationalArray(tif
, type
, tag
, dir
, 1, &v
)); 
 553 #define NITEMS(x)       (sizeof (x) / sizeof (x[0])) 
 555  * Setup a directory entry that references a 
 556  * samples/pixel array of SHORT values and 
 557  * (potentially) write the associated indirect 
 561 TIFFWritePerSampleShorts(TIFF
* tif
, ttag_t tag
, TIFFDirEntry
* dir
) 
 565         int i
, status
, samples 
= tif
->tif_dir
.td_samplesperpixel
; 
 567         if (samples 
> NITEMS(buf
)) 
 568                 w 
= (uint16
*) _TIFFmalloc(samples 
* sizeof (uint16
)); 
 569         TIFFGetField(tif
, tag
, &v
); 
 570         for (i 
= 0; i 
< samples
; i
++) 
 572         status 
= TIFFWriteShortArray(tif
, TIFF_SHORT
, tag
, dir
, samples
, w
); 
 574                 _TIFFfree((char*) w
); 
 579  * Setup a directory entry that references a samples/pixel array of ``type'' 
 580  * values and (potentially) write the associated indirect values.  The source 
 581  * data from TIFFGetField() for the specified tag must be returned as double. 
 584 TIFFWritePerSampleAnys(TIFF
* tif
, 
 585     TIFFDataType type
, ttag_t tag
, TIFFDirEntry
* dir
) 
 590         int samples 
= (int) tif
->tif_dir
.td_samplesperpixel
; 
 592         if (samples 
> NITEMS(buf
)) 
 593                 w 
= (double*) _TIFFmalloc(samples 
* sizeof (double)); 
 594         TIFFGetField(tif
, tag
, &v
); 
 595         for (i 
= 0; i 
< samples
; i
++) 
 597         status 
= TIFFWriteAnyArray(tif
, type
, tag
, dir
, samples
, w
); 
 605  * Setup a pair of shorts that are returned by 
 606  * value, rather than as a reference to an array. 
 609 TIFFSetupShortPair(TIFF
* tif
, ttag_t tag
, TIFFDirEntry
* dir
) 
 613         TIFFGetField(tif
, tag
, &v
[0], &v
[1]); 
 614         return (TIFFWriteShortArray(tif
, TIFF_SHORT
, tag
, dir
, 2, v
)); 
 618  * Setup a directory entry for an NxM table of shorts, 
 619  * where M is known to be 2**bitspersample, and write 
 620  * the associated indirect data. 
 623 TIFFWriteShortTable(TIFF
* tif
, 
 624     ttag_t tag
, TIFFDirEntry
* dir
, uint32 n
, uint16
** table
) 
 629         dir
->tdir_type 
= (short) TIFF_SHORT
; 
 630         /* XXX -- yech, fool TIFFWriteData */ 
 631         dir
->tdir_count 
= (uint32
) (1L<<tif
->tif_dir
.td_bitspersample
); 
 632         off 
= tif
->tif_dataoff
; 
 633         for (i 
= 0; i 
< n
; i
++) 
 634                 if (!TIFFWriteData(tif
, dir
, (char *)table
[i
])) 
 636         dir
->tdir_count 
*= n
; 
 637         dir
->tdir_offset 
= off
; 
 642  * Write/copy data associated with an ASCII or opaque tag value. 
 645 TIFFWriteByteArray(TIFF
* tif
, TIFFDirEntry
* dir
, char* cp
) 
 647         if (dir
->tdir_count 
> 4) { 
 648                 if (!TIFFWriteData(tif
, dir
, cp
)) 
 651                 _TIFFmemcpy(&dir
->tdir_offset
, cp
, dir
->tdir_count
); 
 656  * Setup a directory entry of an array of SHORT 
 657  * or SSHORT and write the associated indirect values. 
 660 TIFFWriteShortArray(TIFF
* tif
, 
 661     TIFFDataType type
, ttag_t tag
, TIFFDirEntry
* dir
, uint32 n
, uint16
* v
) 
 664         dir
->tdir_type 
= (short) type
; 
 667                 if (tif
->tif_header
.tiff_magic 
== TIFF_BIGENDIAN
) { 
 668                         dir
->tdir_offset 
= (uint32
) ((long) v
[0] << 16); 
 670                                 dir
->tdir_offset 
|= v
[1] & 0xffff; 
 672                         dir
->tdir_offset 
= v
[0] & 0xffff; 
 674                                 dir
->tdir_offset 
|= (long) v
[1] << 16; 
 678                 return (TIFFWriteData(tif
, dir
, (char*) v
)); 
 682  * Setup a directory entry of an array of LONG 
 683  * or SLONG and write the associated indirect values. 
 686 TIFFWriteLongArray(TIFF
* tif
, 
 687     TIFFDataType type
, ttag_t tag
, TIFFDirEntry
* dir
, uint32 n
, uint32
* v
) 
 690         dir
->tdir_type 
= (short) type
; 
 693                 dir
->tdir_offset 
= v
[0]; 
 696                 return (TIFFWriteData(tif
, dir
, (char*) v
)); 
 700  * Setup a directory entry of an array of RATIONAL 
 701  * or SRATIONAL and write the associated indirect values. 
 704 TIFFWriteRationalArray(TIFF
* tif
, 
 705     TIFFDataType type
, ttag_t tag
, TIFFDirEntry
* dir
, uint32 n
, float* v
) 
 712         dir
->tdir_type 
= (short) type
; 
 714         t 
= (uint32
*) _TIFFmalloc(2*n 
* sizeof (uint32
)); 
 715         for (i 
= 0; i 
< n
; i
++) { 
 721                         if (type 
== TIFF_RATIONAL
) { 
 722                                 TIFFWarning(tif
->tif_name
, 
 723         "\"%s\": Information lost writing value (%g) as (unsigned) RATIONAL", 
 724                                 _TIFFFieldWithTag(tif
,tag
)->field_name
, fv
); 
 731                         while (fv 
< 1L<<(31-3) && den 
< 1L<<(31-3)) 
 732                                 fv 
*= 1<<3, den 
*= 1L<<3; 
 734                 t
[2*i
+0] = sign 
* (fv 
+ 0.5); 
 737         status 
= TIFFWriteData(tif
, dir
, (char *)t
); 
 738         _TIFFfree((char*) t
); 
 743 TIFFWriteFloatArray(TIFF
* tif
, 
 744     TIFFDataType type
, ttag_t tag
, TIFFDirEntry
* dir
, uint32 n
, float* v
) 
 747         dir
->tdir_type 
= (short) type
; 
 749         TIFFCvtNativeToIEEEFloat(tif
, n
, v
); 
 751                 dir
->tdir_offset 
= *(uint32
*) &v
[0]; 
 754                 return (TIFFWriteData(tif
, dir
, (char*) v
)); 
 758 TIFFWriteDoubleArray(TIFF
* tif
, 
 759     TIFFDataType type
, ttag_t tag
, TIFFDirEntry
* dir
, uint32 n
, double* v
) 
 762         dir
->tdir_type 
= (short) type
; 
 764         TIFFCvtNativeToIEEEDouble(tif
, n
, v
); 
 765         return (TIFFWriteData(tif
, dir
, (char*) v
)); 
 769  * Write an array of ``type'' values for a specified tag (i.e. this is a tag 
 770  * which is allowed to have different types, e.g. SMaxSampleType). 
 771  * Internally the data values are represented as double since a double can 
 772  * hold any of the TIFF tag types (yes, this should really be an abstract 
 773  * type tany_t for portability).  The data is converted into the specified 
 774  * type in a temporary buffer and then handed off to the appropriate array 
 778 TIFFWriteAnyArray(TIFF
* tif
, 
 779     TIFFDataType type
, ttag_t tag
, TIFFDirEntry
* dir
, uint32 n
, double* v
) 
 781         char buf
[10 * sizeof(double)]; 
 785         if (n 
* tiffDataWidth
[type
] > sizeof buf
) 
 786                 w 
= (char*) _TIFFmalloc(n 
* tiffDataWidth
[type
]); 
 789                 { uint8
* bp 
= (uint8
*) w
; 
 790                   for (i 
= 0; i 
< n
; i
++) 
 791                         bp
[i
] = (uint8
) v
[i
]; 
 793                   dir
->tdir_type 
= (short) type
; 
 795                   if (!TIFFWriteByteArray(tif
, dir
, (char*) bp
)) 
 800                 { int8
* bp 
= (int8
*) w
; 
 801                   for (i 
= 0; i 
< n
; i
++) 
 804                   dir
->tdir_type 
= (short) type
; 
 806                   if (!TIFFWriteByteArray(tif
, dir
, (char*) bp
)) 
 811                 { uint16
* bp 
= (uint16
*) w
; 
 812                   for (i 
= 0; i 
< n
; i
++) 
 813                         bp
[i
] = (uint16
) v
[i
]; 
 814                   if (!TIFFWriteShortArray(tif
, type
, tag
, dir
, n
, (uint16
*)bp
)) 
 819                 { int16
* bp 
= (int16
*) w
; 
 820                   for (i 
= 0; i 
< n
; i
++) 
 821                         bp
[i
] = (int16
) v
[i
]; 
 822                   if (!TIFFWriteShortArray(tif
, type
, tag
, dir
, n
, (uint16
*)bp
)) 
 827                 { uint32
* bp 
= (uint32
*) w
; 
 828                   for (i 
= 0; i 
< n
; i
++) 
 829                         bp
[i
] = (uint32
) v
[i
]; 
 830                   if (!TIFFWriteLongArray(tif
, type
, tag
, dir
, n
, bp
)) 
 835                 { int32
* bp 
= (int32
*) w
; 
 836                   for (i 
= 0; i 
< n
; i
++) 
 837                         bp
[i
] = (int32
) v
[i
]; 
 838                   if (!TIFFWriteLongArray(tif
, type
, tag
, dir
, n
, (uint32
*) bp
)) 
 843                 { float* bp 
= (float*) w
; 
 844                   for (i 
= 0; i 
< n
; i
++) 
 845                         bp
[i
] = (float) v
[i
]; 
 846                   if (!TIFFWriteFloatArray(tif
, type
, tag
, dir
, n
, bp
)) 
 851                 return (TIFFWriteDoubleArray(tif
, type
, tag
, dir
, n
, v
)); 
 867 #ifdef COLORIMETRY_SUPPORT 
 869 TIFFWriteTransferFunction(TIFF
* tif
, TIFFDirEntry
* dir
) 
 871         TIFFDirectory
* td 
= &tif
->tif_dir
; 
 872         tsize_t n 
= (1L<<td
->td_bitspersample
) * sizeof (uint16
); 
 873         uint16
** tf 
= td
->td_transferfunction
; 
 877          * Check if the table can be written as a single column, 
 878          * or if it must be written as 3 columns.  Note that we 
 879          * write a 3-column tag if there are 2 samples/pixel and 
 880          * a single column of data won't suffice--hmm. 
 882         switch (td
->td_samplesperpixel 
- td
->td_extrasamples
) { 
 883         default:        if (_TIFFmemcmp(tf
[0], tf
[2], n
)) { ncols 
= 3; break; } 
 884         case 2:         if (_TIFFmemcmp(tf
[0], tf
[1], n
)) { ncols 
= 3; break; } 
 885         case 1: case 0: ncols 
= 1; 
 887         return (TIFFWriteShortTable(tif
, 
 888             TIFFTAG_TRANSFERFUNCTION
, dir
, ncols
, tf
)); 
 894 TIFFWriteInkNames(TIFF
* tif
, TIFFDirEntry
* dir
) 
 896         TIFFDirectory
* td 
= &tif
->tif_dir
; 
 898         dir
->tdir_tag 
= TIFFTAG_INKNAMES
; 
 899         dir
->tdir_type 
= (short) TIFF_ASCII
; 
 900         dir
->tdir_count 
= td
->td_inknameslen
; 
 901         return (TIFFWriteByteArray(tif
, dir
, td
->td_inknames
)); 
 906  * Write a contiguous directory item. 
 909 TIFFWriteData(TIFF
* tif
, TIFFDirEntry
* dir
, char* cp
) 
 913         if (tif
->tif_flags 
& TIFF_SWAB
) { 
 914                 switch (dir
->tdir_type
) { 
 917                         TIFFSwabArrayOfShort((uint16
*) cp
, dir
->tdir_count
); 
 922                         TIFFSwabArrayOfLong((uint32
*) cp
, dir
->tdir_count
); 
 926                         TIFFSwabArrayOfLong((uint32
*) cp
, 2*dir
->tdir_count
); 
 929                         TIFFSwabArrayOfDouble((double*) cp
, dir
->tdir_count
); 
 933         dir
->tdir_offset 
= tif
->tif_dataoff
; 
 934         cc 
= dir
->tdir_count 
* tiffDataWidth
[dir
->tdir_type
]; 
 935         if (SeekOK(tif
, dir
->tdir_offset
) && 
 936             WriteOK(tif
, cp
, cc
)) { 
 937                 tif
->tif_dataoff 
+= (cc 
+ 1) & ~1; 
 940         TIFFError(tif
->tif_name
, "Error writing data for field \"%s\"", 
 941             _TIFFFieldWithTag(tif
, dir
->tdir_tag
)->field_name
); 
 946  * Link the current directory into the 
 947  * directory chain for the file. 
 950 TIFFLinkDirectory(TIFF
* tif
) 
 952         static const char module[] = "TIFFLinkDirectory"; 
 956         tif
->tif_diroff 
= (TIFFSeekFile(tif
, (toff_t
) 0, SEEK_END
)+1) &~ 1; 
 957         diroff 
= (uint32
) tif
->tif_diroff
; 
 958         if (tif
->tif_flags 
& TIFF_SWAB
) 
 959                 TIFFSwabLong(&diroff
); 
 961         if (tif
->tif_flags 
& TIFF_INSUBIFD
) { 
 962                 (void) TIFFSeekFile(tif
, tif
->tif_subifdoff
, SEEK_SET
); 
 963                 if (!WriteOK(tif
, &diroff
, sizeof (diroff
))) { 
 965                             "%s: Error writing SubIFD directory link", 
 970                  * Advance to the next SubIFD or, if this is 
 971                  * the last one configured, revert back to the 
 972                  * normal directory linkage. 
 974                 if (--tif
->tif_nsubifd
) 
 975                         tif
->tif_subifdoff 
+= sizeof (diroff
); 
 977                         tif
->tif_flags 
&= ~TIFF_INSUBIFD
; 
 981         if (tif
->tif_header
.tiff_diroff 
== 0) { 
 983                  * First directory, overwrite offset in header. 
 985                 tif
->tif_header
.tiff_diroff 
= (uint32
) tif
->tif_diroff
; 
 986 #define HDROFF(f)       ((toff_t) &(((TIFFHeader*) 0)->f)) 
 987                 (void) TIFFSeekFile(tif
, HDROFF(tiff_diroff
), SEEK_SET
); 
 988                 if (!WriteOK(tif
, &diroff
, sizeof (diroff
))) { 
 989                         TIFFError(tif
->tif_name
, "Error writing TIFF header"); 
 995          * Not the first directory, search to the last and append. 
 997         nextdir 
= tif
->tif_header
.tiff_diroff
; 
1001                 if (!SeekOK(tif
, nextdir
) || 
1002                     !ReadOK(tif
, &dircount
, sizeof (dircount
))) { 
1003                         TIFFError(module, "Error fetching directory count"); 
1006                 if (tif
->tif_flags 
& TIFF_SWAB
) 
1007                         TIFFSwabShort(&dircount
); 
1008                 (void) TIFFSeekFile(tif
, 
1009                     dircount 
* sizeof (TIFFDirEntry
), SEEK_CUR
); 
1010                 if (!ReadOK(tif
, &nextdir
, sizeof (nextdir
))) { 
1011                         TIFFError(module, "Error fetching directory link"); 
1014                 if (tif
->tif_flags 
& TIFF_SWAB
) 
1015                         TIFFSwabLong(&nextdir
); 
1016         } while (nextdir 
!= 0); 
1017         (void) TIFFSeekFile(tif
, -(toff_t
) sizeof (nextdir
), SEEK_CUR
); 
1018         if (!WriteOK(tif
, &diroff
, sizeof (diroff
))) { 
1019                 TIFFError(module, "Error writing directory link");