1 /* $Id: tif_dirread.c,v 1.92.2.9 2010-06-14 00:21:46 fwarmerdam Exp $ */
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 TIFFDirEntry* TIFFReadDirectoryFind(TIFFDirEntry* dir,
45 uint16 dircount, uint16 tagid);
46 static int EstimateStripByteCounts(TIFF*, TIFFDirEntry*, uint16);
47 static void MissingRequired(TIFF*, const char*);
48 static int TIFFCheckDirOffset(TIFF*, toff_t);
49 static int CheckDirCount(TIFF*, TIFFDirEntry*, uint32);
50 static uint16 TIFFFetchDirectory(TIFF*, toff_t, TIFFDirEntry**, toff_t *);
51 static tsize_t TIFFFetchData(TIFF*, TIFFDirEntry*, char*);
52 static tsize_t TIFFFetchString(TIFF*, TIFFDirEntry*, char*);
53 static float TIFFFetchRational(TIFF*, TIFFDirEntry*);
54 static int TIFFFetchNormalTag(TIFF*, TIFFDirEntry*);
55 static int TIFFFetchPerSampleShorts(TIFF*, TIFFDirEntry*, uint16*);
56 static int TIFFFetchPerSampleLongs(TIFF*, TIFFDirEntry*, uint32*);
57 static int TIFFFetchPerSampleAnys(TIFF*, TIFFDirEntry*, double*);
58 static int TIFFFetchShortArray(TIFF*, TIFFDirEntry*, uint16*);
59 static int TIFFFetchStripThing(TIFF*, TIFFDirEntry*, long, uint32**);
60 static int TIFFFetchRefBlackWhite(TIFF*, TIFFDirEntry*);
61 static int TIFFFetchSubjectDistance(TIFF*, TIFFDirEntry*);
62 static float TIFFFetchFloat(TIFF*, TIFFDirEntry*);
63 static int TIFFFetchFloatArray(TIFF*, TIFFDirEntry*, float*);
64 static int TIFFFetchDoubleArray(TIFF*, TIFFDirEntry*, double*);
65 static int TIFFFetchAnyArray(TIFF*, TIFFDirEntry*, double*);
66 static int TIFFFetchShortPair(TIFF*, TIFFDirEntry*);
67 static void ChopUpSingleUncompressedStrip(TIFF*);
70 * Read the next TIFF directory from a file and convert it to the internal
71 * format. We read directories sequentially.
74 TIFFReadDirectory(TIFF* tif)
76 static const char module[] = "TIFFReadDirectory";
80 TIFFDirEntry *dp, *dir = NULL;
83 const TIFFFieldInfo* fip;
86 int diroutoforderwarning = 0, compressionknown = 0;
87 int haveunknowntags = 0;
89 tif->tif_diroff = tif->tif_nextdiroff;
91 * Check whether we have the last offset or bad offset (IFD looping).
93 if (!TIFFCheckDirOffset(tif, tif->tif_nextdiroff))
96 * Cleanup any previous compression state.
98 (*tif->tif_cleanup)(tif);
100 dircount = TIFFFetchDirectory(tif, tif->tif_nextdiroff,
101 &dir, &tif->tif_nextdiroff);
103 TIFFErrorExt(tif->tif_clientdata, module,
104 "%s: Failed to read directory at offset %u",
105 tif->tif_name, tif->tif_nextdiroff);
109 tif->tif_flags &= ~TIFF_BEENWRITING; /* reset before new dir */
111 * Setup default value and then make a pass over
112 * the fields to check type and tag information,
113 * and to extract info required to size data
114 * structures. A second pass is made afterwards
115 * to read in everthing not taken in the first pass.
118 /* free any old stuff and reinit */
119 TIFFFreeDirectory(tif);
120 TIFFDefaultDirectory(tif);
122 * Electronic Arts writes gray-scale TIFF files
123 * without a PlanarConfiguration directory entry.
124 * Thus we setup a default value here, even though
125 * the TIFF spec says there is no default value.
127 TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
130 * Sigh, we must make a separate pass through the
131 * directory for the following reason:
133 * We must process the Compression tag in the first pass
134 * in order to merge in codec-private tag definitions (otherwise
135 * we may get complaints about unknown tags). However, the
136 * Compression tag may be dependent on the SamplesPerPixel
137 * tag value because older TIFF specs permited Compression
138 * to be written as a SamplesPerPixel-count tag entry.
139 * Thus if we don't first figure out the correct SamplesPerPixel
140 * tag value then we may end up ignoring the Compression tag
141 * value because it has an incorrect count value (if the
142 * true value of SamplesPerPixel is not 1).
144 * It sure would have been nice if Aldus had really thought
145 * this stuff through carefully.
147 for (dp = dir, n = dircount; n > 0; n--, dp++) {
148 if (tif->tif_flags & TIFF_SWAB) {
149 TIFFSwabArrayOfShort(&dp->tdir_tag, 2);
150 TIFFSwabArrayOfLong(&dp->tdir_count, 2);
152 if (dp->tdir_tag == TIFFTAG_SAMPLESPERPIXEL) {
153 if (!TIFFFetchNormalTag(tif, dp))
155 dp->tdir_tag = IGNORE;
159 * First real pass over the directory.
162 for (dp = dir, n = dircount; n > 0; n--, dp++) {
164 if (dp->tdir_tag == IGNORE)
166 if (fix >= tif->tif_nfields)
170 * Silicon Beach (at least) writes unordered
171 * directory tags (violating the spec). Handle
172 * it here, but be obnoxious (maybe they'll fix it?).
174 if (dp->tdir_tag < tif->tif_fieldinfo[fix]->field_tag) {
175 if (!diroutoforderwarning) {
176 TIFFWarningExt(tif->tif_clientdata, module,
177 "%s: invalid TIFF directory; tags are not sorted in ascending order",
179 diroutoforderwarning = 1;
181 fix = 0; /* O(n^2) */
183 while (fix < tif->tif_nfields &&
184 tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
186 if (fix >= tif->tif_nfields ||
187 tif->tif_fieldinfo[fix]->field_tag != dp->tdir_tag) {
188 /* Unknown tag ... we'll deal with it below */
193 * Null out old tags that we ignore.
195 if (tif->tif_fieldinfo[fix]->field_bit == FIELD_IGNORE) {
197 dp->tdir_tag = IGNORE;
203 fip = tif->tif_fieldinfo[fix];
204 while (dp->tdir_type != (unsigned short) fip->field_type
205 && fix < tif->tif_nfields) {
206 if (fip->field_type == TIFF_ANY) /* wildcard */
208 fip = tif->tif_fieldinfo[++fix];
209 if (fix >= tif->tif_nfields ||
210 fip->field_tag != dp->tdir_tag) {
211 TIFFWarningExt(tif->tif_clientdata, module,
212 "%s: wrong data type %d for \"%s\"; tag ignored",
213 tif->tif_name, dp->tdir_type,
214 tif->tif_fieldinfo[fix-1]->field_name);
219 * Check count if known in advance.
221 if (fip->field_readcount != TIFF_VARIABLE
222 && fip->field_readcount != TIFF_VARIABLE2) {
223 uint32 expected = (fip->field_readcount == TIFF_SPP) ?
224 (uint32) td->td_samplesperpixel :
225 (uint32) fip->field_readcount;
226 if (!CheckDirCount(tif, dp, expected))
230 switch (dp->tdir_tag) {
231 case TIFFTAG_COMPRESSION:
233 * The 5.0 spec says the Compression tag has
234 * one value, while earlier specs say it has
235 * one value per sample. Because of this, we
236 * accept the tag if one value is supplied.
238 if (dp->tdir_count == 1) {
239 v = TIFFExtractData(tif,
240 dp->tdir_type, dp->tdir_offset);
241 if (!TIFFSetField(tif, dp->tdir_tag, (uint16)v))
244 compressionknown = 1;
246 /* XXX: workaround for broken TIFFs */
247 } else if (dp->tdir_type == TIFF_LONG) {
248 if (!TIFFFetchPerSampleLongs(tif, dp, &v) ||
249 !TIFFSetField(tif, dp->tdir_tag, (uint16)v))
252 if (!TIFFFetchPerSampleShorts(tif, dp, &iv)
253 || !TIFFSetField(tif, dp->tdir_tag, iv))
256 dp->tdir_tag = IGNORE;
258 case TIFFTAG_STRIPOFFSETS:
259 case TIFFTAG_STRIPBYTECOUNTS:
260 case TIFFTAG_TILEOFFSETS:
261 case TIFFTAG_TILEBYTECOUNTS:
262 TIFFSetFieldBit(tif, fip->field_bit);
264 case TIFFTAG_IMAGEWIDTH:
265 case TIFFTAG_IMAGELENGTH:
266 case TIFFTAG_IMAGEDEPTH:
267 case TIFFTAG_TILELENGTH:
268 case TIFFTAG_TILEWIDTH:
269 case TIFFTAG_TILEDEPTH:
270 case TIFFTAG_PLANARCONFIG:
271 case TIFFTAG_ROWSPERSTRIP:
272 case TIFFTAG_EXTRASAMPLES:
273 if (!TIFFFetchNormalTag(tif, dp))
275 dp->tdir_tag = IGNORE;
281 * If we saw any unknown tags, make an extra pass over the directory
282 * to deal with them. This must be done separately because the tags
283 * could have become known when we registered a codec after finding
284 * the Compression tag. In a correctly-sorted directory there's
285 * no problem because Compression will come before any codec-private
286 * tags, but if the sorting is wrong that might not hold.
288 if (haveunknowntags) {
290 for (dp = dir, n = dircount; n > 0; n--, dp++) {
291 if (dp->tdir_tag == IGNORE)
293 if (fix >= tif->tif_nfields ||
294 dp->tdir_tag < tif->tif_fieldinfo[fix]->field_tag)
295 fix = 0; /* O(n^2) */
296 while (fix < tif->tif_nfields &&
297 tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
299 if (fix >= tif->tif_nfields ||
300 tif->tif_fieldinfo[fix]->field_tag != dp->tdir_tag) {
302 TIFFWarningExt(tif->tif_clientdata,
304 "%s: unknown field with tag %d (0x%x) encountered",
309 if (!_TIFFMergeFieldInfo(tif,
310 _TIFFCreateAnonFieldInfo(tif,
312 (TIFFDataType) dp->tdir_type),
315 TIFFWarningExt(tif->tif_clientdata,
317 "Registering anonymous field with tag %d (0x%x) failed",
320 dp->tdir_tag = IGNORE;
324 while (fix < tif->tif_nfields &&
325 tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
331 fip = tif->tif_fieldinfo[fix];
332 while (dp->tdir_type != (unsigned short) fip->field_type
333 && fix < tif->tif_nfields) {
334 if (fip->field_type == TIFF_ANY) /* wildcard */
336 fip = tif->tif_fieldinfo[++fix];
337 if (fix >= tif->tif_nfields ||
338 fip->field_tag != dp->tdir_tag) {
339 TIFFWarningExt(tif->tif_clientdata, module,
340 "%s: wrong data type %d for \"%s\"; tag ignored",
341 tif->tif_name, dp->tdir_type,
342 tif->tif_fieldinfo[fix-1]->field_name);
343 dp->tdir_tag = IGNORE;
352 * If a) compression is OJPEG, b) planarconfig tag says it's separate,
353 * c) strip offsets/bytecounts tag are both present and
354 * d) both contain exactly one value, then we consistently find
355 * that the buggy implementation of the buggy compression scheme
356 * matches contig planarconfig best. So we 'fix-up' the tag here
358 if ((td->td_compression==COMPRESSION_OJPEG) &&
359 (td->td_planarconfig==PLANARCONFIG_SEPARATE)) {
360 dp = TIFFReadDirectoryFind(dir,dircount,TIFFTAG_STRIPOFFSETS);
361 if ((dp!=0) && (dp->tdir_count==1)) {
362 dp = TIFFReadDirectoryFind(dir, dircount,
363 TIFFTAG_STRIPBYTECOUNTS);
364 if ((dp!=0) && (dp->tdir_count==1)) {
365 td->td_planarconfig=PLANARCONFIG_CONTIG;
366 TIFFWarningExt(tif->tif_clientdata,
368 "Planarconfig tag value assumed incorrect, "
369 "assuming data is contig instead of chunky");
375 * Allocate directory structure and setup defaults.
377 if (!TIFFFieldSet(tif, FIELD_IMAGEDIMENSIONS)) {
378 MissingRequired(tif, "ImageLength");
382 * Setup appropriate structures (by strip or by tile)
384 if (!TIFFFieldSet(tif, FIELD_TILEDIMENSIONS)) {
385 td->td_nstrips = TIFFNumberOfStrips(tif);
386 td->td_tilewidth = td->td_imagewidth;
387 td->td_tilelength = td->td_rowsperstrip;
388 td->td_tiledepth = td->td_imagedepth;
389 tif->tif_flags &= ~TIFF_ISTILED;
391 td->td_nstrips = TIFFNumberOfTiles(tif);
392 tif->tif_flags |= TIFF_ISTILED;
394 if (!td->td_nstrips) {
395 TIFFErrorExt(tif->tif_clientdata, module,
396 "%s: cannot handle zero number of %s",
397 tif->tif_name, isTiled(tif) ? "tiles" : "strips");
400 td->td_stripsperimage = td->td_nstrips;
401 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
402 td->td_stripsperimage /= td->td_samplesperpixel;
403 if (!TIFFFieldSet(tif, FIELD_STRIPOFFSETS)) {
404 if ((td->td_compression==COMPRESSION_OJPEG) &&
406 (td->td_nstrips==1)) {
409 * If a) compression is OJPEG, b) it's not a tiled TIFF,
410 * and c) the number of strips is 1,
411 * then we tolerate the absence of stripoffsets tag,
412 * because, presumably, all required data is in the
413 * JpegInterchangeFormat stream.
415 TIFFSetFieldBit(tif, FIELD_STRIPOFFSETS);
418 isTiled(tif) ? "TileOffsets" : "StripOffsets");
424 * Second pass: extract other information.
426 for (dp = dir, n = dircount; n > 0; n--, dp++) {
427 if (dp->tdir_tag == IGNORE)
429 switch (dp->tdir_tag) {
430 case TIFFTAG_MINSAMPLEVALUE:
431 case TIFFTAG_MAXSAMPLEVALUE:
432 case TIFFTAG_BITSPERSAMPLE:
433 case TIFFTAG_DATATYPE:
434 case TIFFTAG_SAMPLEFORMAT:
436 * The 5.0 spec says the Compression tag has
437 * one value, while earlier specs say it has
438 * one value per sample. Because of this, we
439 * accept the tag if one value is supplied.
441 * The MinSampleValue, MaxSampleValue, BitsPerSample
442 * DataType and SampleFormat tags are supposed to be
443 * written as one value/sample, but some vendors
444 * incorrectly write one value only -- so we accept
445 * that as well (yech). Other vendors write correct
446 * value for NumberOfSamples, but incorrect one for
447 * BitsPerSample and friends, and we will read this
450 if (dp->tdir_count == 1) {
451 v = TIFFExtractData(tif,
452 dp->tdir_type, dp->tdir_offset);
453 if (!TIFFSetField(tif, dp->tdir_tag, (uint16)v))
455 /* XXX: workaround for broken TIFFs */
456 } else if (dp->tdir_tag == TIFFTAG_BITSPERSAMPLE
457 && dp->tdir_type == TIFF_LONG) {
458 if (!TIFFFetchPerSampleLongs(tif, dp, &v) ||
459 !TIFFSetField(tif, dp->tdir_tag, (uint16)v))
462 if (!TIFFFetchPerSampleShorts(tif, dp, &iv) ||
463 !TIFFSetField(tif, dp->tdir_tag, iv))
467 case TIFFTAG_SMINSAMPLEVALUE:
468 case TIFFTAG_SMAXSAMPLEVALUE:
471 if (!TIFFFetchPerSampleAnys(tif, dp, &dv) ||
472 !TIFFSetField(tif, dp->tdir_tag, dv))
476 case TIFFTAG_STRIPOFFSETS:
477 case TIFFTAG_TILEOFFSETS:
478 if (!TIFFFetchStripThing(tif, dp,
479 td->td_nstrips, &td->td_stripoffset))
482 case TIFFTAG_STRIPBYTECOUNTS:
483 case TIFFTAG_TILEBYTECOUNTS:
484 if (!TIFFFetchStripThing(tif, dp,
485 td->td_nstrips, &td->td_stripbytecount))
488 case TIFFTAG_COLORMAP:
489 case TIFFTAG_TRANSFERFUNCTION:
493 * TransferFunction can have either 1x or 3x
494 * data values; Colormap can have only 3x
497 v = 1L<<td->td_bitspersample;
498 if (dp->tdir_tag == TIFFTAG_COLORMAP ||
499 dp->tdir_count != v) {
500 if (!CheckDirCount(tif, dp, 3 * v))
504 cp = (char *)_TIFFCheckMalloc(tif,
507 "to read \"TransferFunction\" tag");
509 if (TIFFFetchData(tif, dp, cp)) {
511 * This deals with there being
512 * only one array to apply to
515 uint32 c = 1L << td->td_bitspersample;
516 if (dp->tdir_count == c)
518 TIFFSetField(tif, dp->tdir_tag,
525 case TIFFTAG_PAGENUMBER:
526 case TIFFTAG_HALFTONEHINTS:
527 case TIFFTAG_YCBCRSUBSAMPLING:
528 case TIFFTAG_DOTRANGE:
529 (void) TIFFFetchShortPair(tif, dp);
531 case TIFFTAG_REFERENCEBLACKWHITE:
532 (void) TIFFFetchRefBlackWhite(tif, dp);
534 /* BEGIN REV 4.0 COMPATIBILITY */
535 case TIFFTAG_OSUBFILETYPE:
537 switch (TIFFExtractData(tif, dp->tdir_type,
539 case OFILETYPE_REDUCEDIMAGE:
540 v = FILETYPE_REDUCEDIMAGE;
547 TIFFSetField(tif, TIFFTAG_SUBFILETYPE, v);
549 /* END REV 4.0 COMPATIBILITY */
551 (void) TIFFFetchNormalTag(tif, dp);
557 * - If a) compression is OJPEG, and b) photometric tag is missing,
558 * then we consistently find that photometric should be YCbCr
559 * - If a) compression is OJPEG, and b) photometric tag says it's RGB,
560 * then we consistently find that the buggy implementation of the
561 * buggy compression scheme matches photometric YCbCr instead.
562 * - If a) compression is OJPEG, and b) bitspersample tag is missing,
563 * then we consistently find bitspersample should be 8.
564 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
565 * and c) photometric is RGB or YCbCr, then we consistently find
566 * samplesperpixel should be 3
567 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,
568 * and c) photometric is MINISWHITE or MINISBLACK, then we consistently
569 * find samplesperpixel should be 3
571 if (td->td_compression==COMPRESSION_OJPEG)
573 if (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
575 TIFFWarningExt(tif->tif_clientdata, "TIFFReadDirectory",
576 "Photometric tag is missing, assuming data is YCbCr");
577 if (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))
580 else if (td->td_photometric==PHOTOMETRIC_RGB)
582 td->td_photometric=PHOTOMETRIC_YCBCR;
583 TIFFWarningExt(tif->tif_clientdata, "TIFFReadDirectory",
584 "Photometric tag value assumed incorrect, "
585 "assuming data is YCbCr instead of RGB");
587 if (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
589 TIFFWarningExt(tif->tif_clientdata,"TIFFReadDirectory",
590 "BitsPerSample tag is missing, assuming 8 bits per sample");
591 if (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))
594 if (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
596 if ((td->td_photometric==PHOTOMETRIC_RGB)
597 || (td->td_photometric==PHOTOMETRIC_YCBCR))
599 TIFFWarningExt(tif->tif_clientdata,
601 "SamplesPerPixel tag is missing, "
602 "assuming correct SamplesPerPixel value is 3");
603 if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))
606 else if ((td->td_photometric==PHOTOMETRIC_MINISWHITE)
607 || (td->td_photometric==PHOTOMETRIC_MINISBLACK))
609 TIFFWarningExt(tif->tif_clientdata,
611 "SamplesPerPixel tag is missing, "
612 "assuming correct SamplesPerPixel value is 1");
613 if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))
619 * Verify Palette image has a Colormap.
621 if (td->td_photometric == PHOTOMETRIC_PALETTE &&
622 !TIFFFieldSet(tif, FIELD_COLORMAP)) {
623 MissingRequired(tif, "Colormap");
628 * We do no further messing with strip/tile offsets/bytecounts in OJPEG
631 if (td->td_compression!=COMPRESSION_OJPEG)
634 * Attempt to deal with a missing StripByteCounts tag.
636 if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {
638 * Some manufacturers violate the spec by not giving
639 * the size of the strips. In this case, assume there
640 * is one uncompressed strip of data.
642 if ((td->td_planarconfig == PLANARCONFIG_CONTIG &&
643 td->td_nstrips > 1) ||
644 (td->td_planarconfig == PLANARCONFIG_SEPARATE &&
645 td->td_nstrips != td->td_samplesperpixel)) {
646 MissingRequired(tif, "StripByteCounts");
649 TIFFWarningExt(tif->tif_clientdata, module,
650 "%s: TIFF directory is missing required "
651 "\"%s\" field, calculating from imagelength",
653 _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
654 if (EstimateStripByteCounts(tif, dir, dircount) < 0)
657 * Assume we have wrong StripByteCount value (in case
658 * of single strip) in following cases:
659 * - it is equal to zero along with StripOffset;
660 * - it is larger than file itself (in case of uncompressed
662 * - it is smaller than the size of the bytes per row
663 * multiplied on the number of rows. The last case should
664 * not be checked in the case of writing new image,
665 * because we may do not know the exact strip size
666 * until the whole image will be written and directory
669 #define BYTECOUNTLOOKSBAD \
670 ( (td->td_stripbytecount[0] == 0 && td->td_stripoffset[0] != 0) || \
671 (td->td_compression == COMPRESSION_NONE && \
672 td->td_stripbytecount[0] > TIFFGetFileSize(tif) - td->td_stripoffset[0]) || \
673 (tif->tif_mode == O_RDONLY && \
674 td->td_compression == COMPRESSION_NONE && \
675 td->td_stripbytecount[0] < TIFFScanlineSize(tif) * td->td_imagelength) )
677 } else if (td->td_nstrips == 1
678 && td->td_stripoffset[0] != 0
679 && BYTECOUNTLOOKSBAD) {
681 * XXX: Plexus (and others) sometimes give a value of
682 * zero for a tag when they don't know what the
683 * correct value is! Try and handle the simple case
684 * of estimating the size of a one strip image.
686 TIFFWarningExt(tif->tif_clientdata, module,
687 "%s: Bogus \"%s\" field, ignoring and calculating from imagelength",
689 _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
690 if(EstimateStripByteCounts(tif, dir, dircount) < 0)
692 } else if (td->td_planarconfig == PLANARCONFIG_CONTIG
693 && td->td_nstrips > 2
694 && td->td_compression == COMPRESSION_NONE
695 && td->td_stripbytecount[0] != td->td_stripbytecount[1]
696 && td->td_stripbytecount[0] != 0
697 && td->td_stripbytecount[1] != 0 ) {
699 * XXX: Some vendors fill StripByteCount array with
700 * absolutely wrong values (it can be equal to
701 * StripOffset array, for example). Catch this case
704 TIFFWarningExt(tif->tif_clientdata, module,
705 "%s: Wrong \"%s\" field, ignoring and calculating from imagelength",
707 _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);
708 if (EstimateStripByteCounts(tif, dir, dircount) < 0)
713 _TIFFfree((char *)dir);
716 if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))
717 td->td_maxsamplevalue = (uint16)((1L<<td->td_bitspersample)-1);
719 * Setup default compression scheme.
723 * XXX: We can optimize checking for the strip bounds using the sorted
724 * bytecounts array. See also comments for TIFFAppendToStrip()
725 * function in tif_write.c.
727 if (td->td_nstrips > 1) {
730 td->td_stripbytecountsorted = 1;
731 for (strip = 1; strip < td->td_nstrips; strip++) {
732 if (td->td_stripoffset[strip - 1] >
733 td->td_stripoffset[strip]) {
734 td->td_stripbytecountsorted = 0;
740 if (!TIFFFieldSet(tif, FIELD_COMPRESSION))
741 TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
743 * Some manufacturers make life difficult by writing
744 * large amounts of uncompressed data as a single strip.
745 * This is contrary to the recommendations of the spec.
746 * The following makes an attempt at breaking such images
747 * into strips closer to the recommended 8k bytes. A
748 * side effect, however, is that the RowsPerStrip tag
749 * value may be changed.
751 if (td->td_nstrips == 1 && td->td_compression == COMPRESSION_NONE &&
752 (tif->tif_flags & (TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP)
753 ChopUpSingleUncompressedStrip(tif);
756 * Reinitialize i/o since we are starting on a new directory.
758 tif->tif_row = (uint32) -1;
759 tif->tif_curstrip = (tstrip_t) -1;
760 tif->tif_col = (uint32) -1;
761 tif->tif_curtile = (ttile_t) -1;
762 tif->tif_tilesize = (tsize_t) -1;
764 tif->tif_scanlinesize = TIFFScanlineSize(tif);
765 if (!tif->tif_scanlinesize) {
766 TIFFErrorExt(tif->tif_clientdata, module,
767 "%s: cannot handle zero scanline size",
773 tif->tif_tilesize = TIFFTileSize(tif);
774 if (!tif->tif_tilesize) {
775 TIFFErrorExt(tif->tif_clientdata, module,
776 "%s: cannot handle zero tile size",
781 if (!TIFFStripSize(tif)) {
782 TIFFErrorExt(tif->tif_clientdata, module,
783 "%s: cannot handle zero strip size",
796 TIFFReadDirectoryFind(TIFFDirEntry* dir, uint16 dircount, uint16 tagid)
800 for (m=dir, n=0; n<dircount; m++, n++)
802 if (m->tdir_tag==tagid)
809 * Read custom directory from the arbitarry offset.
810 * The code is very similar to TIFFReadDirectory().
813 TIFFReadCustomDirectory(TIFF* tif, toff_t diroff,
814 const TIFFFieldInfo info[], size_t n)
816 static const char module[] = "TIFFReadCustomDirectory";
818 TIFFDirectory* td = &tif->tif_dir;
819 TIFFDirEntry *dp, *dir = NULL;
820 const TIFFFieldInfo* fip;
824 _TIFFSetupFieldInfo(tif, info, n);
826 dircount = TIFFFetchDirectory(tif, diroff, &dir, NULL);
828 TIFFErrorExt(tif->tif_clientdata, module,
829 "%s: Failed to read custom directory at offset %u",
830 tif->tif_name, diroff);
834 TIFFFreeDirectory(tif);
835 _TIFFmemset(&tif->tif_dir, 0, sizeof(TIFFDirectory));
838 for (dp = dir, i = dircount; i > 0; i--, dp++) {
839 if (tif->tif_flags & TIFF_SWAB) {
840 TIFFSwabArrayOfShort(&dp->tdir_tag, 2);
841 TIFFSwabArrayOfLong(&dp->tdir_count, 2);
844 if (fix >= tif->tif_nfields || dp->tdir_tag == IGNORE)
847 while (fix < tif->tif_nfields &&
848 tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
851 if (fix >= tif->tif_nfields ||
852 tif->tif_fieldinfo[fix]->field_tag != dp->tdir_tag) {
854 TIFFWarningExt(tif->tif_clientdata, module,
855 "%s: unknown field with tag %d (0x%x) encountered",
856 tif->tif_name, dp->tdir_tag, dp->tdir_tag);
857 if (!_TIFFMergeFieldInfo(tif,
858 _TIFFCreateAnonFieldInfo(tif,
860 (TIFFDataType) dp->tdir_type),
863 TIFFWarningExt(tif->tif_clientdata, module,
864 "Registering anonymous field with tag %d (0x%x) failed",
865 dp->tdir_tag, dp->tdir_tag);
870 while (fix < tif->tif_nfields &&
871 tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)
875 * Null out old tags that we ignore.
877 if (tif->tif_fieldinfo[fix]->field_bit == FIELD_IGNORE) {
879 dp->tdir_tag = IGNORE;
885 fip = tif->tif_fieldinfo[fix];
886 while (dp->tdir_type != (unsigned short) fip->field_type
887 && fix < tif->tif_nfields) {
888 if (fip->field_type == TIFF_ANY) /* wildcard */
890 fip = tif->tif_fieldinfo[++fix];
891 if (fix >= tif->tif_nfields ||
892 fip->field_tag != dp->tdir_tag) {
893 TIFFWarningExt(tif->tif_clientdata, module,
894 "%s: wrong data type %d for \"%s\"; tag ignored",
895 tif->tif_name, dp->tdir_type,
896 tif->tif_fieldinfo[fix-1]->field_name);
901 * Check count if known in advance.
903 if (fip->field_readcount != TIFF_VARIABLE
904 && fip->field_readcount != TIFF_VARIABLE2) {
905 uint32 expected = (fip->field_readcount == TIFF_SPP) ?
906 (uint32) td->td_samplesperpixel :
907 (uint32) fip->field_readcount;
908 if (!CheckDirCount(tif, dp, expected))
913 * EXIF tags which need to be specifically processed.
915 switch (dp->tdir_tag) {
916 case EXIFTAG_SUBJECTDISTANCE:
917 (void) TIFFFetchSubjectDistance(tif, dp);
920 (void) TIFFFetchNormalTag(tif, dp);
931 * EXIF is important special case of custom IFD, so we have a special
932 * function to read it.
935 TIFFReadEXIFDirectory(TIFF* tif, toff_t diroff)
937 size_t exifFieldInfoCount;
938 const TIFFFieldInfo *exifFieldInfo =
939 _TIFFGetExifFieldInfo(&exifFieldInfoCount);
940 return TIFFReadCustomDirectory(tif, diroff, exifFieldInfo,
945 EstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount)
947 static const char module[] = "EstimateStripByteCounts";
950 TIFFDirectory *td = &tif->tif_dir;
953 if (td->td_stripbytecount)
954 _TIFFfree(td->td_stripbytecount);
955 td->td_stripbytecount = (uint32*)
956 _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint32),
957 "for \"StripByteCounts\" array");
958 if( td->td_stripbytecount == NULL )
961 if (td->td_compression != COMPRESSION_NONE) {
962 uint32 space = (uint32)(sizeof (TIFFHeader)
964 + (dircount * sizeof (TIFFDirEntry))
966 toff_t filesize = TIFFGetFileSize(tif);
969 /* calculate amount of space used by indirect values */
970 for (dp = dir, n = dircount; n > 0; n--, dp++)
972 uint32 cc = TIFFDataWidth((TIFFDataType) dp->tdir_type);
974 TIFFErrorExt(tif->tif_clientdata, module,
975 "%s: Cannot determine size of unknown tag type %d",
976 tif->tif_name, dp->tdir_type);
979 cc = cc * dp->tdir_count;
980 if (cc > sizeof (uint32))
983 space = filesize - space;
984 if (td->td_planarconfig == PLANARCONFIG_SEPARATE)
985 space /= td->td_samplesperpixel;
986 for (strip = 0; strip < td->td_nstrips; strip++)
987 td->td_stripbytecount[strip] = space;
989 * This gross hack handles the case were the offset to
990 * the last strip is past the place where we think the strip
991 * should begin. Since a strip of data must be contiguous,
992 * it's safe to assume that we've overestimated the amount
993 * of data in the strip and trim this number back accordingly.
996 if (((toff_t)(td->td_stripoffset[strip]+
997 td->td_stripbytecount[strip])) > filesize)
998 td->td_stripbytecount[strip] =
999 filesize - td->td_stripoffset[strip];
1000 } else if (isTiled(tif)) {
1001 uint32 bytespertile = TIFFTileSize(tif);
1003 for (strip = 0; strip < td->td_nstrips; strip++)
1004 td->td_stripbytecount[strip] = bytespertile;
1006 uint32 rowbytes = TIFFScanlineSize(tif);
1007 uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;
1008 for (strip = 0; strip < td->td_nstrips; strip++)
1009 td->td_stripbytecount[strip] = rowbytes * rowsperstrip;
1011 TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);
1012 if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))
1013 td->td_rowsperstrip = td->td_imagelength;
1018 MissingRequired(TIFF* tif, const char* tagname)
1020 static const char module[] = "MissingRequired";
1022 TIFFErrorExt(tif->tif_clientdata, module,
1023 "%s: TIFF directory is missing required \"%s\" field",
1024 tif->tif_name, tagname);
1028 * Check the directory offset against the list of already seen directory
1029 * offsets. This is a trick to prevent IFD looping. The one can create TIFF
1030 * file with looped directory pointers. We will maintain a list of already
1031 * seen directories and check every IFD offset against that list.
1034 TIFFCheckDirOffset(TIFF* tif, toff_t diroff)
1038 if (diroff == 0) /* no more directories */
1041 for (n = 0; n < tif->tif_dirnumber && tif->tif_dirlist; n++) {
1042 if (tif->tif_dirlist[n] == diroff)
1046 tif->tif_dirnumber++;
1048 if (tif->tif_dirnumber > tif->tif_dirlistsize) {
1049 toff_t* new_dirlist;
1052 * XXX: Reduce memory allocation granularity of the dirlist
1055 new_dirlist = (toff_t *)_TIFFCheckRealloc(tif,
1062 tif->tif_dirlistsize = 2 * tif->tif_dirnumber;
1063 tif->tif_dirlist = new_dirlist;
1066 tif->tif_dirlist[tif->tif_dirnumber - 1] = diroff;
1072 * Check the count field of a directory entry against a known value. The
1073 * caller is expected to skip/ignore the tag if there is a mismatch.
1076 CheckDirCount(TIFF* tif, TIFFDirEntry* dir, uint32 count)
1078 if (count > dir->tdir_count) {
1079 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
1080 "incorrect count for field \"%s\" (%u, expecting %u); tag ignored",
1081 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
1082 dir->tdir_count, count);
1084 } else if (count < dir->tdir_count) {
1085 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
1086 "incorrect count for field \"%s\" (%u, expecting %u); tag trimmed",
1087 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
1088 dir->tdir_count, count);
1095 * Read IFD structure from the specified offset. If the pointer to
1096 * nextdiroff variable has been specified, read it too. Function returns a
1097 * number of fields in the directory or 0 if failed.
1100 TIFFFetchDirectory(TIFF* tif, toff_t diroff, TIFFDirEntry **pdir,
1103 static const char module[] = "TIFFFetchDirectory";
1110 tif->tif_diroff = diroff;
1113 if (!isMapped(tif)) {
1114 if (!SeekOK(tif, tif->tif_diroff)) {
1115 TIFFErrorExt(tif->tif_clientdata, module,
1116 "%s: Seek error accessing TIFF directory",
1120 if (!ReadOK(tif, &dircount, sizeof (uint16))) {
1121 TIFFErrorExt(tif->tif_clientdata, module,
1122 "%s: Can not read TIFF directory count",
1126 if (tif->tif_flags & TIFF_SWAB)
1127 TIFFSwabShort(&dircount);
1128 dir = (TIFFDirEntry *)_TIFFCheckMalloc(tif, dircount,
1129 sizeof (TIFFDirEntry),
1130 "to read TIFF directory");
1133 if (!ReadOK(tif, dir, dircount*sizeof (TIFFDirEntry))) {
1134 TIFFErrorExt(tif->tif_clientdata, module,
1135 "%.100s: Can not read TIFF directory",
1141 * Read offset to next directory for sequential scans if
1145 (void) ReadOK(tif, nextdiroff, sizeof(uint32));
1147 toff_t off = tif->tif_diroff;
1150 * Check for integer overflow when validating the dir_off,
1151 * otherwise a very high offset may cause an OOB read and
1152 * crash the client. Make two comparisons instead of
1154 * off + sizeof(uint16) > tif->tif_size
1156 * to avoid overflow.
1158 if (tif->tif_size < sizeof (uint16) ||
1159 off > tif->tif_size - sizeof(uint16)) {
1160 TIFFErrorExt(tif->tif_clientdata, module,
1161 "%s: Can not read TIFF directory count",
1165 _TIFFmemcpy(&dircount, tif->tif_base + off,
1168 off += sizeof (uint16);
1169 if (tif->tif_flags & TIFF_SWAB)
1170 TIFFSwabShort(&dircount);
1171 dir = (TIFFDirEntry *)_TIFFCheckMalloc(tif, dircount,
1172 sizeof(TIFFDirEntry),
1173 "to read TIFF directory");
1176 if (off + dircount * sizeof (TIFFDirEntry) > tif->tif_size) {
1177 TIFFErrorExt(tif->tif_clientdata, module,
1178 "%s: Can not read TIFF directory",
1183 _TIFFmemcpy(dir, tif->tif_base + off,
1184 dircount * sizeof(TIFFDirEntry));
1187 off += dircount * sizeof (TIFFDirEntry);
1188 if (off + sizeof (uint32) <= tif->tif_size) {
1189 _TIFFmemcpy(nextdiroff, tif->tif_base + off,
1194 if (nextdiroff && tif->tif_flags & TIFF_SWAB)
1195 TIFFSwabLong(nextdiroff);
1201 * Fetch a contiguous directory item.
1204 TIFFFetchData(TIFF* tif, TIFFDirEntry* dir, char* cp)
1206 uint32 w = TIFFDataWidth((TIFFDataType) dir->tdir_type);
1208 * FIXME: butecount should have tsize_t type, but for now libtiff
1209 * defines tsize_t as a signed 32-bit integer and we are losing
1210 * ability to read arrays larger than 2^31 bytes. So we are using
1211 * uint32 instead of tsize_t here.
1213 uint32 cc = dir->tdir_count * w;
1215 /* Check for overflow. */
1216 if (!dir->tdir_count || !w || cc / w != dir->tdir_count)
1219 if (!isMapped(tif)) {
1220 if (!SeekOK(tif, dir->tdir_offset))
1222 if (!ReadOK(tif, cp, cc))
1225 /* Check for overflow. */
1226 if (dir->tdir_offset + cc < dir->tdir_offset
1227 || dir->tdir_offset + cc < cc
1228 || dir->tdir_offset + cc > tif->tif_size)
1230 _TIFFmemcpy(cp, tif->tif_base + dir->tdir_offset, cc);
1232 if (tif->tif_flags & TIFF_SWAB) {
1233 switch (dir->tdir_type) {
1236 TIFFSwabArrayOfShort((uint16*) cp, dir->tdir_count);
1241 TIFFSwabArrayOfLong((uint32*) cp, dir->tdir_count);
1244 case TIFF_SRATIONAL:
1245 TIFFSwabArrayOfLong((uint32*) cp, 2*dir->tdir_count);
1248 TIFFSwabArrayOfDouble((double*) cp, dir->tdir_count);
1254 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
1255 "Error fetching data for field \"%s\"",
1256 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
1261 * Fetch an ASCII item from the file.
1264 TIFFFetchString(TIFF* tif, TIFFDirEntry* dir, char* cp)
1266 if (dir->tdir_count <= 4) {
1267 uint32 l = dir->tdir_offset;
1268 if (tif->tif_flags & TIFF_SWAB)
1270 _TIFFmemcpy(cp, &l, dir->tdir_count);
1273 return (TIFFFetchData(tif, dir, cp));
1277 * Convert numerator+denominator to float.
1280 cvtRational(TIFF* tif, TIFFDirEntry* dir, uint32 num, uint32 denom, float* rv)
1283 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
1284 "%s: Rational with zero denominator (num = %u)",
1285 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name, num);
1288 if (dir->tdir_type == TIFF_RATIONAL)
1289 *rv = ((float)num / (float)denom);
1291 *rv = ((float)(int32)num / (float)(int32)denom);
1297 * Fetch a rational item from the file at offset off and return the value as a
1298 * floating point number.
1301 TIFFFetchRational(TIFF* tif, TIFFDirEntry* dir)
1306 return (!TIFFFetchData(tif, dir, (char *)l) ||
1307 !cvtRational(tif, dir, l[0], l[1], &v) ? 1.0f : v);
1311 * Fetch a single floating point value from the offset field and return it as
1315 TIFFFetchFloat(TIFF* tif, TIFFDirEntry* dir)
1318 int32 l = TIFFExtractData(tif, dir->tdir_type, dir->tdir_offset);
1319 _TIFFmemcpy(&v, &l, sizeof(float));
1320 TIFFCvtIEEEFloatToNative(tif, 1, &v);
1325 * Fetch an array of BYTE or SBYTE values.
1328 TIFFFetchByteArray(TIFF* tif, TIFFDirEntry* dir, uint8* v)
1330 if (dir->tdir_count <= 4) {
1332 * Extract data from offset field.
1334 if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
1335 if (dir->tdir_type == TIFF_SBYTE)
1336 switch (dir->tdir_count) {
1337 case 4: v[3] = dir->tdir_offset & 0xff;
1338 case 3: v[2] = (dir->tdir_offset >> 8) & 0xff;
1339 case 2: v[1] = (dir->tdir_offset >> 16) & 0xff;
1340 case 1: v[0] = dir->tdir_offset >> 24;
1343 switch (dir->tdir_count) {
1344 case 4: v[3] = dir->tdir_offset & 0xff;
1345 case 3: v[2] = (dir->tdir_offset >> 8) & 0xff;
1346 case 2: v[1] = (dir->tdir_offset >> 16) & 0xff;
1347 case 1: v[0] = dir->tdir_offset >> 24;
1350 if (dir->tdir_type == TIFF_SBYTE)
1351 switch (dir->tdir_count) {
1352 case 4: v[3] = dir->tdir_offset >> 24;
1353 case 3: v[2] = (dir->tdir_offset >> 16) & 0xff;
1354 case 2: v[1] = (dir->tdir_offset >> 8) & 0xff;
1355 case 1: v[0] = dir->tdir_offset & 0xff;
1358 switch (dir->tdir_count) {
1359 case 4: v[3] = dir->tdir_offset >> 24;
1360 case 3: v[2] = (dir->tdir_offset >> 16) & 0xff;
1361 case 2: v[1] = (dir->tdir_offset >> 8) & 0xff;
1362 case 1: v[0] = dir->tdir_offset & 0xff;
1367 return (TIFFFetchData(tif, dir, (char*) v) != 0); /* XXX */
1371 * Fetch an array of SHORT or SSHORT values.
1374 TIFFFetchShortArray(TIFF* tif, TIFFDirEntry* dir, uint16* v)
1376 if (dir->tdir_count <= 2) {
1377 if (tif->tif_header.tiff_magic == TIFF_BIGENDIAN) {
1378 switch (dir->tdir_count) {
1379 case 2: v[1] = (uint16) (dir->tdir_offset & 0xffff);
1380 case 1: v[0] = (uint16) (dir->tdir_offset >> 16);
1383 switch (dir->tdir_count) {
1384 case 2: v[1] = (uint16) (dir->tdir_offset >> 16);
1385 case 1: v[0] = (uint16) (dir->tdir_offset & 0xffff);
1390 return (TIFFFetchData(tif, dir, (char *)v) != 0);
1394 * Fetch a pair of SHORT or BYTE values. Some tags may have either BYTE
1395 * or SHORT type and this function works with both ones.
1398 TIFFFetchShortPair(TIFF* tif, TIFFDirEntry* dir)
1401 * Prevent overflowing the v stack arrays below by performing a sanity
1402 * check on tdir_count, this should never be greater than two.
1404 if (dir->tdir_count > 2) {
1405 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
1406 "unexpected count for field \"%s\", %u, expected 2; ignored",
1407 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name,
1412 switch (dir->tdir_type) {
1417 return TIFFFetchByteArray(tif, dir, v)
1418 && TIFFSetField(tif, dir->tdir_tag, v[0], v[1]);
1424 return TIFFFetchShortArray(tif, dir, v)
1425 && TIFFSetField(tif, dir->tdir_tag, v[0], v[1]);
1433 * Fetch an array of LONG or SLONG values.
1436 TIFFFetchLongArray(TIFF* tif, TIFFDirEntry* dir, uint32* v)
1438 if (dir->tdir_count == 1) {
1439 v[0] = dir->tdir_offset;
1442 return (TIFFFetchData(tif, dir, (char*) v) != 0);
1446 * Fetch an array of RATIONAL or SRATIONAL values.
1449 TIFFFetchRationalArray(TIFF* tif, TIFFDirEntry* dir, float* v)
1454 l = (uint32*)_TIFFCheckMalloc(tif,
1455 dir->tdir_count, TIFFDataWidth((TIFFDataType) dir->tdir_type),
1456 "to fetch array of rationals");
1458 if (TIFFFetchData(tif, dir, (char *)l)) {
1460 for (i = 0; i < dir->tdir_count; i++) {
1461 ok = cvtRational(tif, dir,
1462 l[2*i+0], l[2*i+1], &v[i]);
1467 _TIFFfree((char *)l);
1473 * Fetch an array of FLOAT values.
1476 TIFFFetchFloatArray(TIFF* tif, TIFFDirEntry* dir, float* v)
1479 if (dir->tdir_count == 1) {
1486 float_union.i=dir->tdir_offset;
1488 TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v);
1490 } else if (TIFFFetchData(tif, dir, (char*) v)) {
1491 TIFFCvtIEEEFloatToNative(tif, dir->tdir_count, v);
1498 * Fetch an array of DOUBLE values.
1501 TIFFFetchDoubleArray(TIFF* tif, TIFFDirEntry* dir, double* v)
1503 if (TIFFFetchData(tif, dir, (char*) v)) {
1504 TIFFCvtIEEEDoubleToNative(tif, dir->tdir_count, v);
1511 * Fetch an array of ANY values. The actual values are returned as doubles
1512 * which should be able hold all the types. Yes, there really should be an
1513 * tany_t to avoid this potential non-portability ... Note in particular that
1514 * we assume that the double return value vector is large enough to read in
1515 * any fundamental type. We use that vector as a buffer to read in the base
1516 * type vector and then convert it in place to double (from end to front of
1520 TIFFFetchAnyArray(TIFF* tif, TIFFDirEntry* dir, double* v)
1524 switch (dir->tdir_type) {
1527 if (!TIFFFetchByteArray(tif, dir, (uint8*) v))
1529 if (dir->tdir_type == TIFF_BYTE) {
1530 uint8* vp = (uint8*) v;
1531 for (i = dir->tdir_count-1; i >= 0; i--)
1534 int8* vp = (int8*) v;
1535 for (i = dir->tdir_count-1; i >= 0; i--)
1541 if (!TIFFFetchShortArray(tif, dir, (uint16*) v))
1543 if (dir->tdir_type == TIFF_SHORT) {
1544 uint16* vp = (uint16*) v;
1545 for (i = dir->tdir_count-1; i >= 0; i--)
1548 int16* vp = (int16*) v;
1549 for (i = dir->tdir_count-1; i >= 0; i--)
1555 if (!TIFFFetchLongArray(tif, dir, (uint32*) v))
1557 if (dir->tdir_type == TIFF_LONG) {
1558 uint32* vp = (uint32*) v;
1559 for (i = dir->tdir_count-1; i >= 0; i--)
1562 int32* vp = (int32*) v;
1563 for (i = dir->tdir_count-1; i >= 0; i--)
1568 case TIFF_SRATIONAL:
1569 if (!TIFFFetchRationalArray(tif, dir, (float*) v))
1571 { float* vp = (float*) v;
1572 for (i = dir->tdir_count-1; i >= 0; i--)
1577 if (!TIFFFetchFloatArray(tif, dir, (float*) v))
1579 { float* vp = (float*) v;
1580 for (i = dir->tdir_count-1; i >= 0; i--)
1585 return (TIFFFetchDoubleArray(tif, dir, (double*) v));
1589 /* TIFF_UNDEFINED */
1590 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
1591 "cannot read TIFF_ANY type %d for field \"%s\"",
1593 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
1600 * Fetch a tag that is not handled by special case code.
1603 TIFFFetchNormalTag(TIFF* tif, TIFFDirEntry* dp)
1605 static const char mesg[] = "to fetch tag value";
1607 const TIFFFieldInfo* fip = _TIFFFieldWithTag(tif, dp->tdir_tag);
1609 if (dp->tdir_count > 1) { /* array of values */
1612 switch (dp->tdir_type) {
1615 cp = (char *)_TIFFCheckMalloc(tif,
1616 dp->tdir_count, sizeof (uint8), mesg);
1617 ok = cp && TIFFFetchByteArray(tif, dp, (uint8*) cp);
1621 cp = (char *)_TIFFCheckMalloc(tif,
1622 dp->tdir_count, sizeof (uint16), mesg);
1623 ok = cp && TIFFFetchShortArray(tif, dp, (uint16*) cp);
1627 cp = (char *)_TIFFCheckMalloc(tif,
1628 dp->tdir_count, sizeof (uint32), mesg);
1629 ok = cp && TIFFFetchLongArray(tif, dp, (uint32*) cp);
1632 case TIFF_SRATIONAL:
1633 cp = (char *)_TIFFCheckMalloc(tif,
1634 dp->tdir_count, sizeof (float), mesg);
1635 ok = cp && TIFFFetchRationalArray(tif, dp, (float*) cp);
1638 cp = (char *)_TIFFCheckMalloc(tif,
1639 dp->tdir_count, sizeof (float), mesg);
1640 ok = cp && TIFFFetchFloatArray(tif, dp, (float*) cp);
1643 cp = (char *)_TIFFCheckMalloc(tif,
1644 dp->tdir_count, sizeof (double), mesg);
1645 ok = cp && TIFFFetchDoubleArray(tif, dp, (double*) cp);
1648 case TIFF_UNDEFINED: /* bit of a cheat... */
1650 * Some vendors write strings w/o the trailing
1651 * NULL byte, so always append one just in case.
1653 cp = (char *)_TIFFCheckMalloc(tif, dp->tdir_count + 1,
1655 if( (ok = (cp && TIFFFetchString(tif, dp, cp))) != 0 )
1656 cp[dp->tdir_count] = '\0'; /* XXX */
1660 ok = (fip->field_passcount ?
1661 TIFFSetField(tif, dp->tdir_tag, dp->tdir_count, cp)
1662 : TIFFSetField(tif, dp->tdir_tag, cp));
1666 } else if (CheckDirCount(tif, dp, 1)) { /* singleton value */
1667 switch (dp->tdir_type) {
1673 * If the tag is also acceptable as a LONG or SLONG
1674 * then TIFFSetField will expect an uint32 parameter
1675 * passed to it (through varargs). Thus, for machines
1676 * where sizeof (int) != sizeof (uint32) we must do
1677 * a careful check here. It's hard to say if this
1678 * is worth optimizing.
1680 * NB: We use TIFFFieldWithTag here knowing that
1681 * it returns us the first entry in the table
1682 * for the tag and that that entry is for the
1683 * widest potential data type the tag may have.
1685 { TIFFDataType type = fip->field_type;
1686 if (type != TIFF_LONG && type != TIFF_SLONG) {
1688 TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset);
1689 ok = (fip->field_passcount ?
1690 TIFFSetField(tif, dp->tdir_tag, 1, &v)
1691 : TIFFSetField(tif, dp->tdir_tag, v));
1699 TIFFExtractData(tif, dp->tdir_type, dp->tdir_offset);
1700 ok = (fip->field_passcount ?
1701 TIFFSetField(tif, dp->tdir_tag, 1, &v32)
1702 : TIFFSetField(tif, dp->tdir_tag, v32));
1706 case TIFF_SRATIONAL:
1708 { float v = (dp->tdir_type == TIFF_FLOAT ?
1709 TIFFFetchFloat(tif, dp)
1710 : TIFFFetchRational(tif, dp));
1711 ok = (fip->field_passcount ?
1712 TIFFSetField(tif, dp->tdir_tag, 1, &v)
1713 : TIFFSetField(tif, dp->tdir_tag, v));
1718 ok = (TIFFFetchDoubleArray(tif, dp, &v) &&
1719 (fip->field_passcount ?
1720 TIFFSetField(tif, dp->tdir_tag, 1, &v)
1721 : TIFFSetField(tif, dp->tdir_tag, v))
1726 case TIFF_UNDEFINED: /* bit of a cheat... */
1728 if( (ok = (TIFFFetchString(tif, dp, c) != 0)) != 0 ) {
1729 c[1] = '\0'; /* XXX paranoid */
1730 ok = (fip->field_passcount ?
1731 TIFFSetField(tif, dp->tdir_tag, 1, c)
1732 : TIFFSetField(tif, dp->tdir_tag, c));
1741 #define NITEMS(x) (sizeof (x) / sizeof (x[0]))
1743 * Fetch samples/pixel short values for
1744 * the specified tag and verify that
1745 * all values are the same.
1748 TIFFFetchPerSampleShorts(TIFF* tif, TIFFDirEntry* dir, uint16* pl)
1750 uint16 samples = tif->tif_dir.td_samplesperpixel;
1753 if (CheckDirCount(tif, dir, (uint32) samples)) {
1757 if (dir->tdir_count > NITEMS(buf))
1758 v = (uint16*) _TIFFCheckMalloc(tif, dir->tdir_count, sizeof(uint16),
1759 "to fetch per-sample values");
1760 if (v && TIFFFetchShortArray(tif, dir, v)) {
1762 int check_count = dir->tdir_count;
1763 if( samples < check_count )
1764 check_count = samples;
1766 for (i = 1; i < check_count; i++)
1768 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
1769 "Cannot handle different per-sample values for field \"%s\"",
1770 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
1784 * Fetch samples/pixel long values for
1785 * the specified tag and verify that
1786 * all values are the same.
1789 TIFFFetchPerSampleLongs(TIFF* tif, TIFFDirEntry* dir, uint32* pl)
1791 uint16 samples = tif->tif_dir.td_samplesperpixel;
1794 if (CheckDirCount(tif, dir, (uint32) samples)) {
1798 if (dir->tdir_count > NITEMS(buf))
1799 v = (uint32*) _TIFFCheckMalloc(tif, dir->tdir_count, sizeof(uint32),
1800 "to fetch per-sample values");
1801 if (v && TIFFFetchLongArray(tif, dir, v)) {
1803 int check_count = dir->tdir_count;
1805 if( samples < check_count )
1806 check_count = samples;
1807 for (i = 1; i < check_count; i++)
1809 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
1810 "Cannot handle different per-sample values for field \"%s\"",
1811 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
1825 * Fetch samples/pixel ANY values for the specified tag and verify that all
1826 * values are the same.
1829 TIFFFetchPerSampleAnys(TIFF* tif, TIFFDirEntry* dir, double* pl)
1831 uint16 samples = tif->tif_dir.td_samplesperpixel;
1834 if (CheckDirCount(tif, dir, (uint32) samples)) {
1838 if (dir->tdir_count > NITEMS(buf))
1839 v = (double*) _TIFFCheckMalloc(tif, dir->tdir_count, sizeof (double),
1840 "to fetch per-sample values");
1841 if (v && TIFFFetchAnyArray(tif, dir, v)) {
1843 int check_count = dir->tdir_count;
1844 if( samples < check_count )
1845 check_count = samples;
1847 for (i = 1; i < check_count; i++)
1849 TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
1850 "Cannot handle different per-sample values for field \"%s\"",
1851 _TIFFFieldWithTag(tif, dir->tdir_tag)->field_name);
1866 * Fetch a set of offsets or lengths.
1867 * While this routine says "strips", in fact it's also used for tiles.
1870 TIFFFetchStripThing(TIFF* tif, TIFFDirEntry* dir, long nstrips, uint32** lpp)
1872 register uint32* lp;
1875 CheckDirCount(tif, dir, (uint32) nstrips);
1878 * Allocate space for strip information.
1881 (*lpp = (uint32 *)_TIFFCheckMalloc(tif,
1882 nstrips, sizeof (uint32), "for strip array")) == NULL)
1885 _TIFFmemset( lp, 0, sizeof(uint32) * nstrips );
1887 if (dir->tdir_type == (int)TIFF_SHORT) {
1889 * Handle uint16->uint32 expansion.
1891 uint16* dp = (uint16*) _TIFFCheckMalloc(tif,
1892 dir->tdir_count, sizeof (uint16), "to fetch strip tag");
1895 if( (status = TIFFFetchShortArray(tif, dir, dp)) != 0 ) {
1898 for( i = 0; i < nstrips && i < (int) dir->tdir_count; i++ )
1903 _TIFFfree((char*) dp);
1905 } else if( nstrips != (int) dir->tdir_count ) {
1906 /* Special case to correct length */
1908 uint32* dp = (uint32*) _TIFFCheckMalloc(tif,
1909 dir->tdir_count, sizeof (uint32), "to fetch strip tag");
1913 status = TIFFFetchLongArray(tif, dir, dp);
1917 for( i = 0; i < nstrips && i < (int) dir->tdir_count; i++ )
1923 _TIFFfree( (char *) dp );
1925 status = TIFFFetchLongArray(tif, dir, lp);
1931 * Fetch and set the RefBlackWhite tag.
1934 TIFFFetchRefBlackWhite(TIFF* tif, TIFFDirEntry* dir)
1936 static const char mesg[] = "for \"ReferenceBlackWhite\" array";
1940 if (dir->tdir_type == TIFF_RATIONAL)
1941 return (TIFFFetchNormalTag(tif, dir));
1943 * Handle LONG's for backward compatibility.
1945 cp = (char *)_TIFFCheckMalloc(tif, dir->tdir_count,
1946 sizeof (uint32), mesg);
1947 if( (ok = (cp && TIFFFetchLongArray(tif, dir, (uint32*) cp))) != 0) {
1948 float* fp = (float*)
1949 _TIFFCheckMalloc(tif, dir->tdir_count, sizeof (float), mesg);
1950 if( (ok = (fp != NULL)) != 0 ) {
1952 for (i = 0; i < dir->tdir_count; i++)
1953 fp[i] = (float)((uint32*) cp)[i];
1954 ok = TIFFSetField(tif, dir->tdir_tag, fp);
1955 _TIFFfree((char*) fp);
1964 * Fetch and set the SubjectDistance EXIF tag.
1967 TIFFFetchSubjectDistance(TIFF* tif, TIFFDirEntry* dir)
1973 if( dir->tdir_count != 1 || dir->tdir_type != TIFF_RATIONAL )
1975 TIFFWarningExt(tif->tif_clientdata, tif->tif_name,
1976 "incorrect count or type for SubjectDistance, tag ignored" );
1980 if (TIFFFetchData(tif, dir, (char *)l)
1981 && cvtRational(tif, dir, l[0], l[1], &v)) {
1983 * XXX: Numerator 0xFFFFFFFF means that we have infinite
1984 * distance. Indicate that with a negative floating point
1985 * SubjectDistance value.
1987 ok = TIFFSetField(tif, dir->tdir_tag,
1988 (l[0] != 0xFFFFFFFF) ? v : -v);
1995 * Replace a single strip (tile) of uncompressed data by multiple strips
1996 * (tiles), each approximately STRIP_SIZE_DEFAULT bytes. This is useful for
1997 * dealing with large images or for dealing with machines with a limited
2001 ChopUpSingleUncompressedStrip(TIFF* tif)
2003 register TIFFDirectory *td = &tif->tif_dir;
2004 uint32 bytecount = td->td_stripbytecount[0];
2005 uint32 offset = td->td_stripoffset[0];
2006 tsize_t rowbytes = TIFFVTileSize(tif, 1), stripbytes;
2007 tstrip_t strip, nstrips, rowsperstrip;
2012 * Make the rows hold at least one scanline, but fill specified amount
2013 * of data if possible.
2015 if (rowbytes > STRIP_SIZE_DEFAULT) {
2016 stripbytes = rowbytes;
2018 } else if (rowbytes > 0 ) {
2019 rowsperstrip = STRIP_SIZE_DEFAULT / rowbytes;
2020 stripbytes = rowbytes * rowsperstrip;
2026 * never increase the number of strips in an image
2028 if (rowsperstrip >= td->td_rowsperstrip)
2030 nstrips = (tstrip_t) TIFFhowmany(bytecount, stripbytes);
2031 if( nstrips == 0 ) /* something is wonky, do nothing. */
2034 newcounts = (uint32*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint32),
2035 "for chopped \"StripByteCounts\" array");
2036 newoffsets = (uint32*) _TIFFCheckMalloc(tif, nstrips, sizeof (uint32),
2037 "for chopped \"StripOffsets\" array");
2038 if (newcounts == NULL || newoffsets == NULL) {
2040 * Unable to allocate new strip information, give up and use
2041 * the original one strip information.
2043 if (newcounts != NULL)
2044 _TIFFfree(newcounts);
2045 if (newoffsets != NULL)
2046 _TIFFfree(newoffsets);
2050 * Fill the strip information arrays with new bytecounts and offsets
2051 * that reflect the broken-up format.
2053 for (strip = 0; strip < nstrips; strip++) {
2054 if ((uint32)stripbytes > bytecount)
2055 stripbytes = bytecount;
2056 newcounts[strip] = stripbytes;
2057 newoffsets[strip] = offset;
2058 offset += stripbytes;
2059 bytecount -= stripbytes;
2062 * Replace old single strip info with multi-strip info.
2064 td->td_stripsperimage = td->td_nstrips = nstrips;
2065 TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, rowsperstrip);
2067 _TIFFfree(td->td_stripbytecount);
2068 _TIFFfree(td->td_stripoffset);
2069 td->td_stripbytecount = newcounts;
2070 td->td_stripoffset = newoffsets;
2071 td->td_stripbytecountsorted = 1;
2074 /* vim: set ts=8 sts=8 sw=8 noet: */