roentgen b75cab
/* $Id: tif_close.c,v 1.19 2010-03-10 18:56:48 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
#include "tiffiop.h"
roentgen b75cab
#include <string.h></string.h>
roentgen b75cab
roentgen b75cab
/************************************************************************/
roentgen b75cab
/*                            TIFFCleanup()                             */
roentgen b75cab
/************************************************************************/
roentgen b75cab
roentgen b75cab
/**
roentgen b75cab
 * Auxiliary function to free the TIFF structure. Given structure will be
roentgen b75cab
 * completetly freed, so you should save opened file handle and pointer
roentgen b75cab
 * to the close procedure in external variables before calling
roentgen b75cab
 * _TIFFCleanup(), if you will need these ones to close the file.
roentgen b75cab
 * 
roentgen b75cab
 * @param tif A TIFF pointer.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
TIFFCleanup(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	/*
roentgen b75cab
         * Flush buffered data and directory (if dirty).
roentgen b75cab
         */
roentgen b75cab
	if (tif->tif_mode != O_RDONLY)
roentgen b75cab
		TIFFFlush(tif);
roentgen b75cab
	(*tif->tif_cleanup)(tif);
roentgen b75cab
	TIFFFreeDirectory(tif);
roentgen b75cab
roentgen b75cab
	if (tif->tif_dirlist)
roentgen b75cab
		_TIFFfree(tif->tif_dirlist);
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
         * Clean up client info links.
roentgen b75cab
         */
roentgen b75cab
	while( tif->tif_clientinfo )
roentgen b75cab
	{
roentgen b75cab
		TIFFClientInfoLink *link = tif->tif_clientinfo;
roentgen b75cab
roentgen b75cab
		tif->tif_clientinfo = link->next;
roentgen b75cab
		_TIFFfree( link->name );
roentgen b75cab
		_TIFFfree( link );
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
	if (tif->tif_rawdata && (tif->tif_flags&TIFF_MYBUFFER))
roentgen b75cab
		_TIFFfree(tif->tif_rawdata);
roentgen b75cab
	if (isMapped(tif))
roentgen b75cab
		TIFFUnmapFileContents(tif, tif->tif_base, (toff_t)tif->tif_size);
roentgen b75cab
roentgen b75cab
	/*
roentgen b75cab
         * Clean up custom fields.
roentgen b75cab
         */
roentgen b75cab
	if (tif->tif_fields && tif->tif_nfields > 0) {
roentgen b75cab
		uint32 i;
roentgen b75cab
roentgen b75cab
		for (i = 0; i < tif->tif_nfields; i++) {
roentgen b75cab
			TIFFField *fld = tif->tif_fields[i];
roentgen b75cab
			if (fld->field_bit == FIELD_CUSTOM &&
roentgen b75cab
			    strncmp("Tag ", fld->field_name, 4) == 0) {
roentgen b75cab
				_TIFFfree(fld->field_name);
roentgen b75cab
				_TIFFfree(fld);
roentgen b75cab
			}
roentgen b75cab
		}
roentgen b75cab
roentgen b75cab
		_TIFFfree(tif->tif_fields);
roentgen b75cab
	}
roentgen b75cab
roentgen b75cab
        if (tif->tif_nfieldscompat > 0) {
roentgen b75cab
                uint32 i;
roentgen b75cab
roentgen b75cab
                for (i = 0; i < tif->tif_nfieldscompat; i++) {
roentgen b75cab
                        if (tif->tif_fieldscompat[i].allocated_size)
roentgen b75cab
                                _TIFFfree(tif->tif_fieldscompat[i].fields);
roentgen b75cab
                }
roentgen b75cab
                _TIFFfree(tif->tif_fieldscompat);
roentgen b75cab
        }
roentgen b75cab
roentgen b75cab
	_TIFFfree(tif);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/************************************************************************/
roentgen b75cab
/*                            TIFFClose()                               */
roentgen b75cab
/************************************************************************/
roentgen b75cab
roentgen b75cab
/**
roentgen b75cab
 * Close a previously opened TIFF file.
roentgen b75cab
 *
roentgen b75cab
 * TIFFClose closes a file that was previously opened with TIFFOpen().
roentgen b75cab
 * Any buffered data are flushed to the file, including the contents of
roentgen b75cab
 * the current directory (if modified); and all resources are reclaimed.
roentgen b75cab
 * 
roentgen b75cab
 * @param tif A TIFF pointer.
roentgen b75cab
 */
roentgen b75cab
roentgen b75cab
void
roentgen b75cab
TIFFClose(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	TIFFCloseProc closeproc = tif->tif_closeproc;
roentgen b75cab
	thandle_t fd = tif->tif_clientdata;
roentgen b75cab
roentgen b75cab
	TIFFCleanup(tif);
roentgen b75cab
	(void) (*closeproc)(fd);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/* vim: set ts=8 sts=8 sw=8 noet: */
roentgen b75cab
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
 */