/* $Id: tif_flush.c,v 1.9 2010-03-31 06:40:10 fwarmerdam Exp $ */12/*3* Copyright (c) 1988-1997 Sam Leffler4* Copyright (c) 1991-1997 Silicon Graphics, Inc.5*6* Permission to use, copy, modify, distribute, and sell this software and7* its documentation for any purpose is hereby granted without fee, provided8* that (i) the above copyright notices and this permission notice appear in9* all copies of the software and related documentation, and (ii) the names of10* Sam Leffler and Silicon Graphics may not be used in any advertising or11* publicity relating to the software without the specific, prior written12* permission of Sam Leffler and Silicon Graphics.13*14* THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,15* EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY16* WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.17*18* IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR19* ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,20* OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,21* WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF22* LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE23* OF THIS SOFTWARE.24*/2526/*27* TIFF Library.28*/29#include "tiffiop.h"3031int32TIFFFlush(TIFF* tif)33{34if( tif->tif_mode == O_RDONLY )35return 1;3637if (!TIFFFlushData(tif))38return (0);3940/* In update (r+) mode we try to detect the case where41only the strip/tile map has been altered, and we try to42rewrite only that portion of the directory without43making any other changes */4445if( (tif->tif_flags & TIFF_DIRTYSTRIP)46&& !(tif->tif_flags & TIFF_DIRTYDIRECT)47&& tif->tif_mode == O_RDWR )48{49uint64 *offsets=NULL, *sizes=NULL;5051if( TIFFIsTiled(tif) )52{53if( TIFFGetField( tif, TIFFTAG_TILEOFFSETS, &offsets )54&& TIFFGetField( tif, TIFFTAG_TILEBYTECOUNTS, &sizes )55&& _TIFFRewriteField( tif, TIFFTAG_TILEOFFSETS, TIFF_LONG8,56tif->tif_dir.td_nstrips, offsets )57&& _TIFFRewriteField( tif, TIFFTAG_TILEBYTECOUNTS, TIFF_LONG8,58tif->tif_dir.td_nstrips, sizes ) )59{60tif->tif_flags &= ~TIFF_DIRTYSTRIP;61tif->tif_flags &= ~TIFF_BEENWRITING;62return 1;63}64}65else66{67if( TIFFGetField( tif, TIFFTAG_STRIPOFFSETS, &offsets )68&& TIFFGetField( tif, TIFFTAG_STRIPBYTECOUNTS, &sizes )69&& _TIFFRewriteField( tif, TIFFTAG_STRIPOFFSETS, TIFF_LONG8,70tif->tif_dir.td_nstrips, offsets )71&& _TIFFRewriteField( tif, TIFFTAG_STRIPBYTECOUNTS, TIFF_LONG8,72tif->tif_dir.td_nstrips, sizes ) )73{74tif->tif_flags &= ~TIFF_DIRTYSTRIP;75tif->tif_flags &= ~TIFF_BEENWRITING;76return 1;77}78}79}8081if ((tif->tif_flags & (TIFF_DIRTYDIRECT|TIFF_DIRTYSTRIP))82&& !TIFFRewriteDirectory(tif))83return (0);8485return (1);86}8788/*89* Flush buffered data to the file.90*91* Frank Warmerdam'2000: I modified this to return 1 if TIFF_BEENWRITING92* is not set, so that TIFFFlush() will proceed to write out the directory.93* The documentation says returning 1 is an error indicator, but not having94* been writing isn't exactly a an error. Hopefully this doesn't cause95* problems for other people.96*/97int98TIFFFlushData(TIFF* tif)99{100if ((tif->tif_flags & TIFF_BEENWRITING) == 0)101return (1);102if (tif->tif_flags & TIFF_POSTENCODE) {103tif->tif_flags &= ~TIFF_POSTENCODE;104if (!(*tif->tif_postencode)(tif))105return (0);106}107return (TIFFFlushData1(tif));108}109110/* vim: set ts=8 sts=8 sw=8 noet: */111/*112* Local Variables:113* mode: c114* c-basic-offset: 8115* fill-column: 78116* End:117*/118119120