2001.06.12 20:59 "Re: How i can findout total number of pages in TifFile.", by Joel Schumacher
I am a C++ developer. I got a requirement, that without loading TIFF file into the memory, i want to know how many total number of pages existing inside the TIF file. Is there any way to do this?
Is there a quick & dirty way? Like a page count in the file that you can just read? No.
You don't have to read the whole file into memory at once, but you will have to skip around through the file and read bits of it.
I've done this in C for an application and I'd share, but it's technically company property. But the logic basically went as follows:
You have to read the first 2 bytes of the file (0-1) to determine the byte order in the rest of the file. They'll either be II (0x4949) for LSB to MSB or MM (0x4D4D) for MSB to LSB.
The following two bytes (2-3) are a test. If you're interpreting things in the correct order, you should get a value of 42.
Following that is a 4-byte pointer to the first image file header (in pos 4-7). Also called the image file directory (IFD). Seek to that position.
Read the 2 bytes at that position. That tells you how many 12-byte tag entries are in that header, let's say X. Read or seek() over X * 12 bytes. Read the next 4 bytes. This is the pointer to the next image file header.
Repeat and count as you go. When you hit a "next IFD pointer" of 0, you're done. Each image file header represents a page.
Also keep in mind that all of the values you are reading are subject to the byte order you read at the beginning of the file. So you may have to reverse the bytes in order to understand them. That goes for the pointers to IFD headers, the count for how many entries are in the header, and the pointer to the next IFD.
It's not terribly difficult if a page count is all you're interested in. The point being, if memory usage is an issue, you don't have to read the whole file into memory. On the other hand, you end up doing a lot of I/O, skipping around the file on disk.
I don't know whether the libtiff library has something easier and already written. Never used the library itself, just the utilities that come with it.
Joel Schumacher JCPenney Co. - UNIX Network Systems
firstname.lastname@example.org 12700 Park Central Pl M/S 6021
(972) 591-7543 Dallas TX 75251