Discussion:
TIFFCurrentDirectory inconsistency
Roger Leigh
2014-05-09 12:06:00 UTC
Permalink
Hi. I'm using tiff 4.0.3 on Debian.

If I iterate over all IFDs in a TIFF by index using TIFFSetDirectory,
and for each IFD query the index, offset and whether it's last with
TIFFCurrentDirectory, TIFFCurrentDirOffset and TIFFLastDirectory I get
this:

[ RUN ] TIFFTest.IFDsByIndex
IDX: 0 OFF: 8 LAST: 0
IDX: 1 OFF: 839 LAST: 0
IDX: 2 OFF: 1670 LAST: 0
IDX: 3 OFF: 2501 LAST: 0
IDX: 4 OFF: 3332 LAST: 0
IDX: 5 OFF: 4163 LAST: 0
IDX: 6 OFF: 4994 LAST: 0
IDX: 7 OFF: 5825 LAST: 0
IDX: 8 OFF: 6656 LAST: 0
IDX: 9 OFF: 7487 LAST: 1

All looks fine. But, if I iterate by offset using TIFFSetSubDirectory:

[RUN ] TIFFTest.IFDsByOffset
IDX: 5 OFF: 8 LAST: 0
IDX: 6 OFF: 839 LAST: 0
IDX: 7 OFF: 1670 LAST: 0
IDX: 8 OFF: 2501 LAST: 0
IDX: 9 OFF: 3332 LAST: 0
IDX: 10 OFF: 4163 LAST: 0
IDX: 11 OFF: 4994 LAST: 0
IDX: 12 OFF: 5825 LAST: 0
IDX: 13 OFF: 6656 LAST: 0
IDX: 14 OFF: 7487 LAST: 1

You can see here that while the offsets are identical, the indexes
returned by TIFFCurrentDirectory are now somewhat fictional.

Likewise, if I set the directory to index 0 with TIFFSetDirectory,
and then iterate using TIFFReadDirectory. I'm calling
TIFFReadDirectory, then TIFFCurrentDirOffset,
then TIFFSetSubDirectory in a loop (I'm caching the offsets)

[ RUN ] TIFFTest.IFDSimpleIter
IDX: 0 OFF: 8 LAST: 0
IDX: 4 OFF: 839 LAST: 0
IDX: 9 OFF: 1670 LAST: 0
IDX: 14 OFF: 2501 LAST: 0
IDX: 19 OFF: 3332 LAST: 0
IDX: 24 OFF: 4163 LAST: 0
IDX: 29 OFF: 4994 LAST: 0
IDX: 34 OFF: 5825 LAST: 0
IDX: 39 OFF: 6656 LAST: 0
IDX: 44 OFF: 7487 LAST: 1

Again, the offsets are incorrect. And unlike the previous example,
they aren't just off by +5, it's incrementing by 4, then 5 for all
subsequent TIFFReadDirectory calls.

For the last example, if I call TIFFSetSubdirectory three times
in a row instead of once, the indexes change:

[ RUN ] TIFFTest.IFDSimpleIter
IDX: 0 OFF: 8 LAST: 0
IDX: 10 OFF: 839 LAST: 0
IDX: 23 OFF: 1670 LAST: 0
IDX: 36 OFF: 2501 LAST: 0
IDX: 49 OFF: 3332 LAST: 0
IDX: 62 OFF: 4163 LAST: 0
IDX: 75 OFF: 4994 LAST: 0
IDX: 88 OFF: 5825 LAST: 0
IDX: 101 OFF: 6656 LAST: 0
IDX: 114 OFF: 7487 LAST: 1

My question is really just if this is expected behaviour or not?
Are the indexes expected to be valid if I've not called
TIFFSetDirectory explicitly, and instead used TIFFSetSubDirectory
or TIFFReadDirectory? Something seems a bit amiss in how the index
is computed.


Many thanks,
Roger
--
.''`. Roger Leigh
: :' : Debian GNU/Linux http://people.debian.org/~rleigh/
`. `' schroot and sbuild http://alioth.debian.org/projects/buildd-tools
`- GPG Public Key F33D 281D 470A B443 6756 147C 07B3 C8BC 4083 E800
_______________________________________________
Tiff mailing list: ***@lists.maptools.org
http://lists.maptools.org/mailman/listinfo/tiff
http://www.remotesensing.org/libtiff/
Roger Leigh
2014-05-09 13:17:57 UTC
Permalink
Post by Roger Leigh
My question is really just if this is expected behaviour or not?
Are the indexes expected to be valid if I've not called
TIFFSetDirectory explicitly, and instead used TIFFSetSubDirectory
or TIFFReadDirectory? Something seems a bit amiss in how the index
is computed.
Simple testcase:


#include <stdio.h>
#include <tiffio.h>
#include <stdint.h>
#include <stdbool.h>

int main(void)
{
TIFF *tiff = TIFFOpen("test.tiff", "r");

TIFFSetDirectory(tiff, 1);
for (int i = 0; i < 10; ++i)
{
uint64_t offset = TIFFCurrentDirOffset(tiff);
uint16_t index = TIFFCurrentDirectory(tiff);
bool last = TIFFLastDirectory(tiff);
printf("index=%u, offset=%lu, last=%d\n", index, offset, last);
TIFFSetSubDirectory(tiff, offset);
TIFFSetSubDirectory(tiff, offset);
}
}

==>
index=1, offset=839, last=0
index=3, offset=839, last=0
index=5, offset=839, last=0
index=7, offset=839, last=0
index=9, offset=839, last=0
index=11, offset=839, last=0
index=13, offset=839, last=0
index=15, offset=839, last=0
index=17, offset=839, last=0
index=19, offset=839, last=0

Every call to TIFFSetSubDirectory is incrementing the index returned by
TIFFCurrentDirectory. Even though, in fact, the IFD never actually
changed since the IFD offset was constant from the moment TIFFOpen
returned.

Is this a libtiff bug, or is this working as intended?


Regards,
Roger
--
.''`. Roger Leigh
: :' : Debian GNU/Linux http://people.debian.org/~rleigh/
`. `' schroot and sbuild http://alioth.debian.org/projects/buildd-tools
`- GPG Public Key F33D 281D 470A B443 6756 147C 07B3 C8BC 4083 E800
_______________________________________________
Tiff mailing list: ***@lists.maptools.org
http://lists.maptools.org/mailman/listinfo/tiff
http://www.remotesensing.org/libtiff/
Loading...