2004.10.29 10:37 "[Tiff] Read from a TIFF and append to it", by Julien Demaria

2004.11.02 11:11 "Re: [Tiff] Read from a TIFF and append to it", by Julien Demaria

Andrey,

I need to read the first directory of a TIFF and in the same time append a new directory to it. In fact I want to create a multi-directory TIFF and to read the first dir when I create the second,... etc What's the proper way to do this?

My approach which works with libtiff 3.5.7 was to TIFFOpen the file in "w" mode, write the first dir, then TIFFOpen the first dir in "r" mode, and then read in the first and write in the second,... etc

I have never tried that myself, but don't see any reason why it should not works. That is cool if you found this way working.

With the libtiff embeded in GDAL 1.2.3 it doesn't seem work...

Please, try the CVS snapshot of the GDAL, it contains most recent libtiff version. If the problem still exist, I will investigate it.

I've just tested on Linux (tiff_dir_rw.c attachment): it works well with the current CVS libtiff, but doesn't works with the current CVS GDAL's libtiff:

"TIFFReadDirectory: tiff_dir_rw_gdal.tif: Can not read TIFF directory count."

I've also made a simple append test (tiff_append.c attachment) which creates a directory, close the file, then reopen the file in "a" mode and creates a new directory. With libtiff it works well, but with GDAL's libtiff the second directory isn't appended (without any error).

Thanks for help,

Julien

#include "tiff.h"
#include "tiffio.h"

int main(int argc, char **argv) {
    char *fout = argv[1];
    TIFF *tif, *tifr;
    unsigned char buf[24] = {0}, buf2[24] = {0};
    uint16 nb_ifds = 0, nb_ifds_read = 0;
    uint32 *offsets_ifds = NULL, *offsets_ifds_read = NULL;

    buf[0] = 22;
    buf[1] = 44;
    buf[2] = 55;

    tif = TIFFOpen(fout, "w");
    TIFFSetField( tif, TIFFTAG_BITSPERSAMPLE, 8 );
    TIFFSetField( tif, TIFFTAG_SAMPLESPERPIXEL, 1 );
    TIFFSetField( tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK );
    TIFFSetField( tif, TIFFTAG_IMAGEWIDTH, 4 );
    TIFFSetField( tif, TIFFTAG_IMAGELENGTH, 6 );
    TIFFSetField( tif, TIFFTAG_ROWSPERSTRIP, 6 );
    TIFFSetField( tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );
    TIFFWriteEncodedStrip(tif, 0, buf, 24);
    TIFFWriteDirectory(tif);

    tifr = TIFFOpen(fout, "r");
    TIFFSetField( tif, TIFFTAG_BITSPERSAMPLE, 8 );
    TIFFSetField( tif, TIFFTAG_SAMPLESPERPIXEL, 1 );
    TIFFSetField( tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK );
    TIFFSetField( tif, TIFFTAG_IMAGEWIDTH, 2 );
    TIFFSetField( tif, TIFFTAG_IMAGELENGTH, 3 );
    TIFFSetField( tif, TIFFTAG_ROWSPERSTRIP, 3 );
    TIFFSetField( tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );
    TIFFReadEncodedStrip(tifr, 0, buf2, 24);
    TIFFWriteEncodedStrip(tif, 0, buf2, 6);
    TIFFWriteDirectory(tif);
    TIFFClose(tifr);

    tifr = TIFFOpen(fout, "r");
    TIFFSetDirectory(tifr, 1);
    TIFFSetField( tif, TIFFTAG_BITSPERSAMPLE, 8 );
    TIFFSetField( tif, TIFFTAG_SAMPLESPERPIXEL, 1 );
    TIFFSetField( tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK );
    TIFFSetField( tif, TIFFTAG_IMAGEWIDTH, 1 );
    TIFFSetField( tif, TIFFTAG_IMAGELENGTH, 2 );
    TIFFSetField( tif, TIFFTAG_ROWSPERSTRIP, 2 );
    TIFFSetField( tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );
    TIFFReadEncodedStrip(tifr, 0, buf2, 24);
    TIFFWriteEncodedStrip(tif, 0, buf2, 2);
    TIFFWriteDirectory(tif);
    TIFFClose(tifr);

    TIFFClose(tif);

    return 0;
}

#include "tiff.h"
#include "tiffio.h"

int main(int argc, char **argv) {
    char *fout = argv[1];
    TIFF *tif;
    unsigned char buf[24] = {0};

    buf[0] = 22;
    buf[1] = 44;
    buf[2] = 55;

    tif = TIFFOpen(fout, "w");
    TIFFSetField( tif, TIFFTAG_BITSPERSAMPLE, 8 );
    TIFFSetField( tif, TIFFTAG_SAMPLESPERPIXEL, 1 );
    TIFFSetField( tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK );
    TIFFSetField( tif, TIFFTAG_IMAGEWIDTH, 4 );
    TIFFSetField( tif, TIFFTAG_IMAGELENGTH, 6 );
    TIFFSetField( tif, TIFFTAG_ROWSPERSTRIP, 6 );
    TIFFSetField( tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );
    TIFFWriteEncodedStrip(tif, 0, buf, 24);
    TIFFClose(tif);

    tif = TIFFOpen(fout, "a");
    TIFFSetField( tif, TIFFTAG_BITSPERSAMPLE, 8 );
    TIFFSetField( tif, TIFFTAG_SAMPLESPERPIXEL, 1 );
    TIFFSetField( tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_MINISBLACK );
    TIFFSetField( tif, TIFFTAG_IMAGEWIDTH, 2 );
    TIFFSetField( tif, TIFFTAG_IMAGELENGTH, 3 );
    TIFFSetField( tif, TIFFTAG_ROWSPERSTRIP, 3 );
    TIFFSetField( tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );
    TIFFWriteEncodedStrip(tif, 0, buf, 6);
    TIFFClose(tif);

    return 0;
}