Re: [gdal-dev] TIFFMergeFieldInfo error when adding overviews to large tiff

2013-04-17 Thread Even Rouault
Le mercredi 17 avril 2013 00:01:41, Eli Adam a écrit :
 On Tue, Apr 16, 2013 at 2:11 PM, Even Rouault
 
 even.roua...@mines-paris.org wrote:
  I've investigated a bit and I've discovered that while computing
  overviews, libtiff constantly switches from TIFF directories (main
  image, first overview,second overview, etc...). While doing this, it
  calls each time client code to install its own TIFF tags (libgeotiff for
  the geotiff tags, GDAL for the GDAL specific TIFF tags), but doesn't
  reset its array of existing tags. So each time the tags are added again
  and again, hence the huge memory allocation.
  
  Could you test the attached patch (assuming you build GDAL with internal
  libtiff) ?
 
 I can apply and test this on linux but don't know how to do it on
 windows.  I usually use the nightly windows builds from Tamas.  Should
 I test this on linux?

It would have been good to test this on the environmenet on which you got the 
problem, but while it isn't committed, I can see it is not really doable for 
you.

 
  It would be good if you could also open a ticket in Trac about that
  issue.
 
 I've opened a ticket, http://trac.osgeo.org/gdal/ticket/5054

Thanks. I've attached my patch and I'm waiting for the review of a libtiff 
committer (a.k.a. Frank) before committing it in GDAL.

 
 Eli
___
gdal-dev mailing list
gdal-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/gdal-dev


Re: [gdal-dev] TIFFMergeFieldInfo error when adding overviews to large tiff

2013-04-16 Thread Even Rouault
Le lundi 15 avril 2013 23:47:33, Eli Adam a écrit :
 Hi all,
 
 I'm adding internal overviews to a ~20G tiff file and I get this error
 (1000+ times):
 gdaladdo topo_mosaic_4326.tif 2 4 8 16 32 64 128 256 512 1024 --config
 COMPRESS_OVERVIEW PACKBITS
 ERROR 1: topo_mosaic_4326.tif:Failed to allocate memory for for fields
 array (1410046 elements of 16 bytes each)
 gdaladdo still completes and the file seems to work fine.  What does
 that error mean?  Does it impact the output file or overviews?  Is
 there a different way I should approach this?  Are there any general
 rules for which compression to select?  I've found JPEG-In-TIFF, using
 PHOTOMETRIC=YCBCR to work very well for RGB, but otherwise I'm unsure
 of the best compression for the type of raster (in this case single
 band, byte, colormap).

That's interesting. I've tried to reproduce your setup with an artificial blank 
input file of the same dimensions and here's what I get (after several hours):

$ gdaladdo topo_mosaic_4326.tif 2 4 8 16 32 64 128 256 512 1024 --config 
COMPRESS_OVERVIEW PACKBITS
0...10...20...30...40...50...60...70...80...90...100 - done.
Current VSI memory usage: 0 bytes
Maximum VSI memory usage: 2361761086 bytes
Number of calls to VSIMalloc()  : 43981823
Number of calls to VSICalloc()  : 1259
Number of calls to VSIRealloc() : 17378556
Number of calls to VSIFree(): 43983082
VSIMalloc + VSICalloc - VSIFree : 0

