Blame libtiff/tif_flush.c

Packit 7838c8
/* $Id: tif_flush.c,v 1.9 2010-03-31 06:40:10 fwarmerdam Exp $ */
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Copyright (c) 1988-1997 Sam Leffler
Packit 7838c8
 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
Packit 7838c8
 *
Packit 7838c8
 * Permission to use, copy, modify, distribute, and sell this software and 
Packit 7838c8
 * its documentation for any purpose is hereby granted without fee, provided
Packit 7838c8
 * that (i) the above copyright notices and this permission notice appear in
Packit 7838c8
 * all copies of the software and related documentation, and (ii) the names of
Packit 7838c8
 * Sam Leffler and Silicon Graphics may not be used in any advertising or
Packit 7838c8
 * publicity relating to the software without the specific, prior written
Packit 7838c8
 * permission of Sam Leffler and Silicon Graphics.
Packit 7838c8
 * 
Packit 7838c8
 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND, 
Packit 7838c8
 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY 
Packit 7838c8
 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.  
Packit 7838c8
 * 
Packit 7838c8
 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
Packit 7838c8
 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
Packit 7838c8
 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
Packit 7838c8
 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF 
Packit 7838c8
 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE 
Packit 7838c8
 * OF THIS SOFTWARE.
Packit 7838c8
 */
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * TIFF Library.
Packit 7838c8
 */
Packit 7838c8
#include "tiffiop.h"
Packit 7838c8
Packit 7838c8
int
Packit 7838c8
TIFFFlush(TIFF* tif)
Packit 7838c8
{
Packit 7838c8
    if( tif->tif_mode == O_RDONLY )
Packit 7838c8
        return 1;
Packit 7838c8
Packit 7838c8
    if (!TIFFFlushData(tif))
Packit 7838c8
        return (0);
Packit 7838c8
                
Packit 7838c8
    /* In update (r+) mode we try to detect the case where 
Packit 7838c8
       only the strip/tile map has been altered, and we try to 
Packit 7838c8
       rewrite only that portion of the directory without 
Packit 7838c8
       making any other changes */
Packit 7838c8
                
Packit 7838c8
    if( (tif->tif_flags & TIFF_DIRTYSTRIP)
Packit 7838c8
        && !(tif->tif_flags & TIFF_DIRTYDIRECT) 
Packit 7838c8
        && tif->tif_mode == O_RDWR )
Packit 7838c8
    {
Packit 7838c8
        uint64  *offsets=NULL, *sizes=NULL;
Packit 7838c8
Packit 7838c8
        if( TIFFIsTiled(tif) )
Packit 7838c8
        {
Packit 7838c8
            if( TIFFGetField( tif, TIFFTAG_TILEOFFSETS, &offsets ) 
Packit 7838c8
                && TIFFGetField( tif, TIFFTAG_TILEBYTECOUNTS, &sizes ) 
Packit 7838c8
                && _TIFFRewriteField( tif, TIFFTAG_TILEOFFSETS, TIFF_LONG8, 
Packit 7838c8
                                      tif->tif_dir.td_nstrips, offsets )
Packit 7838c8
                && _TIFFRewriteField( tif, TIFFTAG_TILEBYTECOUNTS, TIFF_LONG8, 
Packit 7838c8
                                      tif->tif_dir.td_nstrips, sizes ) )
Packit 7838c8
            {
Packit 7838c8
                tif->tif_flags &= ~TIFF_DIRTYSTRIP;
Packit 7838c8
                tif->tif_flags &= ~TIFF_BEENWRITING;
Packit 7838c8
                return 1;
Packit 7838c8
            }
Packit 7838c8
        }
Packit 7838c8
        else
Packit 7838c8
        {
Packit 7838c8
            if( TIFFGetField( tif, TIFFTAG_STRIPOFFSETS, &offsets ) 
Packit 7838c8
                && TIFFGetField( tif, TIFFTAG_STRIPBYTECOUNTS, &sizes ) 
Packit 7838c8
                && _TIFFRewriteField( tif, TIFFTAG_STRIPOFFSETS, TIFF_LONG8, 
Packit 7838c8
                                      tif->tif_dir.td_nstrips, offsets )
Packit 7838c8
                && _TIFFRewriteField( tif, TIFFTAG_STRIPBYTECOUNTS, TIFF_LONG8, 
Packit 7838c8
                                      tif->tif_dir.td_nstrips, sizes ) )
Packit 7838c8
            {
Packit 7838c8
                tif->tif_flags &= ~TIFF_DIRTYSTRIP;
Packit 7838c8
                tif->tif_flags &= ~TIFF_BEENWRITING;
Packit 7838c8
                return 1;
Packit 7838c8
            }
Packit 7838c8
        }
Packit 7838c8
    }
Packit 7838c8
Packit 7838c8
    if ((tif->tif_flags & (TIFF_DIRTYDIRECT|TIFF_DIRTYSTRIP)) 
Packit 7838c8
        && !TIFFRewriteDirectory(tif))
Packit 7838c8
        return (0);
Packit 7838c8
Packit 7838c8
    return (1);
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
/*
Packit 7838c8
 * Flush buffered data to the file.
Packit 7838c8
 *
Packit 7838c8
 * Frank Warmerdam'2000: I modified this to return 1 if TIFF_BEENWRITING
Packit 7838c8
 * is not set, so that TIFFFlush() will proceed to write out the directory.
Packit 7838c8
 * The documentation says returning 1 is an error indicator, but not having
Packit 7838c8
 * been writing isn't exactly a an error.  Hopefully this doesn't cause
Packit 7838c8
 * problems for other people. 
Packit 7838c8
 */
Packit 7838c8
int
Packit 7838c8
TIFFFlushData(TIFF* tif)
Packit 7838c8
{
Packit 7838c8
	if ((tif->tif_flags & TIFF_BEENWRITING) == 0)
Packit 7838c8
		return (1);
Packit 7838c8
	if (tif->tif_flags & TIFF_POSTENCODE) {
Packit 7838c8
		tif->tif_flags &= ~TIFF_POSTENCODE;
Packit 7838c8
		if (!(*tif->tif_postencode)(tif))
Packit 7838c8
			return (0);
Packit 7838c8
	}
Packit 7838c8
	return (TIFFFlushData1(tif));
Packit 7838c8
}
Packit 7838c8
Packit 7838c8
/* vim: set ts=8 sts=8 sw=8 noet: */
Packit 7838c8
/*
Packit 7838c8
 * Local Variables:
Packit 7838c8
 * mode: c
Packit 7838c8
 * c-basic-offset: 8
Packit 7838c8
 * fill-column: 78
Packit 7838c8
 * End:
Packit 7838c8
 */