roentgen b75cab
/* $Id: tif_dirwrite.c,v 1.77 2012-07-06 19:18:31 bfriesen Exp $ */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Copyright (c) 1988-1997 Sam Leffler
roentgen b75cab
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
roentgen b75cab
 *
roentgen b75cab
 * Permission to use, copy, modify, distribute, and sell this software and
roentgen b75cab
 * its documentation for any purpose is hereby granted without fee, provided
roentgen b75cab
 * that (i) the above copyright notices and this permission notice appear in
roentgen b75cab
 * all copies of the software and related documentation, and (ii) the names of
roentgen b75cab
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
roentgen b75cab
 * publicity relating to the software without the specific, prior written
roentgen b75cab
 * permission of Sam Leffler and Silicon Graphics.
roentgen b75cab
 *
roentgen b75cab
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
roentgen b75cab
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
roentgen b75cab
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
roentgen b75cab
 *
roentgen b75cab
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
roentgen b75cab
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
roentgen b75cab
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
roentgen b75cab
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
roentgen b75cab
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
roentgen b75cab
 * OF THIS SOFTWARE.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * TIFF Library.
roentgen b75cab
 *
roentgen b75cab
 * Directory Write Support Routines.
roentgen b75cab
 */
roentgen b75cab
#include "tiffiop.h"
roentgen b75cab
roentgen b75cab
#ifdef HAVE_IEEEFP
roentgen b75cab
#define TIFFCvtNativeToIEEEFloat(tif, n, fp)
roentgen b75cab
#define TIFFCvtNativeToIEEEDouble(tif, n, dp)
roentgen b75cab
#else
roentgen b75cab
extern void TIFFCvtNativeToIEEEFloat(TIFF* tif, uint32 n, float* fp);
roentgen b75cab
extern void TIFFCvtNativeToIEEEDouble(TIFF* tif, uint32 n, double* dp);
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff);
roentgen b75cab
roentgen b75cab
static int TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
roentgen b75cab
#if 0
roentgen b75cab
static int TIFFWriteDirectoryTagSampleformatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int TIFFWriteDirectoryTagAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value);
roentgen b75cab
static int TIFFWriteDirectoryTagUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
roentgen b75cab
#if 0
roentgen b75cab
static int TIFFWriteDirectoryTagBytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
roentgen b75cab
#endif
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value);
roentgen b75cab
#if 0
roentgen b75cab
static int TIFFWriteDirectoryTagSbytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
roentgen b75cab
static int TIFFWriteDirectoryTagShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value);
roentgen b75cab
static int TIFFWriteDirectoryTagShortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value);
roentgen b75cab
#if 0
roentgen b75cab
static int TIFFWriteDirectoryTagSshortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
roentgen b75cab
static int TIFFWriteDirectoryTagLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
roentgen b75cab
#if 0
roentgen b75cab
static int TIFFWriteDirectoryTagLongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
roentgen b75cab
#endif
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value);
roentgen b75cab
#if 0
roentgen b75cab
static int TIFFWriteDirectoryTagSlongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
roentgen b75cab
#endif
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value);
roentgen b75cab
static int TIFFWriteDirectoryTagRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
roentgen b75cab
static int TIFFWriteDirectoryTagRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
roentgen b75cab
static int TIFFWriteDirectoryTagSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
roentgen b75cab
#if 0
roentgen b75cab
static int TIFFWriteDirectoryTagFloatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
roentgen b75cab
#endif
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
roentgen b75cab
#if 0
roentgen b75cab
static int TIFFWriteDirectoryTagDoublePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagShortLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
roentgen b75cab
static int TIFFWriteDirectoryTagLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
roentgen b75cab
static int TIFFWriteDirectoryTagIfdIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagShortLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagColormap(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
roentgen b75cab
static int TIFFWriteDirectoryTagTransferfunction(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
roentgen b75cab
static int TIFFWriteDirectoryTagSubifd(TIFF* tif, uint32* ndir, TIFFDirEntry* dir);
roentgen b75cab
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value);
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value);
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value);
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value);
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value);
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value);
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value);
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value);
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value);
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value);
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value);
roentgen b75cab
#endif
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value);
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value);
roentgen b75cab
static int TIFFWriteDirectoryTagCheckedIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value);
roentgen b75cab
roentgen b75cab
static int TIFFWriteDirectoryTagData(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 datatype, uint32 count, uint32 datalength, void* data);
roentgen b75cab
roentgen b75cab
static int TIFFLinkDirectory(TIFF*);
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Write the contents of the current directory
roentgen b75cab
 * to the specified file.  This routine doesn't
roentgen b75cab
 * handle overwriting a directory with auxiliary
roentgen b75cab
 * storage that's been changed.
roentgen b75cab
 */
roentgen b75cab
int
roentgen b75cab
TIFFWriteDirectory(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	return TIFFWriteDirectorySec(tif,TRUE,TRUE,NULL);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Similar to TIFFWriteDirectory(), writes the directory out
roentgen b75cab
 * but leaves all data structures in memory so that it can be
roentgen b75cab
 * written again.  This will make a partially written TIFF file
roentgen b75cab
 * readable before it is successfully completed/closed.
roentgen b75cab
 */
roentgen b75cab
int
roentgen b75cab
TIFFCheckpointDirectory(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	int rc;
roentgen b75cab
	/* Setup the strips arrays, if they haven't already been. */
roentgen b75cab
	if (tif->tif_dir.td_stripoffset == NULL)
roentgen b75cab
	    (void) TIFFSetupStrips(tif);
roentgen b75cab
	rc = TIFFWriteDirectorySec(tif,TRUE,FALSE,NULL);
roentgen b75cab
	(void) TIFFSetWriteOffset(tif, TIFFSeekFile(tif, 0, SEEK_END));
roentgen b75cab
	return rc;
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
TIFFWriteCustomDirectory(TIFF* tif, uint64* pdiroff)
roentgen b75cab
{
roentgen b75cab
	return TIFFWriteDirectorySec(tif,FALSE,FALSE,pdiroff);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Similar to TIFFWriteDirectory(), but if the directory has already
roentgen b75cab
 * been written once, it is relocated to the end of the file, in case it
roentgen b75cab
 * has changed in size.  Note that this will result in the loss of the
roentgen b75cab
 * previously used directory space. 
roentgen b75cab
 */ 
roentgen b75cab
int
roentgen b75cab
TIFFRewriteDirectory( TIFF *tif )
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFRewriteDirectory";
roentgen b75cab
roentgen b75cab
	/* We don't need to do anything special if it hasn't been written. */
roentgen b75cab
	if( tif->tif_diroff == 0 )
roentgen b75cab
		return TIFFWriteDirectory( tif );
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * Find and zero the pointer to this directory, so that TIFFLinkDirectory
roentgen b75cab
	 * will cause it to be added after this directories current pre-link.
roentgen b75cab
	 */
roentgen b75cab
roentgen b75cab
	if (!(tif->tif_flags&TIFF_BIGTIFF))
roentgen b75cab
	{
roentgen b75cab
		if (tif->tif_header.classic.tiff_diroff == tif->tif_diroff)
roentgen b75cab
		{
roentgen b75cab
			tif->tif_header.classic.tiff_diroff = 0;
roentgen b75cab
			tif->tif_diroff = 0;
roentgen b75cab
roentgen b75cab
			TIFFSeekFile(tif,4,SEEK_SET);
roentgen b75cab
			if (!WriteOK(tif, &(tif->tif_header.classic.tiff_diroff),4))
roentgen b75cab
			{
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
roentgen b75cab
				    "Error updating TIFF header");
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		else
roentgen b75cab
		{
roentgen b75cab
			uint32 nextdir;
roentgen b75cab
			nextdir = tif->tif_header.classic.tiff_diroff;
roentgen b75cab
			while(1) {
roentgen b75cab
				uint16 dircount;
roentgen b75cab
				uint32 nextnextdir;
roentgen b75cab
roentgen b75cab
				if (!SeekOK(tif, nextdir) ||
roentgen b75cab
				    !ReadOK(tif, &dircount, 2)) {
roentgen b75cab
					TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
					     "Error fetching directory count");
roentgen b75cab
					return (0);
roentgen b75cab
				}
roentgen b75cab
				if (tif->tif_flags & TIFF_SWAB)
roentgen b75cab
					TIFFSwabShort(&dircount);
roentgen b75cab
				(void) TIFFSeekFile(tif,
roentgen b75cab
				    nextdir+2+dircount*12, SEEK_SET);
roentgen b75cab
				if (!ReadOK(tif, &nextnextdir, 4)) {
roentgen b75cab
					TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
					     "Error fetching directory link");
roentgen b75cab
					return (0);
roentgen b75cab
				}
roentgen b75cab
				if (tif->tif_flags & TIFF_SWAB)
roentgen b75cab
					TIFFSwabLong(&nextnextdir);
roentgen b75cab
				if (nextnextdir==tif->tif_diroff)
roentgen b75cab
				{
roentgen b75cab
					uint32 m;
roentgen b75cab
					m=0;
roentgen b75cab
					(void) TIFFSeekFile(tif,
roentgen b75cab
					    nextdir+2+dircount*12, SEEK_SET);
roentgen b75cab
					if (!WriteOK(tif, &m, 4)) {
roentgen b75cab
						TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
						     "Error writing directory link");
roentgen b75cab
						return (0);
roentgen b75cab
					}
roentgen b75cab
					tif->tif_diroff=0;
roentgen b75cab
					break;
roentgen b75cab
				}
roentgen b75cab
				nextdir=nextnextdir;
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	else
roentgen b75cab
	{
roentgen b75cab
		if (tif->tif_header.big.tiff_diroff == tif->tif_diroff)
roentgen b75cab
		{
roentgen b75cab
			tif->tif_header.big.tiff_diroff = 0;
roentgen b75cab
			tif->tif_diroff = 0;
roentgen b75cab
roentgen b75cab
			TIFFSeekFile(tif,8,SEEK_SET);
roentgen b75cab
			if (!WriteOK(tif, &(tif->tif_header.big.tiff_diroff),8))
roentgen b75cab
			{
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
roentgen b75cab
				    "Error updating TIFF header");
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		else
roentgen b75cab
		{
roentgen b75cab
			uint64 nextdir;
roentgen b75cab
			nextdir = tif->tif_header.big.tiff_diroff;
roentgen b75cab
			while(1) {
roentgen b75cab
				uint64 dircount64;
roentgen b75cab
				uint16 dircount;
roentgen b75cab
				uint64 nextnextdir;
roentgen b75cab
roentgen b75cab
				if (!SeekOK(tif, nextdir) ||
roentgen b75cab
				    !ReadOK(tif, &dircount64, 8)) {
roentgen b75cab
					TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
					     "Error fetching directory count");
roentgen b75cab
					return (0);
roentgen b75cab
				}
roentgen b75cab
				if (tif->tif_flags & TIFF_SWAB)
roentgen b75cab
					TIFFSwabLong8(&dircount64);
roentgen b75cab
				if (dircount64>0xFFFF)
roentgen b75cab
				{
roentgen b75cab
					TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
					     "Sanity check on tag count failed, likely corrupt TIFF");
roentgen b75cab
					return (0);
roentgen b75cab
				}
roentgen b75cab
				dircount=(uint16)dircount64;
roentgen b75cab
				(void) TIFFSeekFile(tif,
roentgen b75cab
				    nextdir+8+dircount*20, SEEK_SET);
roentgen b75cab
				if (!ReadOK(tif, &nextnextdir, 8)) {
roentgen b75cab
					TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
					     "Error fetching directory link");
roentgen b75cab
					return (0);
roentgen b75cab
				}
roentgen b75cab
				if (tif->tif_flags & TIFF_SWAB)
roentgen b75cab
					TIFFSwabLong8(&nextnextdir);
roentgen b75cab
				if (nextnextdir==tif->tif_diroff)
roentgen b75cab
				{
roentgen b75cab
					uint64 m;
roentgen b75cab
					m=0;
roentgen b75cab
					(void) TIFFSeekFile(tif,
roentgen b75cab
					    nextdir+8+dircount*20, SEEK_SET);
roentgen b75cab
					if (!WriteOK(tif, &m, 8)) {
roentgen b75cab
						TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
						     "Error writing directory link");
roentgen b75cab
						return (0);
roentgen b75cab
					}
roentgen b75cab
					tif->tif_diroff=0;
roentgen b75cab
					break;
roentgen b75cab
				}
roentgen b75cab
				nextdir=nextnextdir;
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * Now use TIFFWriteDirectory() normally.
roentgen b75cab
	 */
roentgen b75cab
roentgen b75cab
	return TIFFWriteDirectory( tif );
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectorySec(TIFF* tif, int isimage, int imagedone, uint64* pdiroff)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteDirectorySec";
roentgen b75cab
	uint32 ndir;
roentgen b75cab
	TIFFDirEntry* dir;
roentgen b75cab
	uint32 dirsize;
roentgen b75cab
	void* dirmem;
roentgen b75cab
	uint32 m;
roentgen b75cab
	if (tif->tif_mode == O_RDONLY)
roentgen b75cab
		return (1);
roentgen b75cab
roentgen b75cab
        _TIFFFillStriles( tif );
roentgen b75cab
        
roentgen b75cab
	/*
roentgen b75cab
	 * Clear write state so that subsequent images with
roentgen b75cab
	 * different characteristics get the right buffers
roentgen b75cab
	 * setup for them.
roentgen b75cab
	 */
roentgen b75cab
	if (imagedone)
roentgen b75cab
	{
roentgen b75cab
		if (tif->tif_flags & TIFF_POSTENCODE)
roentgen b75cab
		{
roentgen b75cab
			tif->tif_flags &= ~TIFF_POSTENCODE;
roentgen b75cab
			if (!(*tif->tif_postencode)(tif))
roentgen b75cab
			{
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata,module,
roentgen b75cab
				    "Error post-encoding before directory write");
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		(*tif->tif_close)(tif);       /* shutdown encoder */
roentgen b75cab
		/*
roentgen b75cab
		 * Flush any data that might have been written
roentgen b75cab
		 * by the compression close+cleanup routines.  But
roentgen b75cab
                 * be careful not to write stuff if we didn't add data
roentgen b75cab
                 * in the previous steps as the "rawcc" data may well be
roentgen b75cab
                 * a previously read tile/strip in mixed read/write mode.
roentgen b75cab
		 */
roentgen b75cab
		if (tif->tif_rawcc > 0 
roentgen b75cab
		    && (tif->tif_flags & TIFF_BEENWRITING) != 0 )
roentgen b75cab
		{
roentgen b75cab
		    if( !TIFFFlushData1(tif) )
roentgen b75cab
                    {
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
			    "Error flushing data before directory write");
roentgen b75cab
			return (0);
roentgen b75cab
                    }
roentgen b75cab
		}
roentgen b75cab
		if ((tif->tif_flags & TIFF_MYBUFFER) && tif->tif_rawdata)
roentgen b75cab
		{
roentgen b75cab
			_TIFFfree(tif->tif_rawdata);
roentgen b75cab
			tif->tif_rawdata = NULL;
roentgen b75cab
			tif->tif_rawcc = 0;
roentgen b75cab
			tif->tif_rawdatasize = 0;
roentgen b75cab
                        tif->tif_rawdataoff = 0;
roentgen b75cab
                        tif->tif_rawdataloaded = 0;
roentgen b75cab
		}
roentgen b75cab
		tif->tif_flags &= ~(TIFF_BEENWRITING|TIFF_BUFFERSETUP);
roentgen b75cab
	}
roentgen b75cab
	dir=NULL;
roentgen b75cab
	dirmem=NULL;
roentgen b75cab
	dirsize=0;
roentgen b75cab
	while (1)
roentgen b75cab
	{
roentgen b75cab
		ndir=0;
roentgen b75cab
		if (isimage)
roentgen b75cab
		{
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_IMAGEDIMENSIONS))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGEWIDTH,tif->tif_dir.td_imagewidth))
roentgen b75cab
					goto bad;
roentgen b75cab
				if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_IMAGELENGTH,tif->tif_dir.td_imagelength))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_TILEDIMENSIONS))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILEWIDTH,tif->tif_dir.td_tilewidth))
