[libpng16] Check for EOF in contrib/pngminus/pnm2png.c (Paul Stewart).

Ignore "#" delimited comments in input file to pnm2png.c.
diff --git a/ANNOUNCE b/ANNOUNCE
index 268cf09..de4f88b 100644
--- a/ANNOUNCE
+++ b/ANNOUNCE
@@ -1,5 +1,5 @@
 
-Libpng 1.6.3beta05 - May 6, 2013
+Libpng 1.6.3beta05 - May 7, 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.
@@ -45,7 +45,14 @@
   Calculate our own zlib windowBits when decoding rather than trusting the
     CMF bytes in the PNG datastream.
 
-Version 1.6.3beta05 [May 6, 2013]
+Version 1.6.3beta05 [May 7, 2013]
+  Choose to use windowBits==15 or the zlib header setting via the
+    benign-errors setting.  If benign errors are allowed, then ignore
+    the windowBits setting in the zlib header.
+  Zlib-1.2.8 and earlier don't allow us to decrease the windowBits, so
+    undid the improvement in beta04.
+  Check for EOF in contrib/pngminus/pnm2png.c (Paul Stewart).
+  Ignore "#" delimited comments in input file to pnm2png.c.
 
 Send comments/corrections/commendations to png-mng-implement at lists.sf.net
 (subscription required; visit
diff --git a/CHANGES b/CHANGES
index 57cacd2..72c9874 100644
--- a/CHANGES
+++ b/CHANGES
@@ -4528,7 +4528,14 @@
   Calculate our own zlib windowBits when decoding rather than trusting the
     CMF bytes in the PNG datastream.
 
-Version 1.6.3beta05 [May 6, 2013]
+Version 1.6.3beta05 [May 7, 2013]
+  Choose to use windowBits==15 or the zlib header setting via the
+    benign-errors setting.  If benign errors are allowed, then ignore
+    the windowBits setting in the zlib header.
+  Zlib-1.2.8 and earlier don't allow us to decrease the windowBits, so
+    undid the improvement in beta04.
+  Check for EOF in contrib/pngminus/pnm2png.c (Paul Stewart).
+  Ignore "#" delimited comments in input file to pnm2png.c.
 
 Send comments/corrections/commendations to png-mng-implement at lists.sf.net
 (subscription required; visit
diff --git a/contrib/pngminus/pnm2png.c b/contrib/pngminus/pnm2png.c
index d098e33..9530112 100644
--- a/contrib/pngminus/pnm2png.c
+++ b/contrib/pngminus/pnm2png.c
@@ -460,19 +460,32 @@
 void get_token(FILE *pnm_file, char *token)
 {
   int i = 0;
+  int ret;
 
-  /* remove white-space */
+  /* remove white-space and comment lines */
   do
   {
-    token[i] = (unsigned char) fgetc (pnm_file);
+    ret = fgetc(pnm_file);
+    if (ret == '#') {
+      /* the rest of this line is a comment */
+      do
+      {
+        ret = fgetc(pnm_file);
+      }
+      while ((ret != '\n') && (ret != '\r') && (ret != EOF));
+    }
+    if (ret == EOF) break;
+    token[i] = (unsigned char) ret;
   }
   while ((token[i] == '\n') || (token[i] == '\r') || (token[i] == ' '));
 
   /* read string */
   do
   {
+    ret = fgetc(pnm_file);
+    if (ret == EOF) break;
     i++;
-    token[i] = (unsigned char) fgetc (pnm_file);
+    token[i] = (unsigned char) ret;
   }
   while ((token[i] != '\n') && (token[i] != '\r') && (token[i] != ' '));
 
diff --git a/pngrutil.c b/pngrutil.c
index 305d8a5..79898ea 100644
--- a/pngrutil.c
+++ b/pngrutil.c
@@ -4163,7 +4163,8 @@
 }
 #endif /* PNG_SEQUENTIAL_READ_SUPPORTED */
 
-#ifdef PNG_READ_OPTIMIZE_WINDOWBITS_SUPPORTED
+#ifdef PNG_READ_OPTIMIZE_WINDOWBITS_SUPPORTED 
+#if ZLIB_VERNUM > 0x1280
 /* This is the code to to select a windowBits value to match the smallest
  * possible sliding window needed to contain the entire uncompressed image.
  */
@@ -4230,6 +4231,7 @@
       return 0xffffffffU;
 }
 
+#endif /* ZLIB_VERNUM */
 #endif /* PNG_READ_OPTIMIZE_WINDOWBITS_SUPPORTED */
 
 void /* PRIVATE */
@@ -4516,23 +4518,37 @@
       png_free(png_ptr, buffer);
    }
 
-   /* Finally claim the zstream for the inflate of the IDAT data, using the
-    * windowBts predicted from the uncompressed data size, not the value from
-    * the stream.  If READ_OPTIMIZE_WINDOWBITS_SUPPORTED is not defined, then
-    * simply use a 32kbyte window (windowBits=15).
-    *
-    * To do: make this behavior optional via a run-time png_set_something(),
-    * with options to use windowBits=0 (use the zlib header data),
-    * windowBits=15 (use a 32kbyte window), or required_window_bits
-    * computed from the image size, pixel size, and interlacing setting.
-    */
 #ifdef PNG_READ_OPTIMIZE_WINDOWBITS_SUPPORTED
-   if (png_inflate_claim(png_ptr, png_IDAT,
-      required_window_bits(png_read_image_size(png_ptr))) != Z_OK)
+   /* To do in libpng17: get windowBits from the CMF bytes and select the
+    * smaller of that and the required_window_bits.  Requires a one-byte
+    * lookahead into the first IDAT chunk data, and requires actually
+    * injecting the revised CMF bytes into the datastream before reading.
+    */
+   {
+#if ZLIB_VERNUM < 0x1290
+      unsigned int windowBits;
+#endif /* ZLIB_VERNUM */
+
+      if (png_ptr->flags & PNG_FLAG_BENIGN_ERRORS_WARN)
+      {
+#if ZLIB_VERNUM < 0x1290
+         windowBits=15;
 #else
-   if (png_inflate_claim(png_ptr, png_IDAT, 15) != Z_OK)
+         /* Compute required windowBits from the image size, pixel size, and
+          * interlacing setting.
+          */
+         windowBits=required_window_bits(png_read_image_size(png_ptr));
+#endif /* ZLIB_VERNUM */
+      }
+
+      else
 #endif
-      png_error(png_ptr, png_ptr->zstream.msg);
+         windowBits=0; /* Use the setting from the zlib CMF bytes */
+
+      /* Finally claim the zstream for the inflate of the IDAT data */
+      if (png_inflate_claim(png_ptr, png_IDAT, windowBits) != Z_OK)
+         png_error(png_ptr, png_ptr->zstream.msg);
+   }
 
    png_ptr->flags |= PNG_FLAG_ROW_INIT;
 }