TIFF file format question

Thread Starter

davebee

Joined Oct 22, 2008
540
Does anyone understand the TIFF file format?

I want to read and display a TIFF file from its raw data.

I've read many TIFF documents, and have read about the initial header and the IFD tags and their meanings, and have successfully read and decoded the IFD from a sample TIFF file, listing the image height, width, bits per sample, data type, etc.

But I can't find the answer to one little detail - how do you know exactly where in the file the bitmap data starts?

Some IFD tags point to ASCII strings, like scanner manufacturer, scanner model, image name, etc. Does the image follow those strings?

Or is there a tag I'm not understanding that points to the start of the bitmap data?
 

studiot

Joined Nov 9, 2007
4,998
The data pointer or field is the last four bytes of the IFD.

This is either (default) a pointer to the start of the actual data field in which case it gives an offset in bytes from the start of the file to the start of the data.

However if the previous two fields in the IFD (4 bytes called count and 2 bytes called type) is four bytes or less then the data pointer contains the actual start of the data not an offset pointer.
 

Thread Starter

davebee

Joined Oct 22, 2008
540
That makes sense for each individual field of the IFD, but that value has specific meaning depending on the field tag.

The file I've been working with has an IFD with 22 separate fields, for example, and each field has its 4 byte (value/pointer).

Some of those contain information like image width in pixels, height in pixels, stuff like that. My question is does one of the 4 byte values of the the 22 IFD fields point to the bitmap? Or is there some other trick to locating the bitmap?

I've been reading about an IFD field called "stripoffset", tag ID 273, that may be the one I'm looking for.

It may be that learning their terminology is part of the problem, because a "strip" of bits seems to be what TIFF people call the bitmapped data.
 

studiot

Joined Nov 9, 2007
4,998
The TIFF format has a three level heirarchy.

1) A file header

2) One or more directories called IFDs

3) Data

The IFDs provide an indefinitely long series of 12 byte entries.

Each IFD begins with a numeric code or tag specifying the type of data that follows.
The IFD also contains either the data itself or more commonly a poiner to the data.

The possibilities for type code are huge, palette, greyscale, col width, cell size, compresion etc etc

The file may contain multiple images.

This all results in a very complex file format that does not have one answer to your question and why there are said to be several types if TIFF file that some readers will baulk at.

The chapter on Tiff in my copy of Kay and Levine (Graphics File Formats) is 33 pages long and contains many tables of different possibilities)
 

Thread Starter

davebee

Joined Oct 22, 2008
540
Solved. It's true that the generic TIFF has many possibilities for its format, but for any one TIFF file, all those possibilities narrow down to just one format. The trick is reading the tags.

It turned out that reading and displaying image was pretty straightforward, once I'd learned how to read and understand the meaning of the IFD tag information.

The stripoffset tag does point to the data start. From there, the data was just a raster, but the tags must be read to know that.

There is a tag that says the data is not compressed, and another tag says it's stored in raster format, what they call strips, as opposed to a checkerboard pattern.

There is a tag that says how many rows, another tag says how many columns, another tag says how many bytes are stored per pixel, another tag says what the pixel looks like (RGB in my case), another tag says how many bits per element (16, two bytes, in my case), another tag says whether the bytes are stored big endian or little endian, and another tag says whether 0 represents black or white.

Once the retrieval code is set to those parameters, you can read and display the image!
 
Top