public bool RewriteDirectory()
{
const string module = "RewriteDirectory";
// We don't need to do anything special if it hasn't been written.
if (m_diroff == 0)
return WriteDirectory();
// Find and zero the pointer to this directory, so that linkDirectory will cause it to
// be added after this directories current pre-link.
// Is it the first directory in the file?
if (m_header.tiff_diroff == m_diroff)
{
m_header.tiff_diroff = 0;
m_diroff = 0;
seekFile(TiffHeader.TIFF_MAGIC_SIZE + TiffHeader.TIFF_VERSION_SIZE, SeekOrigin.Begin);
if (!writeIntOK((int)m_header.tiff_diroff))
{
ErrorExt(this, m_clientdata, m_name, "Error updating TIFF header");
return false;
}
}
else
{
uint nextdir = m_header.tiff_diroff;
do
{
short dircount;
if (!seekOK(nextdir) || !readShortOK(out dircount))
{
ErrorExt(this, m_clientdata, module, "Error fetching directory count");
return false;
}
if ((m_flags & TiffFlags.SWAB) == TiffFlags.SWAB)
SwabShort(ref dircount);
seekFile(dircount * TiffDirEntry.SizeInBytes, SeekOrigin.Current);
if (!readUIntOK(out nextdir))
{
ErrorExt(this, m_clientdata, module, "Error fetching directory link");
return false;
}
if ((m_flags & TiffFlags.SWAB) == TiffFlags.SWAB)
SwabUInt(ref nextdir);
}
while (nextdir != m_diroff && nextdir != 0);
// get current offset
long off = seekFile(0, SeekOrigin.Current);
seekFile(off - sizeof(int), SeekOrigin.Begin);
m_diroff = 0;
if (!writeIntOK((int)m_diroff))
{
ErrorExt(this, m_clientdata, module, "Error writing directory link");
return false;
}
}
// Now use WriteDirectory() normally.
return WriteDirectory();
}