0

I am receiving data from a camera and saving each image as a page in a multi-page tiff. I can set that each file has e.g. 100 pages and I am calling:

TIFFSetField(out, TIFFTAG_PAGENUMBER, page_number, total_pages);

However, if I am unable to write data to disk fast enough, I will stop the acquisition. At this point I may have written 50 out of 100 pages into a multi-page tiff. Now the multi-page tiff file reports total number of pages as 100, but only 50 pages have been actually written. Some applications will report 100 pages, but for pages 51-100 there will be no data and images will appear to be black.

Therefore I would need to update the total_pages number at the moment when I am ending the disk writing to the value of the last written page. Can this be done at all? Is the total_pages value written once into a common header which I could update and fix the file in this way, or is this value written into each page which means I would have to edit each page that has already been written to disk? Or is there any better approach how to handle this?

user2165039
  • 73
  • 1
  • 8

1 Answers1

1

Actually, the solution is quite simple. Once your image stream has ended and before you close the file, you have to iterate through all the directories (images in the multi-page tiff) and update the TIFFTAG_PAGENUMBER to the total pages written. The catch is you have to do it before you close the tiff by calling TIFFClose. Once a TIFF is closed, its TAGS cannot be edited anymore. (see http://www.libtiff.org/libtiff.html):

Note that unlike the stdio library TIFF image files may not be opened for both reading and writing; there is no support for altering the contents of a TIFF file.

if (pagesTotal - pagesWritten > 0)
{
    for (int i = 0; i < pagesWritten; i++)
    {
         int retVal = TIFFSetDirectory(out, i);
         retVal = TIFFSetField(out, TIFFTAG_PAGENUMBER, i, pagesWritten);
         retVal = TIFFWriteDirectory(out);
     }
}
TIFFClose(out);

pagesTotal is number of pages that we inteded to write into this multi-page file

pagesWritten is number of pages that we actually wrote into the file

user2165039
  • 73
  • 1
  • 8