LibTIFF 4.7.2
TIFF library public API and implementation reference
Loading...
Searching...
No Matches
tif_flush.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 1988-1997 Sam Leffler
3 * Copyright (c) 1991-1997 Silicon Graphics, Inc.
4 *
5 * Permission to use, copy, modify, distribute, and sell this software and
6 * its documentation for any purpose is hereby granted without fee, provided
7 * that (i) the above copyright notices and this permission notice appear in
8 * all copies of the software and related documentation, and (ii) the names of
9 * Sam Leffler and Silicon Graphics may not be used in any advertising or
10 * publicity relating to the software without the specific, prior written
11 * permission of Sam Leffler and Silicon Graphics.
12 *
13 * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
14 * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
15 * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
16 *
17 * IN NO EVENT SHALL SAM LEFFLER OR SILICON GRAPHICS BE LIABLE FOR
18 * ANY SPECIAL, INCIDENTAL, INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND,
19 * OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
20 * WHETHER OR NOT ADVISED OF THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF
21 * LIABILITY, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
22 * OF THIS SOFTWARE.
23 */
24
25/*
26 * TIFF Library.
27 */
33#include "tiffiop.h"
34
35int TIFFFlush(TIFF *tif)
36{
37 if (tif->tif_mode == O_RDONLY)
38 return 1;
39
40 if (!TIFFFlushData(tif))
41 return (0);
42
43 /* In update (r+) mode we try to detect the case where
44 only the strip/tile map has been altered, and we try to
45 rewrite only that portion of the directory without
46 making any other changes */
47
48 if ((tif->tif_flags & TIFF_DIRTYSTRIP) &&
49 !(tif->tif_flags & TIFF_DIRTYDIRECT) && tif->tif_mode == O_RDWR)
50 {
52 return 1;
53 }
54
57 return (0);
58
59 return (1);
60}
61
62/*
63 * This is an advanced writing function that must be used in a particular
64 * sequence, and together with TIFFDeferStrileArrayWriting(),
65 * to make its intended effect. Its aim is to force the writing of
66 * the [Strip/Tile][Offsets/ByteCounts] arrays at the end of the file, when
67 * they have not yet been rewritten.
68 *
69 * The typical sequence of calls is:
70 * TIFFOpen()
71 * [ TIFFCreateDirectory(tif) ]
72 * Set fields with calls to TIFFSetField(tif, ...)
73 * TIFFDeferStrileArrayWriting(tif)
74 * TIFFWriteCheck(tif, ...)
75 * TIFFWriteDirectory(tif)
76 * ... potentially create other directories and come back to the above directory
77 * TIFFForceStrileArrayWriting(tif)
78 *
79 * Returns 1 in case of success, 0 otherwise.
80 */
82{
83 static const char module[] = "TIFFForceStrileArrayWriting";
84 const int isTiled = TIFFIsTiled(tif);
85
86 if (tif->tif_mode == O_RDONLY)
87 {
88 TIFFErrorExtR(tif, tif->tif_name, "File opened in read-only mode");
89 return 0;
90 }
91 if (tif->tif_diroff == 0)
92 {
93 TIFFErrorExtR(tif, module, "Directory has not yet been written");
94 return 0;
95 }
96 if ((tif->tif_flags & TIFF_DIRTYDIRECT) != 0)
97 {
98 TIFFErrorExtR(tif, module,
99 "Directory has changes other than the strile arrays. "
100 "TIFFRewriteDirectory() should be called instead");
101 return 0;
102 }
103
104 if (!(tif->tif_flags & TIFF_DIRTYSTRIP))
105 {
106 if (!(tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&
114 {
115 TIFFErrorExtR(tif, module,
116 "Function not called together with "
117 "TIFFDeferStrileArrayWriting()");
118 return 0;
119 }
120
121 if (tif->tif_dir.td_stripoffset_p == NULL && !TIFFSetupStrips(tif))
122 return 0;
123 }
124
125 if (_TIFFRewriteField(tif,
133 {
134 tif->tif_flags &= ~TIFF_DIRTYSTRIP;
135 tif->tif_flags &= ~TIFF_BEENWRITING;
136 return 1;
137 }
138
139 return 0;
140}
141
142/*
143 * Flush buffered data to the file.
144 *
145 * Frank Warmerdam'2000: I modified this to return 1 if TIFF_BEENWRITING
146 * is not set, so that TIFFFlush() will proceed to write out the directory.
147 * The documentation says returning 1 is an error indicator, but not having
148 * been writing isn't exactly a an error. Hopefully this doesn't cause
149 * problems for other people.
150 */
152{
153 if ((tif->tif_flags & TIFF_BEENWRITING) == 0)
154 return (1);
155 if (tif->tif_flags & TIFF_POSTENCODE)
156 {
157 tif->tif_flags &= ~TIFF_POSTENCODE;
158 if (!(*tif->tif_postencode)(tif))
159 return (0);
160 }
161 return (TIFFFlushData1(tif));
162}
int _TIFFRewriteField(TIFF *tif, uint16_t tag, TIFFDataType in_datatype, tmsize_t count, void *data)
Patch an existing field in the current on-file IFD.
int TIFFFlushData1(TIFF *tif)
Append buffered encoded bytes without invoking postencode recursively.
Definition tif_write.c:1213
#define isTiled(tif)
Test whether a handle uses tiled storage.
Definition tiffiop.h:833
#define TIFF_POSTENCODE
Request a pending codec postencode call.
Definition tiffiop.h:471
#define TIFF_BEENWRITING
Record that scanline or image-data writing has begun.
Definition tiffiop.h:436
#define TIFF_DIRTYSTRIP
Mark strip/tile offset or byte-count arrays that need writing.
Definition tiffiop.h:518
#define TIFF_DIRTYDIRECT
Mark a current directory that needs writing.
Definition tiffiop.h:421
void TIFFErrorExtR(TIFF *tif, const char *module, const char *fmt,...)
Dispatch a error diagnostic to the configured handlers.
Definition tif_error.c:112
int TIFFForceStrileArrayWriting(TIFF *tif)
Patch on-file strile offset and byte-count payloads.
Definition tif_flush.c:81
int TIFFRewriteDirectory(TIFF *tif)
Relocate the current image IFD and repair its main-chain link.
int TIFFSetupStrips(TIFF *tif)
Replace strile offset/count arrays from current geometry.
Definition tif_write.c:729
int TIFFFlushData(TIFF *tif)
Flush pending encoded image data without committing directory changes.
Definition tif_flush.c:151
int TIFFFlush(TIFF *tif)
Flush pending image data and dirty directory information.
Definition tif_flush.c:35
int TIFFIsTiled(TIFF *tif)
Test whether the current image uses tiles.
Definition tif_open.c:896
#define TIFFTAG_TILEBYTECOUNTS
TIFF tag: Byte counts for tiles (introduced in TIFF 6.0).
Definition tiff.h:570
#define TIFFTAG_STRIPBYTECOUNTS
TIFF tag: Bytes counts for strips.
Definition tiff.h:460
#define TIFFTAG_TILEOFFSETS
TIFF tag: Offsets to data tiles (introduced in TIFF 6.0).
Definition tiff.h:568
#define TIFFTAG_STRIPOFFSETS
TIFF tag: Offsets to data strips.
Definition tiff.h:436
@ TIFF_LONG8
BigTIFF unsigned 64-bit integer.
Definition tiff.h:259
uint64_t tdir_count
Number of file-type elements, normalised to host order.
Definition tif_dir.h:89
uint16_t tdir_type
On-disk TIFFDataType code.
Definition tif_dir.h:86
uint64_t toff_long8
Raw 64-bit view of inline bytes or offset storage.
Definition tif_dir.h:99
uint16_t tdir_tag
Directory tag identifier.
Definition tif_dir.h:84
union TIFFDirEntry::@0 tdir_offset
Overlay raw directory inline bytes and offset-width views.
TIFFDirEntry td_stripoffset_entry
Deferred offset entry; tag/type/count are swapped but offset storage is raw.
Definition tif_dir.h:301
uint64_t * td_stripbytecount_p
Owned byte-count array/cache, possibly NULL or partial; use TIFFGetStrileByteCount().
Definition tif_dir.h:281
uint64_t * td_stripoffset_p
Owned offset array/cache, possibly NULL or partial; use TIFFGetStrileOffset().
Definition tif_dir.h:276
uint32_t td_nstrips
Total strile count across all planes; logical array length.
Definition tif_dir.h:271
TIFFDirEntry td_stripbytecount_entry
Deferred byte-count entry; offset storage still requires swapping.
Definition tif_dir.h:305
Hold private state for one open TIFF library instance.
Definition tiffiop.h:389
uint64_t tif_diroff
Current directory byte offset, or zero for an unlinked in-memory IFD.
Definition tiffiop.h:558
char * tif_name
Current filename pointer; may refer to inline or client-supplied storage.
Definition tiffiop.h:394
TIFFDirectory tif_dir
Inline representation of the current image directory.
Definition tiffiop.h:572
TIFFBoolMethod tif_postencode
Codec callback completing buffered encoding.
Definition tiffiop.h:641
uint32_t tif_flags
Bitmask combining the TIFF_* handle-state flags declared below.
Definition tiffiop.h:404
int tif_mode
Current access mode using the O_* constants.
Definition tiffiop.h:400
Declare private library state, callbacks, and implementation helpers.