2015.02.11 01:56 "[Tiff] Interleaved write of multiple TIFF frames", by Paavo

2015.02.11 13:31 "Re: [Tiff] Interleaved write of multiple TIFF frames", by Olivier Paquet

2015-02-10 20:56 GMT-05:00 <paavo@osa.pri.ee>:

Is it possible to write multiple TIFF frames at the same time with libTIFF? I am facing a task of writing TIFF files which could potentially contain multiple multi-gigabyte image frames, with pixel data arriving piecewise in an interleaved fashion and I do not want to collect and hold all these gigabytes in memory buffers.

Yes, it is possible, using TIFFCheckpointDirectory. This was not it's intended purpose so future compatibility is not guaranteed but for now it works if you do roughly this:

tiff = TIFFOpen();
for( int i = 0; i < num_ifd, ++i )
{
    /* Set all your IFD tags here so the directory is complete and its
size known. */

    TIFFCheckpointDirectory( tiff );
    TIFFWriteDirectory();
}
/* This block makes the LZW compressor happy. No idea why but you need
it to use LZW. */
{
    for( int i = 0; i < num_ifd, ++i )
    {
        TIFFSetDirectory( tiff, i );
        TIFFCheckpointDirectory( tiff );
        TIFFWriteDirectory();
    }
    TIFFSetDirectory( tiff, 0 );
}

Then you can write using:

TIFFSetDirectory( tiff, i ); TIFFWriteEncodedStrip(); /* or probably other functions too */ TIFFCheckpointDirectory();

TIFFWriteDirectory();
/* repeat until hard drive is full */

TIFFClose( tiff );

Given the issue with LZW, I suspect your mileage will vary with other compressors as well. Now this is clearly not officially supported use of libtiff so I would only recommend you use this if you link with a static version of libtiff where you know it works. Otherwise, system upgrades could very well break your code as the internals of libtiff change and the above stops working. It would then likely take a while to find out about it and get it fixed.

Is this workable? Maybe there are other better ways to achieve this?

Another option is to write your own TIFF output code. If you only need to support one format and don't set a lot of fancy tags, it's probably not that difficult. Put a header in the file, dump the data as you receive it while recording the tile/strip offsets. Then when all is done, write the IFDs and go fix the header to point to the first IFD.

Olivier