roentgen b75cab
					goto bad;
roentgen b75cab
				if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_TILELENGTH,tif->tif_dir.td_tilelength))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_RESOLUTION))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XRESOLUTION,tif->tif_dir.td_xresolution))
roentgen b75cab
					goto bad;
roentgen b75cab
				if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YRESOLUTION,tif->tif_dir.td_yresolution))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_POSITION))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_XPOSITION,tif->tif_dir.td_xposition))
roentgen b75cab
					goto bad;
roentgen b75cab
				if (!TIFFWriteDirectoryTagRational(tif,&ndir,dir,TIFFTAG_YPOSITION,tif->tif_dir.td_yposition))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_SUBFILETYPE))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_SUBFILETYPE,tif->tif_dir.td_subfiletype))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_BITSPERSAMPLE))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_BITSPERSAMPLE,tif->tif_dir.td_bitspersample))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_COMPRESSION))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_COMPRESSION,tif->tif_dir.td_compression))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_PHOTOMETRIC))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PHOTOMETRIC,tif->tif_dir.td_photometric))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_THRESHHOLDING))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_THRESHHOLDING,tif->tif_dir.td_threshholding))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_FILLORDER))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_FILLORDER,tif->tif_dir.td_fillorder))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_ORIENTATION))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_ORIENTATION,tif->tif_dir.td_orientation))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_SAMPLESPERPIXEL))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_SAMPLESPERPIXEL,tif->tif_dir.td_samplesperpixel))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_ROWSPERSTRIP))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShortLong(tif,&ndir,dir,TIFFTAG_ROWSPERSTRIP,tif->tif_dir.td_rowsperstrip))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_MINSAMPLEVALUE))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MINSAMPLEVALUE,tif->tif_dir.td_minsamplevalue))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_MAXSAMPLEVALUE))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_MAXSAMPLEVALUE,tif->tif_dir.td_maxsamplevalue))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_PLANARCONFIG))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_PLANARCONFIG,tif->tif_dir.td_planarconfig))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_RESOLUTIONUNIT))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_RESOLUTIONUNIT,tif->tif_dir.td_resolutionunit))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_PAGENUMBER))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_PAGENUMBER,2,&tif->tif_dir.td_pagenumber[0]))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_STRIPBYTECOUNTS))
roentgen b75cab
			{
roentgen b75cab
				if (!isTiled(tif))
roentgen b75cab
				{
roentgen b75cab
					if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))
roentgen b75cab
						goto bad;
roentgen b75cab
				}
roentgen b75cab
				else
roentgen b75cab
				{
roentgen b75cab
					if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEBYTECOUNTS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripbytecount))
roentgen b75cab
						goto bad;
roentgen b75cab
				}
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_STRIPOFFSETS))
roentgen b75cab
			{
roentgen b75cab
				if (!isTiled(tif))
roentgen b75cab
				{
roentgen b75cab
					if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_STRIPOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))
roentgen b75cab
						goto bad;
roentgen b75cab
				}
roentgen b75cab
				else
roentgen b75cab
				{
roentgen b75cab
					if (!TIFFWriteDirectoryTagLongLong8Array(tif,&ndir,dir,TIFFTAG_TILEOFFSETS,tif->tif_dir.td_nstrips,tif->tif_dir.td_stripoffset))
roentgen b75cab
						goto bad;
roentgen b75cab
				}
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_COLORMAP))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagColormap(tif,&ndir,dir))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_EXTRASAMPLES))
roentgen b75cab
			{
roentgen b75cab
				if (tif->tif_dir.td_extrasamples)
roentgen b75cab
				{
roentgen b75cab
					uint16 na;
roentgen b75cab
					uint16* nb;
roentgen b75cab
					TIFFGetFieldDefaulted(tif,TIFFTAG_EXTRASAMPLES,&na,&nb);
roentgen b75cab
					if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_EXTRASAMPLES,na,nb))
roentgen b75cab
						goto bad;
roentgen b75cab
				}
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_SAMPLEFORMAT))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShortPerSample(tif,&ndir,dir,TIFFTAG_SAMPLEFORMAT,tif->tif_dir.td_sampleformat))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_SMINSAMPLEVALUE))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMINSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_sminsamplevalue))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_SMAXSAMPLEVALUE))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagSampleformatArray(tif,&ndir,dir,TIFFTAG_SMAXSAMPLEVALUE,tif->tif_dir.td_samplesperpixel,tif->tif_dir.td_smaxsamplevalue))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_IMAGEDEPTH))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_IMAGEDEPTH,tif->tif_dir.td_imagedepth))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_TILEDEPTH))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,TIFFTAG_TILEDEPTH,tif->tif_dir.td_tiledepth))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_HALFTONEHINTS))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_HALFTONEHINTS,2,&tif->tif_dir.td_halftonehints[0]))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_YCBCRSUBSAMPLING))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,TIFFTAG_YCBCRSUBSAMPLING,2,&tif->tif_dir.td_ycbcrsubsampling[0]))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_YCBCRPOSITIONING))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,TIFFTAG_YCBCRPOSITIONING,tif->tif_dir.td_ycbcrpositioning))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_REFBLACKWHITE))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,TIFFTAG_REFERENCEBLACKWHITE,6,tif->tif_dir.td_refblackwhite))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_TRANSFERFUNCTION))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagTransferfunction(tif,&ndir,dir))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_INKNAMES))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,TIFFTAG_INKNAMES,tif->tif_dir.td_inknameslen,tif->tif_dir.td_inknames))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			if (TIFFFieldSet(tif,FIELD_SUBIFD))
roentgen b75cab
			{
roentgen b75cab
				if (!TIFFWriteDirectoryTagSubifd(tif,&ndir,dir))
roentgen b75cab
					goto bad;
roentgen b75cab
			}
roentgen b75cab
			{
roentgen b75cab
				uint32 n;
roentgen b75cab
				for (n=0; n<tif->tif_nfields; n++) {</tif->
roentgen b75cab
					const TIFFField* o;
roentgen b75cab
					o = tif->tif_fields[n];
roentgen b75cab
					if ((o->field_bit>=FIELD_CODEC)&&(TIFFFieldSet(tif,o->field_bit)))
roentgen b75cab
					{
roentgen b75cab
						switch (o->get_field_type)
roentgen b75cab
						{
roentgen b75cab
							case TIFF_SETGET_ASCII:
roentgen b75cab
								{
roentgen b75cab
									uint32 pa;
roentgen b75cab
									char* pb;
roentgen b75cab
									assert(o->field_type==TIFF_ASCII);
roentgen b75cab
									assert(o->field_readcount==TIFF_VARIABLE);
roentgen b75cab
									assert(o->field_passcount==0);
roentgen b75cab
									TIFFGetField(tif,o->field_tag,&pb);
roentgen b75cab
									pa=(uint32)(strlen(pb));
roentgen b75cab
									if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,o->field_tag,pa,pb))
roentgen b75cab
										goto bad;
roentgen b75cab
								}
roentgen b75cab
								break;
roentgen b75cab
							case TIFF_SETGET_UINT16:
roentgen b75cab
								{
roentgen b75cab
									uint16 p;
roentgen b75cab
									assert(o->field_type==TIFF_SHORT);
roentgen b75cab
									assert(o->field_readcount==1);
roentgen b75cab
									assert(o->field_passcount==0);
roentgen b75cab
									TIFFGetField(tif,o->field_tag,&p);
roentgen b75cab
									if (!TIFFWriteDirectoryTagShort(tif,&ndir,dir,o->field_tag,p))
roentgen b75cab
										goto bad;
roentgen b75cab
								}
roentgen b75cab
								break;
roentgen b75cab
							case TIFF_SETGET_UINT32:
roentgen b75cab
								{
roentgen b75cab
									uint32 p;
roentgen b75cab
									assert(o->field_type==TIFF_LONG);
roentgen b75cab
									assert(o->field_readcount==1);
roentgen b75cab
									assert(o->field_passcount==0);
roentgen b75cab
									TIFFGetField(tif,o->field_tag,&p);
roentgen b75cab
									if (!TIFFWriteDirectoryTagLong(tif,&ndir,dir,o->field_tag,p))
roentgen b75cab
										goto bad;
roentgen b75cab
								}
roentgen b75cab
								break;
roentgen b75cab
							case TIFF_SETGET_C32_UINT8:
roentgen b75cab
								{
roentgen b75cab
									uint32 pa;
roentgen b75cab
									void* pb;
roentgen b75cab
									assert(o->field_type==TIFF_UNDEFINED);
roentgen b75cab
									assert(o->field_readcount==TIFF_VARIABLE2);
roentgen b75cab
									assert(o->field_passcount==1);
roentgen b75cab
									TIFFGetField(tif,o->field_tag,&pa,&pb);
roentgen b75cab
									if (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,o->field_tag,pa,pb))
roentgen b75cab
										goto bad;
roentgen b75cab
								}
roentgen b75cab
								break;
roentgen b75cab
							default:
roentgen b75cab
								assert(0);   /* we should never get here */
roentgen b75cab
								break;
roentgen b75cab
						}
roentgen b75cab
					}
