Imported from libpng-0.82.tar
diff --git a/pngwrite.c b/pngwrite.c
index 2e2e371..50ea96b 100644
--- a/pngwrite.c
+++ b/pngwrite.c
@@ -57,25 +57,30 @@
    if (info->valid & PNG_INFO_bKGD)
       png_write_bKGD(png_ptr, &(info->background), info->color_type);
 #endif
+
 #if defined(PNG_WRITE_hIST_SUPPORTED)
    if (info->valid & PNG_INFO_hIST)
       png_write_hIST(png_ptr, info->hist, info->num_palette);
 #endif
+
 #if defined(PNG_WRITE_pHYs_SUPPORTED)
    if (info->valid & PNG_INFO_pHYs)
       png_write_pHYs(png_ptr, info->x_pixels_per_unit,
          info->y_pixels_per_unit, info->phys_unit_type);
 #endif
+
 #if defined(PNG_WRITE_oFFs_SUPPORTED)
    if (info->valid & PNG_INFO_oFFs)
       png_write_oFFs(png_ptr, info->x_offset, info->y_offset,
          info->offset_unit_type);
 #endif
+
 #if defined(PNG_WRITE_tIME_SUPPORTED)
    if (info->valid & PNG_INFO_tIME)
       png_write_tIME(png_ptr, &(info->mod_time));
    /* Check to see if we need to write text chunks */
 #endif
+
 #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
    if (info->num_text)
    {
@@ -84,6 +89,12 @@
       /* loop through the text chunks */
       for (i = 0; i < info->num_text; i++)
       {
+         if (strlen(info->text[i].key) > 80)
+         {
+            (*(png_ptr->warning_fn))(png_ptr,"tEXt keyword more than 80 chars");
+            continue;
+         }
+
          /* if chunk is compressed */
          if (info->text[i].compression >= 0)
          {
@@ -104,7 +115,8 @@
          }
       }
    }
-#endif
+#endif /* PNG_WRITE_tEXt_SUPPORTED || PNG_WRITE_zTXt_SUPPORTED */
+   png_ptr->mode = PNG_HAVE_IHDR;
 }
 
 /* writes the end of the png file.  If you don't want to write comments or
@@ -114,6 +126,9 @@
 void
 png_write_end(png_struct *png_ptr, png_info *info)
 {
+   if (info && png_ptr->mode == PNG_AFTER_IEND)
+     return;
+
    /* see if user wants us to write information chunks */
    if (info)
    {
@@ -122,6 +137,7 @@
       if (info->valid & PNG_INFO_tIME)
          png_write_tIME(png_ptr, &(info->mod_time));
 #endif
+
 #if defined(PNG_WRITE_tEXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
       /* check to see if we need to write comment chunks */
       if (info->num_text)
@@ -151,10 +167,12 @@
             }
          }
       }
-#endif
+#endif /* PNG_WRITE_tEXt_SUPPORTED || PNG_WRITE_zTXt_SUPPORTED */
    }
    /* write end of png file */
    png_write_IEND(png_ptr);
+
+   png_ptr->mode = PNG_AFTER_IEND;
 }
 
 #if defined(PNG_WRITE_tIME_SUPPORTED)
@@ -177,7 +195,7 @@
    tbuf = gmtime(&ttime);
    png_convert_from_struct_tm(ptime, tbuf);
 }
-#endif
+#endif /* PNG_WRITE_tIME_SUPPORTED */
 
 /* initialize png structure, and allocate any memory needed */
 void
@@ -243,11 +261,13 @@
 png_write_row(png_struct *png_ptr, png_bytef *row)
 {
    /* initialize transformations and other stuff if first time */
-   if (png_ptr->row_number == 0 && png_ptr->pass == 0)
+   if (png_ptr->mode < PNG_HAVE_IDAT)
    {
       png_write_start_row(png_ptr);
    }
 
+   png_ptr->mode = PNG_HAVE_IDAT;
+
 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
    /* if interlaced and not interested in row, return */
    if (png_ptr->interlaced && (png_ptr->transformations & PNG_INTERLACE))
@@ -305,7 +325,7 @@
             break;
       }
    }
