[libpng16] Merged libpng-1.7.0 changes to make no-interlace configurations work

with test programs.
diff --git a/ANNOUNCE b/ANNOUNCE
index 8229de2..3931046 100644
--- a/ANNOUNCE
+++ b/ANNOUNCE
@@ -1,5 +1,5 @@
 
-Libpng 1.6.9beta02 - December 28, 2013
+Libpng 1.6.9beta02 - December 29, 2013
 
 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.
@@ -42,13 +42,15 @@
   Rebuilt configure scripts with automake-1.14.1 and autoconf-2.69
     in the tar distributions.
 
-Version 1.6.9beta02 [December 28, 2013]
+Version 1.6.9beta02 [December 29, 2013]
   Added checks for libpng 1.5 to pngvalid.c.  This supports the use of
     this version of pngvalid in libpng 1.5
   Merged with pngvalid.c from libpng-1.7 changes to create a single
     pngvalid.c
   Removed #error macro from contrib/tools/pngfix.c (Thomas Klausner).
   Merged pngrio.c, pngtrans.c, pngwio.c, and pngerror.c with libpng-1.7.0
+  Merged libpng-1.7.0 changes to make no-interlace configurations work
+    with test programs.
 
 Send comments/corrections/commendations to png-mng-implement at lists.sf.net
 (subscription required; visit
diff --git a/CHANGES b/CHANGES
index 92d3c63..2e2e037 100644
--- a/CHANGES
+++ b/CHANGES
@@ -4767,13 +4767,15 @@
   Rebuilt configure scripts with automake-1.14.1 and autoconf-2.69
     in the tar distributions.
 
-Version 1.6.9beta02 [December 28, 2013]
+Version 1.6.9beta02 [December 29, 2013]
   Added checks for libpng 1.5 to pngvalid.c.  This supports the use of
     this version of pngvalid in libpng 1.5
   Merged with pngvalid.c from libpng-1.7 changes to create a single
     pngvalid.c
   Removed #error macro from contrib/tools/pngfix.c (Thomas Klausner).
   Merged pngrio.c, pngtrans.c, pngwio.c, and pngerror.c with libpng-1.7.0
+  Merged libpng-1.7.0 changes to make no-interlace configurations work
+    with test programs.
 
 Send comments/corrections/commendations to png-mng-implement at lists.sf.net
 (subscription required; visit
diff --git a/contrib/libtests/pngvalid.c b/contrib/libtests/pngvalid.c
index 3e04597..c20427d 100644
--- a/contrib/libtests/pngvalid.c
+++ b/contrib/libtests/pngvalid.c
@@ -1,7 +1,7 @@
 
 /* pngvalid.c - validate libpng by constructing then reading png files.
  *
- * Last changed in libpng 1.6.9 [(PENDING RELEASE)]
+ * Last changed in libpng 1.5.18 [(PENDING RELEASE)]
  * Copyright (c) 2013 Glenn Randers-Pehrson
  * Written by John Cunningham Bowler
  *
@@ -49,6 +49,15 @@
 #  include <zlib.h>   /* For crc32 */
 #endif
 
+/* 1.6.1 added support for the configure test harness, which uses 77 to indicate
+ * a skipped test, in earlier versions we need to succeed on a skipped test, so:
+ */
+#if PNG_LIBPNG_VER < 10601
+#  define SKIP 0
+#else
+#  define SKIP 77
+#endif
+
 /* pngvalid requires write support and one of the fixed or floating point APIs.
  */
 #if defined(PNG_WRITE_SUPPORTED) &&\
@@ -3401,10 +3410,28 @@
 #define DEPTH(bd) ((png_byte)(1U << (bd)))
 
 /* This is just a helper for compiling on minimal systems with no write
- * interlacing support.
+ * interlacing support.  If there is no write interlacing we can't generate test
+ * cases with interlace:
  */
-#ifndef PNG_WRITE_INTERLACING_SUPPORTED
+#ifdef PNG_WRITE_INTERLACING_SUPPORTED
+#  define INTERLACE_LAST PNG_INTERLACE_LAST
+#  define check_interlace_type(type) ((void)(type))
+#else
+#  define INTERLACE_LAST (PNG_INTERLACE_NONE+1)
 #  define png_set_interlace_handling(a) (1)
+
+static void
+check_interlace_type(int PNG_CONST interlace_type)
+{
+   if (interlace_type != PNG_INTERLACE_NONE)
+   {
+      /* This is an internal error - --interlace tests should be skipped, not
+       * attempted.
+       */
+      fprintf(stderr, "pngvalid: no interlace support\n");
+      exit(99);
+   }
+}
 #endif
 
 /* Make a standardized image given a an image colour type, bit depth and
@@ -3420,6 +3447,8 @@
 {
    context(ps, fault);
 
+   check_interlace_type(interlace_type);
+
    Try
    {
       png_infop pi;
@@ -3560,12 +3589,8 @@
    {
       int interlace_type;
 
-#     ifdef PNG_WRITE_INTERLACING_SUPPORTED
-         for (interlace_type = PNG_INTERLACE_NONE;
-              interlace_type < PNG_INTERLACE_LAST; ++interlace_type)
-#     else
-         interlace_type = PNG_INTERLACE_NONE;
-#     endif
+      for (interlace_type = PNG_INTERLACE_NONE;
+           interlace_type < INTERLACE_LAST; ++interlace_type)
       {
          char name[FILE_NAME_SIZE];
 
@@ -3655,6 +3680,12 @@
 {
    context(ps, fault);
 
+   /* At present libpng does not support the write of an interlaced image unless
+    * PNG_WRITE_INTERLACING_SUPPORTED, even with do_interlace so the code here
+    * does the pixel interlace itself, so:
+    */
+   check_interlace_type(interlace_type);
+
    Try
    {
       png_infop pi;
@@ -3864,13 +3895,6 @@
 #        ifdef PNG_WRITE_INTERLACING_SUPPORTED
             make_size_image(ps, colour_type, DEPTH(bdlo), PNG_INTERLACE_ADAM7,
                width, height, 0);
-#        endif
-#        if defined(PNG_WRITE_INTERLACING_SUPPORTED) || PNG_LIBPNG_VER > 10518
-            /* This fails in 1.5.8 with a zlib stream error writing the rows of
-             * the internally generated interlaced images, but only when the
-             * read code is disabled: to be investigated.  Probably an erroneous
-             * #define out of the zlib deflate reset.
-             */
             make_size_image(ps, colour_type, DEPTH(bdlo), PNG_INTERLACE_ADAM7,
                width, height, 1);
 #        endif
@@ -3964,6 +3988,8 @@
 
    context(ps, fault);
 
+   check_interlace_type(interlace_type);
+
    Try
    {
       png_structp pp;
@@ -4066,12 +4092,8 @@
    {
       int interlace_type;
 
-#     ifdef PNG_WRITE_INTERLACING_SUPPORTED
-         for (interlace_type = PNG_INTERLACE_NONE;
-              interlace_type < PNG_INTERLACE_LAST; ++interlace_type)
-#     else
-         interlace_type = PNG_INTERLACE_NONE;
-#     endif
+      for (interlace_type = PNG_INTERLACE_NONE;
+           interlace_type < INTERLACE_LAST; ++interlace_type)
       {
          unsigned int test;
          char name[FILE_NAME_SIZE];
@@ -4269,6 +4291,7 @@
       dp->red_sBIT = dp->blue_sBIT = dp->green_sBIT = dp->alpha_sBIT =
          dp->bit_depth;
    dp->interlace_type = INTERLACE_FROM_ID(id);
+   check_interlace_type(dp->interlace_type);
    dp->id = id;
    /* All the rest are filled in after the read_info: */
    dp->w = 0;
@@ -4887,7 +4910,7 @@
    {
       standard_check_text(pp, tp, "image name", dp->ps->current->name);
 
-      /* This exists because prior to 1.6 the progressive reader left the
+      /* This exists because prior to 1.5.18 the progressive reader left the
        * png_struct z_stream unreset at the end of the image, so subsequent
        * attempts to use it simply returns Z_STREAM_END.
        */
@@ -4996,7 +5019,7 @@
     * interlaced images.
     */
    standard_text_validate(dp, pp, pi,
-      PNG_LIBPNG_VER >= 10600/*check_end: see comments above*/);
+      PNG_LIBPNG_VER >= 10518/*check_end: see comments above*/);
    standard_image_validate(dp, pp, 0, -1);
 }
 
@@ -5098,7 +5121,7 @@
       int interlace_type;
 
       for (interlace_type = PNG_INTERLACE_NONE;
-           interlace_type < PNG_INTERLACE_LAST; ++interlace_type)
+           interlace_type < INTERLACE_LAST; ++interlace_type)
       {
          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
             interlace_type, 0, 0, 0), 0/*do_interlace*/, pm->use_update_info);
@@ -5173,6 +5196,7 @@
          if (fail(pm))
             return 0;
 
+#     ifdef PNG_WRITE_INTERLACING_SUPPORTED
          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
             PNG_INTERLACE_ADAM7, w, h, 0), 0/*do_interlace*/,
             pm->use_update_info);
@@ -5186,6 +5210,7 @@
 
          if (fail(pm))
             return 0;
+#     endif
 
          /* Now validate the interlaced read side - do_interlace true,
           * in the progressive case this does actually make a difference
@@ -5198,12 +5223,14 @@
          if (fail(pm))
             return 0;
 
+#     ifdef PNG_WRITE_INTERLACING_SUPPORTED
          standard_test(&pm->this, FILEID(colour_type, DEPTH(bdlo), 0/*palette*/,
             PNG_INTERLACE_ADAM7, w, h, 0), 1/*do_interlace*/,
             pm->use_update_info);
 
          if (fail(pm))
             return 0;
