Return-Path: <tiff-bounces@lists.maptools.org>
Delivered-To: info@awaresystems.be
Received: (qmail 15636 invoked from network); 14 Nov 2008 08:30:25 -0000
Received: from floyd.telenet-ops.be ([195.130.136.4])
          (envelope-sender <tiff-bounces@lists.maptools.org>)
          by angelia.telenet-ops.be (qmail-ldap-1.03) with SMTP
          for <info@awaresystems.be>; 14 Nov 2008 08:30:25 -0000
Received: from mindy.telenet-ops.be (mindy.telenet-ops.be [195.130.136.8])
	by floyd.telenet-ops.be (8.13.1/8.13.1) with ESMTP id mAE8UPZg025555
	for <info@awaresystems.be>; Fri, 14 Nov 2008 09:30:25 +0100
Received: from localhost (localhost.localdomain [127.0.0.1])
	by mindy.telenet-ops.be (Postfix) with SMTP id 38E4D60004
	for <info@awaresystems.be>; Fri, 14 Nov 2008 09:30:25 +0100 (CET)
Received: from penia.telenet-ops.be (penia.telenet-ops.be [195.130.132.39])
	by mindy.telenet-ops.be (Postfix) with ESMTP id 31C3D60003
	for <info@awaresystems.be>; Fri, 14 Nov 2008 09:30:25 +0100 (CET)
Received: from duke.maptools.org (r153.dmsolutions.ca [209.217.116.153])
	by penia.telenet-ops.be (Postfix) with ESMTP id C15AA33E60B
	for <info@awaresystems.be>; Fri, 14 Nov 2008 09:30:24 +0100 (CET)
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 mAE8THTl021611;
	Fri, 14 Nov 2008 03:29:35 -0500
Received: from sender-02.it.helsinki.fi (sender-02.it.helsinki.fi
	[128.214.205.137])
	by duke.maptools.org (8.12.11/8.12.10) with ESMTP id mAE8TFHV021608
	for <tiff@lists.maptools.org>; Fri, 14 Nov 2008 03:29:15 -0500
Received: from webmail.helsinki.fi (mandragora.it.helsinki.fi
	[128.214.205.113])
	by sender-02.it.helsinki.fi (8.13.8/8.13.8) with ESMTP id
	mAE8TCbn020455
	for <tiff@lists.maptools.org>; Fri, 14 Nov 2008 10:29:12 +0200
Received: from dsl-hkibrasgw1-fea8de00-9.dhcp.inet.fi
	(dsl-hkibrasgw1-fea8de00-9.dhcp.inet.fi [80.222.168.9]) by
	webmail.helsinki.fi (Horde Framework) with HTTP;
	Fri, 14 Nov 2008 10:29:12 +0200
Message-ID: <20081114102912.182940jlte5kpjh4@webmail.helsinki.fi>
Date: Fri, 14 Nov 2008 10:29:12 +0200
From: "Ilkka Korpela" <Ilkka.Korpela@Helsinki.fi>
To: tiff@lists.maptools.org
MIME-Version: 1.0
Content-Type: text/plain;
	charset=ISO-8859-1;
	DelSp="Yes";
	format="flowed"
Content-Disposition: inline
User-Agent: Internet Messaging Program (IMP) H3 (4.2)
X-Sender: korpela@mappi.helsinki.fi
Content-Transfer-Encoding: 8bit
X-MIME-Autoconverted: from quoted-printable to 8bit by duke.maptools.org id
	mAE8TFHV021608
Subject: [Tiff] Memory leak (TIFFOpen, TIFFReadTile)?
X-BeenThere: tiff@lists.maptools.org
X-Mailman-Version: 2.1.4
Precedence: list
Reply-To: Ilkka.Korpela@Helsinki.fi
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-SpamCatcher-Score: 50 [XX]

Hi

   I have some huge TIFFs, size upto 2 gB, 1, 3 or 4 bands, 2 bytes
