AWARE SYSTEMS
TIFF and LibTiff Mail List Archive

Thread

2007.04.11 12:49 "[Tiff] DC to tiff", by
2007.04.11 12:49 "[Tiff] DC to tiff", by
2007.04.11 13:05 "Re: [Tiff] DC to tiff", by Chris Losinger
2007.04.11 13:26 "Re: [Tiff] DC to tiff", by Joris
2007.04.11 13:29 "Re: [Tiff] DC to tiff", by Joris
2007.05.31 21:02 "[Tiff] question about TIFFWriteScanline", by
2007.06.01 09:05 "Re: [Tiff] question about TIFFWriteScanline", by Joris

2007.06.01 09:05 "Re: [Tiff] question about TIFFWriteScanline", by Joris

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