roentgen b75cab
/* $Id: tif_flush.c,v 1.9 2010-03-31 06:40:10 fwarmerdam 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
roentgen b75cab
int
roentgen b75cab
TIFFFlush(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
    if( tif->tif_mode == O_RDONLY )
roentgen b75cab
        return 1;
roentgen b75cab
roentgen b75cab
    if (!TIFFFlushData(tif))
roentgen b75cab
        return (0);
roentgen b75cab
                
roentgen b75cab
    /* In update (r+) mode we try to detect the case where 
roentgen b75cab
       only the strip/tile map has been altered, and we try to 
roentgen b75cab
       rewrite only that portion of the directory without 
roentgen b75cab
       making any other changes */
roentgen b75cab
                
roentgen b75cab
    if( (tif->tif_flags & TIFF_DIRTYSTRIP)
roentgen b75cab
        && !(tif->tif_flags & TIFF_DIRTYDIRECT) 
roentgen b75cab
        && tif->tif_mode == O_RDWR )
roentgen b75cab
    {
roentgen b75cab
        uint64  *offsets=NULL, *sizes=NULL;
roentgen b75cab
roentgen b75cab
        if( TIFFIsTiled(tif) )
roentgen b75cab
        {
roentgen b75cab
            if( TIFFGetField( tif, TIFFTAG_TILEOFFSETS, &offsets ) 
roentgen b75cab
                && TIFFGetField( tif, TIFFTAG_TILEBYTECOUNTS, &sizes ) 
roentgen b75cab
                && _TIFFRewriteField( tif, TIFFTAG_TILEOFFSETS, TIFF_LONG8, 
roentgen b75cab
                                      tif->tif_dir.td_nstrips, offsets )
roentgen b75cab
                && _TIFFRewriteField( tif, TIFFTAG_TILEBYTECOUNTS, TIFF_LONG8, 
roentgen b75cab
                                      tif->tif_dir.td_nstrips, sizes ) )
roentgen b75cab
            {
roentgen b75cab
                tif->tif_flags &= ~TIFF_DIRTYSTRIP;
roentgen b75cab
                tif->tif_flags &= ~TIFF_BEENWRITING;
roentgen b75cab
                return 1;
roentgen b75cab
            }
roentgen b75cab
        }
roentgen b75cab
        else
roentgen b75cab
        {
roentgen b75cab
            if( TIFFGetField( tif, TIFFTAG_STRIPOFFSETS, &offsets ) 
roentgen b75cab
                && TIFFGetField( tif, TIFFTAG_STRIPBYTECOUNTS, &sizes ) 
roentgen b75cab
                && _TIFFRewriteField( tif, TIFFTAG_STRIPOFFSETS, TIFF_LONG8, 
roentgen b75cab
                                      tif->tif_dir.td_nstrips, offsets )
roentgen b75cab
                && _TIFFRewriteField( tif, TIFFTAG_STRIPBYTECOUNTS, TIFF_LONG8, 
roentgen b75cab
                                      tif->tif_dir.td_nstrips, sizes ) )
roentgen b75cab
            {
roentgen b75cab
                tif->tif_flags &= ~TIFF_DIRTYSTRIP;
roentgen b75cab
                tif->tif_flags &= ~TIFF_BEENWRITING;
roentgen b75cab
                return 1;
roentgen b75cab
            }
roentgen b75cab
        }
roentgen b75cab
    }
roentgen b75cab
roentgen b75cab
    if ((tif->tif_flags & (TIFF_DIRTYDIRECT|TIFF_DIRTYSTRIP)) 
roentgen b75cab
        && !TIFFRewriteDirectory(tif))
roentgen b75cab
        return (0);
roentgen b75cab
roentgen b75cab
    return (1);
roentgen b75cab
}
roentgen b75cab
roentgen b75cab
/*
roentgen b75cab
 * Flush buffered data to the file.
roentgen b75cab
 *
roentgen b75cab
 * Frank Warmerdam'2000: I modified this to return 1 if TIFF_BEENWRITING
roentgen b75cab
 * is not set, so that TIFFFlush() will proceed to write out the directory.
roentgen b75cab
 * The documentation says returning 1 is an error indicator, but not having
roentgen b75cab
 * been writing isn't exactly a an error.  Hopefully this doesn't cause
roentgen b75cab
 * problems for other people. 
roentgen b75cab
 */
roentgen b75cab
int
roentgen b75cab
TIFFFlushData(TIFF* tif)
roentgen b75cab
{
roentgen b75cab
	if ((tif->tif_flags & TIFF_BEENWRITING) == 0)
roentgen b75cab
		return (1);
roentgen b75cab
	if (tif->tif_flags & TIFF_POSTENCODE) {
roentgen b75cab
		tif->tif_flags &= ~TIFF_POSTENCODE;
roentgen b75cab
		if (!(*tif->tif_postencode)(tif))
roentgen b75cab
			return (0);
roentgen b75cab
	}
roentgen b75cab
	return (TIFFFlushData1(tif));
roentgen b75cab
}
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
 */