The interesting part is the memory usage report. It is based on an 
instrumented version of GDAL that instruments each memory allocation routine. 
This is with the internal version of libtiff that redirects the TIFF memory 
allocation routines to the GDAL ones. So apparently there wouldn't be a memory 
leak (some valgrind'ing should be done on a smaller file to confirm), but a 
maximum memory usage of 2.3 GB ! No wonder why it fails on a Windows 32 bit 
(my test was done a Linux 64bit). I'm not sure why such a high memory use 
occurs. Some investigation should be done.

As far as the error message you've seen, I wouldn't swear that there's not 
some pixel corruption in the result you get...

As a potential track for a workaround (completely untested), perhaps you could 
try to do :

$ gdaladdo topo_mosaic_4326.tif 2 --config COMPRESS_OVERVIEW PACKBITS
$ gdaladdo topo_mosaic_4326.tif 4 --config COMPRESS_OVERVIEW PACKBITS
$ gdaladdo topo_mosaic_4326.tif 8 --config COMPRESS_OVERVIEW PACKBITS
$ gdaladdo topo_mosaic_4326.tif 16 --config COMPRESS_OVERVIEW PACKBITS
etc..



___
gdal-dev mailing list
gdal-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/gdal-dev


Re: [gdal-dev] TIFFMergeFieldInfo error when adding overviews to large tiff

2013-04-16 Thread Eli Adam
Even,

Thanks for checking into this and the ideas.

Eli

On Tue, Apr 16, 2013 at 10:18 AM, Even Rouault
even.roua...@mines-paris.org wrote:
 Le lundi 15 avril 2013 23:47:33, Eli Adam a écrit :
 Hi all,

 I'm adding internal overviews to a ~20G tiff file and I get this error
 (1000+ times):
 gdaladdo topo_mosaic_4326.tif 2 4 8 16 32 64 128 256 512 1024 --config
 COMPRESS_OVERVIEW PACKBITS
 ERROR 1: topo_mosaic_4326.tif:Failed to allocate memory for for fields
 array (1410046 elements of 16 bytes each)
 gdaladdo still completes and the file seems to work fine.  What does
 that error mean?  Does it impact the output file or overviews?  Is
 there a different way I should approach this?  Are there any general
 rules for which compression to select?  I've found JPEG-In-TIFF, using
 PHOTOMETRIC=YCBCR to work very well for RGB, but otherwise I'm unsure
 of the best compression for the type of raster (in this case single
 band, byte, colormap).

 That's interesting. I've tried to reproduce your setup with an artificial 
 blank
 input file of the same dimensions and here's what I get (after several hours):

 $ gdaladdo topo_mosaic_4326.tif 2 4 8 16 32 64 128 256 512 1024 --config
 COMPRESS_OVERVIEW PACKBITS
 0...10...20...30...40...50...60...70...80...90...100 - done.
 Current VSI memory usage: 0 bytes
 Maximum VSI memory usage: 2361761086 bytes
 Number of calls to VSIMalloc()  : 43981823
 Number of calls to VSICalloc()  : 1259
 Number of calls to VSIRealloc() : 17378556
 Number of calls to VSIFree(): 43983082
 VSIMalloc + VSICalloc - VSIFree : 0

 The interesting part is the memory usage report. It is based on an
 instrumented version of GDAL that instruments each memory allocation routine.
 This is with the internal version of libtiff that redirects the TIFF memory
 allocation routines to the GDAL ones. So apparently there wouldn't be a memory
 leak (some valgrind'ing should be done on a smaller file to confirm), but a
 maximum memory usage of 2.3 GB ! No wonder why it fails on a Windows 32 bit
 (my test was done a Linux 64bit). I'm not sure why such a high memory use
 occurs. Some investigation should be done.

 As far as the error message you've seen, I wouldn't swear that there's not
 some pixel corruption in the result you get...

 As a potential track for a workaround (completely untested), perhaps you could
 try to do :

 $ gdaladdo topo_mosaic_4326.tif 2 --config COMPRESS_OVERVIEW PACKBITS
 $ gdaladdo topo_mosaic_4326.tif 4 --config COMPRESS_OVERVIEW PACKBITS
 $ gdaladdo topo_mosaic_4326.tif 8 --config COMPRESS_OVERVIEW PACKBITS
 $ gdaladdo topo_mosaic_4326.tif 16 --config COMPRESS_OVERVIEW PACKBITS
 etc..



___
gdal-dev mailing list
gdal-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/gdal-dev


Re: [gdal-dev] TIFFMergeFieldInfo error when adding overviews to large tiff

2013-04-16 Thread Even Rouault
Le mardi 16 avril 2013 22:07:52, Eli Adam a écrit :
 Even,
 
 Thanks for checking into this and the ideas.

I've investigated a bit and I've discovered that while computing overviews, 
libtiff constantly switches from TIFF directories (main image, first 
overview,second overview, etc...). While doing this, it calls each time client 
code to install its own TIFF tags (libgeotiff for the geotiff tags, GDAL for 
the 
GDAL specific TIFF tags), but doesn't reset its array of existing tags. So each 
time the tags are added again and again, hence the huge memory allocation.

Could you test the attached patch (assuming you build GDAL with internal 
libtiff) ?

It would be good if you could also open a ticket in Trac about that issue.

Even

 
 Eli
 
Index: frmts/gtiff/libtiff/tif_dir.c
===
--- frmts/gtiff/libtiff/tif_dir.c	(révision 25916)
+++ frmts/gtiff/libtiff/tif_dir.c	(copie de travail)
@@ -1302,8 +1302,20 @@
 	tif-tif_tagmethods.printdir = NULL;
 	/*
 	 *  Give client code a chance to install their own
-	 *  tag extensions  methods, prior to compression overloads.
+	 *  tag extensions  methods, prior to compression overloads,
+ *  but do some prior cleanup first.
 	 */
+if (tif-tif_nfieldscompat  0) {
+uint32 i;
+
+for (i = 0; i  tif-tif_nfieldscompat; i++) {
+if (tif-tif_fieldscompat[i].allocated_size)
+_TIFFfree(tif-tif_fieldscompat[i].fields);
+}
+_TIFFfree(tif-tif_fieldscompat);
+tif-tif_nfieldscompat = 0;
+tif-tif_fieldscompat = NULL;
+}
 	if (_TIFFextender)
 		(*_TIFFextender)(tif);
 	(void) TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_NONE);
___
gdal-dev mailing list
gdal-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/gdal-dev

Re: [gdal-dev] TIFFMergeFieldInfo error when adding overviews to large tiff

2013-04-16 Thread Eli Adam
On Tue, Apr 16, 2013 at 2:11 PM, Even Rouault
even.roua...@mines-paris.org wrote:
 I've investigated a bit and I've discovered that while computing overviews,
 libtiff constantly switches from TIFF directories (main image, first
 overview,second overview, etc...). While doing this, it calls each time client
 code to install its own TIFF tags (libgeotiff for the geotiff tags, GDAL for 
 the
 GDAL specific TIFF tags), but doesn't reset its array of existing tags. So 
 each
 time the tags are added again and again, hence the huge memory allocation.

 Could you test the attached patch (assuming you build GDAL with internal
 libtiff) ?

I can apply and test this on linux but don't know how to do it on
windows.  I usually use the nightly windows builds from Tamas.  Should
I test this on linux?


 It would be good if you could also open a ticket in Trac about that issue.

I've opened a ticket, http://trac.osgeo.org/gdal/ticket/5054

Eli
___
gdal-dev mailing list
gdal-dev@lists.osgeo.org
http://lists.osgeo.org/mailman/listinfo/gdal-dev