AWARE SYSTEMS
TIFF and LibTiff Mail List Archive

2004.11.05 17:17 "[Tiff] Append and read subifd", by Julien Demaria

Hi,

I try to append and read subifds to the first directory of an existing tiff file (little code in attachment) and it doesn't work with the current libtiff CVS (Linux).

Thanks in advance,

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 = 2, 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);
    TIFFClose(tif);

    tif = TIFFOpen(fout, "a");
    TIFFSetDirectory(tif, 0);
    offsets_ifds = (uint32 *) malloc(nb_ifds * sizeof(uint32));
    offsets_ifds[0] = 0;
    offsets_ifds[1] = 0;
    TIFFSetField(tif, TIFFTAG_SUBIFD, nb_ifds, offsets_ifds);
    TIFFRewriteDirectory(tif);

    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);
    TIFFWriteDirectory(tif);

    tifr = TIFFOpen(fout, "r");
    if (!TIFFGetField(tifr, TIFFTAG_SUBIFD, &nb_ifds_read, &offsets_ifds_read))
        printf("error\n");
    else {
        printf("%d %d\n", offsets_ifds_read[0], offsets_ifds_read[1]);
        TIFFSetSubDirectory(tifr, offsets_ifds_read[0]);
    }
    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, 1 );
    TIFFSetField( tif, TIFFTAG_ROWSPERSTRIP, 1 );
    TIFFSetField( tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG );
    TIFFReadEncodedStrip(tifr, 0, buf2, 6);
    TIFFWriteEncodedStrip(tif, 0, buf, 1);
    TIFFClose(tifr);
    TIFFClose(tif);

    return 0;
}