Return-Path: owner-tiff@olympiakos.com
Return-Path: <owner-tiff@olympiakos.com>
Received: from localhost (localhost [127.0.0.1])
	by at1895.spb.edu (8.12.0.Beta7/8.12.0.Beta7) with ESMTP id f5D7rdWm026079
	for <dron@localhost>; Wed, 13 Jun 2001 13:32:48 +0400
Received: from eudoramail.com [209.185.123.45]
	by localhost with POP3 (fetchmail-5.8.1)
	for dron@localhost (single-drop); Wed, 13 Jun 2001 13:32:48 +0400 (MSD)
Received: from gate7.olympiakos.com ([207.176.88.85]) by shared1-mail.whowhere.com; Tue Jun 12 18:28:10 2001
Received: from localhost (daemon@localhost)
	by gate7.olympiakos.com (8.9.3/8.9.3) with SMTP id VAA14137;
	Tue, 12 Jun 2001 21:05:23 -0400
Received: by gate7.olympiakos.com (bulk_mailer v1.9); Tue, 12 Jun 2001 20:59:59 -0400
Received: (from majordom@localhost)
	by gate7.olympiakos.com (8.9.3/8.9.3) id UAA13975
	for tiff-outgoing; Tue, 12 Jun 2001 20:59:59 -0400
X-Authentication-Warning: gate7.olympiakos.com: majordom set sender to owner-tiff@olympiakos.com using -f
Received: from snoopy.jcpenney.com (jcpint2a.jcpenney.com [146.235.25.1])
	by gate7.olympiakos.com (8.9.3/8.9.3) with ESMTP id UAA13972
	for <tiff@olympiakos.com>; Tue, 12 Jun 2001 20:59:56 -0400
Received: from uns-dv1.jcpenney.com (uns-dv1.jcpenney.com [10.33.239.136])
	by snoopy.jcpenney.com (8.11.3/8.11.3) with ESMTP id f5D0vbb15736
	for <tiff@olympiakos.com>; Tue, 12 Jun 2001 19:57:37 -0500 (CDT)
Received: (from jschumac@localhost)
	by uns-dv1.jcpenney.com (8.9.3+Sun/8.9.1) id TAA29276
	for tiff@olympiakos.com; Tue, 12 Jun 2001 19:59:53 -0500 (CDT)
Date: Tue, 12 Jun 2001 15:59:28 -0500
From: Joel Schumacher <jschumac@jcpenney.com>
To: srinivas Rao <srinivas@mirrorplus.com>
Subject: Re: How i can findout total number of pages in TifFile.
Message-ID: <20010612155928.A15509@jcpenney.com>
References: <00bd01c0f36c$a96267b0$6800000a@mpt.com>
Mime-Version: 1.0
Content-Type: text/plain; charset=us-ascii
X-Mailer: Mutt 1.0i
In-Reply-To: <00bd01c0f36c$a96267b0$6800000a@mpt.com>; from srinivas@mirrorplus.com on Tue, Jun 12, 2001 at 11:22:36AM -0700
Sender: owner-tiff@olympiakos.com
Status: RO
Content-Length: 2430
Lines: 53

srinivas Rao (srinivas@mirrorplus.com) wrote :
> 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?

Technically, yes.

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
jschumac@jcpenney.com              12700 Park Central Pl   M/S 6021
(972) 591-7543                     Dallas TX  75251

