private bool estimateStripByteCounts(TiffDirEntry[] dir, short dircount)
{
const string module = "estimateStripByteCounts";
m_dir.td_stripbytecount = new uint [m_dir.td_nstrips];
if (m_dir.td_compression != Compression.NONE)
{
long space = TiffHeader.SizeInBytes + sizeof(short) + (dircount * TiffDirEntry.SizeInBytes) + sizeof(int);
long filesize = getFileSize();
// calculate amount of space used by indirect values
for (short n = 0; n < dircount; n++)
{
int cc = DataWidth((TiffType)dir[n].tdir_type);
if (cc == 0)
{
ErrorExt(this, m_clientdata, module,
"{0}: Cannot determine size of unknown tag type {1}",
m_name, dir[n].tdir_type);
return false;
}
cc = cc * dir[n].tdir_count;
if (cc > sizeof(int))
space += cc;
}
space = filesize - space;
if (m_dir.td_planarconfig == PlanarConfig.SEPARATE)
space /= m_dir.td_samplesperpixel;
int strip = 0;
for ( ; strip < m_dir.td_nstrips; strip++)
m_dir.td_stripbytecount[strip] = (uint)space;
// This gross hack handles the case were the offset to the last
// strip is past the place where we think the strip should begin.
// Since a strip of data must be contiguous, it's safe to assume
// that we've overestimated the amount of data in the strip and
// trim this number back accordingly.
strip--;
if ((m_dir.td_stripoffset[strip] + m_dir.td_stripbytecount[strip]) > filesize)
m_dir.td_stripbytecount[strip] = (uint)(filesize - m_dir.td_stripoffset[strip]);
}
else if (IsTiled())
{
int bytespertile = TileSize();
for (int strip = 0; strip < m_dir.td_nstrips; strip++)
m_dir.td_stripbytecount[strip] = (uint)bytespertile;
}
else
{
int rowbytes = ScanlineSize();
int rowsperstrip = m_dir.td_imagelength / m_dir.td_stripsperimage;
for (int strip = 0; strip < m_dir.td_nstrips; strip++)
m_dir.td_stripbytecount[strip] = (uint)(rowbytes * rowsperstrip);
}
setFieldBit(FieldBit.StripByteCounts);
if (!fieldSet(FieldBit.RowsPerStrip))
m_dir.td_rowsperstrip = m_dir.td_imagelength;
return true;
}