#include "tiffiop.h"
int TIFFFlush(TIFF *tif)
{
if (tif->tif_mode == O_RDONLY)
return 1;
if (!TIFFFlushData(tif))
return (0);
if ((tif->tif_flags & TIFF_DIRTYSTRIP) &&
!(tif->tif_flags & TIFF_DIRTYDIRECT) && tif->tif_mode == O_RDWR)
{
if (TIFFForceStrileArrayWriting(tif))
return 1;
}
if ((tif->tif_flags & (TIFF_DIRTYDIRECT | TIFF_DIRTYSTRIP)) &&
!TIFFRewriteDirectory(tif))
return (0);
return (1);
}
int TIFFForceStrileArrayWriting(TIFF *tif)
{
static const char module[] = "TIFFForceStrileArrayWriting";
const int isTiled = TIFFIsTiled(tif);
if (tif->tif_mode == O_RDONLY)
{
TIFFErrorExtR(tif, tif->tif_name, "File opened in read-only mode");
return 0;
}
if (tif->tif_diroff == 0)
{
TIFFErrorExtR(tif, module, "Directory has not yet been written");
return 0;
}
if ((tif->tif_flags & TIFF_DIRTYDIRECT) != 0)
{
TIFFErrorExtR(tif, module,
"Directory has changes other than the strile arrays. "
"TIFFRewriteDirectory() should be called instead");
return 0;
}
if (!(tif->tif_flags & TIFF_DIRTYSTRIP))
{
if (!(tif->tif_dir.td_stripoffset_entry.tdir_tag != 0 &&
tif->tif_dir.td_stripoffset_entry.tdir_count == 0 &&
tif->tif_dir.td_stripoffset_entry.tdir_type == 0 &&
tif->tif_dir.td_stripoffset_entry.tdir_offset.toff_long8 == 0 &&
tif->tif_dir.td_stripbytecount_entry.tdir_tag != 0 &&
tif->tif_dir.td_stripbytecount_entry.tdir_count == 0 &&
tif->tif_dir.td_stripbytecount_entry.tdir_type == 0 &&
tif->tif_dir.td_stripbytecount_entry.tdir_offset.toff_long8 == 0))
{
TIFFErrorExtR(tif, module,
"Function not called together with "
"TIFFDeferStrileArrayWriting()");
return 0;
}
if (tif->tif_dir.td_stripoffset_p == NULL && !TIFFSetupStrips(tif))
return 0;
}
if (_TIFFRewriteField(tif,
isTiled ? TIFFTAG_TILEOFFSETS : TIFFTAG_STRIPOFFSETS,
TIFF_LONG8, tif->tif_dir.td_nstrips,
tif->tif_dir.td_stripoffset_p) &&
_TIFFRewriteField(
tif, isTiled ? TIFFTAG_TILEBYTECOUNTS : TIFFTAG_STRIPBYTECOUNTS,
TIFF_LONG8, tif->tif_dir.td_nstrips,
tif->tif_dir.td_stripbytecount_p))
{
tif->tif_flags &= ~TIFF_DIRTYSTRIP;
tif->tif_flags &= ~TIFF_BEENWRITING;
return 1;
}
return 0;
}
int TIFFFlushData(TIFF *tif)
{
if ((tif->tif_flags & TIFF_BEENWRITING) == 0)
return (1);
if (tif->tif_flags & TIFF_POSTENCODE)
{
tif->tif_flags &= ~TIFF_POSTENCODE;
if (!(*tif->tif_postencode)(tif))
return (0);
}
return (TIFFFlushData1(tif));
}