roentgen b75cab
				}
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		for (m=0; m<(uint32)(tif->tif_dir.td_customValueCount); m++)
roentgen b75cab
		{
roentgen b75cab
			switch (tif->tif_dir.td_customValues[m].info->field_type)
roentgen b75cab
			{
roentgen b75cab
				case TIFF_ASCII:
roentgen b75cab
					if (!TIFFWriteDirectoryTagAscii(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
roentgen b75cab
						goto bad;
roentgen b75cab
					break;
roentgen b75cab
				case TIFF_UNDEFINED:
roentgen b75cab
					if (!TIFFWriteDirectoryTagUndefinedArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
roentgen b75cab
						goto bad;
roentgen b75cab
					break;
roentgen b75cab
				case TIFF_BYTE:
roentgen b75cab
					if (!TIFFWriteDirectoryTagByteArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
roentgen b75cab
						goto bad;
roentgen b75cab
					break;
roentgen b75cab
				case TIFF_SBYTE:
roentgen b75cab
					if (!TIFFWriteDirectoryTagSbyteArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
roentgen b75cab
						goto bad;
roentgen b75cab
					break;
roentgen b75cab
				case TIFF_SHORT:
roentgen b75cab
					if (!TIFFWriteDirectoryTagShortArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
roentgen b75cab
						goto bad;
roentgen b75cab
					break;
roentgen b75cab
				case TIFF_SSHORT:
roentgen b75cab
					if (!TIFFWriteDirectoryTagSshortArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
roentgen b75cab
						goto bad;
roentgen b75cab
					break;
roentgen b75cab
				case TIFF_LONG:
roentgen b75cab
					if (!TIFFWriteDirectoryTagLongArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
roentgen b75cab
						goto bad;
roentgen b75cab
					break;
roentgen b75cab
				case TIFF_SLONG:
roentgen b75cab
					if (!TIFFWriteDirectoryTagSlongArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
roentgen b75cab
						goto bad;
roentgen b75cab
					break;
roentgen b75cab
				case TIFF_LONG8:
roentgen b75cab
					if (!TIFFWriteDirectoryTagLong8Array(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
roentgen b75cab
						goto bad;
roentgen b75cab
					break;
roentgen b75cab
				case TIFF_SLONG8:
roentgen b75cab
					if (!TIFFWriteDirectoryTagSlong8Array(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
roentgen b75cab
						goto bad;
roentgen b75cab
					break;
roentgen b75cab
				case TIFF_RATIONAL:
roentgen b75cab
					if (!TIFFWriteDirectoryTagRationalArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
roentgen b75cab
						goto bad;
roentgen b75cab
					break;
roentgen b75cab
				case TIFF_SRATIONAL:
roentgen b75cab
					if (!TIFFWriteDirectoryTagSrationalArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
roentgen b75cab
						goto bad;
roentgen b75cab
					break;
roentgen b75cab
				case TIFF_FLOAT:
roentgen b75cab
					if (!TIFFWriteDirectoryTagFloatArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
roentgen b75cab
						goto bad;
roentgen b75cab
					break;
roentgen b75cab
				case TIFF_DOUBLE:
roentgen b75cab
					if (!TIFFWriteDirectoryTagDoubleArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
roentgen b75cab
						goto bad;
roentgen b75cab
					break;
roentgen b75cab
				case TIFF_IFD:
roentgen b75cab
					if (!TIFFWriteDirectoryTagIfdArray(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
roentgen b75cab
						goto bad;
roentgen b75cab
					break;
roentgen b75cab
				case TIFF_IFD8:
roentgen b75cab
					if (!TIFFWriteDirectoryTagIfdIfd8Array(tif,&ndir,dir,tif->tif_dir.td_customValues[m].info->field_tag,tif->tif_dir.td_customValues[m].count,tif->tif_dir.td_customValues[m].value))
roentgen b75cab
						goto bad;
roentgen b75cab
					break;
roentgen b75cab
				default:
roentgen b75cab
					assert(0);   /* we should never get here */
roentgen b75cab
					break;
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		if (dir!=NULL)
roentgen b75cab
			break;
roentgen b75cab
		dir=_TIFFmalloc(ndir*sizeof(TIFFDirEntry));
roentgen b75cab
		if (dir==NULL)
roentgen b75cab
		{
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
			goto bad;
roentgen b75cab
		}
roentgen b75cab
		if (isimage)
roentgen b75cab
		{
roentgen b75cab
			if ((tif->tif_diroff==0)&&(!TIFFLinkDirectory(tif)))
roentgen b75cab
				goto bad;
roentgen b75cab
		}
roentgen b75cab
		else
roentgen b75cab
			tif->tif_diroff=(TIFFSeekFile(tif,0,SEEK_END)+1)&(~1);
roentgen b75cab
		if (pdiroff!=NULL)
roentgen b75cab
			*pdiroff=tif->tif_diroff;
roentgen b75cab
		if (!(tif->tif_flags&TIFF_BIGTIFF))
roentgen b75cab
			dirsize=2+ndir*12+4;
roentgen b75cab
		else
roentgen b75cab
			dirsize=8+ndir*20+8;
roentgen b75cab
		tif->tif_dataoff=tif->tif_diroff+dirsize;
roentgen b75cab
		if (!(tif->tif_flags&TIFF_BIGTIFF))
roentgen b75cab
			tif->tif_dataoff=(uint32)tif->tif_dataoff;
roentgen b75cab
		if ((tif->tif_dataoff<tif->tif_diroff)||(tif->tif_dataoff<(uint64)dirsize))</tif->
roentgen b75cab
		{
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");
roentgen b75cab
			goto bad;
roentgen b75cab
		}
roentgen b75cab
		if (tif->tif_dataoff&1)
roentgen b75cab
			tif->tif_dataoff++;
roentgen b75cab
		if (isimage)
roentgen b75cab
			tif->tif_curdir++;
roentgen b75cab
	}
roentgen b75cab
	if (isimage)
roentgen b75cab
	{
roentgen b75cab
		if (TIFFFieldSet(tif,FIELD_SUBIFD)&&(tif->tif_subifdoff==0))
roentgen b75cab
		{
roentgen b75cab
			uint32 na;
roentgen b75cab
			TIFFDirEntry* nb;
roentgen b75cab
			for (na=0, nb=dir; ; na++, nb++)
roentgen b75cab
			{
roentgen b75cab
				assert(na
roentgen b75cab
				if (nb->tdir_tag==TIFFTAG_SUBIFD)
roentgen b75cab
					break;
roentgen b75cab
			}
roentgen b75cab
			if (!(tif->tif_flags&TIFF_BIGTIFF))
roentgen b75cab
				tif->tif_subifdoff=tif->tif_diroff+2+na*12+8;
roentgen b75cab
			else
roentgen b75cab
				tif->tif_subifdoff=tif->tif_diroff+8+na*20+12;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	dirmem=_TIFFmalloc(dirsize);
roentgen b75cab
	if (dirmem==NULL)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
		goto bad;
roentgen b75cab
	}
roentgen b75cab
	if (!(tif->tif_flags&TIFF_BIGTIFF))
roentgen b75cab
	{
roentgen b75cab
		uint8* n;
roentgen b75cab
		uint32 nTmp;
roentgen b75cab
		TIFFDirEntry* o;
roentgen b75cab
		n=dirmem;
roentgen b75cab
		*(uint16*)n=ndir;
roentgen b75cab
		if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
			TIFFSwabShort((uint16*)n);
roentgen b75cab
		n+=2;
roentgen b75cab
		o=dir;
roentgen b75cab
		for (m=0; m
roentgen b75cab
		{
roentgen b75cab
			*(uint16*)n=o->tdir_tag;
roentgen b75cab
			if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
				TIFFSwabShort((uint16*)n);
roentgen b75cab
			n+=2;
roentgen b75cab
			*(uint16*)n=o->tdir_type;
roentgen b75cab
			if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
				TIFFSwabShort((uint16*)n);
roentgen b75cab
			n+=2;
roentgen b75cab
			nTmp = (uint32)o->tdir_count;
roentgen b75cab
			_TIFFmemcpy(n,&nTmp,4);
roentgen b75cab
			if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
				TIFFSwabLong((uint32*)n);
roentgen b75cab
			n+=4;
roentgen b75cab
			/* This is correct. The data has been */
roentgen b75cab
			/* swabbed previously in TIFFWriteDirectoryTagData */
roentgen b75cab
			_TIFFmemcpy(n,&o->tdir_offset,4);
roentgen b75cab
			n+=4;
roentgen b75cab
			o++;
roentgen b75cab
		}
roentgen b75cab
		nTmp = (uint32)tif->tif_nextdiroff;
roentgen b75cab
		if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
			TIFFSwabLong(&nTmp);
roentgen b75cab
		_TIFFmemcpy(n,&nTmp,4);
roentgen b75cab
	}
roentgen b75cab
	else
roentgen b75cab
	{
roentgen b75cab
		uint8* n;
roentgen b75cab
		TIFFDirEntry* o;
roentgen b75cab
		n=dirmem;
roentgen b75cab
		*(uint64*)n=ndir;
roentgen b75cab
		if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
			TIFFSwabLong8((uint64*)n);
roentgen b75cab
		n+=8;
roentgen b75cab
		o=dir;
roentgen b75cab
		for (m=0; m
roentgen b75cab
		{
roentgen b75cab
			*(uint16*)n=o->tdir_tag;
roentgen b75cab
			if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
				TIFFSwabShort((uint16*)n);
roentgen b75cab
			n+=2;
roentgen b75cab
			*(uint16*)n=o->tdir_type;
roentgen b75cab
			if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
				TIFFSwabShort((uint16*)n);
roentgen b75cab
			n+=2;
roentgen b75cab
			_TIFFmemcpy(n,&o->tdir_count,8);
roentgen b75cab
			if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
				TIFFSwabLong8((uint64*)n);
roentgen b75cab
			n+=8;
roentgen b75cab
			_TIFFmemcpy(n,&o->tdir_offset,8);
roentgen b75cab
			n+=8;
roentgen b75cab
			o++;
roentgen b75cab
		}
roentgen b75cab
		_TIFFmemcpy(n,&tif->tif_nextdiroff,8);
roentgen b75cab
		if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
			TIFFSwabLong8((uint64*)n);
roentgen b75cab
	}
roentgen b75cab
	_TIFFfree(dir);
roentgen b75cab
	dir=NULL;
roentgen b75cab
	if (!SeekOK(tif,tif->tif_diroff))
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");
roentgen b75cab
		goto bad;
roentgen b75cab
	}
roentgen b75cab
	if (!WriteOK(tif,dirmem,(tmsize_t)dirsize))
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"IO error writing directory");
roentgen b75cab
		goto bad;
roentgen b75cab
	}
roentgen b75cab
	_TIFFfree(dirmem);
roentgen b75cab
	if (imagedone)
roentgen b75cab
	{
roentgen b75cab
		TIFFFreeDirectory(tif);
roentgen b75cab
		tif->tif_flags &= ~TIFF_DIRTYDIRECT;
roentgen b75cab
		tif->tif_flags &= ~TIFF_DIRTYSTRIP;
roentgen b75cab
		(*tif->tif_cleanup)(tif);
roentgen b75cab
		/*
roentgen b75cab
		* Reset directory-related state for subsequent
roentgen b75cab
		* directories.
roentgen b75cab
		*/
roentgen b75cab
		TIFFCreateDirectory(tif);
roentgen b75cab
	}
roentgen b75cab
	return(1);
roentgen b75cab
bad:
roentgen b75cab
	if (dir!=NULL)
roentgen b75cab
		_TIFFfree(dir);
roentgen b75cab
	if (dirmem!=NULL)
roentgen b75cab
		_TIFFfree(dirmem);
roentgen b75cab
	return(0);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagSampleformatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteDirectoryTagSampleformatArray";
roentgen b75cab
	void* conv;
roentgen b75cab
	uint32 i;
roentgen b75cab
	int ok;
roentgen b75cab
	conv = _TIFFmalloc(count*sizeof(double));
roentgen b75cab
	if (conv == NULL)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata, module, "Out of memory");
roentgen b75cab
		return (0);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	switch (tif->tif_dir.td_sampleformat)
roentgen b75cab
	{
roentgen b75cab
		case SAMPLEFORMAT_IEEEFP:
roentgen b75cab
			if (tif->tif_dir.td_bitspersample<=32)
roentgen b75cab
			{
roentgen b75cab
				for (i = 0; i < count; ++i)
roentgen b75cab
					((float*)conv)[i] = (float)value[i];
roentgen b75cab
				ok = TIFFWriteDirectoryTagFloatArray(tif,ndir,dir,tag,count,(float*)conv);
roentgen b75cab
			}
roentgen b75cab
			else
roentgen b75cab
			{
roentgen b75cab
				ok = TIFFWriteDirectoryTagDoubleArray(tif,ndir,dir,tag,count,value);
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		case SAMPLEFORMAT_INT:
roentgen b75cab
			if (tif->tif_dir.td_bitspersample<=8)
roentgen b75cab
			{
roentgen b75cab
				for (i = 0; i < count; ++i)
roentgen b75cab
					((int8*)conv)[i] = (int8)value[i];
roentgen b75cab
				ok = TIFFWriteDirectoryTagSbyteArray(tif,ndir,dir,tag,count,(int8*)conv);
roentgen b75cab
			}
roentgen b75cab
			else if (tif->tif_dir.td_bitspersample<=16)
roentgen b75cab
			{
roentgen b75cab
				for (i = 0; i < count; ++i)
roentgen b75cab
					((int16*)conv)[i] = (int16)value[i];
roentgen b75cab
				ok = TIFFWriteDirectoryTagSshortArray(tif,ndir,dir,tag,count,(int16*)conv);
roentgen b75cab
			}
roentgen b75cab
			else
roentgen b75cab
			{
roentgen b75cab
				for (i = 0; i < count; ++i)
roentgen b75cab
					((int32*)conv)[i] = (int32)value[i];
roentgen b75cab
				ok = TIFFWriteDirectoryTagSlongArray(tif,ndir,dir,tag,count,(int32*)conv);
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		case SAMPLEFORMAT_UINT:
roentgen b75cab
			if (tif->tif_dir.td_bitspersample<=8)
roentgen b75cab
			{
roentgen b75cab
				for (i = 0; i < count; ++i)
roentgen b75cab
					((uint8*)conv)[i] = (uint8)value[i];
roentgen b75cab
				ok = TIFFWriteDirectoryTagByteArray(tif,ndir,dir,tag,count,(uint8*)conv);
roentgen b75cab
			}
roentgen b75cab
			else if (tif->tif_dir.td_bitspersample<=16)
roentgen b75cab
			{
roentgen b75cab
				for (i = 0; i < count; ++i)
roentgen b75cab
					((uint16*)conv)[i] = (uint16)value[i];
roentgen b75cab
				ok = TIFFWriteDirectoryTagShortArray(tif,ndir,dir,tag,count,(uint16*)conv);
roentgen b75cab
			}
roentgen b75cab
			else
roentgen b75cab
			{
roentgen b75cab
				for (i = 0; i < count; ++i)
roentgen b75cab
					((uint32*)conv)[i] = (uint32)value[i];
roentgen b75cab
				ok = TIFFWriteDirectoryTagLongArray(tif,ndir,dir,tag,count,(uint32*)conv);
roentgen b75cab
			}
roentgen b75cab
			break;
roentgen b75cab
		default:
roentgen b75cab
			ok = 0;
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	_TIFFfree(conv);
roentgen b75cab
	return (ok);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#if 0
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagSampleformatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
roentgen b75cab
{
roentgen b75cab
	switch (tif->tif_dir.td_sampleformat)
roentgen b75cab
	{
roentgen b75cab
		case SAMPLEFORMAT_IEEEFP:
roentgen b75cab
			if (tif->tif_dir.td_bitspersample<=32)
roentgen b75cab
				return(TIFFWriteDirectoryTagFloatPerSample(tif,ndir,dir,tag,(float)value));
roentgen b75cab
			else
roentgen b75cab
				return(TIFFWriteDirectoryTagDoublePerSample(tif,ndir,dir,tag,value));
roentgen b75cab
		case SAMPLEFORMAT_INT:
roentgen b75cab
			if (tif->tif_dir.td_bitspersample<=8)
roentgen b75cab
				return(TIFFWriteDirectoryTagSbytePerSample(tif,ndir,dir,tag,(int8)value));
roentgen b75cab
			else if (tif->tif_dir.td_bitspersample<=16)
roentgen b75cab
				return(TIFFWriteDirectoryTagSshortPerSample(tif,ndir,dir,tag,(int16)value));
roentgen b75cab
			else
roentgen b75cab
				return(TIFFWriteDirectoryTagSlongPerSample(tif,ndir,dir,tag,(int32)value));
roentgen b75cab
		case SAMPLEFORMAT_UINT:
roentgen b75cab
			if (tif->tif_dir.td_bitspersample<=8)
roentgen b75cab
				return(TIFFWriteDirectoryTagBytePerSample(tif,ndir,dir,tag,(uint8)value));
roentgen b75cab
			else if (tif->tif_dir.td_bitspersample<=16)
roentgen b75cab
				return(TIFFWriteDirectoryTagShortPerSample(tif,ndir,dir,tag,(uint16)value));
roentgen b75cab
			else
roentgen b75cab
				return(TIFFWriteDirectoryTagLongPerSample(tif,ndir,dir,tag,(uint32)value));
roentgen b75cab
		default:
roentgen b75cab
			return(1);
roentgen b75cab
	}
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedAscii(tif,ndir,dir,tag,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedUndefinedArray(tif,ndir,dir,tag,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedByte(tif,ndir,dir,tag,value));
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedByteArray(tif,ndir,dir,tag,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#if 0
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagBytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteDirectoryTagBytePerSample";
roentgen b75cab
	uint8* m;
roentgen b75cab
	uint8* na;
roentgen b75cab
	uint16 nb;
roentgen b75cab
	int o;
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint8));
roentgen b75cab
	if (m==NULL)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
		return(0);
roentgen b75cab
	}
roentgen b75cab
	for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)</tif->
roentgen b75cab
		*na=value;
roentgen b75cab
	o=TIFFWriteDirectoryTagCheckedByteArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
roentgen b75cab
	_TIFFfree(m);
roentgen b75cab
	return(o);
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedSbyte(tif,ndir,dir,tag,value));
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedSbyteArray(tif,ndir,dir,tag,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#if 0
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagSbytePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteDirectoryTagSbytePerSample";
roentgen b75cab
	int8* m;
roentgen b75cab
	int8* na;
roentgen b75cab
	uint16 nb;
roentgen b75cab
	int o;
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int8));
roentgen b75cab
	if (m==NULL)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
		return(0);
roentgen b75cab
	}
roentgen b75cab
	for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)</tif->
roentgen b75cab
		*na=value;
roentgen b75cab
	o=TIFFWriteDirectoryTagCheckedSbyteArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
roentgen b75cab
	_TIFFfree(m);
roentgen b75cab
	return(o);
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedShort(tif,ndir,dir,tag,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagShortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteDirectoryTagShortPerSample";
roentgen b75cab
	uint16* m;
roentgen b75cab
	uint16* na;
roentgen b75cab
	uint16 nb;
roentgen b75cab
	int o;
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint16));
roentgen b75cab
	if (m==NULL)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
		return(0);
roentgen b75cab
	}
roentgen b75cab
	for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)</tif->
roentgen b75cab
		*na=value;
roentgen b75cab
	o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
roentgen b75cab
	_TIFFfree(m);
roentgen b75cab
	return(o);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedSshort(tif,ndir,dir,tag,value));
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedSshortArray(tif,ndir,dir,tag,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#if 0
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagSshortPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteDirectoryTagSshortPerSample";
roentgen b75cab
	int16* m;
roentgen b75cab
	int16* na;
roentgen b75cab
	uint16 nb;
roentgen b75cab
	int o;
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int16));
roentgen b75cab
	if (m==NULL)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
		return(0);
roentgen b75cab
	}
roentgen b75cab
	for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)</tif->
roentgen b75cab
		*na=value;
roentgen b75cab
	o=TIFFWriteDirectoryTagCheckedSshortArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
roentgen b75cab
	_TIFFfree(m);
roentgen b75cab
	return(o);
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedLong(tif,ndir,dir,tag,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#if 0
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagLongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteDirectoryTagLongPerSample";
roentgen b75cab
	uint32* m;
roentgen b75cab
	uint32* na;
roentgen b75cab
	uint16 nb;
roentgen b75cab
	int o;
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(uint32));
roentgen b75cab
	if (m==NULL)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
		return(0);
roentgen b75cab
	}
roentgen b75cab
	for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)</tif->
roentgen b75cab
		*na=value;
roentgen b75cab
	o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
roentgen b75cab
	_TIFFfree(m);
roentgen b75cab
	return(o);
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedSlong(tif,ndir,dir,tag,value));
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedSlongArray(tif,ndir,dir,tag,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#if 0
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagSlongPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteDirectoryTagSlongPerSample";
roentgen b75cab
	int32* m;
roentgen b75cab
	int32* na;
roentgen b75cab
	uint16 nb;
roentgen b75cab
	int o;
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(int32));
roentgen b75cab
	if (m==NULL)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
		return(0);
roentgen b75cab
	}
roentgen b75cab
	for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)</tif->
roentgen b75cab
		*na=value;
roentgen b75cab
	o=TIFFWriteDirectoryTagCheckedSlongArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
roentgen b75cab
	_TIFFfree(m);
roentgen b75cab
	return(o);
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedLong8(tif,ndir,dir,tag,value));
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,tag,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedSlong8(tif,ndir,dir,tag,value));
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedSlong8Array(tif,ndir,dir,tag,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedRational(tif,ndir,dir,tag,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedRationalArray(tif,ndir,dir,tag,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedSrationalArray(tif,ndir,dir,tag,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedFloat(tif,ndir,dir,tag,value));
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int TIFFWriteDirectoryTagFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedFloatArray(tif,ndir,dir,tag,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#if 0
roentgen b75cab
static int TIFFWriteDirectoryTagFloatPerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteDirectoryTagFloatPerSample";
roentgen b75cab
	float* m;
roentgen b75cab
	float* na;
roentgen b75cab
	uint16 nb;
roentgen b75cab
	int o;
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(float));
roentgen b75cab
	if (m==NULL)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
		return(0);
roentgen b75cab
	}
roentgen b75cab
	for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)</tif->
roentgen b75cab
		*na=value;
roentgen b75cab
	o=TIFFWriteDirectoryTagCheckedFloatArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
roentgen b75cab
	_TIFFfree(m);
roentgen b75cab
	return(o);
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int TIFFWriteDirectoryTagDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedDouble(tif,ndir,dir,tag,value));
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int TIFFWriteDirectoryTagDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedDoubleArray(tif,ndir,dir,tag,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#if 0
roentgen b75cab
static int TIFFWriteDirectoryTagDoublePerSample(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteDirectoryTagDoublePerSample";
roentgen b75cab
	double* m;
roentgen b75cab
	double* na;
roentgen b75cab
	uint16 nb;
roentgen b75cab
	int o;
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	m=_TIFFmalloc(tif->tif_dir.td_samplesperpixel*sizeof(double));
roentgen b75cab
	if (m==NULL)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
		return(0);
roentgen b75cab
	}
roentgen b75cab
	for (na=m, nb=0; nb<tif->tif_dir.td_samplesperpixel; na++, nb++)</tif->
roentgen b75cab
		*na=value;
roentgen b75cab
	o=TIFFWriteDirectoryTagCheckedDoubleArray(tif,ndir,dir,tag,tif->tif_dir.td_samplesperpixel,m);
roentgen b75cab
	_TIFFfree(m);
roentgen b75cab
	return(o);
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,tag,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,tag,count,value));
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagShortLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
roentgen b75cab
{
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	if (value<=0xFFFF)
roentgen b75cab
		return(TIFFWriteDirectoryTagCheckedShort(tif,ndir,dir,tag,(uint16)value));
roentgen b75cab
	else
roentgen b75cab
		return(TIFFWriteDirectoryTagCheckedLong(tif,ndir,dir,tag,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/************************************************************************/
roentgen b75cab
/*                TIFFWriteDirectoryTagLongLong8Array()                 */
roentgen b75cab
/*                                                                      */
roentgen b75cab
/*      Write out LONG8 array as LONG8 for BigTIFF or LONG for          */
roentgen b75cab
/*      Classic TIFF with some checking.                                */
roentgen b75cab
/************************************************************************/
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
roentgen b75cab
{
roentgen b75cab
    static const char module[] = "TIFFWriteDirectoryTagLongLong8Array";
roentgen b75cab
    uint64* ma;
roentgen b75cab
    uint32 mb;
roentgen b75cab
    uint32* p;
roentgen b75cab
    uint32* q;
roentgen b75cab
    int o;
roentgen b75cab
roentgen b75cab
    /* is this just a counting pass? */
roentgen b75cab
    if (dir==NULL)
roentgen b75cab
    {
roentgen b75cab
        (*ndir)++;
roentgen b75cab
        return(1);
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    /* We always write LONG8 for BigTIFF, no checking needed. */
roentgen b75cab
    if( tif->tif_flags&TIFF_BIGTIFF )
roentgen b75cab
        return TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,
roentgen b75cab
                                                      tag,count,value);
roentgen b75cab
roentgen b75cab
    /*
roentgen b75cab
    ** For classic tiff we want to verify everything is in range for LONG
roentgen b75cab
    ** and convert to long format.
roentgen b75cab
    */
roentgen b75cab
roentgen b75cab
    p = _TIFFmalloc(count*sizeof(uint32));
roentgen b75cab
    if (p==NULL)
roentgen b75cab
    {
roentgen b75cab
        TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
        return(0);
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    for (q=p, ma=value, mb=0; mb
roentgen b75cab
    {
roentgen b75cab
        if (*ma>0xFFFFFFFF)
roentgen b75cab
        {
roentgen b75cab
            TIFFErrorExt(tif->tif_clientdata,module,
roentgen b75cab
                         "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
roentgen b75cab
            _TIFFfree(p);
roentgen b75cab
            return(0);
roentgen b75cab
        }
roentgen b75cab
        *q= (uint32)(*ma);
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,p);
roentgen b75cab
    _TIFFfree(p);
roentgen b75cab
roentgen b75cab
    return(o);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/************************************************************************/
roentgen b75cab
/*                 TIFFWriteDirectoryTagIfdIfd8Array()                  */
roentgen b75cab
/*                                                                      */
roentgen b75cab
/*      Write either IFD8 or IFD array depending on file type.          */
roentgen b75cab
/************************************************************************/
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagIfdIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
roentgen b75cab
{
roentgen b75cab
    static const char module[] = "TIFFWriteDirectoryTagIfdIfd8Array";
roentgen b75cab
    uint64* ma;
roentgen b75cab
    uint32 mb;
roentgen b75cab
    uint32* p;
roentgen b75cab
    uint32* q;
roentgen b75cab
    int o;
roentgen b75cab
roentgen b75cab
    /* is this just a counting pass? */
roentgen b75cab
    if (dir==NULL)
roentgen b75cab
    {
roentgen b75cab
        (*ndir)++;
roentgen b75cab
        return(1);
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    /* We always write IFD8 for BigTIFF, no checking needed. */
roentgen b75cab
    if( tif->tif_flags&TIFF_BIGTIFF )
roentgen b75cab
        return TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,
roentgen b75cab
                                                     tag,count,value);
roentgen b75cab
roentgen b75cab
    /*
roentgen b75cab
    ** For classic tiff we want to verify everything is in range for IFD
roentgen b75cab
    ** and convert to long format.
roentgen b75cab
    */
roentgen b75cab
roentgen b75cab
    p = _TIFFmalloc(count*sizeof(uint32));
roentgen b75cab
    if (p==NULL)
roentgen b75cab
    {
roentgen b75cab
        TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
        return(0);
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    for (q=p, ma=value, mb=0; mb
roentgen b75cab
    {
roentgen b75cab
        if (*ma>0xFFFFFFFF)
roentgen b75cab
        {
roentgen b75cab
            TIFFErrorExt(tif->tif_clientdata,module,
roentgen b75cab
                         "Attempt to write value larger than 0xFFFFFFFF in Classic TIFF file.");
roentgen b75cab
            _TIFFfree(p);
roentgen b75cab
            return(0);
roentgen b75cab
        }
roentgen b75cab
        *q= (uint32)(*ma);
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    o=TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,tag,count,p);
roentgen b75cab
    _TIFFfree(p);
roentgen b75cab
roentgen b75cab
    return(o);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagShortLongLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteDirectoryTagShortLongLong8Array";
roentgen b75cab
	uint64* ma;
roentgen b75cab
	uint32 mb;
roentgen b75cab
	uint8 n;
roentgen b75cab
	int o;
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	n=0;
roentgen b75cab
	for (ma=value, mb=0; mb
roentgen b75cab
	{
roentgen b75cab
		if ((n==0)&&(*ma>0xFFFF))
roentgen b75cab
			n=1;
roentgen b75cab
		if ((n==1)&&(*ma>0xFFFFFFFF))
roentgen b75cab
		{
roentgen b75cab
			n=2;
roentgen b75cab
			break;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	if (n==0)
roentgen b75cab
	{
roentgen b75cab
		uint16* p;
roentgen b75cab
		uint16* q;
roentgen b75cab
		p=_TIFFmalloc(count*sizeof(uint16));
roentgen b75cab
		if (p==NULL)
roentgen b75cab
		{
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
			return(0);
roentgen b75cab
		}
roentgen b75cab
		for (ma=value, mb=0, q=p; mb
roentgen b75cab
			*q=(uint16)(*ma);
roentgen b75cab
		o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,tag,count,p);
roentgen b75cab
		_TIFFfree(p);
roentgen b75cab
	}
roentgen b75cab
	else if (n==1)
roentgen b75cab
	{
roentgen b75cab
		uint32* p;
roentgen b75cab
		uint32* q;
roentgen b75cab
		p=_TIFFmalloc(count*sizeof(uint32));
roentgen b75cab
		if (p==NULL)
roentgen b75cab
		{
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
			return(0);
roentgen b75cab
		}
roentgen b75cab
		for (ma=value, mb=0, q=p; mb
roentgen b75cab
			*q=(uint32)(*ma);
roentgen b75cab
		o=TIFFWriteDirectoryTagCheckedLongArray(tif,ndir,dir,tag,count,p);
roentgen b75cab
		_TIFFfree(p);
roentgen b75cab
	}
roentgen b75cab
	else
roentgen b75cab
	{
roentgen b75cab
		assert(n==2);
roentgen b75cab
		o=TIFFWriteDirectoryTagCheckedLong8Array(tif,ndir,dir,tag,count,value);
roentgen b75cab
	}
roentgen b75cab
	return(o);
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagColormap(TIFF* tif, uint32* ndir, TIFFDirEntry* dir)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteDirectoryTagColormap";
roentgen b75cab
	uint32 m;
roentgen b75cab
	uint16* n;
roentgen b75cab
	int o;
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	m=(1<<tif->tif_dir.td_bitspersample);</tif->
roentgen b75cab
	n=_TIFFmalloc(3*m*sizeof(uint16));
roentgen b75cab
	if (n==NULL)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
		return(0);
roentgen b75cab
	}
roentgen b75cab
	_TIFFmemcpy(&n[0],tif->tif_dir.td_colormap[0],m*sizeof(uint16));
roentgen b75cab
	_TIFFmemcpy(&n[m],tif->tif_dir.td_colormap[1],m*sizeof(uint16));
roentgen b75cab
	_TIFFmemcpy(&n[2*m],tif->tif_dir.td_colormap[2],m*sizeof(uint16));
roentgen b75cab
	o=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,TIFFTAG_COLORMAP,3*m,n);
roentgen b75cab
	_TIFFfree(n);
roentgen b75cab
	return(o);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagTransferfunction(TIFF* tif, uint32* ndir, TIFFDirEntry* dir)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteDirectoryTagTransferfunction";
roentgen b75cab
	uint32 m;
roentgen b75cab
	uint16 n;
roentgen b75cab
	uint16* o;
roentgen b75cab
	int p;
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	m=(1<<tif->tif_dir.td_bitspersample);</tif->
roentgen b75cab
	n=tif->tif_dir.td_samplesperpixel-tif->tif_dir.td_extrasamples;
roentgen b75cab
	/*
roentgen b75cab
	 * Check if the table can be written as a single column,
roentgen b75cab
	 * or if it must be written as 3 columns.  Note that we
roentgen b75cab
	 * write a 3-column tag if there are 2 samples/pixel and
roentgen b75cab
	 * a single column of data won't suffice--hmm.
roentgen b75cab
	 */
roentgen b75cab
	if (n>3)
roentgen b75cab
		n=3;
roentgen b75cab
	if (n==3)
roentgen b75cab
	{
roentgen b75cab
		if (!_TIFFmemcmp(tif->tif_dir.td_transferfunction[0],tif->tif_dir.td_transferfunction[2],m*sizeof(uint16)))
roentgen b75cab
			n=2;
roentgen b75cab
	}
roentgen b75cab
	if (n==2)
roentgen b75cab
	{
roentgen b75cab
		if (!_TIFFmemcmp(tif->tif_dir.td_transferfunction[0],tif->tif_dir.td_transferfunction[1],m*sizeof(uint16)))
roentgen b75cab
			n=1;
roentgen b75cab
	}
roentgen b75cab
	if (n==0)
roentgen b75cab
		n=1;
roentgen b75cab
	o=_TIFFmalloc(n*m*sizeof(uint16));
roentgen b75cab
	if (o==NULL)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
		return(0);
roentgen b75cab
	}
roentgen b75cab
	_TIFFmemcpy(&o[0],tif->tif_dir.td_transferfunction[0],m*sizeof(uint16));
roentgen b75cab
	if (n>1)
roentgen b75cab
		_TIFFmemcpy(&o[m],tif->tif_dir.td_transferfunction[1],m*sizeof(uint16));
roentgen b75cab
	if (n>2)
roentgen b75cab
		_TIFFmemcpy(&o[2*m],tif->tif_dir.td_transferfunction[2],m*sizeof(uint16));
roentgen b75cab
	p=TIFFWriteDirectoryTagCheckedShortArray(tif,ndir,dir,TIFFTAG_TRANSFERFUNCTION,n*m,o);
roentgen b75cab
	_TIFFfree(o);
roentgen b75cab
	return(p);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagSubifd(TIFF* tif, uint32* ndir, TIFFDirEntry* dir)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteDirectoryTagSubifd";
roentgen b75cab
	uint64 m;
roentgen b75cab
	int n;
roentgen b75cab
	if (tif->tif_dir.td_nsubifd==0)
roentgen b75cab
		return(1);
roentgen b75cab
	if (dir==NULL)
roentgen b75cab
	{
roentgen b75cab
		(*ndir)++;
roentgen b75cab
		return(1);
roentgen b75cab
	}
roentgen b75cab
	m=tif->tif_dataoff;
roentgen b75cab
	if (!(tif->tif_flags&TIFF_BIGTIFF))
roentgen b75cab
	{
roentgen b75cab
		uint32* o;
roentgen b75cab
		uint64* pa;
roentgen b75cab
		uint32* pb;
roentgen b75cab
		uint16 p;
roentgen b75cab
		o=_TIFFmalloc(tif->tif_dir.td_nsubifd*sizeof(uint32));
roentgen b75cab
		if (o==NULL)
roentgen b75cab
		{
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
			return(0);
roentgen b75cab
		}
roentgen b75cab
		pa=tif->tif_dir.td_subifd;
roentgen b75cab
		pb=o;
roentgen b75cab
		for (p=0; p < tif->tif_dir.td_nsubifd; p++)
roentgen b75cab
		{
roentgen b75cab
                        assert(pa != 0);
roentgen b75cab
			assert(*pa <= 0xFFFFFFFFUL);
roentgen b75cab
			*pb++=(uint32)(*pa++);
roentgen b75cab
		}
roentgen b75cab
		n=TIFFWriteDirectoryTagCheckedIfdArray(tif,ndir,dir,TIFFTAG_SUBIFD,tif->tif_dir.td_nsubifd,o);
roentgen b75cab
		_TIFFfree(o);
roentgen b75cab
	}
roentgen b75cab
	else
roentgen b75cab
		n=TIFFWriteDirectoryTagCheckedIfd8Array(tif,ndir,dir,TIFFTAG_SUBIFD,tif->tif_dir.td_nsubifd,tif->tif_dir.td_subifd);
roentgen b75cab
	if (!n)
roentgen b75cab
		return(0);
roentgen b75cab
	/*
roentgen b75cab
	 * Total hack: if this directory includes a SubIFD
roentgen b75cab
	 * tag then force the next <n> directories to be</n>
roentgen b75cab
	 * written as ``sub directories'' of this one.  This
roentgen b75cab
	 * is used to write things like thumbnails and
roentgen b75cab
	 * image masks that one wants to keep out of the
roentgen b75cab
	 * normal directory linkage access mechanism.
roentgen b75cab
	 */
roentgen b75cab
	tif->tif_flags|=TIFF_INSUBIFD;
roentgen b75cab
	tif->tif_nsubifd=tif->tif_dir.td_nsubifd;
roentgen b75cab
	if (tif->tif_dir.td_nsubifd==1)
roentgen b75cab
		tif->tif_subifdoff=0;
roentgen b75cab
	else
roentgen b75cab
		tif->tif_subifdoff=m;
roentgen b75cab
	return(1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedAscii(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, char* value)
roentgen b75cab
{
roentgen b75cab
	assert(sizeof(char)==1);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_ASCII,count,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedUndefinedArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
roentgen b75cab
{
roentgen b75cab
	assert(sizeof(uint8)==1);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_UNDEFINED,count,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedByte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint8 value)
roentgen b75cab
{
roentgen b75cab
	assert(sizeof(uint8)==1);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_BYTE,1,1,&value));
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedByteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint8* value)
roentgen b75cab
{
roentgen b75cab
	assert(sizeof(uint8)==1);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_BYTE,count,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedSbyte(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int8 value)
roentgen b75cab
{
roentgen b75cab
	assert(sizeof(int8)==1);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SBYTE,1,1,&value));
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedSbyteArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int8* value)
roentgen b75cab
{
roentgen b75cab
	assert(sizeof(int8)==1);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SBYTE,count,count,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedShort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 value)
roentgen b75cab
{
roentgen b75cab
	uint16 m;
roentgen b75cab
	assert(sizeof(uint16)==2);
roentgen b75cab
	m=value;
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabShort(&m);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SHORT,1,2,&m));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedShortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint16* value)
roentgen b75cab
{
roentgen b75cab
	assert(count<0x80000000);
roentgen b75cab
	assert(sizeof(uint16)==2);
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabArrayOfShort(value,count);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SHORT,count,count*2,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedSshort(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int16 value)
roentgen b75cab
{
roentgen b75cab
	int16 m;
roentgen b75cab
	assert(sizeof(int16)==2);
roentgen b75cab
	m=value;
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabShort((uint16*)(&m));
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SSHORT,1,2,&m));
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedSshortArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int16* value)
roentgen b75cab
{
roentgen b75cab
	assert(count<0x80000000);
roentgen b75cab
	assert(sizeof(int16)==2);
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabArrayOfShort((uint16*)value,count);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SSHORT,count,count*2,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedLong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 value)
roentgen b75cab
{
roentgen b75cab
	uint32 m;
roentgen b75cab
	assert(sizeof(uint32)==4);
roentgen b75cab
	m=value;
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabLong(&m);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG,1,4,&m));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedLongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
roentgen b75cab
{
roentgen b75cab
	assert(count<0x40000000);
roentgen b75cab
	assert(sizeof(uint32)==4);
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabArrayOfLong(value,count);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG,count,count*4,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedSlong(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int32 value)
roentgen b75cab
{
roentgen b75cab
	int32 m;
roentgen b75cab
	assert(sizeof(int32)==4);
roentgen b75cab
	m=value;
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabLong((uint32*)(&m));
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG,1,4,&m));
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedSlongArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int32* value)
roentgen b75cab
{
roentgen b75cab
	assert(count<0x40000000);
roentgen b75cab
	assert(sizeof(int32)==4);
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabArrayOfLong((uint32*)value,count);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG,count,count*4,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedLong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint64 value)
roentgen b75cab
{
roentgen b75cab
	uint64 m;
roentgen b75cab
	assert(sizeof(uint64)==8);
roentgen b75cab
	assert(tif->tif_flags&TIFF_BIGTIFF);
roentgen b75cab
	m=value;
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabLong8(&m);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG8,1,8,&m));
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedLong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
roentgen b75cab
{
roentgen b75cab
	assert(count<0x20000000);
roentgen b75cab
	assert(sizeof(uint64)==8);
roentgen b75cab
	assert(tif->tif_flags&TIFF_BIGTIFF);
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabArrayOfLong8(value,count);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_LONG8,count,count*8,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedSlong8(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, int64 value)
roentgen b75cab
{
roentgen b75cab
	int64 m;
roentgen b75cab
	assert(sizeof(int64)==8);
roentgen b75cab
	assert(tif->tif_flags&TIFF_BIGTIFF);
roentgen b75cab
	m=value;
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabLong8((uint64*)(&m));
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG8,1,8,&m));
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedSlong8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, int64* value)
roentgen b75cab
{
roentgen b75cab
	assert(count<0x20000000);
roentgen b75cab
	assert(sizeof(int64)==8);
roentgen b75cab
	assert(tif->tif_flags&TIFF_BIGTIFF);
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabArrayOfLong8((uint64*)value,count);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SLONG8,count,count*8,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedRational(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
roentgen b75cab
{
roentgen b75cab
	uint32 m[2];
roentgen b75cab
	assert(value>=0.0);
roentgen b75cab
	assert(sizeof(uint32)==4);
roentgen b75cab
	if (value<=0.0)
roentgen b75cab
	{
roentgen b75cab
		m[0]=0;
roentgen b75cab
		m[1]=1;
roentgen b75cab
	}
roentgen b75cab
	else if (value==(double)(uint32)value)
roentgen b75cab
	{
roentgen b75cab
		m[0]=(uint32)value;
roentgen b75cab
		m[1]=1;
roentgen b75cab
	}
roentgen b75cab
	else if (value<1.0)
roentgen b75cab
	{
roentgen b75cab
		m[0]=(uint32)(value*0xFFFFFFFF);
roentgen b75cab
		m[1]=0xFFFFFFFF;
roentgen b75cab
	}
roentgen b75cab
	else
roentgen b75cab
	{
roentgen b75cab
		m[0]=0xFFFFFFFF;
roentgen b75cab
		m[1]=(uint32)(0xFFFFFFFF/value);
roentgen b75cab
	}
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
	{
roentgen b75cab
		TIFFSwabLong(&m[0]);
roentgen b75cab
		TIFFSwabLong(&m[1]);
roentgen b75cab
	}
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_RATIONAL,1,8,&m[0]));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedRationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteDirectoryTagCheckedRationalArray";
roentgen b75cab
	uint32* m;
roentgen b75cab
	float* na;
roentgen b75cab
	uint32* nb;
roentgen b75cab
	uint32 nc;
roentgen b75cab
	int o;
roentgen b75cab
	assert(sizeof(uint32)==4);
roentgen b75cab
	m=_TIFFmalloc(count*2*sizeof(uint32));
roentgen b75cab
	if (m==NULL)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
		return(0);
roentgen b75cab
	}
roentgen b75cab
	for (na=value, nb=m, nc=0; nc
roentgen b75cab
	{
roentgen b75cab
		if (*na<=0.0)
roentgen b75cab
		{
roentgen b75cab
			nb[0]=0;
roentgen b75cab
			nb[1]=1;
roentgen b75cab
		}
roentgen b75cab
		else if (*na==(float)(uint32)(*na))
roentgen b75cab
		{
roentgen b75cab
			nb[0]=(uint32)(*na);
roentgen b75cab
			nb[1]=1;
roentgen b75cab
		}
roentgen b75cab
		else if (*na<1.0)
roentgen b75cab
		{
roentgen b75cab
			nb[0]=(uint32)((*na)*0xFFFFFFFF);
roentgen b75cab
			nb[1]=0xFFFFFFFF;
roentgen b75cab
		}
roentgen b75cab
		else
roentgen b75cab
		{
roentgen b75cab
			nb[0]=0xFFFFFFFF;
roentgen b75cab
			nb[1]=(uint32)(0xFFFFFFFF/(*na));
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabArrayOfLong(m,count*2);
roentgen b75cab
	o=TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_RATIONAL,count,count*8,&m[0]);
roentgen b75cab
	_TIFFfree(m);
roentgen b75cab
	return(o);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedSrationalArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteDirectoryTagCheckedSrationalArray";
roentgen b75cab
	int32* m;
roentgen b75cab
	float* na;
roentgen b75cab
	int32* nb;
roentgen b75cab
	uint32 nc;
roentgen b75cab
	int o;
roentgen b75cab
	assert(sizeof(int32)==4);
roentgen b75cab
	m=_TIFFmalloc(count*2*sizeof(int32));
roentgen b75cab
	if (m==NULL)
roentgen b75cab
	{
roentgen b75cab
		TIFFErrorExt(tif->tif_clientdata,module,"Out of memory");
roentgen b75cab
		return(0);
roentgen b75cab
	}
roentgen b75cab
	for (na=value, nb=m, nc=0; nc
roentgen b75cab
	{
roentgen b75cab
		if (*na<0.0)
roentgen b75cab
		{
roentgen b75cab
			if (*na==(int32)(*na))
roentgen b75cab
			{
roentgen b75cab
				nb[0]=(int32)(*na);
roentgen b75cab
				nb[1]=1;
roentgen b75cab
			}
roentgen b75cab
			else if (*na>-1.0)
roentgen b75cab
			{
roentgen b75cab
				nb[0]=-(int32)((-*na)*0x7FFFFFFF);
roentgen b75cab
				nb[1]=0x7FFFFFFF;
roentgen b75cab
			}
roentgen b75cab
			else
roentgen b75cab
			{
roentgen b75cab
				nb[0]=-0x7FFFFFFF;
roentgen b75cab
				nb[1]=(int32)(0x7FFFFFFF/(-*na));
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
		else
roentgen b75cab
		{
roentgen b75cab
			if (*na==(int32)(*na))
roentgen b75cab
			{
roentgen b75cab
				nb[0]=(int32)(*na);
roentgen b75cab
				nb[1]=1;
roentgen b75cab
			}
roentgen b75cab
			else if (*na<1.0)
roentgen b75cab
			{
roentgen b75cab
				nb[0]=(int32)((*na)*0x7FFFFFFF);
roentgen b75cab
				nb[1]=0x7FFFFFFF;
roentgen b75cab
			}
roentgen b75cab
			else
roentgen b75cab
			{
roentgen b75cab
				nb[0]=0x7FFFFFFF;
roentgen b75cab
				nb[1]=(int32)(0x7FFFFFFF/(*na));
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabArrayOfLong((uint32*)m,count*2);
roentgen b75cab
	o=TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_SRATIONAL,count,count*8,&m[0]);
roentgen b75cab
	_TIFFfree(m);
roentgen b75cab
	return(o);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedFloat(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, float value)
roentgen b75cab
{
roentgen b75cab
	float m;
roentgen b75cab
	assert(sizeof(float)==4);
roentgen b75cab
	m=value;
roentgen b75cab
	TIFFCvtNativeToIEEEFloat(tif,1,&m);
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabFloat(&m);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_FLOAT,1,4,&m));
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedFloatArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, float* value)
roentgen b75cab
{
roentgen b75cab
	assert(count<0x40000000);
roentgen b75cab
	assert(sizeof(float)==4);
roentgen b75cab
	TIFFCvtNativeToIEEEFloat(tif,count,&value);
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabArrayOfFloat(value,count);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_FLOAT,count,count*4,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
#ifdef notdef
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedDouble(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, double value)
roentgen b75cab
{
roentgen b75cab
	double m;
roentgen b75cab
	assert(sizeof(double)==8);
roentgen b75cab
	m=value;
roentgen b75cab
	TIFFCvtNativeToIEEEDouble(tif,1,&m);
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabDouble(&m);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_DOUBLE,1,8,&m));
roentgen b75cab
}
roentgen b75cab
#endif
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedDoubleArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, double* value)
roentgen b75cab
{
roentgen b75cab
	assert(count<0x20000000);
roentgen b75cab
	assert(sizeof(double)==8);
roentgen b75cab
	TIFFCvtNativeToIEEEDouble(tif,count,&value);
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabArrayOfDouble(value,count);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_DOUBLE,count,count*8,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedIfdArray(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint32* value)
roentgen b75cab
{
roentgen b75cab
	assert(count<0x40000000);
roentgen b75cab
	assert(sizeof(uint32)==4);
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabArrayOfLong(value,count);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_IFD,count,count*4,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagCheckedIfd8Array(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint32 count, uint64* value)
roentgen b75cab
{
roentgen b75cab
	assert(count<0x20000000);
roentgen b75cab
	assert(sizeof(uint64)==8);
roentgen b75cab
	assert(tif->tif_flags&TIFF_BIGTIFF);
roentgen b75cab
	if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
		TIFFSwabArrayOfLong8(value,count);
roentgen b75cab
	return(TIFFWriteDirectoryTagData(tif,ndir,dir,tag,TIFF_IFD8,count,count*8,value));
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
static int
roentgen b75cab
TIFFWriteDirectoryTagData(TIFF* tif, uint32* ndir, TIFFDirEntry* dir, uint16 tag, uint16 datatype, uint32 count, uint32 datalength, void* data)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFWriteDirectoryTagData";
roentgen b75cab
	uint32 m;
roentgen b75cab
	m=0;
roentgen b75cab
	while (m<(*ndir))
roentgen b75cab
	{
roentgen b75cab
		assert(dir[m].tdir_tag!=tag);
roentgen b75cab
		if (dir[m].tdir_tag>tag)
roentgen b75cab
			break;
roentgen b75cab
		m++;
roentgen b75cab
	}
roentgen b75cab
	if (m<(*ndir))
roentgen b75cab
	{
roentgen b75cab
		uint32 n;
roentgen b75cab
		for (n=*ndir; n>m; n--)
roentgen b75cab
			dir[n]=dir[n-1];
roentgen b75cab
	}
roentgen b75cab
	dir[m].tdir_tag=tag;
roentgen b75cab
	dir[m].tdir_type=datatype;
roentgen b75cab
	dir[m].tdir_count=count;
roentgen b75cab
	dir[m].tdir_offset.toff_long8 = 0;
roentgen b75cab
	if (datalength<=((tif->tif_flags&TIFF_BIGTIFF)?0x8U:0x4U))
roentgen b75cab
		_TIFFmemcpy(&dir[m].tdir_offset,data,datalength);
roentgen b75cab
	else
roentgen b75cab
	{
roentgen b75cab
		uint64 na,nb;
roentgen b75cab
		na=tif->tif_dataoff;
roentgen b75cab
		nb=na+datalength;
roentgen b75cab
		if (!(tif->tif_flags&TIFF_BIGTIFF))
roentgen b75cab
			nb=(uint32)nb;
roentgen b75cab
		if ((nb
roentgen b75cab
		{
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata,module,"Maximum TIFF file size exceeded");
roentgen b75cab
			return(0);
roentgen b75cab
		}
roentgen b75cab
		if (!SeekOK(tif,na))
roentgen b75cab
		{
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");
roentgen b75cab
			return(0);
roentgen b75cab
		}
roentgen b75cab
		assert(datalength<0x80000000UL);
roentgen b75cab
		if (!WriteOK(tif,data,(tmsize_t)datalength))
roentgen b75cab
		{
roentgen b75cab
			TIFFErrorExt(tif->tif_clientdata,module,"IO error writing tag data");
roentgen b75cab
			return(0);
roentgen b75cab
		}
roentgen b75cab
		tif->tif_dataoff=nb;
roentgen b75cab
		if (tif->tif_dataoff&1)
roentgen b75cab
			tif->tif_dataoff++;
roentgen b75cab
		if (!(tif->tif_flags&TIFF_BIGTIFF))
roentgen b75cab
		{
roentgen b75cab
			uint32 o;
roentgen b75cab
			o=(uint32)na;
roentgen b75cab
			if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
				TIFFSwabLong(&o);
roentgen b75cab
			_TIFFmemcpy(&dir[m].tdir_offset,&o,4);
roentgen b75cab
		}
roentgen b75cab
		else
roentgen b75cab
		{
roentgen b75cab
			dir[m].tdir_offset.toff_long8 = na;
roentgen b75cab
			if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
				TIFFSwabLong8(&dir[m].tdir_offset.toff_long8);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	(*ndir)++;
roentgen b75cab
	return(1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Link the current directory into the directory chain for the file.
roentgen b75cab
 */
roentgen b75cab
static int
roentgen b75cab
TIFFLinkDirectory(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	static const char module[] = "TIFFLinkDirectory";
roentgen b75cab
roentgen b75cab
	tif->tif_diroff = (TIFFSeekFile(tif,0,SEEK_END)+1) &~ 1;
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
	 * Handle SubIFDs
roentgen b75cab
	 */
roentgen b75cab
	if (tif->tif_flags & TIFF_INSUBIFD)
roentgen b75cab
	{
roentgen b75cab
		if (!(tif->tif_flags&TIFF_BIGTIFF))
roentgen b75cab
		{
roentgen b75cab
			uint32 m;
roentgen b75cab
			m = (uint32)tif->tif_diroff;
roentgen b75cab
			if (tif->tif_flags & TIFF_SWAB)
roentgen b75cab
				TIFFSwabLong(&m);
roentgen b75cab
			(void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
roentgen b75cab
			if (!WriteOK(tif, &m, 4)) {
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
				     "Error writing SubIFD directory link");
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
			/*
roentgen b75cab
			 * Advance to the next SubIFD or, if this is
roentgen b75cab
			 * the last one configured, revert back to the
roentgen b75cab
			 * normal directory linkage.
roentgen b75cab
			 */
roentgen b75cab
			if (--tif->tif_nsubifd)
roentgen b75cab
				tif->tif_subifdoff += 4;
roentgen b75cab
			else
roentgen b75cab
				tif->tif_flags &= ~TIFF_INSUBIFD;
roentgen b75cab
			return (1);
roentgen b75cab
		}
roentgen b75cab
		else
roentgen b75cab
		{
roentgen b75cab
			uint64 m;
roentgen b75cab
			m = tif->tif_diroff;
roentgen b75cab
			if (tif->tif_flags & TIFF_SWAB)
roentgen b75cab
				TIFFSwabLong8(&m);
roentgen b75cab
			(void) TIFFSeekFile(tif, tif->tif_subifdoff, SEEK_SET);
roentgen b75cab
			if (!WriteOK(tif, &m, 8)) {
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
				     "Error writing SubIFD directory link");
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
			/*
roentgen b75cab
			 * Advance to the next SubIFD or, if this is
roentgen b75cab
			 * the last one configured, revert back to the
roentgen b75cab
			 * normal directory linkage.
roentgen b75cab
			 */
roentgen b75cab
			if (--tif->tif_nsubifd)
roentgen b75cab
				tif->tif_subifdoff += 8;
roentgen b75cab
			else
roentgen b75cab
				tif->tif_flags &= ~TIFF_INSUBIFD;
roentgen b75cab
			return (1);
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	if (!(tif->tif_flags&TIFF_BIGTIFF))
roentgen b75cab
	{
roentgen b75cab
		uint32 m;
roentgen b75cab
		uint32 nextdir;
roentgen b75cab
		m = (uint32)(tif->tif_diroff);
roentgen b75cab
		if (tif->tif_flags & TIFF_SWAB)
roentgen b75cab
			TIFFSwabLong(&m);
roentgen b75cab
		if (tif->tif_header.classic.tiff_diroff == 0) {
roentgen b75cab
			/*
roentgen b75cab
			 * First directory, overwrite offset in header.
roentgen b75cab
			 */
roentgen b75cab
			tif->tif_header.classic.tiff_diroff = (uint32) tif->tif_diroff;
roentgen b75cab
			(void) TIFFSeekFile(tif,4, SEEK_SET);
roentgen b75cab
			if (!WriteOK(tif, &m, 4)) {
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
roentgen b75cab
					     "Error writing TIFF header");
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
			return (1);
roentgen b75cab
		}
roentgen b75cab
		/*
roentgen b75cab
		 * Not the first directory, search to the last and append.
roentgen b75cab
		 */
roentgen b75cab
		nextdir = tif->tif_header.classic.tiff_diroff;
roentgen b75cab
		while(1) {
roentgen b75cab
			uint16 dircount;
roentgen b75cab
			uint32 nextnextdir;
roentgen b75cab
roentgen b75cab
			if (!SeekOK(tif, nextdir) ||
roentgen b75cab
			    !ReadOK(tif, &dircount, 2)) {
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
					     "Error fetching directory count");
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
			if (tif->tif_flags & TIFF_SWAB)
roentgen b75cab
				TIFFSwabShort(&dircount);
roentgen b75cab
			(void) TIFFSeekFile(tif,
roentgen b75cab
			    nextdir+2+dircount*12, SEEK_SET);
roentgen b75cab
			if (!ReadOK(tif, &nextnextdir, 4)) {
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
					     "Error fetching directory link");
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
			if (tif->tif_flags & TIFF_SWAB)
roentgen b75cab
				TIFFSwabLong(&nextnextdir);
roentgen b75cab
			if (nextnextdir==0)
roentgen b75cab
			{
roentgen b75cab
				(void) TIFFSeekFile(tif,
roentgen b75cab
				    nextdir+2+dircount*12, SEEK_SET);
roentgen b75cab
				if (!WriteOK(tif, &m, 4)) {
roentgen b75cab
					TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
					     "Error writing directory link");
roentgen b75cab
					return (0);
roentgen b75cab
				}
roentgen b75cab
				break;
roentgen b75cab
			}
roentgen b75cab
			nextdir=nextnextdir;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	else
roentgen b75cab
	{
roentgen b75cab
		uint64 m;
roentgen b75cab
		uint64 nextdir;
roentgen b75cab
		m = tif->tif_diroff;
roentgen b75cab
		if (tif->tif_flags & TIFF_SWAB)
roentgen b75cab
			TIFFSwabLong8(&m);
roentgen b75cab
		if (tif->tif_header.big.tiff_diroff == 0) {
roentgen b75cab
			/*
roentgen b75cab
			 * First directory, overwrite offset in header.
roentgen b75cab
			 */
roentgen b75cab
			tif->tif_header.big.tiff_diroff = tif->tif_diroff;
roentgen b75cab
			(void) TIFFSeekFile(tif,8, SEEK_SET);
roentgen b75cab
			if (!WriteOK(tif, &m, 8)) {
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata, tif->tif_name,
roentgen b75cab
					     "Error writing TIFF header");
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
			return (1);
roentgen b75cab
		}
roentgen b75cab
		/*
roentgen b75cab
		 * Not the first directory, search to the last and append.
roentgen b75cab
		 */
roentgen b75cab
		nextdir = tif->tif_header.big.tiff_diroff;
roentgen b75cab
		while(1) {
roentgen b75cab
			uint64 dircount64;
roentgen b75cab
			uint16 dircount;
roentgen b75cab
			uint64 nextnextdir;
roentgen b75cab
roentgen b75cab
			if (!SeekOK(tif, nextdir) ||
roentgen b75cab
			    !ReadOK(tif, &dircount64, 8)) {
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
					     "Error fetching directory count");
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
			if (tif->tif_flags & TIFF_SWAB)
roentgen b75cab
				TIFFSwabLong8(&dircount64);
roentgen b75cab
			if (dircount64>0xFFFF)
roentgen b75cab
			{
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
					     "Sanity check on tag count failed, likely corrupt TIFF");
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
			dircount=(uint16)dircount64;
roentgen b75cab
			(void) TIFFSeekFile(tif,
roentgen b75cab
			    nextdir+8+dircount*20, SEEK_SET);
roentgen b75cab
			if (!ReadOK(tif, &nextnextdir, 8)) {
roentgen b75cab
				TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
					     "Error fetching directory link");
roentgen b75cab
				return (0);
roentgen b75cab
			}
roentgen b75cab
			if (tif->tif_flags & TIFF_SWAB)
roentgen b75cab
				TIFFSwabLong8(&nextnextdir);
roentgen b75cab
			if (nextnextdir==0)
roentgen b75cab
			{
roentgen b75cab
				(void) TIFFSeekFile(tif,
roentgen b75cab
				    nextdir+8+dircount*20, SEEK_SET);
roentgen b75cab
				if (!WriteOK(tif, &m, 8)) {
roentgen b75cab
					TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
					     "Error writing directory link");
roentgen b75cab
					return (0);
roentgen b75cab
				}
roentgen b75cab
				break;
roentgen b75cab
			}
roentgen b75cab
			nextdir=nextnextdir;
roentgen b75cab
		}
roentgen b75cab
	}
roentgen b75cab
	return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/************************************************************************/
roentgen b75cab
/*                          TIFFRewriteField()                          */
roentgen b75cab
/*                                                                      */
roentgen b75cab
/*      Rewrite a field in the directory on disk without regard to      */
roentgen b75cab
/*      updating the TIFF directory structure in memory.  Currently     */
roentgen b75cab
/*      only supported for field that already exist in the on-disk      */
roentgen b75cab
/*      directory.  Mainly used for updating stripoffset /              */
roentgen b75cab
/*      stripbytecount values after the directory is already on         */
roentgen b75cab
/*      disk.                                                           */
roentgen b75cab
/*                                                                      */
roentgen b75cab
/*      Returns zero on failure, and one on success.                    */
roentgen b75cab
/************************************************************************/
roentgen b75cab
roentgen b75cab
int
roentgen b75cab
_TIFFRewriteField(TIFF* tif, uint16 tag, TIFFDataType in_datatype, 
roentgen b75cab
                  tmsize_t count, void* data)
roentgen b75cab
{
roentgen b75cab
    static const char module[] = "TIFFResetField";
roentgen b75cab
    /* const TIFFField* fip = NULL; */
roentgen b75cab
    uint16 dircount;
roentgen b75cab
    tmsize_t dirsize;
roentgen b75cab
    uint8 direntry_raw[20];
roentgen b75cab
    uint16 entry_tag = 0;
roentgen b75cab
    uint16 entry_type = 0;
roentgen b75cab
    uint64 entry_count = 0;
roentgen b75cab
    uint64 entry_offset = 0;
roentgen b75cab
    int    value_in_entry = 0;
roentgen b75cab
    uint64 read_offset;
roentgen b75cab
    uint8 *buf_to_write = NULL;
roentgen b75cab
    TIFFDataType datatype;
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      Find field definition.                                          */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
    /*fip =*/ TIFFFindField(tif, tag, TIFF_ANY);
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      Do some checking this is a straight forward case.               */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
    if( isMapped(tif) )
roentgen b75cab
    {
roentgen b75cab
        TIFFErrorExt( tif->tif_clientdata, module, 
roentgen b75cab
                      "Memory mapped files not currently supported for this operation." );
roentgen b75cab
        return 0;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    if( tif->tif_diroff == 0 )
roentgen b75cab
    {
roentgen b75cab
        TIFFErrorExt( tif->tif_clientdata, module, 
roentgen b75cab
                      "Attempt to reset field on directory not already on disk." );
roentgen b75cab
        return 0;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      Read the directory entry count.                                 */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
    if (!SeekOK(tif, tif->tif_diroff)) {
roentgen b75cab
        TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
                     "%s: Seek error accessing TIFF directory",
roentgen b75cab
                     tif->tif_name);
roentgen b75cab
        return 0;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    read_offset = tif->tif_diroff;
roentgen b75cab
roentgen b75cab
    if (!(tif->tif_flags&TIFF_BIGTIFF))
roentgen b75cab
    {
roentgen b75cab
        if (!ReadOK(tif, &dircount, sizeof (uint16))) {
roentgen b75cab
            TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
                         "%s: Can not read TIFF directory count",
roentgen b75cab
                         tif->tif_name);
roentgen b75cab
            return 0;
roentgen b75cab
        }
roentgen b75cab
        if (tif->tif_flags & TIFF_SWAB)
roentgen b75cab
            TIFFSwabShort(&dircount);
roentgen b75cab
        dirsize = 12;
roentgen b75cab
        read_offset += 2;
roentgen b75cab
    } else {
roentgen b75cab
        uint64 dircount64;
roentgen b75cab
        if (!ReadOK(tif, &dircount64, sizeof (uint64))) {
roentgen b75cab
            TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
                         "%s: Can not read TIFF directory count",
roentgen b75cab
                         tif->tif_name);
roentgen b75cab
            return 0;
roentgen b75cab
        }
roentgen b75cab
        if (tif->tif_flags & TIFF_SWAB)
roentgen b75cab
            TIFFSwabLong8(&dircount64);
roentgen b75cab
        dircount = (uint16)dircount64;
roentgen b75cab
        dirsize = 20;
roentgen b75cab
        read_offset += 8;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      Read through directory to find target tag.                      */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
    while( dircount > 0 )
roentgen b75cab
    {
roentgen b75cab
        if (!ReadOK(tif, direntry_raw, dirsize)) {
roentgen b75cab
            TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
                         "%s: Can not read TIFF directory entry.",
roentgen b75cab
                         tif->tif_name);
roentgen b75cab
            return 0;
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
        memcpy( &entry_tag, direntry_raw + 0, sizeof(uint16) );
roentgen b75cab
        if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
            TIFFSwabShort( &entry_tag );
roentgen b75cab
roentgen b75cab
        if( entry_tag == tag )
roentgen b75cab
            break;
roentgen b75cab
roentgen b75cab
        read_offset += dirsize;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    if( entry_tag != tag )
roentgen b75cab
    {
roentgen b75cab
        TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
                     "%s: Could not find tag %d.",
roentgen b75cab
                     tif->tif_name, tag );
roentgen b75cab
        return 0;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      Extract the type, count and offset for this entry.              */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
    memcpy( &entry_type, direntry_raw + 2, sizeof(uint16) );
roentgen b75cab
    if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
        TIFFSwabShort( &entry_type );
roentgen b75cab
roentgen b75cab
    if (!(tif->tif_flags&TIFF_BIGTIFF))
roentgen b75cab
    {
roentgen b75cab
        uint32 value;
roentgen b75cab
        
roentgen b75cab
        memcpy( &value, direntry_raw + 4, sizeof(uint32) );
roentgen b75cab
        if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
            TIFFSwabLong( &value );
roentgen b75cab
        entry_count = value;
roentgen b75cab
roentgen b75cab
        memcpy( &value, direntry_raw + 8, sizeof(uint32) );
roentgen b75cab
        if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
            TIFFSwabLong( &value );
roentgen b75cab
        entry_offset = value;
roentgen b75cab
    }
roentgen b75cab
    else
roentgen b75cab
    {
roentgen b75cab
        memcpy( &entry_count, direntry_raw + 4, sizeof(uint64) );
roentgen b75cab
        if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
            TIFFSwabLong8( &entry_count );
roentgen b75cab
roentgen b75cab
        memcpy( &entry_offset, direntry_raw + 12, sizeof(uint64) );
roentgen b75cab
        if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
            TIFFSwabLong8( &entry_offset );
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      What data type do we want to write this as?                     */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
    if( TIFFDataWidth(in_datatype) == 8 && !(tif->tif_flags&TIFF_BIGTIFF) )
roentgen b75cab
    {
roentgen b75cab
        if( in_datatype == TIFF_LONG8 )
roentgen b75cab
            datatype = TIFF_LONG;
roentgen b75cab
        else if( in_datatype == TIFF_SLONG8 )
roentgen b75cab
            datatype = TIFF_SLONG;
roentgen b75cab
        else if( in_datatype == TIFF_IFD8 )
roentgen b75cab
            datatype = TIFF_IFD;
roentgen b75cab
        else
roentgen b75cab
            datatype = in_datatype;
roentgen b75cab
    }
roentgen b75cab
    else 
roentgen b75cab
        datatype = in_datatype;
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      Prepare buffer of actual data to write.  This includes          */
roentgen b75cab
/*      swabbing as needed.                                             */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
    buf_to_write =
roentgen b75cab
	    (uint8 *)_TIFFCheckMalloc(tif, count, TIFFDataWidth(datatype),
roentgen b75cab
				      "for field buffer.");
roentgen b75cab
    if (!buf_to_write)
roentgen b75cab
        return 0;
roentgen b75cab
roentgen b75cab
    if( datatype == in_datatype )
roentgen b75cab
        memcpy( buf_to_write, data, count * TIFFDataWidth(datatype) );
roentgen b75cab
    else if( datatype == TIFF_SLONG && in_datatype == TIFF_SLONG8 )
roentgen b75cab
    {
roentgen b75cab
	tmsize_t i;
roentgen b75cab
roentgen b75cab
        for( i = 0; i < count; i++ )
roentgen b75cab
        {
roentgen b75cab
            ((int32 *) buf_to_write)[i] = 
roentgen b75cab
                (int32) ((int64 *) data)[i];
roentgen b75cab
            if( (int64) ((int32 *) buf_to_write)[i] != ((int64 *) data)[i] )
roentgen b75cab
            {
roentgen b75cab
                _TIFFfree( buf_to_write );
roentgen b75cab
                TIFFErrorExt( tif->tif_clientdata, module, 
roentgen b75cab
                              "Value exceeds 32bit range of output type." );
roentgen b75cab
                return 0;
roentgen b75cab
            }
roentgen b75cab
        }
roentgen b75cab
    }
roentgen b75cab
    else if( (datatype == TIFF_LONG && in_datatype == TIFF_LONG8)
roentgen b75cab
             || (datatype == TIFF_IFD && in_datatype == TIFF_IFD8) )
roentgen b75cab
    {
roentgen b75cab
	tmsize_t i;
roentgen b75cab
roentgen b75cab
        for( i = 0; i < count; i++ )
roentgen b75cab
        {
roentgen b75cab
            ((uint32 *) buf_to_write)[i] = 
roentgen b75cab
                (uint32) ((uint64 *) data)[i];
roentgen b75cab
            if( (uint64) ((uint32 *) buf_to_write)[i] != ((uint64 *) data)[i] )
roentgen b75cab
            {
roentgen b75cab
                _TIFFfree( buf_to_write );
roentgen b75cab
                TIFFErrorExt( tif->tif_clientdata, module, 
roentgen b75cab
                              "Value exceeds 32bit range of output type." );
roentgen b75cab
                return 0;
roentgen b75cab
            }
roentgen b75cab
        }
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    if( TIFFDataWidth(datatype) > 1 && (tif->tif_flags&TIFF_SWAB) )
roentgen b75cab
    {
roentgen b75cab
        if( TIFFDataWidth(datatype) == 2 )
roentgen b75cab
            TIFFSwabArrayOfShort( (uint16 *) buf_to_write, count );
roentgen b75cab
        else if( TIFFDataWidth(datatype) == 4 )
roentgen b75cab
            TIFFSwabArrayOfLong( (uint32 *) buf_to_write, count );
roentgen b75cab
        else if( TIFFDataWidth(datatype) == 8 )
roentgen b75cab
            TIFFSwabArrayOfLong8( (uint64 *) buf_to_write, count );
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      Is this a value that fits into the directory entry?             */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
    if (!(tif->tif_flags&TIFF_BIGTIFF))
roentgen b75cab
    {
roentgen b75cab
        if( TIFFDataWidth(datatype) * count <= 4 )
roentgen b75cab
        {
roentgen b75cab
            entry_offset = read_offset + 8;
roentgen b75cab
            value_in_entry = 1;
roentgen b75cab
        }
roentgen b75cab
    }
roentgen b75cab
    else
roentgen b75cab
    {
roentgen b75cab
        if( TIFFDataWidth(datatype) * count <= 8 )
roentgen b75cab
        {
roentgen b75cab
            entry_offset = read_offset + 12;
roentgen b75cab
            value_in_entry = 1;
roentgen b75cab
        }
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      If the tag type, and count match, then we just write it out     */
roentgen b75cab
/*      over the old values without altering the directory entry at     */
roentgen b75cab
/*      all.                                                            */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
    if( entry_count == (uint64)count && entry_type == (uint16) datatype )
roentgen b75cab
    {
roentgen b75cab
        if (!SeekOK(tif, entry_offset)) {
roentgen b75cab
            _TIFFfree( buf_to_write );
roentgen b75cab
            TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
                         "%s: Seek error accessing TIFF directory",
roentgen b75cab
                         tif->tif_name);
roentgen b75cab
            return 0;
roentgen b75cab
        }
roentgen b75cab
        if (!WriteOK(tif, buf_to_write, count*TIFFDataWidth(datatype))) {
roentgen b75cab
            _TIFFfree( buf_to_write );
roentgen b75cab
            TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
                         "Error writing directory link");
roentgen b75cab
            return (0);
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
        _TIFFfree( buf_to_write );
roentgen b75cab
        return 1;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      Otherwise, we write the new tag data at the end of the file.    */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
    if( !value_in_entry )
roentgen b75cab
    {
roentgen b75cab
        entry_offset = TIFFSeekFile(tif,0,SEEK_END);
roentgen b75cab
        
roentgen b75cab
        if (!WriteOK(tif, buf_to_write, count*TIFFDataWidth(datatype))) {
roentgen b75cab
            _TIFFfree( buf_to_write );
roentgen b75cab
            TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
                         "Error writing directory link");
roentgen b75cab
            return (0);
roentgen b75cab
        }
roentgen b75cab
        
roentgen b75cab
        _TIFFfree( buf_to_write );
roentgen b75cab
    }
roentgen b75cab
    else
roentgen b75cab
    {
roentgen b75cab
        memcpy( &entry_offset, buf_to_write, count*TIFFDataWidth(datatype));
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      Adjust the directory entry.                                     */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
    entry_type = datatype;
roentgen b75cab
    memcpy( direntry_raw + 2, &entry_type, sizeof(uint16) );
roentgen b75cab
    if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
        TIFFSwabShort( (uint16 *) (direntry_raw + 2) );
roentgen b75cab
roentgen b75cab
    if (!(tif->tif_flags&TIFF_BIGTIFF))
roentgen b75cab
    {
roentgen b75cab
        uint32 value;
roentgen b75cab
roentgen b75cab
        value = (uint32) entry_count;
roentgen b75cab
        memcpy( direntry_raw + 4, &value, sizeof(uint32) );
roentgen b75cab
        if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
            TIFFSwabLong( (uint32 *) (direntry_raw + 4) );
roentgen b75cab
roentgen b75cab
        value = (uint32) entry_offset;
roentgen b75cab
        memcpy( direntry_raw + 8, &value, sizeof(uint32) );
roentgen b75cab
        if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
            TIFFSwabLong( (uint32 *) (direntry_raw + 8) );
roentgen b75cab
    }
roentgen b75cab
    else
roentgen b75cab
    {
roentgen b75cab
        memcpy( direntry_raw + 4, &entry_count, sizeof(uint64) );
roentgen b75cab
        if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
            TIFFSwabLong8( (uint64 *) (direntry_raw + 4) );
roentgen b75cab
roentgen b75cab
        memcpy( direntry_raw + 12, &entry_offset, sizeof(uint64) );
roentgen b75cab
        if (tif->tif_flags&TIFF_SWAB)
roentgen b75cab
            TIFFSwabLong8( (uint64 *) (direntry_raw + 12) );
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
/*      Write the directory entry out to disk.                          */
roentgen b75cab
/* -------------------------------------------------------------------- */
roentgen b75cab
    if (!SeekOK(tif, read_offset )) {
roentgen b75cab
        TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
                     "%s: Seek error accessing TIFF directory",
roentgen b75cab
                     tif->tif_name);
roentgen b75cab
        return 0;
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    if (!WriteOK(tif, direntry_raw,dirsize))
roentgen b75cab
    {
roentgen b75cab
        TIFFErrorExt(tif->tif_clientdata, module,
roentgen b75cab
                     "%s: Can not write TIFF directory entry.",
roentgen b75cab
                     tif->tif_name);
roentgen b75cab
        return 0;
roentgen b75cab
    }
roentgen b75cab
    
roentgen b75cab
    return 1;
roentgen b75cab
}
roentgen b75cab
/* vim: set ts=8 sts=8 sw=8 noet: */
roentgen b75cab
/*
roentgen b75cab
 * Local Variables:
roentgen b75cab
 * mode: c
roentgen b75cab
 * c-basic-offset: 8
roentgen b75cab
 * fill-column: 78
roentgen b75cab
 * End:
roentgen b75cab
 */