2007.05.31 21:02 "[Tiff] question about TIFFWriteScanline", by
I have verified that the format of data in the buffer returned by TIFFReadScanline in contiguous mode follows this format:
image(row, col, chan) = buffer[col*channels + chan];
However, when I use the same format with TIFFWriteScanline to write to a file, it mixes up all the channels and I get garbage:
buffer[col*channels + chan] = image(row, col, chan);
As a simple test I have just read in/write out each scanline. In comes an image, out comes a garbled image. So how do I need to reorganize the buffer for writing?
TIFF *tif = TIFFOpen(fname, opt);
uint32 rows;
TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &rows);
uint32 cols;
TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &cols);
uint16 _channels;
TIFFGetField(tif, TIFFTAG_SAMPLESPERPIXEL, &channels);
uint16 config;
TIFFGetField(tif, TIFFTAG_PLANARCONFIG, &config);
if (config == PLANARCONFIG_CONTIG)
//scanline organized as: R G B R G B...
{
tdata_t buf = _TIFFmalloc(TIFFScanlineSize(tif));
uint8 *b = (uint8*)buf;
/*open a file for writing*/
TIFF *out = TIFFOpen("TEST.tif", "w");
TIFFSetField(out, TIFFTAG_IMAGEWIDTH, (uint32)cols);
TIFFSetField(out, TIFFTAG_IMAGELENGTH, (uint32)rows);
TIFFSetField(out, TIFFTAG_SAMPLESPERPIXEL, (uint16)channels);
TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG); TIFFSetField(out,
/*read/write scanlines back out to new file*/
for(unsigned int row = 0; row < rows; row++)
{
TIFFReadScanline(tif, buf, row);
TIFFWriteScanline(out, buf, row);
}
_TIFFfree(buf);
TIFFClose(out);
}
TIFFClose(tif);