2004.12.07 09:55 "Re: [Tiff] Read from a TIFF and append to it", by Julien Demaria
Andrey,
I've just remade my tests about tiff directory and subdirectory writing and appending with last CVS libtiff and CVS GDAL's libtiff, and I've still found these problems:
- tiff_append.c : write a simple TIFF, close it, reopen it in append mode and add a simple second directory doesn't work with GDAL's libtiff : output file has only one directory ;
- tiff_dir_rw.c : read a directory of a TIFF and add one to it simultaneously : doesn't work with GDAL's libtiff : "TIFFReadDirectory: tiff_dir_rw_gdal.tif: Can not read TIFF directory count. Segmentation fault (core dumped)"
- tiff_subdir.c : write a subdirectory to a TIFF : doesn't work with the 2 libtiff, standard and GDAL : the subdirectory isn't written.
I've also posted a bug report on TIFF bugzilla.
Note my little test programs are auto sufficient and needn't any data.
Thanks in advance for help,
Julien
#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;
}
#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, *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;
}