tif_dirread.c

来自「支持各种栅格图像和矢量图像读取的库」· C语言 代码 · 共 1,972 行 · 第 1/4 页

C
1,972
字号
			    dp->tdir_offset)) {			case OFILETYPE_REDUCEDIMAGE:				v = FILETYPE_REDUCEDIMAGE;				break;			case OFILETYPE_PAGE:				v = FILETYPE_PAGE;				break;			}			if (v)				TIFFSetField(tif, TIFFTAG_SUBFILETYPE, v);			break;/* END REV 4.0 COMPATIBILITY */		default:			(void) TIFFFetchNormalTag(tif, dp);			break;		}	}	/*	 * Joris: OJPEG hack:	 * - If a) compression is OJPEG, and b) photometric tag is missing,	 * then we consistently find that photometric should be YCbCr	 * - If a) compression is OJPEG, and b) photometric tag says it's RGB,	 * then we consistently find that the buggy implementation of the	 * buggy compression scheme matches photometric YCbCr instead.	 * - If a) compression is OJPEG, and b) bitspersample tag is missing,	 * then we consistently find bitspersample should be 8.	 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,	 * and c) photometric is RGB or YCbCr, then we consistently find	 * samplesperpixel should be 3	 * - If a) compression is OJPEG, b) samplesperpixel tag is missing,	 * and c) photometric is MINISWHITE or MINISBLACK, then we consistently	 * find samplesperpixel should be 3	 */	if (td->td_compression==COMPRESSION_OJPEG)	{		if (!TIFFFieldSet(tif,FIELD_PHOTOMETRIC))		{			TIFFWarningExt(tif->tif_clientdata, "TIFFReadDirectory",			"Photometric tag is missing, assuming data is YCbCr");			if (!TIFFSetField(tif,TIFFTAG_PHOTOMETRIC,PHOTOMETRIC_YCBCR))				goto bad;		}		else if (td->td_photometric==PHOTOMETRIC_RGB)		{			td->td_photometric=PHOTOMETRIC_YCBCR;			TIFFWarningExt(tif->tif_clientdata, "TIFFReadDirectory",			"Photometric tag value assumed incorrect, "			"assuming data is YCbCr instead of RGB");		}		if (!TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))		{			TIFFWarningExt(tif->tif_clientdata,"TIFFReadDirectory",		"BitsPerSample tag is missing, assuming 8 bits per sample");			if (!TIFFSetField(tif,TIFFTAG_BITSPERSAMPLE,8))				goto bad;		}		if (!TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))		{			if ((td->td_photometric==PHOTOMETRIC_RGB)			    || (td->td_photometric==PHOTOMETRIC_YCBCR))			{				TIFFWarningExt(tif->tif_clientdata,					       "TIFFReadDirectory",				"SamplesPerPixel tag is missing, "				"assuming correct SamplesPerPixel value is 3");				if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,3))					goto bad;			}			else if ((td->td_photometric==PHOTOMETRIC_MINISWHITE)				 || (td->td_photometric==PHOTOMETRIC_MINISBLACK))			{				TIFFWarningExt(tif->tif_clientdata,					       "TIFFReadDirectory",				"SamplesPerPixel tag is missing, "				"assuming correct SamplesPerPixel value is 1");				if (!TIFFSetField(tif,TIFFTAG_SAMPLESPERPIXEL,1))					goto bad;			}		}	}	/*	 * Verify Palette image has a Colormap.	 */	if (td->td_photometric == PHOTOMETRIC_PALETTE &&	    !TIFFFieldSet(tif, FIELD_COLORMAP)) {		MissingRequired(tif, "Colormap");		goto bad;	}	/*	 * Joris: OJPEG hack:	 * We do no further messing with strip/tile offsets/bytecounts in OJPEG	 * TIFFs	 */	if (td->td_compression!=COMPRESSION_OJPEG)	{		/*		 * Attempt to deal with a missing StripByteCounts tag.		 */		if (!TIFFFieldSet(tif, FIELD_STRIPBYTECOUNTS)) {			/*			 * Some manufacturers violate the spec by not giving			 * the size of the strips.  In this case, assume there			 * is one uncompressed strip of data.			 */			if ((td->td_planarconfig == PLANARCONFIG_CONTIG &&			    td->td_nstrips > 1) ||			    (td->td_planarconfig == PLANARCONFIG_SEPARATE &&			     td->td_nstrips != td->td_samplesperpixel)) {			    MissingRequired(tif, "StripByteCounts");			    goto bad;			}			TIFFWarningExt(tif->tif_clientdata, module,				"%s: TIFF directory is missing required "				"\"%s\" field, calculating from imagelength",				tif->tif_name,				_TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);			if (EstimateStripByteCounts(tif, dir, dircount) < 0)			    goto bad;		/*		 * Assume we have wrong StripByteCount value (in case		 * of single strip) in following cases:		 *   - it is equal to zero along with StripOffset;		 *   - it is larger than file itself (in case of uncompressed		 *     image);		 *   - it is smaller than the size of the bytes per row		 *     multiplied on the number of rows.  The last case should		 *     not be checked in the case of writing new image,		 *     because we may do not know the exact strip size		 *     until the whole image will be written and directory		 *     dumped out.		 */		#define	BYTECOUNTLOOKSBAD \		    ( (td->td_stripbytecount[0] == 0 && td->td_stripoffset[0] != 0) || \		      (td->td_compression == COMPRESSION_NONE && \		       td->td_stripbytecount[0] > TIFFGetFileSize(tif) - td->td_stripoffset[0]) || \		      (tif->tif_mode == O_RDONLY && \		       td->td_compression == COMPRESSION_NONE && \		       td->td_stripbytecount[0] < TIFFScanlineSize(tif) * td->td_imagelength) )		} else if (td->td_nstrips == 1			   && td->td_stripoffset[0] != 0			   && BYTECOUNTLOOKSBAD) {			/*			 * XXX: Plexus (and others) sometimes give a value of			 * zero for a tag when they don't know what the			 * correct value is!  Try and handle the simple case			 * of estimating the size of a one strip image.			 */			TIFFWarningExt(tif->tif_clientdata, module,	"%s: Bogus \"%s\" field, ignoring and calculating from imagelength",				    tif->tif_name,				    _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);			if(EstimateStripByteCounts(tif, dir, dircount) < 0)			    goto bad;		} else if (td->td_planarconfig == PLANARCONFIG_CONTIG			   && td->td_nstrips > 2			   && td->td_compression == COMPRESSION_NONE			   && td->td_stripbytecount[0] != td->td_stripbytecount[1]                           && td->td_stripbytecount[0] != 0                            && td->td_stripbytecount[1] != 0 ) {			/*			 * XXX: Some vendors fill StripByteCount array with                          * absolutely wrong values (it can be equal to                          * StripOffset array, for example). Catch this case                          * here.			 */			TIFFWarningExt(tif->tif_clientdata, module,	"%s: Wrong \"%s\" field, ignoring and calculating from imagelength",				    tif->tif_name,				    _TIFFFieldWithTag(tif,TIFFTAG_STRIPBYTECOUNTS)->field_name);			if (EstimateStripByteCounts(tif, dir, dircount) < 0)			    goto bad;		}	}	if (dir) {		_TIFFfree((char *)dir);		dir = NULL;	}	if (!TIFFFieldSet(tif, FIELD_MAXSAMPLEVALUE))		td->td_maxsamplevalue = (uint16)((1L<<td->td_bitspersample)-1);	/*	 * Setup default compression scheme.	 */	/*	 * XXX: We can optimize checking for the strip bounds using the sorted	 * bytecounts array. See also comments for TIFFAppendToStrip()	 * function in tif_write.c.	 */	if (td->td_nstrips > 1) {		tstrip_t strip;		td->td_stripbytecountsorted = 1;		for (strip = 1; strip < td->td_nstrips; strip++) {			if (td->td_stripoffset[strip - 1] >			    td->td_stripoffset[strip]) {				td->td_stripbytecountsorted = 0;				break;			}		}	}	if (!TIFFFieldSet(tif, FIELD_COMPRESSION))		TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);	/*	 * Some manufacturers make life difficult by writing	 * large amounts of uncompressed data as a single strip.	 * This is contrary to the recommendations of the spec.	 * The following makes an attempt at breaking such images	 * into strips closer to the recommended 8k bytes.  A	 * side effect, however, is that the RowsPerStrip tag	 * value may be changed.	 */	if (td->td_nstrips == 1 && td->td_compression == COMPRESSION_NONE &&	    (tif->tif_flags & (TIFF_STRIPCHOP|TIFF_ISTILED)) == TIFF_STRIPCHOP)		ChopUpSingleUncompressedStrip(tif);	/*	 * Reinitialize i/o since we are starting on a new directory.	 */	tif->tif_row = (uint32) -1;	tif->tif_curstrip = (tstrip_t) -1;	tif->tif_col = (uint32) -1;	tif->tif_curtile = (ttile_t) -1;	tif->tif_tilesize = (tsize_t) -1;	tif->tif_scanlinesize = TIFFScanlineSize(tif);	if (!tif->tif_scanlinesize) {		TIFFErrorExt(tif->tif_clientdata, module,			     "%s: cannot handle zero scanline size",			     tif->tif_name);		return (0);	}	if (isTiled(tif)) {		tif->tif_tilesize = TIFFTileSize(tif);		if (!tif->tif_tilesize) {			TIFFErrorExt(tif->tif_clientdata, module,				     "%s: cannot handle zero tile size",				     tif->tif_name);			return (0);		}	} else {		if (!TIFFStripSize(tif)) {			TIFFErrorExt(tif->tif_clientdata, module,				     "%s: cannot handle zero strip size",				     tif->tif_name);			return (0);		}	}	return (1);bad:	if (dir)		_TIFFfree(dir);	return (0);}static TIFFDirEntry*TIFFReadDirectoryFind(TIFFDirEntry* dir, uint16 dircount, uint16 tagid){	TIFFDirEntry* m;	uint16 n;	for (m=dir, n=0; n<dircount; m++, n++)	{		if (m->tdir_tag==tagid)			return(m);	}	return(0);}/* * Read custom directory from the arbitarry offset. * The code is very similar to TIFFReadDirectory(). */intTIFFReadCustomDirectory(TIFF* tif, toff_t diroff,			const TIFFFieldInfo info[], size_t n){	static const char module[] = "TIFFReadCustomDirectory";	TIFFDirectory* td = &tif->tif_dir;	TIFFDirEntry *dp, *dir = NULL;	const TIFFFieldInfo* fip;	size_t fix;	uint16 i, dircount;	_TIFFSetupFieldInfo(tif, info, n);	dircount = TIFFFetchDirectory(tif, diroff, &dir, NULL);	if (!dircount) {		TIFFErrorExt(tif->tif_clientdata, module,			"%s: Failed to read custom directory at offset %lu",			     tif->tif_name, diroff);		return 0;	}	TIFFFreeDirectory(tif);	fix = 0;	for (dp = dir, i = dircount; i > 0; i--, dp++) {		if (tif->tif_flags & TIFF_SWAB) {			TIFFSwabArrayOfShort(&dp->tdir_tag, 2);			TIFFSwabArrayOfLong(&dp->tdir_count, 2);		}		if (fix >= tif->tif_nfields || dp->tdir_tag == IGNORE)			continue;		while (fix < tif->tif_nfields &&		       tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)			fix++;		if (fix >= tif->tif_nfields ||		    tif->tif_fieldinfo[fix]->field_tag != dp->tdir_tag) {			TIFFWarningExt(tif->tif_clientdata, module,                        "%s: unknown field with tag %d (0x%x) encountered",				    tif->tif_name, dp->tdir_tag, dp->tdir_tag,				    dp->tdir_type);			TIFFMergeFieldInfo(tif,					   _TIFFCreateAnonFieldInfo(tif,						dp->tdir_tag,						(TIFFDataType)dp->tdir_type),					   1);			fix = 0;			while (fix < tif->tif_nfields &&			       tif->tif_fieldinfo[fix]->field_tag < dp->tdir_tag)				fix++;		}		/*		 * Null out old tags that we ignore.		 */		if (tif->tif_fieldinfo[fix]->field_bit == FIELD_IGNORE) {	ignore:			dp->tdir_tag = IGNORE;			continue;		}		/*		 * Check data type.		 */		fip = tif->tif_fieldinfo[fix];		while (dp->tdir_type != (unsigned short) fip->field_type                       && fix < tif->tif_nfields) {			if (fip->field_type == TIFF_ANY)	/* wildcard */				break;                        fip = tif->tif_fieldinfo[++fix];			if (fix >= tif->tif_nfields ||			    fip->field_tag != dp->tdir_tag) {				TIFFWarningExt(tif->tif_clientdata, module,			"%s: wrong data type %d for \"%s\"; tag ignored",					    tif->tif_name, dp->tdir_type,					    tif->tif_fieldinfo[fix-1]->field_name);				goto ignore;			}		}		/*		 * Check count if known in advance.		 */		if (fip->field_readcount != TIFF_VARIABLE		    && fip->field_readcount != TIFF_VARIABLE2) {			uint32 expected = (fip->field_readcount == TIFF_SPP) ?			    (uint32) td->td_samplesperpixel :			    (uint32) fip->field_readcount;			if (!CheckDirCount(tif, dp, expected))				goto ignore;		}		(void) TIFFFetchNormalTag(tif, dp);	}		if (dir)		_TIFFfree(dir);	return 1;}/* * EXIF is important special case of custom IFD, so we have a special * function to read it. */intTIFFReadEXIFDirectory(TIFF* tif, toff_t diroff){	size_t exifFieldInfoCount;	const TIFFFieldInfo *exifFieldInfo =		_TIFFGetExifFieldInfo(&exifFieldInfoCount);	return TIFFReadCustomDirectory(tif, diroff, exifFieldInfo,				       exifFieldInfoCount);}static intEstimateStripByteCounts(TIFF* tif, TIFFDirEntry* dir, uint16 dircount){	static const char module[] = "EstimateStripByteCounts";	TIFFDirEntry *dp;	TIFFDirectory *td = &tif->tif_dir;	uint32 strip;	if (td->td_stripbytecount)		_TIFFfree(td->td_stripbytecount);	td->td_stripbytecount = (uint32*)	    _TIFFCheckMalloc(tif, td->td_nstrips, sizeof (uint32),		"for \"StripByteCounts\" array");	if (td->td_compression != COMPRESSION_NONE) {		uint32 space = (uint32)(sizeof (TIFFHeader)		    + sizeof (uint16)		    + (dircount * sizeof (TIFFDirEntry))		    + sizeof (uint32));		toff_t filesize = TIFFGetFileSize(tif);		uint16 n;		/* calculate amount of space used by indirect values */		for (dp = dir, n = dircount; n > 0; n--, dp++)		{			uint32 cc = TIFFDataWidth((TIFFDataType) dp->tdir_type);			if (cc == 0) {				TIFFErrorExt(tif->tif_clientdata, module,			"%s: Cannot determine size of unknown tag type %d",					  tif->tif_name, dp->tdir_type);				return -1;			}			cc = cc * dp->tdir_count;			if (cc > sizeof (uint32))				space += cc;		}		space = filesize - space;		if (td->td_planarconfig == PLANARCONFIG_SEPARATE)			space /= td->td_samplesperpixel;		for (strip = 0; strip < td->td_nstrips; strip++)			td->td_stripbytecount[strip] = space;		/*		 * This gross hack handles the case were the offset to		 * the last strip is past the place where we think the strip		 * should begin.  Since a strip of data must be contiguous,		 * it's safe to assume that we've overestimated the amount		 * of data in the strip and trim this number back accordingly.		 */ 		strip--;		if (((toff_t)(td->td_stripoffset[strip]+			      td->td_stripbytecount[strip])) > filesize)			td->td_stripbytecount[strip] =			    filesize - td->td_stripoffset[strip];	} else if (isTiled(tif)) {		uint32 bytespertile = TIFFTileSize(tif);		for (strip = 0; strip < td->td_nstrips; strip++)                    td->td_stripbytecount[strip] = bytespertile;	} else {		uint32 rowbytes = TIFFScanlineSize(tif);		uint32 rowsperstrip = td->td_imagelength/td->td_stripsperimage;		for (strip = 0; strip < td->td_nstrips; strip++)			td->td_stripbytecount[strip] = rowbytes * rowsperstrip;	}	TIFFSetFieldBit(tif, FIELD_STRIPBYTECOUNTS);	if (!TIFFFieldSet(tif, FIELD_ROWSPERSTRIP))		td->td_rowsperstrip = td->td_imagelength;	return 1;}static voidMissingRequired(TIFF* tif, const char* tagname){	static const char module[] = "MissingRequired";	TIFFErrorExt(tif->tif_clientdata, module,		  "%s: TIFF directory is missing required \"%s\" field",		  tif->tif_name, tagname);}/* * Check the directory offset against the list of already seen directory * offsets. This is a trick to prevent IFD looping. The one can create TIFF * file with looped directory pointers. We will maintain a list of already * seen directories and check every IFD offset against that list. */static intTIFFCheckDirOffset(TIFF* tif, toff_t diroff){	uint16 n;	if (diroff == 0)			/* no more directories */		return 0;	for (n = 0; n < tif->tif_dirnumber && tif->tif_dirlist; n++) {		if (tif->tif_dirlist[n] == diroff)			return 0;	}	tif->tif_dirnumber++;	if (tif->tif_dirnumber > tif->tif_dirlistsize) {

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?