+#     endif
       }
    }
 
@@ -9251,6 +9278,28 @@
       r = random_32();
       background.blue = (png_uint_16)r;
       background.gray = (png_uint_16)(r >> 16);
+
+      /* In earlier libpng versions, those where DIGITIZE is set, any background
+       * gamma correction in the expand16 case was done using 8-bit gamma
+       * correction tables, resulting in larger errors.  To cope with those
+       * cases use a 16-bit background value which will handle this gamma
+       * correction.
+       */
+#     if DIGITIZE
+         if (expand_16 && (do_background == PNG_BACKGROUND_GAMMA_UNIQUE ||
+                           do_background == PNG_BACKGROUND_GAMMA_FILE) &&
+            fabs(bg*screen_gamma-1) > PNG_GAMMA_THRESHOLD)
+         {
+            /* The background values will be looked up in an 8-bit table to do
+             * the gamma correction, so only select values which are an exact
+             * match for the 8-bit table entries:
+             */
+            background.red = (png_uint_16)((background.red >> 8) * 257);
+            background.green = (png_uint_16)((background.green >> 8) * 257);
+            background.blue = (png_uint_16)((background.blue >> 8) * 257);
+            background.gray = (png_uint_16)((background.gray >> 8) * 257);
+         }
+#     endif
    }
 
    else /* 8 bit colors */
