[libpng16]  Work around for duplicate row start calls; added warning messages.

This turns on PNG_FLAG_DETECT_UNINITIALIZED to detect app code that
fails to call one of the 'start' routines (not enabled in libpng-1.5
because it is technically an API change, since it did normally work
before.)  It also makes duplicate calls to png_read_start_row (an
internal function called at the start of the image read) benign, as
they were before changes to use png_inflate_claim. Somehow webkit is
causing this to happen; this is probably a mis-feature in the zlib
changes so this commit is only a work-round.
diff --git a/ANNOUNCE b/ANNOUNCE
index 40d92e3..b678012 100644
--- a/ANNOUNCE
+++ b/ANNOUNCE
@@ -1,5 +1,5 @@
 
-Libpng 1.6.0beta19 - March 17, 2012
+Libpng 1.6.0beta19 - March 18, 2012
 
 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.
@@ -318,7 +318,16 @@
     read benign errors to warnings (regardless of the system default, unless
     this is disabled in which case the simplified API can't be built.)
 
-Version 1.6.0beta19 [March 17, 2012]
+Version 1.6.0beta19 [March 18, 2012]
+  Work around for duplicate row start calls; added warning messages.
+    This turns on PNG_FLAG_DETECT_UNINITIALIZED to detect app code that
+    fails to call one of the 'start' routines (not enabled in libpng-1.5
+    because it is technically an API change, since it did normally work
+    before.)  It also makes duplicate calls to png_read_start_row (an
+    internal function called at the start of the image read) benign, as
+    they were before changes to use png_inflate_claim. Somehow webkit is
+    causing this to happen; this is probably a mis-feature in the zlib
+    changes so this commit is only a work-round.
 
 Send comments/corrections/commendations to png-mng-implement at lists.sf.net
 (subscription required; visit
diff --git a/CHANGES b/CHANGES
index cf990b8..9d49879 100644
--- a/CHANGES
+++ b/CHANGES
@@ -4069,7 +4069,16 @@
     read benign errors to warnings (regardless of the system default, unless
     this is disabled in which case the simplified API can't be built.)
 
-Version 1.6.0beta19 [March 17, 2012]
+Version 1.6.0beta19 [March 18, 2012]
+  Work around for duplicate row start calls; added warning messages.
+    This turns on PNG_FLAG_DETECT_UNINITIALIZED to detect app code that
+    fails to call one of the 'start' routines (not enabled in libpng-1.5
+    because it is technically an API change, since it did normally work
+    before.)  It also makes duplicate calls to png_read_start_row (an
+    internal function called at the start of the image read) benign, as
+    they were before changes to use png_inflate_claim. Somehow webkit is
+    causing this to happen; this is probably a mis-feature in the zlib
+    changes so this commit is only a work-round.
 
 Send comments/corrections/commendations to png-mng-implement at lists.sf.net
 (subscription required; visit
diff --git a/pngread.c b/pngread.c
index 4601f8e..56281a2 100644
--- a/pngread.c
+++ b/pngread.c
@@ -50,6 +50,11 @@
    {
       png_ptr->mode = PNG_IS_READ_STRUCT;
 
+      /* Turn this on for all transforms in an attempt to detect failure to call
+       * the image reading start stuff.
+       */
+      png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED;
+
       /* Added in libpng-1.6.0; this can be used to detect a read structure if
        * required (it will be zero in a write structure.)
        */
diff --git a/pngrtran.c b/pngrtran.c
index 1cc61c6..b9a7443 100644
--- a/pngrtran.c
+++ b/pngrtran.c
@@ -826,7 +826,12 @@
       return;
 
    png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
-   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
+   if (png_ptr->flags & PNG_FLAG_ROW_INIT)
+   {
+      /* TODO: should probably be an error */
+      png_warning(png_ptr, "png_set_expand called after row initialization");
+      png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
+   }
 }
 
 /* GRR 19990627:  the following three functions currently are identical
@@ -857,7 +862,13 @@
       return;
 
    png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
-   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
+   if (png_ptr->flags & PNG_FLAG_ROW_INIT)
+   {
+      /* TODO: should probably be an error */
+      png_warning(png_ptr,
+         "png_set_palette_to_rgb called after row initialization");
+      png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
+   }
 }
 
 /* Expand grayscale images of less than 8-bit depth to 8 bits. */
@@ -870,7 +881,13 @@
       return;
 
    png_ptr->transformations |= PNG_EXPAND;
-   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
+   if (png_ptr->flags & PNG_FLAG_ROW_INIT)
+   {
+      /* TODO: should probably be an error */
+      png_warning(png_ptr,
+         "png_set_expand_gray_1_2_4_to_8 called after row initialization");
+      png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
+   }
 }
 
 
@@ -882,7 +899,13 @@
    png_debug(1, "in png_set_tRNS_to_alpha");
 
    png_ptr->transformations |= (PNG_EXPAND | PNG_EXPAND_tRNS);
-   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
+   if (png_ptr->flags & PNG_FLAG_ROW_INIT)
+   {
+      /* TODO: should probably be an error */
+      png_warning(png_ptr,
+         "png_set_tRNS_to_alpha called after row initialization");
+      png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
+   }
 }
 #endif /* defined(PNG_READ_EXPAND_SUPPORTED) */
 
@@ -899,7 +922,13 @@
       return;
 
    png_ptr->transformations |= (PNG_EXPAND_16 | PNG_EXPAND | PNG_EXPAND_tRNS);
-   png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
+   if (png_ptr->flags & PNG_FLAG_ROW_INIT)
+   {
+      /* TODO: should probably be an error */
+      png_warning(png_ptr,
+         "png_set_expand_16 called after row initialization");
+      png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
+   }
 
    /* New API, make sure apps call the correct initializers: */
    png_ptr->flags |= PNG_FLAG_DETECT_UNINITIALIZED;
@@ -917,7 +946,13 @@
       /* Because rgb must be 8 bits or more: */
       png_set_expand_gray_1_2_4_to_8(png_ptr);
       png_ptr->transformations |= PNG_GRAY_TO_RGB;
-      png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
+      if (png_ptr->flags & PNG_FLAG_ROW_INIT)
+      {
+         /* TODO: should probably be an error */
+         png_warning(png_ptr,
+            "png_set_gray_to_rgb called after row initialization");
+         png_ptr->flags &= ~PNG_FLAG_ROW_INIT;
+      }
    }
 }
 #endif
diff --git a/pngrutil.c b/pngrutil.c
index 6d7eec4..3a89247 100644
--- a/pngrutil.c
+++ b/pngrutil.c
@@ -4105,6 +4105,17 @@
 
    png_debug(1, "in png_read_start_row");
 
+   /* Because init_read_transformations, below, modifies values in png_struct
+    * it will not always work correctly if called twice.  This error detects
+    * that condition but just warns, because it does tend to work most of the
+    * time.
+    */
+   if (png_ptr->flags & PNG_FLAG_ROW_INIT)
+   {
+      png_warning(png_ptr, "unexpected duplicate call to png_read_start_row");
+      png_ptr->zowner = 0; /* release previous claim */
+   }
+
 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
    png_init_read_transformations(png_ptr);
 #endif