Merge pull request #1516 from stweil/actions
[openjpeg.git] / thirdparty / libtiff / tif_flush.c
index 7ecc4d8345d4a64051a89c0f9c977da84ad7e48a..fd14e4cdae11b1889c61e5a21136f43b636a55fa 100644 (file)
@@ -1,4 +1,4 @@
-/* $Header: /cvs/maptools/cvsroot/libtiff/libtiff/tif_flush.c,v 1.3.2.1 2010-06-08 18:50:42 bfriesen Exp $ */
+/* $Id: tif_flush.c,v 1.9 2010-03-31 06:40:10 fwarmerdam Exp $ */
 
 /*
  * Copyright (c) 1988-1997 Sam Leffler
 int
 TIFFFlush(TIFF* tif)
 {
+    if( tif->tif_mode == O_RDONLY )
+        return 1;
 
-       if (tif->tif_mode != O_RDONLY) {
-               if (!TIFFFlushData(tif))
-                       return (0);
-               if ((tif->tif_flags & TIFF_DIRTYDIRECT) &&
-                   !TIFFWriteDirectory(tif))
-                       return (0);
-       }
-       return (1);
+    if (!TIFFFlushData(tif))
+        return (0);
+                
+    /* In update (r+) mode we try to detect the case where 
+       only the strip/tile map has been altered, and we try to 
+       rewrite only that portion of the directory without 
+       making any other changes */
+                
+    if( (tif->tif_flags & TIFF_DIRTYSTRIP)
+        && !(tif->tif_flags & TIFF_DIRTYDIRECT) 
+        && tif->tif_mode == O_RDWR )
+    {
+        uint64  *offsets=NULL, *sizes=NULL;
+
+        if( TIFFIsTiled(tif) )
+        {
+            if( TIFFGetField( tif, TIFFTAG_TILEOFFSETS, &offsets ) 
+                && TIFFGetField( tif, TIFFTAG_TILEBYTECOUNTS, &sizes ) 
+                && _TIFFRewriteField( tif, TIFFTAG_TILEOFFSETS, TIFF_LONG8, 
+                                      tif->tif_dir.td_nstrips, offsets )
+                && _TIFFRewriteField( tif, TIFFTAG_TILEBYTECOUNTS, TIFF_LONG8, 
+                                      tif->tif_dir.td_nstrips, sizes ) )
+            {
+                tif->tif_flags &= ~TIFF_DIRTYSTRIP;
+                tif->tif_flags &= ~TIFF_BEENWRITING;
+                return 1;
+            }
+        }
+        else
+        {
+            if( TIFFGetField( tif, TIFFTAG_STRIPOFFSETS, &offsets ) 
+                && TIFFGetField( tif, TIFFTAG_STRIPBYTECOUNTS, &sizes ) 
+                && _TIFFRewriteField( tif, TIFFTAG_STRIPOFFSETS, TIFF_LONG8, 
+                                      tif->tif_dir.td_nstrips, offsets )
+                && _TIFFRewriteField( tif, TIFFTAG_STRIPBYTECOUNTS, TIFF_LONG8, 
+                                      tif->tif_dir.td_nstrips, sizes ) )
+            {
+                tif->tif_flags &= ~TIFF_DIRTYSTRIP;
+                tif->tif_flags &= ~TIFF_BEENWRITING;
+                return 1;
+            }
+        }
+    }
+
+    if ((tif->tif_flags & (TIFF_DIRTYDIRECT|TIFF_DIRTYSTRIP)) 
+        && !TIFFRewriteDirectory(tif))
+        return (0);
+
+    return (1);
 }
 
 /*
@@ -56,7 +99,7 @@ int
 TIFFFlushData(TIFF* tif)
 {
        if ((tif->tif_flags & TIFF_BEENWRITING) == 0)
-               return (0);
+               return (1);
        if (tif->tif_flags & TIFF_POSTENCODE) {
                tif->tif_flags &= ~TIFF_POSTENCODE;
                if (!(*tif->tif_postencode)(tif))
@@ -65,6 +108,7 @@ TIFFFlushData(TIFF* tif)
        return (TIFFFlushData1(tif));
 }
 
+/* vim: set ts=8 sts=8 sw=8 noet: */
 /*
  * Local Variables:
  * mode: c