@@ -10222,7 +10271,14 @@
          ++pm.use_update_info; /* Can call multiple times */
 
       else if (strcmp(*argv, "--interlace") == 0)
-         pm.interlace_type = PNG_INTERLACE_ADAM7;
+      {
+#        ifdef PNG_WRITE_INTERLACING_SUPPORTED
+            pm.interlace_type = PNG_INTERLACE_ADAM7;
+#        else
+            fprintf(stderr, "pngvalid: no write interlace support\n");
+            return SKIP;
+#        endif
+      }
 
       else if (strcmp(*argv, "--use-input-precision") == 0)
          pm.use_input_precision = 1U;
@@ -10521,11 +10577,6 @@
    fprintf(stderr,
       "pngvalid: no low level write support in libpng, all tests skipped\n");
    /* So the test is skipped: */
-#if PNG_LIBPNG_VER < 10601
-   /* Test harness support was only added in libpng 1.6.1: */
-   return 0;
-#else
-   return 77;
-#endif
+   return SKIP;
 }
 #endif
diff --git a/pngtest.c b/pngtest.c
index 422bef8..01038c9 100644
--- a/pngtest.c
+++ b/pngtest.c
@@ -1,7 +1,7 @@
 
 /* pngtest.c - a simple test program to test libpng
  *
- * Last changed in libpng 1.6.8 [December 19, 2013]
+ * Last changed in libpng 1.6.9 [(PENDING RELEASE)]
  * Copyright (c) 1998-2013 Glenn Randers-Pehrson
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -45,6 +45,11 @@
 
 /* Known chunks that exist in pngtest.png must be supported or pngtest will fail
  * simply as a result of re-ordering them.  This may be fixed in 1.7
+ *
+ * pngtest allocates a single row buffer for each row and overwrites it,
+ * therefore if the write side doesn't support the writing of interlaced images
+ * nothing can be done for an interlaced image (and the code below will fail
+ * horribly trying to write extra data after writing garbage).
  */
 #if defined PNG_READ_SUPPORTED && /* else nothing can be done */\
    defined PNG_READ_bKGD_SUPPORTED &&\
