Received: from mailgate010.isp.belgacom.be (mailgate010.isp.belgacom.be [195.238.6.3])
	by maildelivery010.isp.belgacom.be (Postfix) with ESMTP id D27471DC003
	for <joris.at.lebbeke@skynet.be>; Fri, 24 Aug 2007 23:20:39 +0200 (CEST)
Received: from r153.dmsolutions.ca (HELO duke.maptools.org) ([209.217.116.153])
  by in.mx.skynet.be with ESMTP; 24 Aug 2007 23:20:38 +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 l7OL2R1E011111;
	Fri, 24 Aug 2007 17:03:32 -0400
Received: from rune.pobox.com (rune.pobox.com [208.210.124.79])
	by duke.maptools.org (8.12.11/8.12.10) with ESMTP id l7OKwTXT010999;
	Fri, 24 Aug 2007 16:58:29 -0400
Received: from rune (localhost [127.0.0.1])
	by rune.pobox.com (Postfix) with ESMTP id AE2CE127469;
	Fri, 24 Aug 2007 16:58:48 -0400 (EDT)
Received: from [192.168.2.3] (dpc674728121.direcpc.com [67.47.28.121])
	by rune.sasl.smtp.pobox.com (Postfix) with ESMTP id 4BB141258E2;
	Fri, 24 Aug 2007 16:58:41 -0400 (EDT)
Message-ID: <46CF48E4.3070306@pobox.com>
Date: Fri, 24 Aug 2007 17:08:52 -0400
From: Frank Warmerdam <warmerdam@pobox.com>
User-Agent: Thunderbird 1.5.0.9 (X11/20070104)
MIME-Version: 1.0
To: TIFF mailing list <tiff@lists.maptools.org>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
Content-Transfer-Encoding: 7bit
Cc: gdal-dev <gdal-dev@lists.maptools.org>
Subject: [Tiff] A few libtiff4 changes 
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.0000275c
X-MSP-STORE: Fri, 24 Aug 2007 23:20:40 +0200
Status: RO

Joris / Andrey,

I made a few changes in libtiff4 to get GDAL overview and multi-directory
support working smoothly again.  These changes are now all in CVS.

This change just added a runtime check for a check that was
only an assertion before.  I don't know if it is strictly useful,
but it seemed prudent.

Index: tif_dirread.c
===================================================================
RCS file: /cvs/maptools/cvsroot/libtiff/libtiff/tif_dirread.c,v
retrieving revision 1.129
diff -r1.129 tif_dirread.c
4504a4505,4511
 >         if( fii == 0xFFFFFFFF )
 >         {
 >             TIFFErrorExt(tif->tif_clientdata, "TIFFFetchNormalTag",
 >                          "No definition found for tag %d",
 >                          dp->tdir_tag);
 >             return 0;
 >         }

This change, in _TIFFMergeFields() handled the case where a field
is defined that was already defined.  Previously an error was issued
and no fields were defined in the call.  This change only merges in
field definitions for which a definition does not already exist.
Duplicates are silently ignored.  Without this it seemed that any
multi-directory file was resulting in terrible redefinition warnings
even just with tiffinfo.

diff -r1.94 tif_dirinfo.c
333,344d332
<       for (i = 0; i < n; i++) {
<               const TIFFField *fip =
<                       TIFFFindField(tif, info[i].field_tag, TIFF_ANY);
<               if (fip) {
<       for (i = 0; i < n; i++) {
<               const TIFFField *fip =
<                       TIFFFindField(tif, info[i].field_tag, TIFF_ANY);
<               if (fip) {
<                       TIFFErrorExt(tif->tif_clientdata, module,
<                       "Field with tag %lu is already registered as \"%s\"",
<                                    (unsigned int) info[i].field_tag,
<                                    fip->field_name);
<                       return 0;
<               }
<       }
<
361a350
 >
363,364c352,361
<       for (i = 0; i < n; i++)
<               *tp++ = (TIFFField *) (info + i);       /* XXX */
---
 >       for (i = 0; i < n; i++) {
 >               const TIFFField *fip =
 >                       TIFFFindField(tif, info[i].field_tag, TIFF_ANY);
 >
 >                 /* only add definitions that aren't already present */
 >               if (!fip) {
 >                         tif->tif_fields[tif->tif_nfields] = (TIFFField *) (inf
o+i);
 >                         tif->tif_nfields++;
 >                 }
 >       }
367c364
<       qsort(tif->tif_fields, tif->tif_nfields += n,
---
 >       qsort(tif->tif_fields, tif->tif_nfields,

This change is the "real beef" I needed for GDAL.  GDAL's overview writer
creates directories for several directories for overviews, then goes back
and generates the imagery and updates the strip/tile pointers and rewrites
the directory.  The TIFFWriteDirectory() function was always writing a
"zero" next pointer instead of writing an existing valid next pointer back.
This change makes it possible to read a directory in the middle of the
chain, change something (hopefully not changing the size of the directory!)
and then write it back without disrupting the chain.

diff -r1.56 tif_dirwrite.c
784c784
<               *(uint32*)n=0;
---
 >               *(uint32*)n = tif->tif_nextdiroff;
814c814
<               *(uint64*)n=0;
---
 >               *(uint64*)n = tif->tif_nextdiroff;

With these changes GDAL overview generation seems to work fine, and in
fact the full GDAL test suite passes.  I am now inclined to migrate
libtiff4 into GDAL as it's primary working version.

Best regards,
-- 
---------------------------------------+--------------------------------------
I set the clouds in motion - turn up   | Frank Warmerdam, warmerdam@pobox.com
light and sound - activate the windows | http://pobox.com/~warmerdam
and watch the world go round - Rush    | President OSGeo, http://osgeo.org

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