Loading and Saving Large Tiff/BigTIFF Files

TIFF files use 32-bit offsets, so they are limited to 4GB. A newer version of this file format, BigTIFF, uses 64-bit offsets and can create files larger than 4GB. In other aspects BigTIFF files are the same as TIFF files. For the rest of this topic, the generic term "TIFF file" will be used for both TIFF and BigTIFF files.

TIFF files can contain large numbers of pages. This can make loading and saving them very time-consuming. Using an IFD (Image File Directory) speeds up loading, saving, and getting information about very large TIFF files. The IFD is essentially an offset within the TIFF file where a page starts.

Several structures contain an IFD member that contains the offset of the desired page within the TIFF file. These structures include FILEINFO, SAVEFILEOPTION, and LOADFILEOPTION. For 32-bit applications, the same structures also contain an IFD64 member that contains the 64-bit offset of the desired page within a BigTIFF file.

For 32-bit applications, LFile::GetInfo will report the IFD of a TIFF page by filling FILEINFO.IFD. For 32-bit applications. L_FileInfo will also report a 64-bit version of the IFD from a BigTIFF page by filling FILEINFO.IFD64.

To get LFile::GetInfo to use an IFD, set LOADFILEOPTION.IFD to the IFD and set ELO_USEIFD in LOADFILEOPTION.uFlags to signal that the IFD field is valid. For 32-bit applications, it is best to set LOADFILEOPTION.IFD64 to the IFD and set ELO2_USEIFD64 in LOADFILEOPTION.Flags2 to signal that the 64-bit bersion of the IFD should be used instead. The 64-bit version of the IFD is useful when dealing with BigTIFF files larger than 4 GB. For 64-bit applications, LOADFILEOPTION.IFD is a 64-bit value, so LOADFILEINFO.IFD can be used for both TIFF and BigTIFF files. 

Caution should be taken when using the IFD, since incorrect values can have unexpected consequences. Also note that page numbering is different if the IFD is used: page 1 is the page whose IFD is indicated in LOADFILEOPTION, page 2 is the page after the page whose IFD is indicated, etc.

Example:

void CMainFrame::OnFileinfo() 
{ 
   // We will assume that "mtif.tif" has over 42 pages. 
   // The error checks 
   // are not performed to simplify the example get info for page 40 
   LOADFILEOPTION LoadFileOption; 
   FILEINFO FileInfo; 
   memset(&FileInfo, 0, sizeof(FILEINFO)); 
   // get the info and IFD for page 40 
   memset(&LoadFileOption, 0, sizeof(LOADFILEOPTION)); 
   LoadFileOption.PageNumber = 40; 
   LFile MyFile; 
   MyFile.SetFileName(TEXT("d:\\work\\images\\mtif.tif")); 
   MyFile.GetInfo(&FileInfo, FILEINFO_TOTALPAGES, &LoadFileOption); 
   // get the info and IFD for page 41. 
   LoadFileOption.PageNumber = 2; 
   LoadFileOption.IFD = FileInfo.IFD; 
   LoadFileOption.Flags = ELO_USEIFD; 
   MyFile.GetInfo(&FileInfo, FILEINFO_TOTALPAGES, &LoadFileOption); 
} 

The example above gets the info for page 41 on the second call. The second call is lightning-quick because we dont have to skip over the first 40 pages to get the information about page 41. Instead, we skip only page 40, because the IFD provided in LOADFILEOPTION allowed us to jump directly to page 40.

FILEINFO.IFD will be filled whenever retrieving information about a TIFF file. The IFD field is valid if Fileinfo.uFlags has the FILEINFO_IFDVALID flag set. Also, the IFD value cannot be less that 8 for TIFF (values of 7 and below are invalid) or 16 for BigTIFF.

FILEINFO.IFD can be used for faster load by setting it in LOADFILEOPTION.IFD as explained above.

When loading a page from a TIFF file, setting the LOADFILEOPTION.IFD parameter of LoadXXX function tells LEADTOOLS to treat the page beginning at the specified offset as page 1. Therefore, LEADTOOLS does not have to go through all pages from the beginning of the file prior to loading the desired page.

Similarly, when saving a page to a TIFF file, setting the SAVEFILEOPTION parameter of a SaveXXX function tells the control to treat the page beginning at the specified offset as page 1. Pages after this offset can be appended more quickly, since the control does not have to go through all pages from the beginning of the file.

The LFile::Compact function lets you compact TIFF files. It also lets you copy or extract one or more pages from a TIFF file and copy them without recompression to another TIFF file.

NOTE: To save a region inside a TIFF file, you must have an unlocked Vector, Document, or Medical.

For technical reasons, the following restrictions apply when you pass an IFD offset by setting the IFD member of the SAVEFILEOPTION structure if you set PageNumber to 1:

Help Version 19.0.2017.10.27
Products | Support | Contact Us | Copyright Notices
© 1991-2017 LEAD Technologies, Inc. All Rights Reserved.
LEADTOOLS Raster Imaging C++ Class Library Help