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 Read Support Routines. 
  34 #define IGNORE  0               /* tag placeholder used below */ 
  37 #define TIFFCvtIEEEFloatToNative(tif, n, fp) 
  38 #define TIFFCvtIEEEDoubleToNative(tif, n, dp) 
  40 extern  void TIFFCvtIEEEFloatToNative(TIFF
*, uint32
, float*); 
  41 extern  void TIFFCvtIEEEDoubleToNative(TIFF
*, uint32
, double*); 
  44 static  void EstimateStripByteCounts(TIFF
*, TIFFDirEntry
*, uint16
); 
  45 static  void MissingRequired(TIFF
*, const char*); 
  46 static  int CheckDirCount(TIFF
*, TIFFDirEntry
*, uint32
); 
  47 static  tsize_t 
TIFFFetchData(TIFF
*, TIFFDirEntry
*, char*); 
  48 static  tsize_t 
TIFFFetchString(TIFF
*, TIFFDirEntry
*, char*); 
  49 static  float TIFFFetchRational(TIFF
*, TIFFDirEntry
*); 
  50 static  int TIFFFetchNormalTag(TIFF
*, TIFFDirEntry
*); 
  51 static  int TIFFFetchPerSampleShorts(TIFF
*, TIFFDirEntry
*, int*); 
  52 static  int TIFFFetchPerSampleAnys(TIFF
*, TIFFDirEntry
*, double*); 
  53 static  int TIFFFetchShortArray(TIFF
*, TIFFDirEntry
*, uint16
*); 
  54 static  int TIFFFetchStripThing(TIFF
*, TIFFDirEntry
*, long, uint32
**); 
  55 static  int TIFFFetchExtraSamples(TIFF
*, TIFFDirEntry
*); 
  56 static  int TIFFFetchRefBlackWhite(TIFF
*, TIFFDirEntry
*); 
  57 static  float TIFFFetchFloat(TIFF
*, TIFFDirEntry
*); 
  58 static  int TIFFFetchFloatArray(TIFF
*, TIFFDirEntry
*, float*); 
  59 static  int TIFFFetchDoubleArray(TIFF
*, TIFFDirEntry
*, double*); 
  60 static  int TIFFFetchAnyArray(TIFF
*, TIFFDirEntry
*, double*); 
  61 static  int TIFFFetchShortPair(TIFF
*, TIFFDirEntry
*); 
  62 static  void ChopUpSingleUncompressedStrip(TIFF
*); 
  65 CheckMalloc(TIFF
* tif
, tsize_t n
, const char* what
) 
  67         char *cp 
= (char*)_TIFFmalloc(n
); 
  69                 TIFFError(tif
->tif_name
, "No space %s", what
); 
  74  * Read the next TIFF directory from a file 
  75  * and convert it to the internal format. 
  76  * We read directories sequentially. 
  79 TIFFReadDirectory(TIFF
* tif
) 
  81         register TIFFDirEntry
* dp
; 
  83         register TIFFDirectory
* td
; 
  88         const TIFFFieldInfo
* fip
; 
  93         int diroutoforderwarning 
= 0; 
  95         tif
