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 int 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, size_t nmemb, size_t elem_size, const char* what)
68
tsize_t bytes = nmemb * elem_size;
70
if (nmemb && elem_size && bytes / elem_size == nmemb)
71
cp = (char*)_TIFFmalloc(bytes);
74
TIFFError(tif->tif_name, "No space %s", what);
80
* Read the next TIFF directory from a file
81
* and convert it to the internal format.
82
* We read directories sequentially.
85
TIFFReadDirectory(TIFF* tif)
87
static const char module[] = "TIFFReadDirectory";
89
register TIFFDirEntry* dp;
91
register TIFFDirectory* td;
96
const TIFFFieldInfo* fip;
101
int diroutoforderwarning = 0;
104
tif->tif_diroff = tif->tif_nextdiroff;
105
if (tif->tif_diroff == 0) /* no more directories */
109
* XXX: Trick to prevent IFD looping. The one can create TIFF file
110
* with looped directory pointers. We will maintain a list of already
111
* seen directories and check every IFD offset against this list.
113
for (n = 0; n < tif->tif_dirnumber; n++) {
114
if (tif->tif_dirlist[n] == tif->tif_diroff)
117
tif->tif_dirnumber++;
118
new_dirlist = _TIFFrealloc(tif->tif_dirlist,
119
tif->tif_dirnumber * sizeof(toff_t));
122
"%.1000s: Failed to allocate space for IFD list",
126
tif->tif_dirlist = new_dirlist;
127
tif->tif_dirlist[tif->tif_dirnumber - 1] = tif->tif_diroff;
130
* Cleanup any previous compression state.
132
(*tif->tif_cleanup)(tif);
135
if (!isMapped(tif)) {
136
if (!SeekOK(tif, tif->tif_diroff)) {
138
"%.1000s: Seek error accessing TIFF directory",
142
if (!ReadOK(tif, &dircount, sizeof (uint16))) {
144
"%.1000s: Can not read TIFF directory count",
148
if (tif->tif_flags & TIFF_SWAB)
149
TIFFSwabShort(&dircount);
150
dir = (TIFFDirEntry *)CheckMalloc(tif,
151
dircount, sizeof (TIFFDirEntry), "to read TIFF directory");
154
if (!ReadOK(tif, dir, dircount*sizeof (TIFFDirEntry))) {
156
"%.100s: Can not read TIFF directory",
161
* Read offset to next directory for sequential scans.
163
(void) ReadOK(tif, &nextdiroff, sizeof (uint32));
165
toff_t off = tif->tif_diroff;
167
if (off + sizeof (uint16) > tif->tif_size) {
169
"%.1000s: Can not read TIFF directory count",
173
_TIFFmemcpy(&dircount, tif->tif_base + off, sizeof (uint16));
174
off += sizeof (uint16);
175
if (tif->tif_flags & TIFF_SWAB)
176
TIFFSwabShort(&dircount);
177
dir = (TIFFDirEntry *)CheckMalloc(tif,
178
dircount, sizeof (TIFFDirEntry), "to read TIFF directory");
181
if (off + dircount*sizeof (TIFFDirEntry) > tif->tif_size) {
183
"%.1000s: Can not read TIFF directory",
187
_TIFFmemcpy(dir, tif->tif_base + off,
188
dircount*sizeof (TIFFDirEntry));
190
off += dircount* sizeof (TIFFDirEntry);
191
if (off + sizeof (uint32) <= tif->tif_size)
192
_TIFFmemcpy(&nextdiroff, tif->tif_base+off, sizeof (uint32));
194
if (tif->tif_flags & TIFF_SWAB)
195
TIFFSwabLong(&nextdiroff);
196
tif->tif_nextdiroff = nextdiroff;
198
tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
200
* Setup default value and then make a pass over
201
* the fields to check type and tag information,
202
* and to extract info required to size data
203
* structures. A second pass is made afterwards
204
* to read in everthing not taken in the first pass.
207
/* free any old stuff and reinit */
208
TIFFFreeDirectory(tif);
209
TIFFDefaultDirectory(tif);
211
* Electronic Arts writes gray-scale TIFF files
212
* without a PlanarConfiguration directory entry.
213
* Thus we setup a default value here, even though
214
* the TIFF spec says there is no default value.
216
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
219
* Sigh, we must make a separate pass through the
220
* directory for the following reason:
222
* We must process the Compression tag in the first pass
223
* in order to merge in codec-private tag definitions (otherwise
224
* we may get complaints about unknown tags). However, the
225
* Compression tag may be dependent on the SamplesPerPixel
226
* tag value because older TIFF specs permited Compression
227
* to be written as a SamplesPerPixel-count tag entry.
228
* Thus if we don't first figure out the correct SamplesPerPixel
229
* tag value then we may end up ignoring the Compression tag
230
* value because it has an incorrect count value (if the
231
* true value of SamplesPerPixel is not 1).
233
* It sure would have been nice if Aldus had really thought
234
* this stuff through carefully.
236
for (dp = dir, n = dircount; n > 0; n--, dp++) {
237
if (tif->tif_flags & TIFF_SWAB) {
238
TIFFSwabArrayOfShort(&dp->tdir_tag, 2);
239
TIFFSwabArrayOfLong(&dp->tdir_count, 2);
241
if (dp->tdir_tag == TIFFTAG_SAMPLESPERPIXEL) {
242
if (!TIFFFetchNormalTag(tif, dp))
244
dp->tdir_tag = IGNORE;
248
* First real pass over the directory.
251
for (dp = dir, n = dircount; n > 0; n--, dp++) {
254
* Find the field information entry for this tag.
255
* Added check for tags to ignore ... [BFC]
257
if( TIFFReassignTagToIgnore(TIS_EXTRACT, dp->tdir_tag) )
258
dp->tdir_tag = IGNORE;
260
if (fix >= tif->tif_nfields || dp->tdir_tag == IGNORE)
264
* Silicon Beach (at least) writes unordered
265
* directory tags (violating the spec). Handle
266
* it here, but be obnoxious (maybe they'll fix it?).
268
if (dp->tdir_tag < tif->tif_fieldinfo[fix]->field_tag) {
269
if (!diroutoforderwarning) {
271
"%.1000s: invalid TIFF directory; tags are not sorted in ascending order",
273
diroutoforderwarning = 1;
275
fix = 0; /* O(n^2) */
277
while (fix < tif->tif_nfields &&
278
tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
280
if (fix >= tif->tif_nfields ||
281
tif->tif_fieldinfo[fix]->field_tag != dp->tdir_tag) {
284
"%.1000s: unknown field with tag %d (0x%x) encountered",
285
tif->tif_name, dp->tdir_tag, dp->tdir_tag);
287
TIFFMergeFieldInfo( tif,
288
_TIFFCreateAnonFieldInfo( tif,
290
(TIFFDataType) dp->tdir_type ),
293
while (fix < tif->tif_nfields &&
294
tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
298
* Null out old tags that we ignore.
300
if (tif->tif_fieldinfo[fix]->field_bit == FIELD_IGNORE) {
302
dp->tdir_tag = IGNORE;
308
fip = tif->tif_fieldinfo[fix];
309
while (dp->tdir_type != (u_short) fip->field_type) {
310
if (fip->field_type == TIFF_ANY) /* wildcard */
313
if (fix == tif->tif_nfields ||
314
fip->field_tag != dp->tdir_tag) {
316
"%.1000s: wrong data type %d for \"%s\"; tag ignored",
317
tif->tif_name, dp->tdir_type,
323
* Check count if known in advance.
325
if (fip->field_readcount != TIFF_VARIABLE) {
326
uint32 expected = (fip->field_readcount == TIFF_SPP) ?
327
(uint32) td->td_samplesperpixel :
328
(uint32) fip->field_readcount;
329
if (!CheckDirCount(tif, dp, expected))
333
switch (dp->tdir_tag) {
334
case TIFFTAG_COMPRESSION:
336
* The 5.0 spec says the Compression tag has
337
* one value, while earlier specs say it has
338
* one value per sample. Because of this, we
339
* accept the tag if one value is supplied.
341
if (dp->tdir_count == 1) {
342
v = TIFFExtractData(tif,
343
dp->tdir_type, dp->tdir_offset);
344
if (!TIFFSetField(tif, dp->tdir_tag, (int)v))
348
if (!TIFFFetchPerSampleShorts(tif, dp, &iv) ||
349
!TIFFSetField(tif, dp->tdir_tag, iv))
351
dp->tdir_tag = IGNORE;
353
case TIFFTAG_STRIPOFFSETS:
354
case TIFFTAG_STRIPBYTECOUNTS:
355
case TIFFTAG_TILEOFFSETS:
356
case TIFFTAG_TILEBYTECOUNTS:
357
TIFFSetFieldBit(tif, fip->field_bit);
359
case TIFFTAG_IMAGEWIDTH:
360
case TIFFTAG_IMAGELENGTH:
361
case TIFFTAG_IMAGEDEPTH:
362
case TIFFTAG_TILELENGTH:
363
case TIFFTAG_TILEWIDTH:
364
case TIFFTAG_TILEDEPTH:
365
case TIFFTAG_PLANARCONFIG:
366
case TIFFTAG_ROWSPERSTRIP:
367
if (!TIFFFetchNormalTag(tif, dp))
369
dp->tdir_tag = IGNORE;
371
case TIFFTAG_EXTRASAMPLES:
372
(void) TIFFFetchExtraSamples(tif, dp);
373
dp->tdir_tag = IGNORE;
379
* Allocate directory structure and setup defaults.
381
if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) {
382
MissingRequired(tif, "ImageLength");
385
if (!TIFFFieldSet(tif, FIELD_PLANARCONFIG)) {
386
MissingRequired(tif, "PlanarConfiguration");
390
* Setup appropriate structures (by strip or by tile)
392
if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
393
td->td_nstrips = TIFFNumberOfStrips(tif);
394
td->td_tilewidth = td->td_imagewidth;
395
td->td_tilelength = td->td_rowsperstrip;
396
td->td_tiledepth = td->td_imagedepth;
397
tif->tif_flags &= ~TIFF_ISTILED;
399
td->td_nstrips = TIFFNumberOfTiles(tif);
400
tif->tif_flags |= TIFF_ISTILED;
402
if (!td->td_nstrips) {
403
TIFFError(module, "%s: cannot handle zero number of %s",
404
tif->tif_name, isTiled(tif) ? "tiles" : "strips");
407
td->td_stripsperimage = td->td_nstrips;
408
if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
409
td->td_stripsperimage /= td->td_samplesperpixel;
410
if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {
412
isTiled(tif) ? "TileOffsets" : "StripOffsets");
417
* Second pass: extract other information.
419
for (dp = dir, n = dircount; n > 0; n--, dp++) {
420
if (dp->tdir_tag == IGNORE)
422
switch (dp->tdir_tag) {
423
case TIFFTAG_MINSAMPLEVALUE:
424
case TIFFTAG_MAXSAMPLEVALUE:
425
case TIFFTAG_BITSPERSAMPLE:
427
* The 5.0 spec says the Compression tag has
428
* one value, while earlier specs say it has
429
* one value per sample. Because of this, we
430
* accept the tag if one value is supplied.
432
* The MinSampleValue, MaxSampleValue and
433
* BitsPerSample tags are supposed to be written
434
* as one value/sample, but some vendors incorrectly
435
* write one value only -- so we accept that
438
if (dp->tdir_count == 1) {
439
v = TIFFExtractData(tif,
440
dp->tdir_type, dp->tdir_offset);
441
if (!TIFFSetField(tif, dp->tdir_tag, (int)v))
446
case TIFFTAG_DATATYPE:
447
case TIFFTAG_SAMPLEFORMAT:
448
if (!TIFFFetchPerSampleShorts(tif, dp, &iv) ||
449
!TIFFSetField(tif, dp->tdir_tag, iv))
452
case TIFFTAG_SMINSAMPLEVALUE:
453
case TIFFTAG_SMAXSAMPLEVALUE:
454
if (!TIFFFetchPerSampleAnys(tif, dp, &dv) ||
455
!TIFFSetField(tif, dp->tdir_tag, dv))
458
case TIFFTAG_STRIPOFFSETS:
459
case TIFFTAG_TILEOFFSETS:
460
if (!TIFFFetchStripThing(tif, dp,
461
td->td_nstrips, &td->td_stripoffset))
464
case TIFFTAG_STRIPBYTECOUNTS:
465
case TIFFTAG_TILEBYTECOUNTS:
466
if (!TIFFFetchStripThing(tif, dp,
467
td->td_nstrips, &td->td_stripbytecount))
470
case TIFFTAG_COLORMAP:
471
case TIFFTAG_TRANSFERFUNCTION:
473
* TransferFunction can have either 1x or 3x data
474
* values; Colormap can have only 3x items.
476
v = 1L<<td->td_bitspersample;
477
if (dp->tdir_tag == TIFFTAG_COLORMAP ||
478
dp->tdir_count != (uint32) v) {
479
if (!CheckDirCount(tif, dp, (uint32)(3*v)))
482
v *= sizeof (uint16);
483
cp = CheckMalloc(tif, dp->tdir_count, sizeof (uint16),
484
"to read \"TransferFunction\" tag");
486
if (TIFFFetchData(tif, dp, cp)) {
488
* This deals with there being only
489
* one array to apply to all samples.
492
(uint32)1 << td->td_bitspersample;
493
if (dp->tdir_count == c)
495
TIFFSetField(tif, dp->tdir_tag,
501
case TIFFTAG_PAGENUMBER:
502
case TIFFTAG_HALFTONEHINTS:
503
case TIFFTAG_YCBCRSUBSAMPLING:
504
case TIFFTAG_DOTRANGE:
505
(void) TIFFFetchShortPair(tif, dp);
507
#ifdef COLORIMETRY_SUPPORT
508
case TIFFTAG_REFERENCEBLACKWHITE:
509
(void) TIFFFetchRefBlackWhite(tif, dp);
512
/* BEGIN REV 4.0 COMPATIBILITY */
513
case TIFFTAG_OSUBFILETYPE:
515
switch (TIFFExtractData(tif, dp->tdir_type,
517
case OFILETYPE_REDUCEDIMAGE:
518
v = FILETYPE_REDUCEDIMAGE;
525
(void) TIFFSetField(tif,
526
TIFFTAG_SUBFILETYPE, (int)v);
528
/* END REV 4.0 COMPATIBILITY */
530
(void) TIFFFetchNormalTag(tif, dp);
535
* Verify Palette image has a Colormap.
537
if (td->td_photometric == PHOTOMETRIC_PALETTE &&
538
!TIFFFieldSet(tif, FIELD_COLORMAP)) {
539
MissingRequired(tif, "Colormap");
543
* Attempt to deal with a missing StripByteCounts tag.
545
if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
547
* Some manufacturers violate the spec by not giving
548
* the size of the strips. In this case, assume there
549
* is one uncompressed strip of data.
551
if ((td->td_planarconfig == PLANARCONFIG_CONTIG &&
552
td->td_nstrips > 1) ||
553
(td->td_planarconfig == PLANARCONFIG_SEPARATE &&
554
td->td_nstrips != td->td_samplesperpixel)) {
555
MissingRequired(tif, "StripByteCounts");
559
"%.1000s: TIFF directory is missing required "
560
"\"%s\" field, calculating from imagelength",
562
_TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
563
if (EstimateStripByteCounts(tif, dir, dircount) < 0)
566
* Assume we have wrong StripByteCount value (in case of single strip) in
568
* - it is equal to zero along with StripOffset;
569
* - it is larger than file itself (in case of uncompressed image).
571
#define BYTECOUNTLOOKSBAD \
572
( (td->td_stripbytecount[0] == 0 && td->td_stripoffset[0] != 0) || \
573
(td->td_compression == COMPRESSION_NONE && \
574
td->td_stripbytecount[0] > TIFFGetFileSize(tif) - td->td_stripoffset[0]) )
575
} else if (td->td_nstrips == 1 && BYTECOUNTLOOKSBAD) {
577
* Plexus (and others) sometimes give a value
578
* of zero for a tag when they don't know what
579
* the correct value is! Try and handle the
580
* simple case of estimating the size of a one
584
"%.1000s: Bogus \"%s\" field, ignoring and calculating from imagelength",
586
_TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
587
if(EstimateStripByteCounts(tif, dir, dircount) < 0)
591
_TIFFfree((char *)dir);
594
if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
595
td->td_maxsamplevalue = (uint16)((1L<<td->td_bitspersample)-1);
597
* Setup default compression scheme.
599
if (!TIFFFieldSet(tif, FIELD_COMPRESSION))
600
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
602
* Some manufacturers make life difficult by writing
603
* large amounts of uncompressed data as a single strip.
604
* This is contrary to the recommendations of the spec.
605
* The following makes an attempt at breaking such images
606
* into strips closer to the recommended 8k bytes. A
607
* side effect, however, is that the RowsPerStrip tag
608
* value may be changed.
610
if (td->td_nstrips == 1 && td->td_compression == COMPRESSION_NONE &&
611
(tif->tif_flags & (TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP)
612
ChopUpSingleUncompressedStrip(tif);
614
* Reinitialize i/o since we are starting on a new directory.
616
tif->tif_row = (uint32) -1;
617
tif->tif_curstrip = (tstrip_t) -1;
618
tif->tif_col = (uint32) -1;
619
tif->tif_curtile = (ttile_t) -1;
620
tif->tif_tilesize = TIFFTileSize(tif);
621
tif->tif_scanlinesize = TIFFScanlineSize(tif);
623
if (!tif->tif_tilesize) {
624
TIFFError(module, "%s: cannot handle zero tile size",
628
if (!tif->tif_scanlinesize) {
629
TIFFError(module, "%s: cannot handle zero scanline size",
641
EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
643
static const char module[] = "EstimateStripByteCounts";
645
register TIFFDirEntry *dp;
646
register TIFFDirectory *td = &tif->tif_dir;
649
if (td->td_stripbytecount)
650
_TIFFfree(td->td_stripbytecount);
651
td->td_stripbytecount = (uint32*)
652
CheckMalloc(tif, td->td_nstrips, sizeof (uint32),
653
"for \"StripByteCounts\" array");
654
if (td->td_compression != COMPRESSION_NONE) {
655
uint32 space = (uint32)(sizeof (TIFFHeader)
657
+ (dircount * sizeof (TIFFDirEntry))
659
toff_t filesize = TIFFGetFileSize(tif);
662
/* calculate amount of space used by indirect values */
663
for (dp = dir, n = dircount; n > 0; n--, dp++)
665
uint32 cc = TIFFDataWidth((TIFFDataType) dp->tdir_type);
668
"%.1000s: Cannot determine size of unknown tag type %d",
669
tif->tif_name, dp->tdir_type);
672
cc = cc * dp->tdir_count;
673
if (cc > sizeof (uint32))
676
space = filesize - space;
677
if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
678
space /= td->td_samplesperpixel;
679
for (i = 0; i < td->td_nstrips; i++)
680
td->td_stripbytecount[i] = space;
682
* This gross hack handles the case were the offset to
683
* the last strip is past the place where we think the strip
684
* should begin. Since a strip of data must be contiguous,
685
* it's safe to assume that we've overestimated the amount
686
* of data in the strip and trim this number back accordingly.
689
if (((toff_t)(td->td_stripoffset[i]+td->td_stripbytecount[i]))
691
td->td_stripbytecount[i] =
692
filesize - td->td_stripoffset[i];
694
uint32 rowbytes = TIFFScanlineSize(tif);
695
uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;
696
for (i = 0; i < td->td_nstrips; i++)
697
td->td_stripbytecount[i] = rowbytes*rowsperstrip;
699
TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
700
if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
701
td->td_rowsperstrip = td->td_imagelength;
706
MissingRequired(TIFF* tif, const char* tagname)
708
static const char module[] = "MissingRequired";
711
"%.1000s: TIFF directory is missing required \"%s\" field",
712
tif->tif_name, tagname);
716
* Check the count field of a directory
717
* entry against a known value. The caller
718
* is expected to skip/ignore the tag if
719
* there is a mismatch.
722
CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
724
if (count != dir->tdir_count) {
725
TIFFWarning(tif->tif_name,
726
"incorrect count for field \"%s\" (%lu, expecting %lu); tag ignored",
727
_TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
728
dir->tdir_count, count);
735
* Fetch a contiguous directory item.
738
TIFFFetchData(TIFF* tif, TIFFDirEntry* dir, char* cp)
740
int w = TIFFDataWidth((TIFFDataType) dir->tdir_type);
741
tsize_t cc = dir->tdir_count * w;
743
if (!isMapped(tif)) {
744
if (!SeekOK(tif, dir->tdir_offset))
746
if (!ReadOK(tif, cp, cc))
749
if (dir->tdir_offset + cc > tif->tif_size)
751
_TIFFmemcpy(cp, tif->tif_base + dir->tdir_offset, cc);
753
if (tif->tif_flags & TIFF_SWAB) {
754
switch (dir->tdir_type) {
757
TIFFSwabArrayOfShort((uint16*) cp, dir->tdir_count);
762
TIFFSwabArrayOfLong((uint32*) cp, dir->tdir_count);
766
TIFFSwabArrayOfLong((uint32*) cp, 2*dir->tdir_count);
769
TIFFSwabArrayOfDouble((double*) cp, dir->tdir_count);
775
TIFFError(tif->tif_name, "Error fetching data for field \"%s\"",
776
_TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
777
return ((tsize_t) 0);
781
* Fetch an ASCII item from the file.
784
TIFFFetchString(TIFF* tif, TIFFDirEntry* dir, char* cp)
786
if (dir->tdir_count <= 4) {
787
uint32 l = dir->tdir_offset;
788
if (tif->tif_flags & TIFF_SWAB)
790
_TIFFmemcpy(cp, &l, dir->tdir_count);
793
return (TIFFFetchData(tif, dir, cp));
797
* Convert numerator+denominator to float.
800
cvtRational(TIFF* tif, TIFFDirEntry* dir, uint32 num, uint32 denom, float* rv)
803
TIFFError(tif->tif_name,
804
"%s: Rational with zero denominator (num = %lu)",
805
_TIFFFieldWithTag(tif, dir->tdir_tag)->field_name, num);
808
if (dir->tdir_type == TIFF_RATIONAL)
809
*rv = ((float)num / (float)denom);
811
*rv = ((float)(int32)num / (float)(int32)denom);
817
* Fetch a rational item from the file
818
* at offset off and return the value
819
* as a floating point number.
822
TIFFFetchRational(TIFF* tif, TIFFDirEntry* dir)
827
return (!TIFFFetchData(tif, dir, (char *)l) ||
828
!cvtRational(tif, dir, l[0], l[1], &v) ? 1.0f : v);
832
* Fetch a single floating point value
833
* from the offset field and return it
837
TIFFFetchFloat(TIFF* tif, TIFFDirEntry* dir)
839
long l = TIFFExtractData(tif, dir->tdir_type, dir->tdir_offset);
840
float v = *(float*) &l;
841
TIFFCvtIEEEFloatToNative(tif, 1, &v);
846
* Fetch an array of BYTE or SBYTE values.
849
TIFFFetchByteArray(TIFF* tif, TIFFDirEntry* dir, uint16* v)
851
if (dir->tdir_count <= 4) {
853
* Extract data from offset field.
855
if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
856
if (dir->tdir_type == TIFF_SBYTE)
857
switch (dir->tdir_count) {
858
case 4: v[3] = (signed char)(dir->tdir_offset & 0xff);
859
case 3: v[2] = (signed char)((dir->tdir_offset >> 8) & 0xff);
860
case 2: v[1] = (signed char)((dir->tdir_offset >> 16) & 0xff);
861
case 1: v[0] = (signed char)(dir->tdir_offset >> 24);
864
switch (dir->tdir_count) {
865
case 4: v[3] = (uint16)(dir->tdir_offset & 0xff);
866
case 3: v[2] = (uint16)((dir->tdir_offset >> 8) & 0xff);
867
case 2: v[1] = (uint16)((dir->tdir_offset >> 16) & 0xff);
868
case 1: v[0] = (uint16)(dir->tdir_offset >> 24);
871
if (dir->tdir_type == TIFF_SBYTE)
872
switch (dir->tdir_count) {
873
case 4: v[3] = (signed char)(dir->tdir_offset >> 24);
874
case 3: v[2] = (signed char)((dir->tdir_offset >> 16) & 0xff);
875
case 2: v[1] = (signed char)((dir->tdir_offset >> 8) & 0xff);
876
case 1: v[0] = (signed char)(dir->tdir_offset & 0xff);
879
switch (dir->tdir_count) {
880
case 4: v[3] = (uint16)(dir->tdir_offset >> 24);
881
case 3: v[2] = (uint16)((dir->tdir_offset >> 16) & 0xff);
882
case 2: v[1] = (uint16)((dir->tdir_offset >> 8) & 0xff);
883
case 1: v[0] = (uint16)(dir->tdir_offset & 0xff);
888
return (TIFFFetchData(tif, dir, (char*) v) != 0); /* XXX */
892
* Fetch an array of SHORT or SSHORT values.
895
TIFFFetchShortArray(TIFF* tif, TIFFDirEntry* dir, uint16* v)
897
if (dir->tdir_count <= 2) {
898
if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
899
switch (dir->tdir_count) {
900
case 2: v[1] = (uint16) (dir->tdir_offset & 0xffff);
901
case 1: v[0] = (uint16) (dir->tdir_offset >> 16);
904
switch (dir->tdir_count) {
905
case 2: v[1] = (uint16) (dir->tdir_offset >> 16);
906
case 1: v[0] = (uint16) (dir->tdir_offset & 0xffff);
911
return (TIFFFetchData(tif, dir, (char *)v) != 0);
915
* Fetch a pair of SHORT or BYTE values.
918
TIFFFetchShortPair(TIFF* tif, TIFFDirEntry* dir)
923
switch (dir->tdir_type) {
926
ok = TIFFFetchShortArray(tif, dir, v);
930
ok = TIFFFetchByteArray(tif, dir, v);
934
TIFFSetField(tif, dir->tdir_tag, v[0], v[1]);
939
* Fetch an array of LONG or SLONG values.
942
TIFFFetchLongArray(TIFF* tif, TIFFDirEntry* dir, uint32* v)
944
if (dir->tdir_count == 1) {
945
v[0] = dir->tdir_offset;
948
return (TIFFFetchData(tif, dir, (char*) v) != 0);
952
* Fetch an array of RATIONAL or SRATIONAL values.
955
TIFFFetchRationalArray(TIFF* tif, TIFFDirEntry* dir, float* v)
960
l = (uint32*)CheckMalloc(tif,
961
dir->tdir_count, TIFFDataWidth((TIFFDataType) dir->tdir_type),
962
"to fetch array of rationals");
964
if (TIFFFetchData(tif, dir, (char *)l)) {
966
for (i = 0; i < dir->tdir_count; i++) {
967
ok = cvtRational(tif, dir,
968
l[2*i+0], l[2*i+1], &v[i]);
973
_TIFFfree((char *)l);
979
* Fetch an array of FLOAT values.
982
TIFFFetchFloatArray(TIFF* tif, TIFFDirEntry* dir, float* v)
985
if (dir->tdir_count == 1) {
986
v[0] = *(float*) &dir->tdir_offset;
987
TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v);
989
} else if (TIFFFetchData(tif, dir, (char*) v)) {
990
TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v);
997
* Fetch an array of DOUBLE values.
1000
TIFFFetchDoubleArray(TIFF* tif, TIFFDirEntry* dir, double* v)
1002
if (TIFFFetchData(tif, dir, (char*) v)) {
1003
TIFFCvtIEEEDoubleToNative(tif, dir->tdir_count, v);
1010
* Fetch an array of ANY values. The actual values are
1011
* returned as doubles which should be able hold all the
1012
* types. Yes, there really should be an tany_t to avoid
1013
* this potential non-portability ... Note in particular
1014
* that we assume that the double return value vector is
1015
* large enough to read in any fundamental type. We use
1016
* that vector as a buffer to read in the base type vector
1017
* and then convert it in place to double (from end
1018
* to front of course).
1021
TIFFFetchAnyArray(TIFF* tif, TIFFDirEntry* dir, double* v)
1025
switch (dir->tdir_type) {
1028
if (!TIFFFetchByteArray(tif, dir, (uint16*) v))
1030
if (dir->tdir_type == TIFF_BYTE) {
1031
uint16* vp = (uint16*) v;
1032
for (i = dir->tdir_count-1; i >= 0; i--)
1035
int16* vp = (int16*) v;
1036
for (i = dir->tdir_count-1; i >= 0; i--)
1042
if (!TIFFFetchShortArray(tif, dir, (uint16*) v))
1044
if (dir->tdir_type == TIFF_SHORT) {
1045
uint16* vp = (uint16*) v;
1046
for (i = dir->tdir_count-1; i >= 0; i--)
1049
int16* vp = (int16*) v;
1050
for (i = dir->tdir_count-1; i >= 0; i--)
1056
if (!TIFFFetchLongArray(tif, dir, (uint32*) v))
1058
if (dir->tdir_type == TIFF_LONG) {
1059
uint32* vp = (uint32*) v;
1060
for (i = dir->tdir_count-1; i >= 0; i--)
1063
int32* vp = (int32*) v;
1064
for (i = dir->tdir_count-1; i >= 0; i--)
1069
case TIFF_SRATIONAL:
1070
if (!TIFFFetchRationalArray(tif, dir, (float*) v))
1072
{ float* vp = (float*) v;
1073
for (i = dir->tdir_count-1; i >= 0; i--)
1078
if (!TIFFFetchFloatArray(tif, dir, (float*) v))
1080
{ float* vp = (float*) v;
1081
for (i = dir->tdir_count-1; i >= 0; i--)
1086
return (TIFFFetchDoubleArray(tif, dir, (double*) v));
1090
/* TIFF_UNDEFINED */
1091
TIFFError(tif->tif_name,
1092
"cannot read TIFF_ANY type %d for field \"%s\"",
1093
_TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
1100
* Fetch a tag that is not handled by special case code.
1103
TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp)
1105
static const char mesg[] = "to fetch tag value";
1107
const TIFFFieldInfo* fip = _TIFFFieldWithTag(tif, dp->tdir_tag);
1109
if (dp->tdir_count > 1) { /* array of values */
1112
switch (dp->tdir_type) {
1115
/* NB: always expand BYTE values to shorts */
1116
cp = CheckMalloc(tif,
1117
dp->tdir_count, sizeof (uint16), mesg);
1118
ok = cp && TIFFFetchByteArray(tif, dp, (uint16*) cp);
1122
cp = CheckMalloc(tif,
1123
dp->tdir_count, sizeof (uint16), mesg);
1124
ok = cp && TIFFFetchShortArray(tif, dp, (uint16*) cp);
1128
cp = CheckMalloc(tif,
1129
dp->tdir_count, sizeof (uint32), mesg);
1130
ok = cp && TIFFFetchLongArray(tif, dp, (uint32*) cp);
1133
case TIFF_SRATIONAL:
1134
cp = CheckMalloc(tif,
1135
dp->tdir_count, sizeof (float), mesg);
1136
ok = cp && TIFFFetchRationalArray(tif, dp, (float*) cp);
1139
cp = CheckMalloc(tif,
1140
dp->tdir_count, sizeof (float), mesg);
1141
ok = cp && TIFFFetchFloatArray(tif, dp, (float*) cp);
1144
cp = CheckMalloc(tif,
1145
dp->tdir_count, sizeof (double), mesg);
1146
ok = cp && TIFFFetchDoubleArray(tif, dp, (double*) cp);
1149
case TIFF_UNDEFINED: /* bit of a cheat... */
1151
* Some vendors write strings w/o the trailing
1152
* NULL byte, so always append one just in case.
1154
cp = CheckMalloc(tif, dp->tdir_count+1, 1, mesg);
1155
if( (ok = (cp && TIFFFetchString(tif, dp, cp))) != 0 )
1156
cp[dp->tdir_count] = '\0'; /* XXX */
1160
ok = (fip->field_passcount ?
1161
TIFFSetField(tif, dp->tdir_tag, dp->tdir_count, cp)
1162
: TIFFSetField(tif, dp->tdir_tag, cp));
1166
} else if (CheckDirCount(tif, dp, 1)) { /* singleton value */
1167
switch (dp->tdir_type) {
1173
* If the tag is also acceptable as a LONG or SLONG
1174
* then TIFFSetField will expect an uint32 parameter
1175
* passed to it (through varargs). Thus, for machines
1176
* where sizeof (int) != sizeof (uint32) we must do
1177
* a careful check here. It's hard to say if this
1178
* is worth optimizing.
1180
* NB: We use TIFFFieldWithTag here knowing that
1181
* it returns us the first entry in the table
1182
* for the tag and that that entry is for the
1183
* widest potential data type the tag may have.
1185
{ TIFFDataType type = fip->field_type;
1186
if (type != TIFF_LONG && type != TIFF_SLONG) {
1188
TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset);
1189
ok = (fip->field_passcount ?
1190
TIFFSetField(tif, dp->tdir_tag, 1, &v)
1191
: TIFFSetField(tif, dp->tdir_tag, v));
1199
TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset);
1200
ok = (fip->field_passcount ?
1201
TIFFSetField(tif, dp->tdir_tag, 1, &v32)
1202
: TIFFSetField(tif, dp->tdir_tag, v32));
1206
case TIFF_SRATIONAL:
1208
{ float v = (dp->tdir_type == TIFF_FLOAT ?
1209
TIFFFetchFloat(tif, dp)
1210
: TIFFFetchRational(tif, dp));
1211
ok = (fip->field_passcount ?
1212
TIFFSetField(tif, dp->tdir_tag, 1, &v)
1213
: TIFFSetField(tif, dp->tdir_tag, v));
1218
ok = (TIFFFetchDoubleArray(tif, dp, &v) &&
1219
(fip->field_passcount ?
1220
TIFFSetField(tif, dp->tdir_tag, 1, &v)
1221
: TIFFSetField(tif, dp->tdir_tag, v))
1226
case TIFF_UNDEFINED: /* bit of a cheat... */
1228
if( (ok = (TIFFFetchString(tif, dp, c) != 0)) != 0 ){
1229
c[1] = '\0'; /* XXX paranoid */
1230
ok = TIFFSetField(tif, dp->tdir_tag, c);
1239
#define NITEMS(x) (sizeof (x) / sizeof (x[0]))
1241
* Fetch samples/pixel short values for
1242
* the specified tag and verify that
1243
* all values are the same.
1246
TIFFFetchPerSampleShorts(TIFF* tif, TIFFDirEntry* dir, int* pl)
1248
int samples = tif->tif_dir.td_samplesperpixel;
1251
if (CheckDirCount(tif, dir, (uint32) samples)) {
1255
if (samples > NITEMS(buf))
1256
v = (uint16*) CheckMalloc(tif, samples, sizeof (uint16),
1257
"to fetch per-sample values");
1258
if (v && TIFFFetchShortArray(tif, dir, v)) {
1260
for (i = 1; i < samples; i++)
1262
TIFFError(tif->tif_name,
1263
"Cannot handle different per-sample values for field \"%s\"",
1264
_TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
1272
_TIFFfree((char*) v);
1278
* Fetch samples/pixel ANY values for
1279
* the specified tag and verify that
1280
* all values are the same.
1283
TIFFFetchPerSampleAnys(TIFF* tif, TIFFDirEntry* dir, double* pl)
1285
int samples = (int) tif->tif_dir.td_samplesperpixel;
1288
if (CheckDirCount(tif, dir, (uint32) samples)) {
1292
if (samples > NITEMS(buf))
1293
v = (double*) CheckMalloc(tif, samples, sizeof (double),
1294
"to fetch per-sample values");
1295
if (v && TIFFFetchAnyArray(tif, dir, v)) {
1297
for (i = 1; i < samples; i++)
1299
TIFFError(tif->tif_name,
1300
"Cannot handle different per-sample values for field \"%s\"",
1301
_TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
1316
* Fetch a set of offsets or lengths.
1317
* While this routine says "strips",
1318
* in fact it's also used for tiles.
1321
TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, long nstrips, uint32** lpp)
1323
register uint32* lp;
1326
CheckDirCount(tif, dir, (uint32) nstrips);
1329
* Allocate space for strip information.
1332
(*lpp = (uint32 *)CheckMalloc(tif,
1333
nstrips, sizeof (uint32), "for strip array")) == NULL)
1336
memset( lp, 0, sizeof(uint32) * nstrips );
1338
if (dir->tdir_type == (int)TIFF_SHORT) {
1340
* Handle uint16->uint32 expansion.
1342
uint16* dp = (uint16*) CheckMalloc(tif,
1343
dir->tdir_count, sizeof (uint16), "to fetch strip tag");
1346
if( (status = TIFFFetchShortArray(tif, dir, dp)) != 0 ) {
1349
for( i = 0; i < nstrips && i < (int) dir->tdir_count; i++ )
1354
_TIFFfree((char*) dp);
1356
} else if( nstrips != (int) dir->tdir_count ) {
1357
/* Special case to correct length */
1359
uint32* dp = (uint32*) CheckMalloc(tif,
1360
dir->tdir_count, sizeof (uint32), "to fetch strip tag");
1364
status = TIFFFetchLongArray(tif, dir, dp);
1368
for( i = 0; i < nstrips && i < (int) dir->tdir_count; i++ )
1374
_TIFFfree( (char *) dp );
1376
status = TIFFFetchLongArray(tif, dir, lp);
1381
#define NITEMS(x) (sizeof (x) / sizeof (x[0]))
1383
* Fetch and set the ExtraSamples tag.
1386
TIFFFetchExtraSamples(TIFF* tif, TIFFDirEntry* dir)
1392
if (dir->tdir_count > NITEMS(buf)) {
1393
v = (uint16*) CheckMalloc(tif, dir->tdir_count, sizeof (uint16),
1394
"to fetch extra samples");
1398
if (dir->tdir_type == TIFF_BYTE)
1399
status = TIFFFetchByteArray(tif, dir, v);
1401
status = TIFFFetchShortArray(tif, dir, v);
1403
status = TIFFSetField(tif, dir->tdir_tag, dir->tdir_count, v);
1405
_TIFFfree((char*) v);
1410
#ifdef COLORIMETRY_SUPPORT
1412
* Fetch and set the RefBlackWhite tag.
1415
TIFFFetchRefBlackWhite(TIFF* tif, TIFFDirEntry* dir)
1417
static const char mesg[] = "for \"ReferenceBlackWhite\" array";
1421
if (dir->tdir_type == TIFF_RATIONAL)
1422
return (TIFFFetchNormalTag(tif, dir));
1424
* Handle LONG's for backward compatibility.
1426
cp = CheckMalloc(tif, dir->tdir_count, sizeof (uint32), mesg);
1427
if( (ok = (cp && TIFFFetchLongArray(tif, dir, (uint32*) cp))) != 0) {
1428
float* fp = (float*)
1429
CheckMalloc(tif, dir->tdir_count, sizeof (float), mesg);
1430
if( (ok = (fp != NULL)) != 0 ) {
1432
for (i = 0; i < dir->tdir_count; i++)
1433
fp[i] = (float)((uint32*) cp)[i];
1434
ok = TIFFSetField(tif, dir->tdir_tag, fp);
1435
_TIFFfree((char*) fp);
1445
* Replace a single strip (tile) of uncompressed data by
1446
* multiple strips (tiles), each approximately 8Kbytes.
1447
* This is useful for dealing with large images or
1448
* for dealing with machines with a limited amount
1452
ChopUpSingleUncompressedStrip(TIFF* tif)
1454
register TIFFDirectory *td = &tif->tif_dir;
1455
uint32 bytecount = td->td_stripbytecount[0];
1456
uint32 offset = td->td_stripoffset[0];
1457
tsize_t rowbytes = TIFFVTileSize(tif, 1), stripbytes;
1458
tstrip_t strip, nstrips, rowsperstrip;
1463
* Make the rows hold at least one
1464
* scanline, but fill 8k if possible.
1466
if (rowbytes > 8192) {
1467
stripbytes = rowbytes;
1469
} else if (rowbytes > 0 ) {
1470
rowsperstrip = 8192 / rowbytes;
1471
stripbytes = rowbytes * rowsperstrip;
1476
/* never increase the number of strips in an image */
1477
if (rowsperstrip >= td->td_rowsperstrip)
1479
nstrips = (tstrip_t) TIFFhowmany(bytecount, stripbytes);
1480
newcounts = (uint32*) CheckMalloc(tif, nstrips, sizeof (uint32),
1481
"for chopped \"StripByteCounts\" array");
1482
newoffsets = (uint32*) CheckMalloc(tif, nstrips, sizeof (uint32),
1483
"for chopped \"StripOffsets\" array");
1484
if (newcounts == NULL || newoffsets == NULL) {
1486
* Unable to allocate new strip information, give
1487
* up and use the original one strip information.
1489
if (newcounts != NULL)
1490
_TIFFfree(newcounts);
1491
if (newoffsets != NULL)
1492
_TIFFfree(newoffsets);
1496
* Fill the strip information arrays with
1497
* new bytecounts and offsets that reflect
1498
* the broken-up format.
1500
for (strip = 0; strip < nstrips; strip++) {
1501
if (stripbytes > (tsize_t) bytecount)
1502
stripbytes = bytecount;
1503
newcounts[strip] = stripbytes;
1504
newoffsets[strip] = offset;
1505
offset += stripbytes;
1506
bytecount -= stripbytes;
1509
* Replace old single strip info with multi-strip info.
1511
td->td_stripsperimage = td->td_nstrips = nstrips;
1512
TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
1514
_TIFFfree(td->td_stripbytecount);
1515
_TIFFfree(td->td_stripoffset);
1516
td->td_stripbytecount = newcounts;
1517
td->td_stripoffset = newoffsets;