-#endif
+#endif /* PNG_WRITE_INTERLACE_SUPPORTED */
 
    /* set up row info for transformations */
    png_ptr->row_info.color_type = png_ptr->color_type;
@@ -334,7 +354,7 @@
          return;
       }
    }
-#endif
+#endif /* PNG_WRITE_INTERLACE_SUPPORTED */
 
    /* handle other transformations */
    if (png_ptr->transformations)
@@ -367,6 +387,7 @@
    /* set up the zlib input buffer */
    png_ptr->zstream->next_in = png_ptr->row_buf;
    png_ptr->zstream->avail_in = (uInt)png_ptr->row_info.rowbytes + 1;
+
    /* repeat until we have compressed all the data */
    do
    {
@@ -378,9 +399,9 @@
       if (ret != Z_OK)
       {
          if (png_ptr->zstream->msg)
-            png_error(png_ptr, png_ptr->zstream->msg);
+            (*(png_ptr->error_fn))(png_ptr, png_ptr->zstream->msg);
          else
-            png_error(png_ptr, "zlib error");
+            (*(png_ptr->error_fn))(png_ptr, "zlib error");
       }
 
       /* see if it is time to write another IDAT */
@@ -396,8 +417,77 @@
 
    /* finish row - updates counters and flushes zlib if last row */
    png_write_finish_row(png_ptr);
+
+#if defined(PNG_WRITE_FLUSH_SUPPORTED)
+   png_ptr->flush_rows++;
+
+   if (png_ptr->flush_dist > 0 &&
+       png_ptr->flush_rows >= png_ptr->flush_dist)
+   {
+      png_write_flush(png_ptr);
+   }
+#endif /* PNG_WRITE_FLUSH_SUPPORTED */
 }
 
+#if defined(PNG_WRITE_FLUSH_SUPPORTED)
+/* Set the automatic flush interval or 0 to turn flushing off */
+void
+png_set_flush(png_struct *png_ptr, int nrows)
+{
+   png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
+}
+
+/* flush the current output buffers now */
+void
+png_write_flush(png_struct *png_ptr)
+{
+   char wrote_IDAT;
+
+   if (png_ptr->mode != PNG_HAVE_IDAT)
+     return;
+
+   do
+   {
+      int ret;
+
+      /* compress the data */
+      ret = deflate(png_ptr->zstream, Z_SYNC_FLUSH);
+      wrote_IDAT = 0;
+
+      /* check for compression errors */
+      if (ret != Z_OK)
+      {
+         if (png_ptr->zstream->msg)
+            (*(png_ptr->error_fn))(png_ptr, png_ptr->zstream->msg);
+         else
+            (*(png_ptr->error_fn))(png_ptr, "zlib error");
+      }
+
+      if (!png_ptr->zstream->avail_out)
+      {
+         /* write the IDAT and reset the zlib output buffer */
+         png_write_IDAT(png_ptr, png_ptr->zbuf,
+                        png_ptr->zbuf_size);
+         png_ptr->zstream->next_out = png_ptr->zbuf;
+         png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size;
+         wrote_IDAT = 1;
+      }
+   } while(wrote_IDAT == 1);
+
+   /* If there is any data left to be output, write it into a new IDAT */
+   if (png_ptr->zbuf_size != png_ptr->zstream->avail_out)
+   {
+      /* write the IDAT and reset the zlib output buffer */
+      png_write_IDAT(png_ptr, png_ptr->zbuf,
+                     png_ptr->zbuf_size - png_ptr->zstream->avail_out);
+      png_ptr->zstream->next_out = png_ptr->zbuf;
+      png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size;
+   }
+   png_ptr->flush_rows = 0;
+   (*(png_ptr->output_flush_fn))(png_ptr);
+}
+#endif /* PNG_WRITE_FLUSH_SUPPORTED */
+
 /* free any memory used in png struct */
 void
 png_write_destroy(png_struct *png_ptr)
@@ -416,6 +506,7 @@
    png_memset(png_ptr, 0, sizeof (png_struct));
    png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
 }
+
 void
 png_set_filtering(png_struct *png_ptr, int filter)
 {