->tif_diroff 
= tif
->tif_nextdiroff
; 
  96         if (tif
->tif_diroff 
== 0)               /* no more directories */ 
  99          * Cleanup any previous compression state. 
 101         (*tif
->tif_cleanup
)(tif
); 
 104         if (!isMapped(tif
)) { 
 105                 if (!SeekOK(tif
, tif
->tif_diroff
)) { 
 106                         TIFFError(tif
->tif_name
, 
 107                             "Seek error accessing TIFF directory"); 
 110                 if (!ReadOK(tif
, &dircount
, sizeof (uint16
))) { 
 111                         TIFFError(tif
->tif_name
, 
 112                             "Can not read TIFF directory count"); 
 115                 if (tif
->tif_flags 
& TIFF_SWAB
) 
 116                         TIFFSwabShort(&dircount
); 
 117                 dir 
= (TIFFDirEntry 
*)CheckMalloc(tif
, 
 118                     dircount 
* sizeof (TIFFDirEntry
), "to read TIFF directory"); 
 121                 if (!ReadOK(tif
, dir
, dircount
*sizeof (TIFFDirEntry
))) { 
 122                         TIFFError(tif
->tif_name
, "Can not read TIFF directory"); 
 126                  * Read offset to next directory for sequential scans. 
 128                 (void) ReadOK(tif
, &nextdiroff
, sizeof (uint32
)); 
 130                 toff_t off 
= tif
->tif_diroff
; 
 132                 if (off 
+ sizeof (uint16
) > tif
->tif_size
) { 
 133                         TIFFError(tif
->tif_name
, 
 134                             "Can not read TIFF directory count"); 
 137                         _TIFFmemcpy(&dircount
, tif
->tif_base 
+ off
, sizeof (uint16
)); 
 138                 off 
+= sizeof (uint16
); 
 139                 if (tif
->tif_flags 
& TIFF_SWAB
) 
 140                         TIFFSwabShort(&dircount
); 
 141                 dir 
= (TIFFDirEntry 
*)CheckMalloc(tif
, 
 142                     dircount 
* sizeof (TIFFDirEntry
), "to read TIFF directory"); 
 145                 if (off 
+ dircount
*sizeof (TIFFDirEntry
) > tif
->tif_size
) { 
 146                         TIFFError(tif
->tif_name
, "Can not read TIFF directory"); 
 149                         _TIFFmemcpy(dir
, tif
->tif_base 
+ off
, 
 150                             dircount
*sizeof (TIFFDirEntry
)); 
 151                 off 
+= dircount
* sizeof (TIFFDirEntry
); 
 152                 if (off 
+ sizeof (uint32
) <= tif
->tif_size
) 
 153                         _TIFFmemcpy(&nextdiroff
, tif
->tif_base
+off
, sizeof (uint32
)); 
 155         if (tif
->tif_flags 
& TIFF_SWAB
) 
 156                 TIFFSwabLong(&nextdiroff
); 
 157         tif
->tif_nextdiroff 
= nextdiroff
; 
 159         tif
->tif_flags 
&= ~TIFF_BEENWRITING
;    /* reset before new dir */ 
 161          * Setup default value and then make a pass over 
 162          * the fields to check type and tag information, 
 163          * and to extract info required to size data 
 164          * structures.  A second pass is made afterwards 
 165          * to read in everthing not taken in the first pass. 
 168         /* free any old stuff and reinit */ 
 169         TIFFFreeDirectory(tif
); 
 170         TIFFDefaultDirectory(tif
); 
 172          * Electronic Arts writes gray-scale TIFF files 
 173          * without a PlanarConfiguration directory entry. 
 174          * Thus we setup a default value here, even though 
 175          * the TIFF spec says there is no default value. 
 177         TIFFSetField(tif
, TIFFTAG_PLANARCONFIG
, PLANARCONFIG_CONTIG
); 
 180          * Sigh, we must make a separate pass through the 
 181          * directory for the following reason: 
 183          * We must process the Compression tag in the first pass 
 184          * in order to merge in codec-private tag definitions (otherwise 
 185          * we may get complaints about unknown tags).  However, the 
 186          * Compression tag may be dependent on the SamplesPerPixel 
 187          * tag value because older TIFF specs permited Compression 
 188          * to be written as a SamplesPerPixel-count tag entry. 
 189          * Thus if we don't first figure out the correct SamplesPerPixel 
 190          * tag value then we may end up ignoring the Compression tag 
 191          * value because it has an incorrect count value (if the 
 192          * true value of SamplesPerPixel is not 1). 
 194          * It sure would have been nice if Aldus had really thought 
 195          * this stuff through carefully. 
 197         for (dp 
= dir
, n 
= dircount
; n 
> 0; n
--, dp
++) { 
 198                 if (tif
->tif_flags 
& TIFF_SWAB
) { 
 199                         TIFFSwabArrayOfShort(&dp
->tdir_tag
, 2); 
 200                         TIFFSwabArrayOfLong(&dp
->tdir_count
, 2); 
 202                 if (dp
->tdir_tag 
== TIFFTAG_SAMPLESPERPIXEL
) { 
 203                         if (!TIFFFetchNormalTag(tif
, dp
)) 
 205                         dp
->tdir_tag 
= IGNORE
; 
 209          * First real pass over the directory. 
 212         for (dp 
= dir
, n 
= dircount
; n 
> 0; n
--, dp
++) { 
 215                  * Find the field information entry for this tag. 
 216                  * Added check for tags to ignore ... [BFC] 
 218                 if( TIFFReassignTagToIgnore(TIS_EXTRACT
, dp
->tdir_tag
) ) 
 219                     dp
->tdir_tag 
= IGNORE
; 
 221                 if (dp
->tdir_tag 
== IGNORE
) 
 225                  * Silicon Beach (at least) writes unordered 
 226                  * directory tags (violating the spec).  Handle 
 227                  * it here, but be obnoxious (maybe they'll fix it?). 
 229                 if (dp
->tdir_tag 
< tif
->tif_fieldinfo
[fix
]->field_tag
) { 
 230                         if (!diroutoforderwarning
) { 
 231                                 TIFFWarning(tif
->tif_name
, 
 232         "invalid TIFF directory; tags are not sorted in ascending order"); 
 233                                 diroutoforderwarning 
= 1; 
 235                         fix 
= 0;                        /* O(n^2) */ 
 237                 while (fix 
< tif
->tif_nfields 
&& 
 238                     tif
->tif_fieldinfo
[fix
]->field_tag 
< dp
->tdir_tag
) 
 240                 if (fix 
== tif
->tif_nfields 
|| 
 241                     tif
->tif_fieldinfo
[fix
]->field_tag 
!= dp
->tdir_tag
) { 
 242                         TIFFWarning(tif
->tif_name
, 
 243                             "unknown field with tag %d (0x%x) ignored", 
 244                             dp
->tdir_tag
,  dp
->tdir_tag
); 
 245                         dp
->tdir_tag 
= IGNORE
; 
 246                         fix 
= 0;                        /* restart search */ 
 250                  * Null out old tags that we ignore. 
 252                 if (tif
->tif_fieldinfo
[fix
]->field_bit 
== FIELD_IGNORE
) { 
 254                         dp
->tdir_tag 
= IGNORE
; 
 260                 fip 
= tif
->tif_fieldinfo
[fix
]; 
 261                 while (dp
->tdir_type 
!= (u_short
) fip
->field_type
) { 
 262                         if (fip
->field_type 
== TIFF_ANY
)        /* wildcard */ 
 265                         if (fix 
== tif
->tif_nfields 
|| 
 266                             fip
->field_tag 
!= dp
->tdir_tag
) { 
 267                                 TIFFWarning(tif
->tif_name
, 
 268                                    "wrong data type %d for \"%s\"; tag ignored", 
 269                                     dp
->tdir_type
, fip
[-1].field_name
); 
 274                  * Check count if known in advance. 
 276                 if (fip
->field_readcount 
!= TIFF_VARIABLE
) { 
 277                         uint32 expected 
= (fip
->field_readcount 
== TIFF_SPP
) ? 
 278                             (uint32
) td
->td_samplesperpixel 
: 
 279                             (uint32
) fip
->field_readcount
; 
 280                         if (!CheckDirCount(tif
, dp
, expected
)) 
 284                 switch (dp
->tdir_tag
) { 
 285                 case TIFFTAG_COMPRESSION
: 
 287                          * The 5.0 spec says the Compression tag has 
 288                          * one value, while earlier specs say it has 
 289                          * one value per sample.  Because of this, we 
 290                          * accept the tag if one value is supplied. 
 292                         if (dp
->tdir_count 
== 1) { 
 293                                 v 
= TIFFExtractData(tif
, 
 294                                     dp
->tdir_type
, dp
->tdir_offset
); 
 295                                 if (!TIFFSetField(tif
, dp
->tdir_tag
, (int)v
)) 
 299                         if (!TIFFFetchPerSampleShorts(tif
, dp
, &iv
) || 
 300                             !TIFFSetField(tif
, dp
->tdir_tag
, iv
)) 
 302                         dp
->tdir_tag 
= IGNORE
; 
 304                 case TIFFTAG_STRIPOFFSETS
: 
 305                 case TIFFTAG_STRIPBYTECOUNTS
: 
 306                 case TIFFTAG_TILEOFFSETS
: 
 307                 case TIFFTAG_TILEBYTECOUNTS
: 
 308                         TIFFSetFieldBit(tif
, fip
->field_bit
); 
 310                 case TIFFTAG_IMAGEWIDTH
: 
 311                 case TIFFTAG_IMAGELENGTH
: 
 312                 case TIFFTAG_IMAGEDEPTH
: 
 313                 case TIFFTAG_TILELENGTH
: 
 314                 case TIFFTAG_TILEWIDTH
: 
 315                 case TIFFTAG_TILEDEPTH
: 
 316                 case TIFFTAG_PLANARCONFIG
: 
 317                 case TIFFTAG_ROWSPERSTRIP
: 
 318                         if (!TIFFFetchNormalTag(tif
, dp
)) 
 320                         dp
->tdir_tag 
= IGNORE
; 
 322                 case TIFFTAG_EXTRASAMPLES
: 
 323                         (void) TIFFFetchExtraSamples(tif
, dp
); 
 324                         dp
->tdir_tag 
= IGNORE
; 
 330          * Allocate directory structure and setup defaults. 
 332         if (!TIFFFieldSet(tif
, FIELD_IMAGEDIMENSIONS
)) { 
 333                 MissingRequired(tif
, "ImageLength"); 
 336         if (!TIFFFieldSet(tif
, FIELD_PLANARCONFIG
)) { 
 337                 MissingRequired(tif
, "PlanarConfiguration"); 
 341          * Setup appropriate structures (by strip or by tile) 
 343         if (!TIFFFieldSet(tif
, FIELD_TILEDIMENSIONS
)) { 
 344                 td
->td_nstrips 
= TIFFNumberOfStrips(tif
); 
 345                 td
->td_tilewidth 
= td
->td_imagewidth
; 
 346                 td
->td_tilelength 
= td
->td_rowsperstrip
; 
 347                 td
->td_tiledepth 
= td
->td_imagedepth
; 
 348                 tif
->tif_flags 
&= ~TIFF_ISTILED
; 
 350                 td
->td_nstrips 
= TIFFNumberOfTiles(tif
); 
 351                 tif
->tif_flags 
|= TIFF_ISTILED
; 
 353         td
->td_stripsperimage 
= td
->td_nstrips
; 
 354         if (td
->td_planarconfig 
== PLANARCONFIG_SEPARATE
) 
 355                 td
->td_stripsperimage 
/= td
->td_samplesperpixel
; 
 356         if (!TIFFFieldSet(tif
, FIELD_STRIPOFFSETS
)) { 
 358                     isTiled(tif
) ? "TileOffsets" : "StripOffsets"); 
 363          * Second pass: extract other information. 
 365         for (dp 
= dir
, n 
= dircount
; n 
> 0; n
--, dp
++) { 
 366                 if (dp
->tdir_tag 
== IGNORE
) 
 368                 switch (dp
->tdir_tag
) { 
 369                 case TIFFTAG_MINSAMPLEVALUE
: 
 370                 case TIFFTAG_MAXSAMPLEVALUE
: 
 371                 case TIFFTAG_BITSPERSAMPLE
: 
 373                          * The 5.0 spec says the Compression tag has 
 374                          * one value, while earlier specs say it has 
 375                          * one value per sample.  Because of this, we 
 376                          * accept the tag if one value is supplied. 
 378                          * The MinSampleValue, MaxSampleValue and 
 379                          * BitsPerSample tags are supposed to be written 
 380                          * as one value/sample, but some vendors incorrectly 
 381                          * write one value only -- so we accept that 
 384                         if (dp
->tdir_count 
== 1) { 
 385                                 v 
= TIFFExtractData(tif
, 
 386                                     dp
->tdir_type
, dp
->tdir_offset
); 
 387                                 if (!TIFFSetField(tif
, dp
->tdir_tag
, (int)v
)) 
 392                 case TIFFTAG_DATATYPE
: 
 393                 case TIFFTAG_SAMPLEFORMAT
: 
 394                         if (!TIFFFetchPerSampleShorts(tif
, dp
, &iv
) || 
 395                             !TIFFSetField(tif
, dp
->tdir_tag
, iv
)) 
 398                 case TIFFTAG_SMINSAMPLEVALUE
: 
 399                 case TIFFTAG_SMAXSAMPLEVALUE
: 
 400                         if (!TIFFFetchPerSampleAnys(tif
, dp
, &dv
) || 
 401                             !TIFFSetField(tif
, dp
->tdir_tag
, dv
)) 
 404                 case TIFFTAG_STRIPOFFSETS
: 
 405                 case TIFFTAG_TILEOFFSETS
: 
 406                         if (!TIFFFetchStripThing(tif
, dp
, 
 407                             td
->td_nstrips
, &td
->td_stripoffset
)) 
 410                 case TIFFTAG_STRIPBYTECOUNTS
: 
 411                 case TIFFTAG_TILEBYTECOUNTS
: 
 412                         if (!TIFFFetchStripThing(tif
, dp
, 
 413                             td
->td_nstrips
, &td
->td_stripbytecount
)) 
 416                 case TIFFTAG_COLORMAP
: 
 417                 case TIFFTAG_TRANSFERFUNCTION
: 
 419                          * TransferFunction can have either 1x or 3x data 
 420                          * values; Colormap can have only 3x items. 
 422                         v 
= 1L<<td
->td_bitspersample
; 
 423                         if (dp
->tdir_tag 
== TIFFTAG_COLORMAP 
|| 
 424                             dp
->tdir_count 
!= (uint32
) v
) { 
 425                                 if (!CheckDirCount(tif
, dp
, (uint32
)(3*v
))) 
 428                         v 
*= sizeof (uint16
); 
 429                         cp 
= CheckMalloc(tif
, dp
->tdir_count 
* sizeof (uint16
), 
 430                             "to read \"TransferFunction\" tag"); 
 432                                 if (TIFFFetchData(tif
, dp
, cp
)) { 
 434                                          * This deals with there being only 
 435                                          * one array to apply to all samples. 
 438                                             (uint32
)1 << td
->td_bitspersample
; 
 439                                         if (dp
->tdir_count 
== c
) 
 441                                         TIFFSetField(tif
, dp
->tdir_tag
, 
 447                 case TIFFTAG_PAGENUMBER
: 
 448                 case TIFFTAG_HALFTONEHINTS
: 
 449                 case TIFFTAG_YCBCRSUBSAMPLING
: 
 450                 case TIFFTAG_DOTRANGE
: 
 451                         (void) TIFFFetchShortPair(tif
, dp
); 
 453 #ifdef COLORIMETRY_SUPPORT 
 454                 case TIFFTAG_REFERENCEBLACKWHITE
: 
 455                         (void) TIFFFetchRefBlackWhite(tif
, dp
); 
 458 /* BEGIN REV 4.0 COMPATIBILITY */ 
 459                 case TIFFTAG_OSUBFILETYPE
: 
 461                         switch (TIFFExtractData(tif
, dp
->tdir_type
, 
 463                         case OFILETYPE_REDUCEDIMAGE
: 
 464                                 v 
= FILETYPE_REDUCEDIMAGE
; 
 471                                 (void) TIFFSetField(tif
, 
 472                                     TIFFTAG_SUBFILETYPE
, (int)v
); 
 474 /* END REV 4.0 COMPATIBILITY */ 
 476                         (void) TIFFFetchNormalTag(tif
, dp
); 
 481          * Verify Palette image has a Colormap. 
 483         if (td
->td_photometric 
== PHOTOMETRIC_PALETTE 
&& 
 484             !TIFFFieldSet(tif
, FIELD_COLORMAP
)) { 
 485                 MissingRequired(tif
, "Colormap"); 
 489          * Attempt to deal with a missing StripByteCounts tag. 
 491         if (!TIFFFieldSet(tif
, FIELD_STRIPBYTECOUNTS
)) { 
 493                  * Some manufacturers violate the spec by not giving 
 494                  * the size of the strips.  In this case, assume there 
 495                  * is one uncompressed strip of data. 
 497                 if ((td
->td_planarconfig 
== PLANARCONFIG_CONTIG 
&& 
 498                     td
->td_nstrips 
> 1) || 
 499                     (td
->td_planarconfig 
== PLANARCONFIG_SEPARATE 
&& 
 500                      td
->td_nstrips 
!= td
->td_samplesperpixel
)) { 
 501                     MissingRequired(tif
, "StripByteCounts"); 
 504                 TIFFWarning(tif
->tif_name
, 
 505 "TIFF directory is missing required \"%s\" field, calculating from imagelength", 
 506                     _TIFFFieldWithTag(tif
,TIFFTAG_STRIPBYTECOUNTS
)->field_name
); 
 507                 EstimateStripByteCounts(tif
, dir
, dircount
); 
 508 #define BYTECOUNTLOOKSBAD \ 
 509     (td->td_stripbytecount[0] == 0 || \ 
 510     (td->td_compression == COMPRESSION_NONE && \ 
 511      td->td_stripbytecount[0] > TIFFGetFileSize(tif) - td->td_stripoffset[0])) 
 512         } else if (td
->td_nstrips 
== 1 && BYTECOUNTLOOKSBAD
) { 
 514                  * Plexus (and others) sometimes give a value 
 515                  * of zero for a tag when they don't know what 
 516                  * the correct value is!  Try and handle the 
 517                  * simple case of estimating the size of a one 
 520                 TIFFWarning(tif
->tif_name
, 
 521             "Bogus \"%s\" field, ignoring and calculating from imagelength", 
 522                     _TIFFFieldWithTag(tif
,TIFFTAG_STRIPBYTECOUNTS
)->field_name
); 
 523                 EstimateStripByteCounts(tif
, dir
, dircount
); 
 526                 _TIFFfree((char *)dir
); 
 527         if (!TIFFFieldSet(tif
, FIELD_MAXSAMPLEVALUE
)) 
 528                 td
->td_maxsamplevalue 
= (uint16
)((1L<<td
->td_bitspersample
)-1); 
 530          * Setup default compression scheme. 
 532         if (!TIFFFieldSet(tif
, FIELD_COMPRESSION
)) 
 533                 TIFFSetField(tif
, TIFFTAG_COMPRESSION
, COMPRESSION_NONE
); 
 535          * Some manufacturers make life difficult by writing 
 536          * large amounts of uncompressed data as a single strip. 
 537          * This is contrary to the recommendations of the spec. 
 538          * The following makes an attempt at breaking such images 
 539          * into strips closer to the recommended 8k bytes.  A 
 540          * side effect, however, is that the RowsPerStrip tag 
 541          * value may be changed. 
 543         if (td
->td_nstrips 
== 1 && td
->td_compression 
== COMPRESSION_NONE 
&& 
 544             (tif
->tif_flags 
& (TIFF_STRIPCHOP
|TIFF_ISTILED
)) == TIFF_STRIPCHOP
) 
 545                 ChopUpSingleUncompressedStrip(tif
); 
 547          * Reinitialize i/o since we are starting on a new directory. 
 549         tif
->tif_row 
= (uint32
) -1; 
 550         tif
->tif_curstrip 
= (tstrip_t
) -1; 
 551         tif
->tif_col 
= (uint32
) -1; 
 552         tif
->tif_curtile 
= (ttile_t
) -1; 
 553         tif
->tif_tilesize 
= TIFFTileSize(tif
); 
 554         tif
->tif_scanlinesize 
= TIFFScanlineSize(tif
); 
 563 EstimateStripByteCounts(TIFF
* tif
, TIFFDirEntry
* dir
, uint16 dircount
) 
 565         register TIFFDirEntry 
*dp
; 
 566         register TIFFDirectory 
*td 
= &tif
->tif_dir
; 
 569         if (td
->td_stripbytecount
) 
 570                 _TIFFfree(td
->td_stripbytecount
); 
 571         td
->td_stripbytecount 
= (uint32
*) 
 572             CheckMalloc(tif
, td
->td_nstrips 
* sizeof (uint32
), 
 573                 "for \"StripByteCounts\" array"); 
 574         if (td
->td_compression 
!= COMPRESSION_NONE
) { 
 575                 uint32 space 
= (uint32
)(sizeof (TIFFHeader
) 
 577                     + (dircount 
* sizeof (TIFFDirEntry
)) 
 579                 toff_t filesize 
= TIFFGetFileSize(tif
); 
 582                 /* calculate amount of space used by indirect values */ 
 583                 for (dp 
= dir
, n 
= dircount
; n 
> 0; n
--, dp
++) { 
 584                         uint32 cc 
= dp
->tdir_count
*tiffDataWidth
[dp
->tdir_type
]; 
 585                         if (cc 
> sizeof (uint32
)) 
 588                 space 
= filesize 
- space
; 
 589                 if (td
->td_planarconfig 
== PLANARCONFIG_SEPARATE
) 
 590                         space 
/= td
->td_samplesperpixel
; 
 591                 for (i 
= 0; i 
< td
->td_nstrips
; i
++) 
 592                         td
->td_stripbytecount
[i
] = space
; 
 594                  * This gross hack handles the case were the offset to 
 595                  * the last strip is past the place where we think the strip 
 596                  * should begin.  Since a strip of data must be contiguous, 
 597                  * it's safe to assume that we've overestimated the amount 
 598                  * of data in the strip and trim this number back accordingly. 
 601                 if (td
->td_stripoffset
[i
] + td
->td_stripbytecount
[i
] > filesize
) 
 602                         td
->td_stripbytecount
[i
] = 
 603                             filesize 
- td
->td_stripoffset
[i
]; 
 605                 uint32 rowbytes 
= TIFFScanlineSize(tif
); 
 606                 uint32 rowsperstrip 
= td
->td_imagelength
/td
->td_stripsperimage
; 
 607                 for (i 
= 0; i 
< td
->td_nstrips
; i
++) 
 608                         td
->td_stripbytecount
[i
] = rowbytes
*rowsperstrip
; 
 610         TIFFSetFieldBit(tif
, FIELD_STRIPBYTECOUNTS
); 
 611         if (!TIFFFieldSet(tif
, FIELD_ROWSPERSTRIP
)) 
 612                 td
->td_rowsperstrip 
= td
->td_imagelength
; 
 616 MissingRequired(TIFF
* tif
, const char* tagname
) 
 618         TIFFError(tif
->tif_name
, 
 619             "TIFF directory is missing required \"%s\" field", tagname
); 
 623  * Check the count field of a directory 
 624  * entry against a known value.  The caller 
 625  * is expected to skip/ignore the tag if 
 626  * there is a mismatch. 
 629 CheckDirCount(TIFF
* tif
, TIFFDirEntry
* dir
, uint32 count
) 
 631         if (count 
!= dir
->tdir_count
) { 
 632                 TIFFWarning(tif
->tif_name
, 
 633         "incorrect count for field \"%s\" (%lu, expecting %lu); tag ignored", 
 634                     _TIFFFieldWithTag(tif
, dir
->tdir_tag
)->field_name
, 
 635                     dir
->tdir_count
, count
); 
 642  * Fetch a contiguous directory item. 
 645 TIFFFetchData(TIFF
* tif
, TIFFDirEntry
* dir
, char* cp
) 
 647         int w 
= tiffDataWidth
[dir
->tdir_type
]; 
 648         tsize_t cc 
= dir
->tdir_count 
* w
; 
 650         if (!isMapped(tif
)) { 
 651                 if (!SeekOK(tif
, dir
->tdir_offset
)) 
 653                 if (!ReadOK(tif
, cp
, cc
)) 
 656                 if (dir
->tdir_offset 
+ cc 
> tif
->tif_size
) 
 658                 _TIFFmemcpy(cp
, tif
->tif_base 
+ dir
->tdir_offset
, cc
); 
 660         if (tif
->tif_flags 
& TIFF_SWAB
) { 
 661                 switch (dir
->tdir_type
) { 
 664                         TIFFSwabArrayOfShort((uint16
*) cp
, dir
->tdir_count
); 
 669                         TIFFSwabArrayOfLong((uint32
*) cp
, dir
->tdir_count
); 
 673                         TIFFSwabArrayOfLong((uint32
*) cp
, 2*dir
->tdir_count
); 
 676                         TIFFSwabArrayOfDouble((double*) cp
, dir
->tdir_count
); 
 682         TIFFError(tif
->tif_name
, "Error fetching data for field \"%s\"", 
 683             _TIFFFieldWithTag(tif
, dir
->tdir_tag
)->field_name
); 
 684         return ((tsize_t
) 0); 
 688  * Fetch an ASCII item from the file. 
 691 TIFFFetchString(TIFF
* tif
, TIFFDirEntry
* dir
, char* cp
) 
 693         if (dir
->tdir_count 
<= 4) { 
 694                 uint32 l 
= dir
->tdir_offset
; 
 695                 if (tif
->tif_flags 
& TIFF_SWAB
) 
 697                 _TIFFmemcpy(cp
, &l
, dir
->tdir_count
); 
 700         return (TIFFFetchData(tif
, dir
, cp
)); 
 704  * Convert numerator+denominator to float. 
 707 cvtRational(TIFF
* tif
, TIFFDirEntry
* dir
, uint32 num
, uint32 denom
, float* rv
) 
 710                 TIFFError(tif
->tif_name
, 
 711                     "%s: Rational with zero denominator (num = %lu)", 
 712                     _TIFFFieldWithTag(tif
, dir
->tdir_tag
)->field_name
, num
); 
 715                 if (dir
->tdir_type 
== TIFF_RATIONAL
) 
 716                         *rv 
= ((float)num 
/ (float)denom
); 
 718                         *rv 
= ((float)(int32
)num 
/ (float)(int32
)denom
); 
 724  * Fetch a rational item from the file 
 725  * at offset off and return the value 
 726  * as a floating point number. 
 729 TIFFFetchRational(TIFF
* tif
, TIFFDirEntry
* dir
) 
 734         return (!TIFFFetchData(tif
, dir
, (char *)l
) || 
 735             !cvtRational(tif
, dir
, l
[0], l
[1], &v
) ? 1.0f 
: v
); 
 739  * Fetch a single floating point value 
 740  * from the offset field and return it 
 744 TIFFFetchFloat(TIFF
* tif
, TIFFDirEntry
* dir
) 
 746         long l 
= TIFFExtractData(tif
, dir
->tdir_type
, dir
->tdir_offset
); 
 747         float v 
= *(float*) &l
; 
 748         TIFFCvtIEEEFloatToNative(tif
, 1, &v
); 
 753  * Fetch an array of BYTE or SBYTE values. 
 756 TIFFFetchByteArray(TIFF
* tif
, TIFFDirEntry
* dir
, uint16
* v
) 
 758         if (dir
->tdir_count 
<= 4) { 
 760                  * Extract data from offset field. 
 762                 if (tif
->tif_header
.tiff_magic 
== TIFF_BIGENDIAN
) { 
 763                         switch (dir
->tdir_count
) { 
 764                         case 4: v
[3] = dir
->tdir_offset 
& 0xff; 
 765                         case 3: v
[2] = (dir
->tdir_offset 
>> 8) & 0xff; 
 766                         case 2: v
[1] = (dir
->tdir_offset 
>> 16) & 0xff; 
 767                         case 1: v
[0] = dir
->tdir_offset 
>> 24; 
 770                         switch (dir
->tdir_count
) { 
 771                         case 4: v
[3] = dir
->tdir_offset 
>> 24; 
 772                         case 3: v
[2] = (dir
->tdir_offset 
>> 16) & 0xff; 
 773                         case 2: v
[1] = (dir
->tdir_offset 
>> 8) & 0xff; 
 774                         case 1: v
[0] = dir
->tdir_offset 
& 0xff; 
 779                 return (TIFFFetchData(tif
, dir
, (char*) v
) != 0);       /* XXX */ 
 783  * Fetch an array of SHORT or SSHORT values. 
 786 TIFFFetchShortArray(TIFF
* tif
, TIFFDirEntry
* dir
, uint16
* v
) 
 788         if (dir
->tdir_count 
<= 2) { 
 789                 if (tif
->tif_header
.tiff_magic 
== TIFF_BIGENDIAN
) { 
 790                         switch (dir
->tdir_count
) { 
 791                         case 2: v
[1] = dir
->tdir_offset 
& 0xffff; 
 792                         case 1: v
[0] = dir
->tdir_offset 
>> 16; 
 795                         switch (dir
->tdir_count
) { 
 796                         case 2: v
[1] = dir
->tdir_offset 
>> 16; 
 797                         case 1: v
[0] = dir
->tdir_offset 
& 0xffff; 
 802                 return (TIFFFetchData(tif
, dir
, (char *)v
) != 0); 
 806  * Fetch a pair of SHORT or BYTE values. 
 809 TIFFFetchShortPair(TIFF
* tif
, TIFFDirEntry
* dir
) 
 814         switch (dir
->tdir_type
) { 
 817                 ok 
= TIFFFetchShortArray(tif
, dir
, v
); 
 821                 ok  
= TIFFFetchByteArray(tif
, dir
, v
); 
 825                 TIFFSetField(tif
, dir
->tdir_tag
, v
[0], v
[1]); 
 830  * Fetch an array of LONG or SLONG values. 
 833 TIFFFetchLongArray(TIFF
* tif
, TIFFDirEntry
* dir
, uint32
* v
) 
 835         if (dir
->tdir_count 
== 1) { 
 836                 v
[0] = dir
->tdir_offset
; 
 839                 return (TIFFFetchData(tif
, dir
, (char*) v
) != 0); 
 843  * Fetch an array of RATIONAL or SRATIONAL values. 
 846 TIFFFetchRationalArray(TIFF
* tif
, TIFFDirEntry
* dir
, float* v
) 
 851         l 
= (uint32
*)CheckMalloc(tif
, 
 852             dir
->tdir_count
*tiffDataWidth
[dir
->tdir_type
], 
 853             "to fetch array of rationals"); 
 855                 if (TIFFFetchData(tif
, dir
, (char *)l
)) { 
 857                         for (i 
= 0; i 
< dir
->tdir_count
; i
++) { 
 858                                 ok 
= cvtRational(tif
, dir
, 
 859                                     l
[2*i
+0], l
[2*i
+1], &v
[i
]); 
 864                 _TIFFfree((char *)l
); 
 870  * Fetch an array of FLOAT values. 
 873 TIFFFetchFloatArray(TIFF
* tif
, TIFFDirEntry
* dir
, float* v
) 
 876         if (dir
->tdir_count 
== 1) { 
 877                 v
[0] = *(float*) &dir
->tdir_offset
; 
 878                 TIFFCvtIEEEFloatToNative(tif
, dir
->tdir_count
, v
); 
 880         } else  if (TIFFFetchData(tif
, dir
, (char*) v
)) { 
 881                 TIFFCvtIEEEFloatToNative(tif
, dir
->tdir_count
, v
); 
 888  * Fetch an array of DOUBLE values. 
 891 TIFFFetchDoubleArray(TIFF
* tif
, TIFFDirEntry
* dir
, double* v
) 
 893         if (TIFFFetchData(tif
, dir
, (char*) v
)) { 
 894                 TIFFCvtIEEEDoubleToNative(tif
, dir
->tdir_count
, v
); 
 901  * Fetch an array of ANY values.  The actual values are 
 902  * returned as doubles which should be able hold all the 
 903  * types.  Yes, there really should be an tany_t to avoid 
 904  * this potential non-portability ...  Note in particular 
 905  * that we assume that the double return value vector is 
 906  * large enough to read in any fundamental type.  We use 
 907  * that vector as a buffer to read in the base type vector 
 908  * and then convert it in place to double (from end 
 909  * to front of course). 
 912 TIFFFetchAnyArray(TIFF
* tif
, TIFFDirEntry
* dir
, double* v
) 
 916         switch (dir
->tdir_type
) { 
 919                 if (!TIFFFetchByteArray(tif
, dir
, (uint16
*) v
)) 
 921                 if (dir
->tdir_type 
== TIFF_BYTE
) { 
 922                         uint16
* vp 
= (uint16
*) v
; 
 923                         for (i 
= dir
->tdir_count
-1; i 
>= 0; i
--) 
 926                         int16
* vp 
= (int16
*) v
; 
 927                         for (i 
= dir
->tdir_count
-1; i 
>= 0; i
--) 
 933                 if (!TIFFFetchShortArray(tif
, dir
, (uint16
*) v
)) 
 935                 if (dir
->tdir_type 
== TIFF_SHORT
) { 
 936                         uint16
* vp 
= (uint16
*) v
; 
 937                         for (i 
= dir
->tdir_count
-1; i 
>= 0; i
--) 
 940                         int16
* vp 
= (int16
*) v
; 
 941                         for (i 
= dir
->tdir_count
-1; i 
>= 0; i
--) 
 947                 if (!TIFFFetchLongArray(tif
, dir
, (uint32
*) v
)) 
 949                 if (dir
->tdir_type 
== TIFF_LONG
) { 
 950                         uint32
* vp 
= (uint32
*) v
; 
 951                         for (i 
= dir
->tdir_count
-1; i 
>= 0; i
--) 
 954                         int32
* vp 
= (int32
*) v
; 
 955                         for (i 
= dir
->tdir_count
-1; i 
>= 0; i
--) 
 961                 if (!TIFFFetchRationalArray(tif
, dir
, (float*) v
)) 
 963                 { float* vp 
= (float*) v
; 
 964                   for (i 
= dir
->tdir_count
-1; i 
>= 0; i
--) 
 969                 if (!TIFFFetchFloatArray(tif
, dir
, (float*) v
)) 
 971                 { float* vp 
= (float*) v
; 
 972                   for (i 
= dir
->tdir_count
-1; i 
>= 0; i
--) 
 977                 return (TIFFFetchDoubleArray(tif
, dir
, (double*) v
)); 
 982                 TIFFError(tif
->tif_name
, 
 983                     "Cannot read TIFF_ANY type %d for field \"%s\"", 
 984                     _TIFFFieldWithTag(tif
, dir
->tdir_tag
)->field_name
); 
 991  * Fetch a tag that is not handled by special case code. 
 994 TIFFFetchNormalTag(TIFF
* tif
, TIFFDirEntry
* dp
) 
 996         static const char mesg
[] = "to fetch tag value"; 
 998         const TIFFFieldInfo
* fip 
= _TIFFFieldWithTag(tif
, dp
->tdir_tag
); 
1000         if (dp
->tdir_count 
> 1) {               /* array of values */ 
1003                 switch (dp
->tdir_type
) { 
1006                         /* NB: always expand BYTE values to shorts */ 
1007                         cp 
= CheckMalloc(tif
, 
1008                             dp
->tdir_count 
* sizeof (uint16
), mesg
); 
1009                         ok 
= cp 
&& TIFFFetchByteArray(tif
, dp
, (uint16
*) cp
); 
1013                         cp 
= CheckMalloc(tif
, 
1014                             dp
->tdir_count 
* sizeof (uint16
), mesg
); 
1015                         ok 
= cp 
&& TIFFFetchShortArray(tif
, dp
, (uint16
*) cp
); 
1019                         cp 
= CheckMalloc(tif
, 
1020                             dp
->tdir_count 
* sizeof (uint32
), mesg
); 
1021                         ok 
= cp 
&& TIFFFetchLongArray(tif
, dp
, (uint32
*) cp
); 
1024                 case TIFF_SRATIONAL
: 
1025                         cp 
= CheckMalloc(tif
, 
1026                             dp
->tdir_count 
* sizeof (float), mesg
); 
1027                         ok 
= cp 
&& TIFFFetchRationalArray(tif
, dp
, (float*) cp
); 
1030                         cp 
= CheckMalloc(tif
, 
1031                             dp
->tdir_count 
* sizeof (float), mesg
); 
1032                         ok 
= cp 
&& TIFFFetchFloatArray(tif
, dp
, (float*) cp
); 
1035                         cp 
= CheckMalloc(tif
, 
1036                             dp
->tdir_count 
* sizeof (double), mesg
); 
1037                         ok 
= cp 
&& TIFFFetchDoubleArray(tif
, dp
, (double*) cp
); 
1040                 case TIFF_UNDEFINED
:            /* bit of a cheat... */ 
1042                          * Some vendors write strings w/o the trailing 
1043                          * NULL byte, so always append one just in case. 
1045                         cp 
= CheckMalloc(tif
, dp
->tdir_count
+1, mesg
); 
1046                         if( (ok 
= (cp 
&& TIFFFetchString(tif
, dp
, cp
))) != 0 ) 
1047                                 cp
[dp
->tdir_count
] = '\0';      /* XXX */ 
1051                         ok 
= (fip
->field_passcount 
? 
1052                             TIFFSetField(tif
, dp
->tdir_tag
, dp
->tdir_count
, cp
) 
1053                           : TIFFSetField(tif
, dp
->tdir_tag
, cp
)); 
1057         } else if (CheckDirCount(tif
, dp
, 1)) { /* singleton value */ 
1058                 switch (dp
->tdir_type
) { 
1064                          * If the tag is also acceptable as a LONG or SLONG 
1065                          * then TIFFSetField will expect an uint32 parameter 
1066                          * passed to it (through varargs).  Thus, for machines 
1067                          * where sizeof (int) != sizeof (uint32) we must do 
1068                          * a careful check here.  It's hard to say if this 
1069                          * is worth optimizing. 
1071                          * NB: We use TIFFFieldWithTag here knowing that 
1072                          *     it returns us the first entry in the table 
1073                          *     for the tag and that that entry is for the 
1074                          *     widest potential data type the tag may have. 
1076                         { TIFFDataType type 
= fip
->field_type
; 
1077                           if (type 
!= TIFF_LONG 
&& type 
!= TIFF_SLONG
) { 
1079                            TIFFExtractData(tif
, dp
->tdir_type
, dp
->tdir_offset
); 
1080                                 ok 
= (fip
->field_passcount 
? 
1081                                     TIFFSetField(tif
, dp
->tdir_tag
, 1, &v
) 
1082                                   : TIFFSetField(tif
, dp
->tdir_tag
, v
)); 
1090                     TIFFExtractData(tif
, dp
->tdir_type
, dp
->tdir_offset
); 
1091                           ok 
= (fip
->field_passcount 
?  
1092                               TIFFSetField(tif
, dp
->tdir_tag
, 1, &v32
) 
1093                             : TIFFSetField(tif
, dp
->tdir_tag
, v32
)); 
1097                 case TIFF_SRATIONAL
: 
1099                         { float v 
= (dp
->tdir_type 
== TIFF_FLOAT 
?  
1100                               TIFFFetchFloat(tif
, dp
) 
1101                             : TIFFFetchRational(tif
, dp
)); 
1102                           ok 
= (fip
->field_passcount 
? 
1103                               TIFFSetField(tif
, dp
->tdir_tag
, 1, &v
) 
1104                             : TIFFSetField(tif
, dp
->tdir_tag
, v
)); 
1109                           ok 
= (TIFFFetchDoubleArray(tif
, dp
, &v
) && 
1110                             (fip
->field_passcount 
? 
1111                               TIFFSetField(tif
, dp
->tdir_tag
, 1, &v
) 
1112                             : TIFFSetField(tif
, dp
->tdir_tag
, v
)) 
1117                 case TIFF_UNDEFINED
:            /* bit of a cheat... */ 
1119                           if( (ok 
= (TIFFFetchString(tif
, dp
, c
) != 0)) != 0 ){ 
1120                                 c
[1] = '\0';            /* XXX paranoid */ 
1121                                 ok 
= TIFFSetField(tif
, dp
->tdir_tag
, c
); 
1130 #define NITEMS(x)       (sizeof (x) / sizeof (x[0])) 
1132  * Fetch samples/pixel short values for  
1133  * the specified tag and verify that 
1134  * all values are the same. 
1137 TIFFFetchPerSampleShorts(TIFF
* tif
, TIFFDirEntry
* dir
, int* pl
) 
1139         int samples 
= tif
->tif_dir
.td_samplesperpixel
; 
1142         if (CheckDirCount(tif
, dir
, (uint32
) samples
)) { 
1146                 if (samples 
> NITEMS(buf
)) 
1147                         v 
= (uint16
*) _TIFFmalloc(samples 
* sizeof (uint16
)); 
1148                 if (TIFFFetchShortArray(tif
, dir
, v
)) { 
1150                         for (i 
= 1; i 
< samples
; i
++) 
1152                                         TIFFError(tif
->tif_name
, 
1153                 "Cannot handle different per-sample values for field \"%s\"", 
1154                            _TIFFFieldWithTag(tif
, dir
->tdir_tag
)->field_name
); 
1162                         _TIFFfree((char*) v
); 
1168  * Fetch samples/pixel ANY values for  
1169  * the specified tag and verify that 
1170  * all values are the same. 
1173 TIFFFetchPerSampleAnys(TIFF
* tif
, TIFFDirEntry
* dir
, double* pl
) 
1175         int samples 
= (int) tif
->tif_dir
.td_samplesperpixel
; 
1178         if (CheckDirCount(tif
, dir
, (uint32
) samples
)) { 
1182                 if (samples 
> NITEMS(buf
)) 
1183                         v 
= (double*) _TIFFmalloc(samples 
* sizeof (double)); 
1184                 if (TIFFFetchAnyArray(tif
, dir
, v
)) { 
1186                         for (i 
= 1; i 
< samples
; i
++) 
1188                                         TIFFError(tif
->tif_name
, 
1189                 "Cannot handle different per-sample values for field \"%s\"", 
1190                            _TIFFFieldWithTag(tif
, dir
->tdir_tag
)->field_name
); 
1205  * Fetch a set of offsets or lengths. 
1206  * While this routine says "strips", 
1207  * in fact it's also used for tiles. 
1210 TIFFFetchStripThing(TIFF
* tif
, TIFFDirEntry
* dir
, long nstrips
, uint32
** lpp
) 
1212         register uint32
* lp
; 
1215         if (!CheckDirCount(tif
, dir
, (uint32
) nstrips
)) 
1218          * Allocate space for strip information. 
1221             (*lpp 
= (uint32 
*)CheckMalloc(tif
, 
1222               nstrips 
* sizeof (uint32
), "for strip array")) == NULL
) 
1225         if (dir
->tdir_type 
== (int)TIFF_SHORT
) { 
1227                  * Handle uint16->uint32 expansion. 
1229                 uint16
* dp 
= (uint16
*) CheckMalloc(tif
, 
1230                     dir
->tdir_count
* sizeof (uint16
), "to fetch strip tag"); 
1233                 if( (status 
= TIFFFetchShortArray(tif
, dir
, dp
)) != 0 ) { 
1234                         register uint16
* wp 
= dp
; 
1235                         while (nstrips
-- > 0) 
1238                 _TIFFfree((char*) dp
); 
1240                 status 
= TIFFFetchLongArray(tif
, dir
, lp
); 
1244 #define NITEMS(x)       (sizeof (x) / sizeof (x[0])) 
1246  * Fetch and set the ExtraSamples tag. 
1249 TIFFFetchExtraSamples(TIFF
* tif
, TIFFDirEntry
* dir
) 
1255         if (dir
->tdir_count 
> NITEMS(buf
)) 
1256                 v 
= (uint16
*) _TIFFmalloc(dir
->tdir_count 
* sizeof (uint16
)); 
1257         if (dir
->tdir_type 
== TIFF_BYTE
) 
1258                 status 
= TIFFFetchByteArray(tif
, dir
, v
); 
1260                 status 
= TIFFFetchShortArray(tif
, dir
, v
); 
1262                 status 
= TIFFSetField(tif
, dir
->tdir_tag
, dir
->tdir_count
, v
); 
1264                 _TIFFfree((char*) v
); 
1269 #ifdef COLORIMETRY_SUPPORT 
1271  * Fetch and set the RefBlackWhite tag. 
1274 TIFFFetchRefBlackWhite(TIFF
* tif
, TIFFDirEntry
* dir
) 
1276         static const char mesg
[] = "for \"ReferenceBlackWhite\" array"; 
1280         if (dir
->tdir_type 
== TIFF_RATIONAL
) 
1281                 return (TIFFFetchNormalTag(tif
, dir
)); 
1283          * Handle LONG's for backward compatibility. 
1285         cp 
= CheckMalloc(tif
, dir
->tdir_count 
* sizeof (uint32
), mesg
); 
1286         if( (ok 
= (cp 
&& TIFFFetchLongArray(tif
, dir
, (uint32
*) cp
))) != 0) { 
1287                 float* fp 
= (float*) 
1288                     CheckMalloc(tif
, dir
->tdir_count 
* sizeof (float), mesg
); 
1289                 if( (ok 
= (fp 
!= NULL
)) != 0 ) { 
1291                         for (i 
= 0; i 
< dir
->tdir_count
; i
++) 
1292                                 fp
[i
] = (float)((uint32
*) cp
)[i
]; 
1293                         ok 
= TIFFSetField(tif
, dir
->tdir_tag
, fp
); 
1294                         _TIFFfree((char*) fp
); 
1304  * Replace a single strip (tile) of uncompressed data by 
1305  * multiple strips (tiles), each approximately 8Kbytes. 
1306  * This is useful for dealing with large images or 
1307  * for dealing with machines with a limited amount 
1311 ChopUpSingleUncompressedStrip(TIFF
* tif
) 
1313         register TIFFDirectory 
*td 
= &tif
->tif_dir
; 
1314         uint32 bytecount 
= td
->td_stripbytecount
[0]; 
1315         uint32 offset 
= td
->td_stripoffset
[0]; 
1316         tsize_t rowbytes 
= TIFFVTileSize(tif
, 1), stripbytes
; 
1317         tstrip_t strip
, nstrips
, rowsperstrip
; 
1322          * Make the rows hold at least one 
1323          * scanline, but fill 8k if possible. 
1325         if (rowbytes 
> 8192) { 
1326                 stripbytes 
= rowbytes
; 
1329                 rowsperstrip 
= 8192 / rowbytes
; 
1330                 stripbytes 
= rowbytes 
* rowsperstrip
; 
1332         /* never increase the number of strips in an image */ 
1333         if (rowsperstrip 
>= td
->td_rowsperstrip
) 
1335         nstrips 
= (tstrip_t
) TIFFhowmany(bytecount
, stripbytes
); 
1336         newcounts 
= (uint32
*) CheckMalloc(tif
, nstrips 
* sizeof (uint32
), 
1337                                 "for chopped \"StripByteCounts\" array"); 
1338         newoffsets 
= (uint32
*) CheckMalloc(tif
, nstrips 
* sizeof (uint32
), 
1339                                 "for chopped \"StripOffsets\" array"); 
1340         if (newcounts 
== NULL 
|| newoffsets 
== NULL
) { 
1342                  * Unable to allocate new strip information, give 
1343                  * up and use the original one strip information. 
1345                 if (newcounts 
!= NULL
) 
1346                         _TIFFfree(newcounts
); 
1347                 if (newoffsets 
!= NULL
) 
1348                         _TIFFfree(newoffsets
); 
1352          * Fill the strip information arrays with 
1353          * new bytecounts and offsets that reflect 
1354          * the broken-up format. 
1356         for (strip 
= 0; strip 
< nstrips
; strip
++) { 
1357                 if (stripbytes 
> bytecount
) 
1358                         stripbytes 
= bytecount
; 
1359                 newcounts
[strip
] = stripbytes
; 
1360                 newoffsets
[strip
] = offset
; 
1361                 offset 
+= stripbytes
; 
1362                 bytecount 
-= stripbytes
; 
1365          * Replace old single strip info with multi-strip info. 
1367         td
->td_stripsperimage 
= td
->td_nstrips 
= nstrips
; 
1368         TIFFSetField(tif
, TIFFTAG_ROWSPERSTRIP
, rowsperstrip
); 
1370         _TIFFfree(td
->td_stripbytecount
); 
1371         _TIFFfree(td
->td_stripoffset
); 
1372         td
->td_stripbytecount 
= newcounts
; 
1373         td
->td_stripoffset 
= newoffsets
;