2005.03.04 22:36 "[Tiff] Advice on replacing use of tiffiop.h internals with public APIs", by Jon Leech

I'm porting some old software to current versions of libtiff, and trying to eliminate use of libtiff non-public data structures and APIs in tiffiop.h, so there's no dependency on a specific version of libtiff being installed (it was written more than a decade ago, and at the time, was built and bundled with its own copy of libtiff). I've been browsing the libtiff code, but would appreciate sanity-checking and advice.

Right now when writing TIFF images this software constructs a lot of the TIFF and TIFFDirectory header information itself. For starters, there are a bunch of reads and writes of TIFFDirectory members, which I think can almost all be replaced with TIFFSetField/GetField. Is the member / tag mapping below accurate (for libtiff 3.5.7 and earlier, anyway)?

    TIFFDirectory member            Corresponding tag
    --------------------            -----------------
    tif_dir.td_bitspersample        TIFFTAG_BITSPERSAMPLE
    tif_dir.td_compression          TIFFTAG_COMPRESSION
    tif_dir.td_extrasamples         TIFFTAG_EXTRASAMPLES (first value)
        tif_dir.td_sampleinfo       TIFFTAG_EXTRASAMPLES (second value)
    tif_dir.td_imagedepth           TIFFTAG_IMAGEDEPTH
    tif_dir.td_imagedescription     TIFFTAG_IMAGEDESCRIPTION
    tif_dir.td_imagelength          TIFFTAG_IMAGELENGTH
    tif_dir.td_imagewidth           TIFFTAG_IMAGEWIDTH
    tif_dir.td_orientation          TIFFTAG_ORIENTATION
    tif_dir.td_photometric          TIFFTAG_PHOTOMETRIC
    tif_dir.td_planarconfig         TIFFTAG_PLANARCONFIG
    tif_dir.td_rowsperstrip         TIFFTAG_ROWSPERSTRIP
    tif_dir.td_sampleformat         TIFFTAG_SAMPLEFORMAT
    tif_dir.td_samplesperpixel      TIFFTAG_SAMPLESPERPIXEL
    tif_dir.td_stripbytecount       TIFFTAG_STRIPBYTECOUNTS
    tif_dir.td_stripoffset          TIFFTAG_STRIPOFFSETS
    tif_dir.td_tiledepth            TIFFTAG_TILEDEPTH
    tif_dir.td_tilelength           TIFFTAG_TILELENGTH
    tif_dir.td_tilewidth            TIFFTAG_TILEWIDTH
    tif_dif.td_stripsperimage      (does this have a tag? It appears to
                                    be derived from tag data in some
                                    fashion)

There is also some use of internal macros, functions, and TIFF members, which will be harder to fix. I'd appreciate any help in understanding what the following internals mean, and how they are initialized or used by libtiff itself. The following code fragment is used when preparing to write a TIFF image, either the first image in a file after opening it, or appending a new image to an existing file, in which case a TIFFWriteDirectory() preceded. It's obviously performing some initialization of the directory for the new image, but I'm not clear if any of this is actually needed under the circumstances?

    /* either TIFFOpen() or TIFFWriteDirectory(), followed by: */
    TIFFFreeDirectory(tiff);
    TIFFDefaultDirectory(tiff);
    tiff->tif_flags &= ~TIFF_ISTILED;
    tiff->tif_diroff = 0;

That's followed by setting a whole bunch of the tags listed above, and also some setting or querying of TIFF struct members including:

    tiff->tif_curoff = 0;
    tiff->tif_scanlinesize = TIFFScanlineSize(tiff);
    tiff->tif_tilesize
    tiff->tif_scanlinesize
    tiff->tif_mode  (replaceable by TIFFGetMode()?)

And finally, there's repeated use of the macro

        isTiled(tiff)

which is used to control how some of those members are initialized.

Thanks for any advice you can offer!

Jon Leech
SGI

P.S. Yes, that reply address works.