2021.12.16 09:01 "[Tiff] Ensuring buffer alignment when creating tiff files", by Milian Wolff

2021.12.16 09:01 "[Tiff] Ensuring buffer alignment when creating tiff files", by Milian Wolff

Hey there,

we would like to directly mmap large image buffers from tiff files. Our software checks whether:

So far so good, this seems to work well for the main buffers. But now we received a test file with an embedded mipmap via SubIFD. Sadly, these buffers are all misaligned, i.e. we have these SubIFD:

    BitsPerSample (258) SHORT (3) 1<16>
    SubIFD (330) IFD (13) 4<0xa006f6 0xa80ad4 0xaa0d32 0xaa8ed0>

But `TIFFGetStrileOffsetWithErr` returns the following values for the respective IFDs:

    8006f5 a00ad3 a80d31 aa0ecf

As you can see, these offsets are misaligned for a 2byte/16bit greyscale image.

Looking at the libtiff API, we cannot find anything that would allow us to ensure that the SubIFDs are aligned correctly. Are we missing something or is this simply not possible currently?

We think that it would only require a small change in the code base, namely ensuring that the seek at [1] ends at an aligned address based on the BitsPerSample for the current IFD.

[1] https://gitlab.com/libtiff/libtiff/-/blob/master/libtiff/tif_write.c#L781

Thanks

Milian Wolff | milian.wolff@kdab.com | Senior Software Engineer
KDAB (Deutschland) GmbH, a KDAB Group company
Tel: +49-30-521325470
KDAB - The Qt, C++ and OpenGL Experts