Return-Path: <tiff-bounces@lists.maptools.org>
Received: from mailgate105.isp.belgacom.be (mailgate105.isp.belgacom.be [195.238.6.86])
        by inmx009.isp.belgacom.be (8.12.11.20060308/8.12.11/Skynet-IN-2.32) with ESMTP id l4VLGYtq029025
        for <joris.at.lebbeke@skynet.be>; Thu, 31 May 2007 23:16:36 +0200
        (envelope-from <tiff-bounces@lists.maptools.org>)
Received: from r153.dmsolutions.ca (HELO duke.maptools.org) ([209.217.116.153])
  by in.mx.skynet.be with ESMTP; 31 May 2007 23:15:47 +0200
Received: from duke.maptools.org (duke.maptools.org [127.0.0.1])
	by duke.maptools.org (8.12.11/8.12.10) with ESMTP id l4VL2raM013958;
	Thu, 31 May 2007 17:04:39 -0400
Received: from uni20wi.unity.ncsu.edu (uni20wi.unity.ncsu.edu [152.1.227.220])
	by duke.maptools.org (8.12.11/8.12.10) with ESMTP id l4VL2oX4013955
	for <tiff@lists.maptools.org>; Thu, 31 May 2007 17:02:51 -0400
Received: from uni20wi.unity.ncsu.edu (localhost.localdomain [127.0.0.1])
	by uni20wi.unity.ncsu.edu (8.12.11.20060308/8.12.11) with ESMTP id
	l4VL2muc019007
	for <tiff@lists.maptools.org>; Thu, 31 May 2007 17:02:48 -0400
Received: (from nobody@localhost)
	by uni20wi.unity.ncsu.edu (8.12.11.20060308/8.12.11/Submit) id
	l4VL2mc5019005; Thu, 31 May 2007 17:02:48 -0400
X-Authentication-Warning: uni20wi.unity.ncsu.edu: nobody set sender to
	sbheinri@ncsu.edu using -f
Received: from 69.134.62.92 (SquirrelMail authenticated user sbheinri)
	by webmail.ncsu.edu with HTTP; Thu, 31 May 2007 17:02:48 -0400 (EDT)
Message-ID: <3914.69.134.62.92.1180645368.squirrel@webmail.ncsu.edu>
Date: Thu, 31 May 2007 17:02:48 -0400 (EDT)
From: sbheinri@ncsu.edu
To: tiff@lists.maptools.org
User-Agent: SquirrelMail/1.4.10a
MIME-Version: 1.0
Content-Type: text/plain;charset=iso-8859-1
Content-Transfer-Encoding: 8bit
X-Priority: 3 (Normal)
Importance: Normal
Subject: [Tiff] question about TIFFWriteScanline
X-BeenThere: tiff@lists.maptools.org
X-Mailman-Version: 2.1.4
Precedence: list
List-Id: TIFF and libtiff discussion <tiff.lists.maptools.org>
List-Unsubscribe: <http://lists.maptools.org/mailman/listinfo/tiff>,
	<mailto:tiff-request@lists.maptools.org?subject=unsubscribe>
List-Archive: <http://lists.maptools.org/mailman/private/tiff>
List-Post: <mailto:tiff@lists.maptools.org>
List-Help: <mailto:tiff-request@lists.maptools.org?subject=help>
List-Subscribe: <http://lists.maptools.org/mailman/listinfo/tiff>,
	<mailto:tiff-request@lists.maptools.org?subject=subscribe>
Sender: tiff-bounces@lists.maptools.org
Errors-To: tiff-bounces@lists.maptools.org
X-UIDL: 000000000023ceae.000023df
X-MSP-STORE: Thu, 31 May 2007 23:16:39 +0200
Status:   

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);
	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);
_______________________________________________
Tiff mailing list: Tiff@lists.maptools.org
http://lists.maptools.org/mailman/listinfo/tiff
http://www.remotesensing.org/libtiff/