@@ -58,9 +63,15 @@
    defined PNG_READ_sRGB_SUPPORTED &&\
    defined PNG_READ_tEXt_SUPPORTED &&\
    defined PNG_READ_tIME_SUPPORTED &&\
-   defined PNG_READ_zTXt_SUPPORTED
+   defined PNG_READ_zTXt_SUPPORTED &&\
+   defined PNG_WRITE_INTERLACING_SUPPORTED
 
-#include "zlib.h"
+#ifdef PNG_ZLIB_HEADER
+#  include PNG_ZLIB_HEADER /* defined by pnglibconf.h from 1.7 */
+#else
+#  include "zlib.h"
+#endif
+
 /* Copied from pngpriv.h but only used in error messages below. */
 #ifndef PNG_ZBUF_SIZE
 #  define PNG_ZBUF_SIZE 8192
@@ -116,10 +127,6 @@
 static int error_count = 0; /* count calls to png_error */
 static int warning_count = 0; /* count calls to png_warning */
 
-#ifdef __TURBOC__
-#include <mem.h>
-#endif
-
 /* Define png_jmpbuf() in case we are using a pre-1.0.6 version of libpng */
 #ifndef png_jmpbuf
 #  define png_jmpbuf(png_ptr) png_ptr->jmpbuf
@@ -725,18 +732,18 @@
 static void
 write_sTER_chunk(png_structp write_ptr)
 {
-   png_byte png_sTER[5] = {115,  84,  69,  82, '\0'};
+   png_byte sTER[5] = {115,  84,  69,  82, '\0'};
 
    if (verbose)
       fprintf(STDERR, "\n stereo mode = %d\n", user_chunk_data.sTER_mode);
 
-   png_write_chunk(write_ptr, png_sTER, &user_chunk_data.sTER_mode, 1);
+   png_write_chunk(write_ptr, sTER, &user_chunk_data.sTER_mode, 1);
 }
 
 static void
 write_vpAg_chunk(png_structp write_ptr)
 {
-   png_byte png_vpAg[5] = {118, 112,  65, 103, '\0'};
+   png_byte vpAg[5] = {118, 112,  65, 103, '\0'};
 
    png_byte vpag_chunk_data[9];
 
@@ -749,7 +756,7 @@
    png_save_uint_32(vpag_chunk_data, user_chunk_data.vpAg_width);
    png_save_uint_32(vpag_chunk_data + 4, user_chunk_data.vpAg_height);
    vpag_chunk_data[8] = user_chunk_data.vpAg_units;
-   png_write_chunk(write_ptr, png_vpAg, vpag_chunk_data, 9);
+   png_write_chunk(write_ptr, vpAg, vpag_chunk_data, 9);
 }
 
 static void
