1ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann/* $Id: tif_flush.c,v 1.9 2010-03-31 06:40:10 fwarmerdam Exp $ */
2ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
3ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann/*
4ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * Copyright (c) 1988-1997 Sam Leffler
5ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * Copyright (c) 1991-1997 Silicon Graphics, Inc.
6ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann *
7ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * Permission to use, copy, modify, distribute, and sell this software and
8ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * its documentation for any purpose is hereby granted without fee, provided
9ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * that (i) the above copyright notices and this permission notice appear in
10ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * all copies of the software and related documentation, and (ii) the names of
11ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * Sam Leffler and Silicon Graphics may not be used in any advertising or
12ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * publicity relating to the software without the specific, prior written
13ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * permission of Sam Leffler and Silicon Graphics.
14ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann *
15ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
16ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
17ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
18ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann *
19ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
20ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
21ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
22ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
23ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
24ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * OF THIS SOFTWARE.
25ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann */
26ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
27ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann/*
28ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * TIFF Library.
29ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann */
30ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann#include "tiffiop.h"
31ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
32ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmannint
33ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. MoltmannTIFFFlush(TIFF* tif)
34ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann{
35ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    if( tif->tif_mode == O_RDONLY )
36ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann        return 1;
37ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
38ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    if (!TIFFFlushData(tif))
39ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann        return (0);
40ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
41ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    /* In update (r+) mode we try to detect the case where
42ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann       only the strip/tile map has been altered, and we try to
43ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann       rewrite only that portion of the directory without
44ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann       making any other changes */
45ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
46ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    if( (tif->tif_flags & TIFF_DIRTYSTRIP)
47ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann        && !(tif->tif_flags & TIFF_DIRTYDIRECT)
48ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann        && tif->tif_mode == O_RDWR )
49ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    {
50ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann        uint64  *offsets=NULL, *sizes=NULL;
51ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
52ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann        if( TIFFIsTiled(tif) )
53ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann        {
54ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann            if( TIFFGetField( tif, TIFFTAG_TILEOFFSETS, &offsets )
55ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                && TIFFGetField( tif, TIFFTAG_TILEBYTECOUNTS, &sizes )
56ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                && _TIFFRewriteField( tif, TIFFTAG_TILEOFFSETS, TIFF_LONG8,
57ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                                      tif->tif_dir.td_nstrips, offsets )
58ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                && _TIFFRewriteField( tif, TIFFTAG_TILEBYTECOUNTS, TIFF_LONG8,
59ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                                      tif->tif_dir.td_nstrips, sizes ) )
60ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann            {
61ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                tif->tif_flags &= ~TIFF_DIRTYSTRIP;
62ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                tif->tif_flags &= ~TIFF_BEENWRITING;
63ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                return 1;
64ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann            }
65ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann        }
66ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann        else
67ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann        {
68ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann            if( TIFFGetField( tif, TIFFTAG_STRIPOFFSETS, &offsets )
69ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                && TIFFGetField( tif, TIFFTAG_STRIPBYTECOUNTS, &sizes )
70ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                && _TIFFRewriteField( tif, TIFFTAG_STRIPOFFSETS, TIFF_LONG8,
71ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                                      tif->tif_dir.td_nstrips, offsets )
72ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                && _TIFFRewriteField( tif, TIFFTAG_STRIPBYTECOUNTS, TIFF_LONG8,
73ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                                      tif->tif_dir.td_nstrips, sizes ) )
74ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann            {
75ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                tif->tif_flags &= ~TIFF_DIRTYSTRIP;
76ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                tif->tif_flags &= ~TIFF_BEENWRITING;
77ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann                return 1;
78ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann            }
79ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann        }
80ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    }
81ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
82ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    if ((tif->tif_flags & (TIFF_DIRTYDIRECT|TIFF_DIRTYSTRIP))
83ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann        && !TIFFRewriteDirectory(tif))
84ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann        return (0);
85ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
86ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann    return (1);
87ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann}
88ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
89ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann/*
90ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * Flush buffered data to the file.
91ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann *
92ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * Frank Warmerdam'2000: I modified this to return 1 if TIFF_BEENWRITING
93ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * is not set, so that TIFFFlush() will proceed to write out the directory.
94ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * The documentation says returning 1 is an error indicator, but not having
95ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * been writing isn't exactly a an error.  Hopefully this doesn't cause
96ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * problems for other people.
97ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann */
98ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmannint
99ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. MoltmannTIFFFlushData(TIFF* tif)
100ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann{
101ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann	if ((tif->tif_flags & TIFF_BEENWRITING) == 0)
102ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann		return (1);
103ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann	if (tif->tif_flags & TIFF_POSTENCODE) {
104ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann		tif->tif_flags &= ~TIFF_POSTENCODE;
105ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann		if (!(*tif->tif_postencode)(tif))
106ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann			return (0);
107ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann	}
108ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann	return (TIFFFlushData1(tif));
109ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann}
110ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann
111ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann/* vim: set ts=8 sts=8 sw=8 noet: */
112ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann/*
113ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * Local Variables:
114ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * mode: c
115ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * c-basic-offset: 8
116ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * fill-column: 78
117ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann * End:
118ac3d58cff7c80b0ef56bf55130d91da17cbaa3c4Philip P. Moltmann */
119