per band, tiled. Huge aerial image-scanner mats with the ads80 instrument,

   I'm trying to make untiled greyscale/rgb pixel-interleaved
raw-image versions, I am working with MS Vis studio 6 and C
in an XP machine.

   I believe I am using the 3.6.1 version of libbtiff. libtiff.lib
is from Feb 2004.

  I now have a working solution for the task of untiling and writing
a bip-image, but there is a memory leak somewhere? I am reading
and converting a 1517454784 bytes file.  XP's task manager tells me
  the process is gradually consuming nearly 1.3 Gbytes at the end.

   I tried with _TIFFmalloc() and malloc() but it does not make
a difference - memory consumption goes up. Any suggestions out there?
Here is the simple code (include statements not there),:

ilkka

struct NRGB16 {
  uint16 N;
  uint16 R;
  uint16 G;
  uint16 B;
};

int main(int argc, char* argv[])
{
// Open the 1-2 GByte block file (ads40-imagery)
TIFF* tif = TIFFOpen("c:\\temp\\ads40\\O-08230753NRGBN00AL2_0_0.tif", "r");
FILE *fOut;
  uint32 i;
	 if (tif) {
	 uint32 imageWidth, imageLength;
	 uint32 tileWidth, tileLength;
	 uint32 x, y;
           NRGB16* buf;
	 unsigned char Red, Green, Nir;
           int Column, Row, OldRow;
          // Query the Image & Tile dimensions
	 TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &imageWidth);
	 TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &imageLength);
	 TIFFGetField(tif, TIFFTAG_TILEWIDTH, &tileWidth);
	 TIFFGetField(tif, TIFFTAG_TILELENGTH, &tileLength);
// Open the output file, 3-channel viewable version of the aerial image
fOut=fopen("c:\\temp\\ads40\\O-08230753NRGBN00AL2_0_0_CIR_uint8.raw","wb");
OldRow = -1;
// Allocate storage for the 4-channel, 2 bytes per channel image TILE  
		  buf =  (NRGB16*)malloc(tileLength*tileWidth*4*2);
// Start untiling the image
	  for (y = 0; y < imageLength; y += tileLength)
	   for (x = 0; x < imageWidth; x += tileWidth)
	  {
	   i=TIFFReadTile(tif, &buf[0], x, y, 0, 0);
	   for (i=0;i < tileLength*tileWidth;i=i+1)
	   {   // Carry out simple 16-8 bit reduction
	   Nir = 255; Green = 255; Red = 255;
	  if ( double(buf[i].N)/20 <= 255) Nir = unsigned char(buf[i].N/20);
	  if ( double(buf[i].R)/15 <= 255) Red = unsigned char(buf[i].R/15);
	  if ( double(buf[i].G)/15 <= 255) Green = unsigned char(buf[i].G/15);
            // Compute the global col,row coordinates in the untiled image
            Column = x + (i % tileWidth);
            Row = y + (i / tileWidth);
	 // Check if we started a new row, set the file pointer accordingly
    if (Row != OldRow) {fseek(fOut,Row*imageWidth*3+Column*3, SEEK_SET);}
    OldRow = Row;
    // Write the RGB-bytes
    fwrite(&Nir, 1, 1, fOut);
    fwrite(&Green, 1, 1, fOut);
    fwrite(&Red, 1, 1, fOut);
    }
    // Memory leak? Try reallocating the buf
    buf = (NRGB16*)realloc(buf,tileLength*tileWidth*4*2);
// Watch for memory sizes of these blocks (showing no leak here)
    printf("%d %d\n",_msize(tif),_msize(buf));
    }
    // Finally free everything
    free(buf);
    fclose(fOut);
    TIFFClose(tif);
  }
  return 0;
}










-- 
Ilkka Korpela
http://www.helsinki.fi/~korpela


_______________________________________________
Tiff mailing list: Tiff@lists.maptools.org
http://lists.maptools.org/mailman/listinfo/tiff
http://www.remotesensing.org/libtiff/