@@ -830,6 +837,7 @@
    png_structp write_ptr;
    png_infop write_info_ptr;
    png_infop write_end_info_ptr;
+   int interlace_preserved = 1;
 #else
    png_structp write_ptr = NULL;
    png_infop write_info_ptr = NULL;
@@ -838,7 +846,7 @@
    png_bytep row_buf;
    png_uint_32 y;
    png_uint_32 width, height;
-   int num_pass, pass;
+   int num_pass = 1, pass;
    int bit_depth, color_type;
 
    row_buf = NULL;
@@ -1044,10 +1052,26 @@
           &color_type, &interlace_type, &compression_type, &filter_type))
       {
          png_set_IHDR(write_ptr, write_info_ptr, width, height, bit_depth,
-#ifdef PNG_WRITE_INTERLACING_SUPPORTED
             color_type, interlace_type, compression_type, filter_type);
-#else
-            color_type, PNG_INTERLACE_NONE, compression_type, filter_type);
+#ifndef PNG_READ_INTERLACING_SUPPORTED
+         /* num_pass will not be set below, set it here if the image is
+          * interlaced: what happens is that write interlacing is *not* turned
+          * on an the partial interlaced rows are written directly.
+          */
+         switch (interlace_type)
+         {
+            case PNG_INTERLACE_NONE:
+               num_pass = 1;
+               break;
+
+            case PNG_INTERLACE_ADAM7:
+               num_pass = 7;
+                break;
+
+            default:
+                png_error(read_ptr, "invalid interlace type");
+                /*NOT REACHED*/
+         }
 #endif
       }
    }
@@ -1340,14 +1364,10 @@
 #endif /* SINGLE_ROWBUF_ALLOC */
    pngtest_debug("Writing row data");
 
-#if defined(PNG_READ_INTERLACING_SUPPORTED) || \
-  defined(PNG_WRITE_INTERLACING_SUPPORTED)
+#ifdef PNG_READ_INTERLACING_SUPPORTED
    num_pass = png_set_interlace_handling(read_ptr);
-#  ifdef PNG_WRITE_SUPPORTED
-   png_set_interlace_handling(write_ptr);
-#  endif
-#else
-   num_pass = 1;
+   if (png_set_interlace_handling(write_ptr) != num_pass)
+      png_error(write_ptr, "png_set_interlace_handling: inconsistent num_pass");
 #endif
 
 #ifdef PNGTEST_TIMING
@@ -1579,6 +1599,7 @@
    }
 
 #ifdef PNG_WRITE_SUPPORTED /* else nothing was written */
+   if (interlace_preserved) /* else the files will be changed */
    {
       for (;;)
       {
@@ -1965,7 +1986,7 @@
    fprintf(STDERR,
       " test ignored because libpng was not built with read support\n");
    /* And skip this test */
-   return 77;
+   return PNG_LIBPNG_VER < 10600 ? 0 : 77;
 }
 #endif
 
diff --git a/tests/pngvalid-progressive-standard b/tests/pngvalid-progressive-standard
index 20ecd15..aa1b4b4 100755
--- a/tests/pngvalid-progressive-standard
+++ b/tests/pngvalid-progressive-standard
@@ -1,2 +1,2 @@
 #!/bin/sh
-exec ./pngvalid --standard --interlace
+exec ./pngvalid --standard --progressive-read