Pyramid TIFF writing.

Pyramid TIFFs are not well defined.

The following is my approach to this (Works with QGIS). Some of this was found by reading the GDAL GeoTIFF driver the rest by trial and error.

When we open a TIFF file for writing the first TIFF directory is created.

Before we write any layers out we record where that layer is:

const toff_t nBaseDirOffset = TIFFCurrentDirOffset(tif);

We then write out each layer. The first layer is the highest resolution with each subsequent layer a reduced resolution layer.

This is the start of the loop for writing out pyramid layers.

For the reduced resolution layers we write out the following (we don't write this bit out for the highest resolution layer):

// This is a bit of a hack to cause (*tif->tif_cleanup)(tif); to be called.
// See https://trac.osgeo.org/gdal/ticket/2055
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);

// Create a new directory entry - this is done for the reduced resolution images.
TIFFCreateDirectory(tif);
TIFFSetField(tif, TIFFTAG_SUBFILETYPE, FILETYPE_REDUCEDIMAGE);

The basic TIFF settings:
TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, bpp);
TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_TOPLEFT);
TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
TIFFSetField(tif, TIFFTAG_RESOLUTIONUNIT, RESUNIT_NONE);
TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
Write out the basic pixel sizing and extent information.
Write out GeoTIFF tags and metadata
Write out tile size information.
Write the tile data however may times you need too.
After writing the tile data we need to write out the TIFF directory:
TIFFWriteDirectory(tif);
For simplicity set the top level Directory:
TIFFSetSubDirectory(tif, nBaseDirOffset);

Write the next level out by going to the start of the loop (above).

Comments