⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tif_dirwrite.c

📁 一个国人自己实现图像库的程序(有参考价值)
💻 C
📖 第 1 页 / 共 3 页
字号:
/* $Header: /cvsroot/osrs/libtiff/libtiff/tif_dirwrite.c,v 1.9 2001/09/26 17:42:18 warmerda Exp $ *//* * Copyright (c) 1988-1997 Sam Leffler * Copyright (c) 1991-1997 Silicon Graphics, Inc. * * Permission to use, copy, modify, distribute, and sell this software and  * its documentation for any purpose is hereby granted without fee, provided * that (i) the above copyright notices and this permission notice appear in * all copies of the software and related documentation, and (ii) the names of * Sam Leffler and Silicon Graphics may not be used in any advertising or * publicity relating to the software without the specific, prior written * permission of Sam Leffler and Silicon Graphics. *  * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,  * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY  * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.   *  * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF  * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE  * OF THIS SOFTWARE. *//* * TIFF Library. * * Directory Write Support Routines. */#include "tiffiop.h"#if HAVE_IEEEFP#define	TIFFCvtNativeToIEEEFloat(tif, n, fp)#define	TIFFCvtNativeToIEEEDouble(tif, n, dp)#elseextern	void TIFFCvtNativeToIEEEFloat(TIFF*, uint32, float*);extern	void TIFFCvtNativeToIEEEDouble(TIFF*, uint32, double*);#endifstatic	int TIFFWriteNormalTag(TIFF*, TIFFDirEntry*, const TIFFFieldInfo*);static	void TIFFSetupShortLong(TIFF*, ttag_t, TIFFDirEntry*, uint32);static	int TIFFSetupShortPair(TIFF*, ttag_t, TIFFDirEntry*);static	int TIFFWritePerSampleShorts(TIFF*, ttag_t, TIFFDirEntry*);static	int TIFFWritePerSampleAnys(TIFF*, TIFFDataType, ttag_t, TIFFDirEntry*);static	int TIFFWriteShortTable(TIFF*, ttag_t, TIFFDirEntry*, uint32, uint16**);static	int TIFFWriteShortArray(TIFF*,	    TIFFDataType, ttag_t, TIFFDirEntry*, uint32, uint16*);static	int TIFFWriteLongArray(TIFF *,	    TIFFDataType, ttag_t, TIFFDirEntry*, uint32, uint32*);static	int TIFFWriteRationalArray(TIFF *,	    TIFFDataType, ttag_t, TIFFDirEntry*, uint32, float*);static	int TIFFWriteFloatArray(TIFF *,	    TIFFDataType, ttag_t, TIFFDirEntry*, uint32, float*);static	int TIFFWriteDoubleArray(TIFF *,	    TIFFDataType, ttag_t, TIFFDirEntry*, uint32, double*);static	int TIFFWriteByteArray(TIFF*, TIFFDirEntry*, char*);static	int TIFFWriteAnyArray(TIFF*,	    TIFFDataType, ttag_t, TIFFDirEntry*, uint32, double*);#ifdef COLORIMETRY_SUPPORTstatic	int TIFFWriteTransferFunction(TIFF*, TIFFDirEntry*);#endif#ifdef CMYK_SUPPORTstatic	int TIFFWriteInkNames(TIFF*, TIFFDirEntry*);#endifstatic	int TIFFWriteData(TIFF*, TIFFDirEntry*, char*);static	int TIFFLinkDirectory(TIFF*);#define	WriteRationalPair(type, tag1, v1, tag2, v2) {		\	if (!TIFFWriteRational(tif, type, tag1, dir, v1))	\		goto bad;					\	if (!TIFFWriteRational(tif, type, tag2, dir+1, v2))	\		goto bad;					\	dir++;							\}#define	TIFFWriteRational(tif, type, tag, dir, v) \	TIFFWriteRationalArray((tif), (type), (tag), (dir), 1, &(v))#ifndef TIFFWriteRationalstatic	int TIFFWriteRational(TIFF*,	    TIFFDataType, ttag_t, TIFFDirEntry*, float);#endif/* * Write the contents of the current directory * to the specified file.  This routine doesn't * handle overwriting a directory with auxiliary * storage that's been changed. */intTIFFWriteDirectory(TIFF* tif){	uint16 dircount;	toff_t diroff;	ttag_t tag;	uint32 nfields;	tsize_t dirsize;	char* data;	TIFFDirEntry* dir;	TIFFDirectory* td;	u_long b, fields[FIELD_SETLONGS];	int fi, nfi;	if (tif->tif_mode == O_RDONLY)		return (1);	/*	 * Clear write state so that subsequent images with	 * different characteristics get the right buffers	 * setup for them.	 */	if (tif->tif_flags & TIFF_POSTENCODE) {		tif->tif_flags &= ~TIFF_POSTENCODE;		if (!(*tif->tif_postencode)(tif)) {			TIFFError(tif->tif_name,			    "Error post-encoding before directory write");			return (0);		}	}	(*tif->tif_close)(tif);			/* shutdown encoder */	/*	 * Flush any data that might have been written	 * by the compression close+cleanup routines.	 */	if (tif->tif_rawcc > 0 && !TIFFFlushData1(tif)) {		TIFFError(tif->tif_name,		    "Error flushing data before directory write");		return (0);	}	if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata) {		_TIFFfree(tif->tif_rawdata);		tif->tif_rawdata = NULL;		tif->tif_rawcc = 0;                tif->tif_rawdatasize = 0;	}	tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);	td = &tif->tif_dir;	/*	 * Size the directory so that we can calculate	 * offsets for the data items that aren't kept	 * in-place in each field.	 */	nfields = 0;	for (b = 0; b <= FIELD_LAST; b++)		if (TIFFFieldSet(tif, b))			nfields += (b < FIELD_SUBFILETYPE ? 2 : 1);	dirsize = nfields * sizeof (TIFFDirEntry);	data = (char*) _TIFFmalloc(dirsize);	if (data == NULL) {		TIFFError(tif->tif_name,		    "Cannot write directory, out of space");		return (0);	}	/*	 * Directory hasn't been placed yet, put	 * it at the end of the file and link it	 * into the existing directory structure.	 */	if (tif->tif_diroff == 0 && !TIFFLinkDirectory(tif))		goto bad;	tif->tif_dataoff = (toff_t)(	    tif->tif_diroff + sizeof (uint16) + dirsize + sizeof (toff_t));	if (tif->tif_dataoff & 1)		tif->tif_dataoff++;	(void) TIFFSeekFile(tif, tif->tif_dataoff, SEEK_SET);	tif->tif_curdir++;	dir = (TIFFDirEntry*) data;	/*	 * Setup external form of directory	 * entries and write data items.	 */	_TIFFmemcpy(fields, td->td_fieldsset, sizeof (fields));	/*	 * Write out ExtraSamples tag only if	 * extra samples are present in the data.	 */	if (FieldSet(fields, FIELD_EXTRASAMPLES) && !td->td_extrasamples) {		ResetFieldBit(fields, FIELD_EXTRASAMPLES);		nfields--;		dirsize -= sizeof (TIFFDirEntry);	}								/*XXX*/	for (fi = 0, nfi = tif->tif_nfields; nfi > 0; nfi--, fi++) {		const TIFFFieldInfo* fip = tif->tif_fieldinfo[fi];		if (!FieldSet(fields, fip->field_bit))			continue;		switch (fip->field_bit) {		case FIELD_STRIPOFFSETS:			/*			 * We use one field bit for both strip and tile			 * offsets, and so must be careful in selecting			 * the appropriate field descriptor (so that tags			 * are written in sorted order).			 */			tag = isTiled(tif) ?			    TIFFTAG_TILEOFFSETS : TIFFTAG_STRIPOFFSETS;			if (tag != fip->field_tag)				continue;			if (!TIFFWriteLongArray(tif, TIFF_LONG, tag, dir,			    (uint32) td->td_nstrips, td->td_stripoffset))				goto bad;			break;		case FIELD_STRIPBYTECOUNTS:			/*			 * We use one field bit for both strip and tile			 * byte counts, and so must be careful in selecting			 * the appropriate field descriptor (so that tags			 * are written in sorted order).			 */			tag = isTiled(tif) ?			    TIFFTAG_TILEBYTECOUNTS : TIFFTAG_STRIPBYTECOUNTS;			if (tag != fip->field_tag)				continue;			if (!TIFFWriteLongArray(tif, TIFF_LONG, tag, dir,			    (uint32) td->td_nstrips, td->td_stripbytecount))				goto bad;			break;		case FIELD_ROWSPERSTRIP:			TIFFSetupShortLong(tif, TIFFTAG_ROWSPERSTRIP,			    dir, td->td_rowsperstrip);			break;		case FIELD_COLORMAP:			if (!TIFFWriteShortTable(tif, TIFFTAG_COLORMAP, dir,			    3, td->td_colormap))				goto bad;			break;		case FIELD_IMAGEDIMENSIONS:			TIFFSetupShortLong(tif, TIFFTAG_IMAGEWIDTH,			    dir++, td->td_imagewidth);			TIFFSetupShortLong(tif, TIFFTAG_IMAGELENGTH,			    dir, td->td_imagelength);			break;		case FIELD_TILEDIMENSIONS:			TIFFSetupShortLong(tif, TIFFTAG_TILEWIDTH,			    dir++, td->td_tilewidth);			TIFFSetupShortLong(tif, TIFFTAG_TILELENGTH,			    dir, td->td_tilelength);			break;		case FIELD_POSITION:			WriteRationalPair(TIFF_RATIONAL,			    TIFFTAG_XPOSITION, td->td_xposition,			    TIFFTAG_YPOSITION, td->td_yposition);			break;		case FIELD_RESOLUTION:			WriteRationalPair(TIFF_RATIONAL,			    TIFFTAG_XRESOLUTION, td->td_xresolution,			    TIFFTAG_YRESOLUTION, td->td_yresolution);			break;		case FIELD_BITSPERSAMPLE:		case FIELD_MINSAMPLEVALUE:		case FIELD_MAXSAMPLEVALUE:		case FIELD_SAMPLEFORMAT:			if (!TIFFWritePerSampleShorts(tif, fip->field_tag, dir))				goto bad;			break;		case FIELD_SMINSAMPLEVALUE:		case FIELD_SMAXSAMPLEVALUE:			if (!TIFFWritePerSampleAnys(tif,			    _TIFFSampleToTagType(tif), fip->field_tag, dir))				goto bad;			break;		case FIELD_PAGENUMBER:		case FIELD_HALFTONEHINTS:#ifdef YCBCR_SUPPORT		case FIELD_YCBCRSUBSAMPLING:#endif#ifdef CMYK_SUPPORT		case FIELD_DOTRANGE:#endif			if (!TIFFSetupShortPair(tif, fip->field_tag, dir))				goto bad;			break;#ifdef CMYK_SUPPORT		case FIELD_INKNAMES:			if (!TIFFWriteInkNames(tif, dir))				goto bad;			break;#endif#ifdef COLORIMETRY_SUPPORT		case FIELD_TRANSFERFUNCTION:			if (!TIFFWriteTransferFunction(tif, dir))				goto bad;			break;#endif#if SUBIFD_SUPPORT		case FIELD_SUBIFD:			if (!TIFFWriteNormalTag(tif, dir, fip))				goto bad;			/*			 * Total hack: if this directory includes a SubIFD			 * tag then force the next <n> directories to be			 * written as ``sub directories'' of this one.  This			 * is used to write things like thumbnails and			 * image masks that one wants to keep out of the			 * normal directory linkage access mechanism.			 */			if (dir->tdir_count > 0) {				tif->tif_flags |= TIFF_INSUBIFD;				tif->tif_nsubifd = (uint16) dir->tdir_count;				if (dir->tdir_count > 1)					tif->tif_subifdoff = dir->tdir_offset;				else					tif->tif_subifdoff = (uint32)(					      tif->tif_diroff					    + sizeof (uint16)					    + ((char*)&dir->tdir_offset-data));			}			break;#endif		default:			if (!TIFFWriteNormalTag(tif, dir, fip))				goto bad;			break;		}		dir++;		ResetFieldBit(fields, fip->field_bit);	}	/*	 * Write directory.	 */	dircount = (uint16) nfields;	diroff = (uint32) tif->tif_nextdiroff;	if (tif->tif_flags & TIFF_SWAB) {		/*		 * The file's byte order is opposite to the		 * native machine architecture.  We overwrite		 * the directory information with impunity		 * because it'll be released below after we		 * write it to the file.  Note that all the		 * other tag construction routines assume that		 * we do this byte-swapping; i.e. they only		 * byte-swap indirect data.		 */		for (dir = (TIFFDirEntry*) data; dircount; dir++, dircount--) {			TIFFSwabArrayOfShort(&dir->tdir_tag, 2);			TIFFSwabArrayOfLong(&dir->tdir_count, 2);		}		dircount = (uint16) nfields;		TIFFSwabShort(&dircount);		TIFFSwabLong(&diroff);	}	(void) TIFFSeekFile(tif, tif->tif_diroff, SEEK_SET);	if (!WriteOK(tif, &dircount, sizeof (dircount))) {		TIFFError(tif->tif_name, "Error writing directory count");		goto bad;	}	if (!WriteOK(tif, data, dirsize)) {		TIFFError(tif->tif_name, "Error writing directory contents");		goto bad;	}	if (!WriteOK(tif, &diroff, sizeof (diroff))) {		TIFFError(tif->tif_name, "Error writing directory link");		goto bad;	}	TIFFFreeDirectory(tif);	_TIFFfree(data);	tif->tif_flags &= ~TIFF_DIRTYDIRECT;	(*tif->tif_cleanup)(tif);	/*	 * Reset directory-related state for subsequent	 * directories.	 */        TIFFCreateDirectory(tif);	return (1);bad:	_TIFFfree(data);	return (0);}

⌨️ 快捷键说明

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