[devel] Improved the optimization of the zlib CMF byte

(see libpng-1.2.6beta03).
diff --git a/ANNOUNCE b/ANNOUNCE
index 1ed965e..cef2d7b 100644
--- a/ANNOUNCE
+++ b/ANNOUNCE
@@ -1,5 +1,5 @@
 
-Libpng 1.5.3beta02 - April 1, 2011
+Libpng 1.5.3beta02 - April 2, 2011
 
 This is not intended to be a public release.  It will be replaced
 within a few weeks by a public version or by another test version.
@@ -31,10 +31,11 @@
   Added API functions to set parameters for zlib compression of non-IDAT
     chunks.
 
-Version 1.5.3beta02 [April 1, 2011]
+Version 1.5.3beta02 [April 2, 2011]
   Updated scripts/symbols.def with new API functions.
   Only compile the new zlib re-initializing code when text or iCCP is
     supported, using PNG_WRITE_COMPRESSED_TEXT_SUPPORTED macro.
+  Improved the optimization of the zlib CMF byte (see libpng-1.2.6beta03).
 
 Send comments/corrections/commendations to png-mng-implement at lists.sf.net:
 (subscription required; visit
diff --git a/CHANGES b/CHANGES
index d2c3134..6320989 100644
--- a/CHANGES
+++ b/CHANGES
@@ -3292,10 +3292,11 @@
   Added API functions to set parameters for zlib compression of non-IDAT
     chunks.
 
-Version 1.5.3beta02 [April 1, 2011]
+Version 1.5.3beta02 [April 2, 2011]
   Updated scripts/symbols.def with new API functions.
   Only compile the new zlib re-initializing code when text or iCCP is
     supported, using PNG_WRITE_COMPRESSED_TEXT_SUPPORTED macro.
+  Improved the optimization of the zlib CMF byte (see libpng-1.2.6beta03).
 
 Send comments/corrections/commendations to png-mng-implement at lists.sf.net
 (subscription required; visit
diff --git a/pngwutil.c b/pngwutil.c
index bf5872f..31fea37 100644
--- a/pngwutil.c
+++ b/pngwutil.c
@@ -839,13 +839,23 @@
          if (length >= 2 &&
              png_ptr->height < 16384 && png_ptr->width < 16384)
          {
-            /* Compute the maximum possible length of the datastream
-             *
-             * To do: verify that this works with interlaced files
+            /* Compute the maximum possible length of the datastream */
+
+            /* Number of pixels, plus for each row a filter byte and possible
+             * padding byte
              */
             png_uint_32 uncompressed_idat_size = png_ptr->height *
                 ((png_ptr->width *
                 png_ptr->channels * png_ptr->bit_depth + 15) >> 3);
+
+            /* If it's interlaced, each block of 8 rows is sent as up to
+             * 14 rows, i.e., 6 additional rows, each with a filter byte
+             * and possibly a padding byte
+             */
+            if (png_ptr->interlaced)
+               uncompressed_idat_size += ((png_ptr->height + 7)/8) *
+                   (png_ptr->bit_depth < 8 ? 12 : 6);
+
             unsigned int z_cinfo = z_cmf >> 4;
             unsigned int half_z_window_size = 1 << (z_cinfo + 7);
             while (uncompressed_idat_size <= half_z_window_size &&