AWARE [SYSTEMS]
AWare Systems, , Home TIFF and LibTiff Mailing List Archive

LibTiff Mailing List

TIFF and LibTiff Mailing List Archive
May 2007

Previous Thread
Next Thread

Previous by Thread
Next by Thread

Previous by Date
Next by Date

Contact

The TIFF Mailing List Homepage
This list is run by Frank Warmerdam
Archive maintained by AWare Systems



New Datamatrix section



Valid HTML 4.01!



Thread

2007.05.31 21:02 "question about TIFFWriteScanline", by <sbheinri@ncsu.edu>
2007.06.01 09:05 "Re: question about TIFFWriteScanline", by Joris Van Damme

2007.06.01 09:05 "Re: question about TIFFWriteScanline", by Joris Van Damme

sbheinri@ncsu.edu wrote:
> 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);
> TIFFSetField(out, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
>
> /*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);

You forget to take care of TIFFTAG_BITSPERSAMPLE, and TIFFTAG_PHOTOMETRIC. 
If the input TIFF is tiled, you also need to take care of TIFFTAG_TILEWIDTH 
and TIFFTAG_TILELENGTH, but in the tiled case the Scanline access doesn't 
work anyway. If the TIFF has strips instead, you also need to take care of 
TIFFTAG_ROWSPERSTRIP, and in this case the Scanline access should work fine.

I suspect the main reason why you get unexpected output is that you didn't 
set the PHOTOMETRIC and BITSPERSAMPLE values. Actually, I would suspect 
TIFFWriteScanline to return an error if you start using it without setting 
these tags first, and it may be you just missed the error because you don't 
seem to check for error returns.


Best regards,

Joris Van Damme
info@awaresystems.be
http://www.awaresystems.be/
Download your free TIFF tag viewer for windows here:
http://www.awaresystems.be/imaging/tiff/astifftagviewer.html