Imported from libpng-1.0.2a.tar
diff --git a/ANNOUNCE b/ANNOUNCE
index 2862fd7..83fa5fd 100644
--- a/ANNOUNCE
+++ b/ANNOUNCE
@@ -1,67 +1,38 @@
 
-Libpng 1.0.2 - June 14, 1998
+Libpng 1.0.2a - December 29, 1998
 
-This is a public release of libpng, intended for use in production codes.
+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.
 
-Changes since the previous public release (1.0.1):
+Changes since the last public release:
 
-  Optimized Paeth calculations by replacing abs() function calls with intrinsics
-  plus other loop optimizations. Improves avg decoding speed by about 20%.
-  Commented out i386istic "align" compiler flags in makefile.lnx.
-  Reduced the default warning level in some makefiles, to make them consistent.
-  Removed references to IJG and JPEG in the ansi2knr.c copyright statement.
-  Fixed a bug in png_do_strip_filler with XXRRGGBB => RRGGBB transformation.
-  Added grayscale and 16-bit capability to png_do_read_filler().
-  Fixed a bug in pngset.c, introduced in version 0.99c, that sets rowbytes
-    too large when writing an image with bit_depth < 8 (Bob Dellaca).
-  Corrected some bugs in the experimental weighted filtering heuristics.
-  Moved a misplaced pngrutil code block that truncates tRNS if it has more
-    than num_palette entries -- test was done before num_palette was defined.
-  Fixed a png_convert_to_rfc1123() bug that converts day 31 to 0 (Steve Eddins).
-  Changed compiler flags in makefile.wat for better optimization (Pawel Mrochen).
-  Relocated png_do_gray_to_rgb() within png_do_read_transformations() (Greg).
-  Relocated the png_composite macros from pngrtran.c to png.h (Greg).
-  Added makefile.sco (contributed by Mike Hopkirk).
-  Fixed a bug in pngrtran.c that would set channels=5 under some circumstances.
-  Added warnings when people try to use transforms they've defined out.
-  Collapsed 4 "i" and "c" loops into single "i" loops in pngrtran and pngwtran.
-  Revised paragraph about png_set_expand() in libpng.txt and libpng.3 (Greg)
-  Added max_pixel_depth=32 in pngrutil.c when using FILLER with palette images.
-  Moved PNG_WRITE_WEIGHTED_FILTER_SUPPORTED and PNG_WRITE_FLUSH_SUPPORTED
-    out of the PNG_WRITE_TRANSFORMS_NOT_SUPPORTED block of pngconf.h
-  Added "PNG_NO_WRITE_TRANSFORMS" etc., as alternatives for *_NOT_SUPPORTED,
-    for consistency, in pngconf.h
-  Added individual "ifndef PNG_NO_CAPABILITY" for the capabilities in pngconf.h
-    to make it easier to remove unwanted capabilities via the compile line
-  Made some corrections to grammar (which, it's) in documentation (Greg).
-  Corrected example.c, use of row_pointers in png_write_image().
-  Revised png_read_rows() to avoid repeated if-testing for NULL (A Kleinert)
-  More corrections to example.c, use of row_pointers in png_write_image()
-    and png_read_rows().
-  Added pngdll.mak and pngdef.pas to scripts directory, contributed by
-    Bob Dellaca, to make a png32bd.dll with Borland C++ 4.5
-  Fixed error in example.c with png_set_text: num_text is 3, not 2 (Guido V.)
-  Changed several loops from count-down to count-up, for consistency.
-  Revised libpng.txt and libpng.3 description of png_set_read|write_fn(), and
-    added warnings when people try to set png_read_fn and png_write_fn in
-    the same structure.
-  Added a test such that png_do_gamma will be done when num_trans==0
-    for truecolor images that have defined a background.  This corrects an
-    error that was introduced in libpng-0.90 that can cause gamma processing
-    to be skipped.
-  Added tests in png.h to include "trans" and "trans_values" in structures
-    when PNG_READ_BACKGROUND_SUPPORTED or PNG_READ_EXPAND_SUPPORTED is defined.
-  Add png_free(png_ptr->time_buffer) in png_destroy_read_struct()
-  Moved png_convert_to_rfc_1123() from pngwrite.c to png.c
-  Added capability for user-provided malloc_fn() and free_fn() functions,
-    and revised pngtest.c to demonstrate their use, replacing the
-    PNGTEST_DEBUG_MEM feature.
-  Added makefile.w32, for Microsoft C++ 4.0 and later (Tim Wegner).
-  Fixed two bugs in makefile.bor
+libpng-1.0.2a:
+
+  Replaced and extended code that was removed from png_set_filler() in 1.0.1a.
+  Fixed a bug in png_do_filler() that made it fail to write filler bytes in
+    the left-most pixel of each row (Kevin Bracey).
+  Changed "static pngcharp tIME_string" to "static char tIME_string[30]"
+    in pngtest.c (Duncan Simpson).
+  Fixed a problem in pngrutil.c: gray_to_rgb didn't always work with 16-bit.
+  Fixed a problem in png_read_push_finish_row(), which would not skip some
+    passes that it should skip, for images that are less than 3 pixels high.
+  Interchanged the order of calls to png_do_swap() and png_do_shift()
+    in pngwtran.c (John Cromer).
+  Added #ifdef PNG_DEBUG/#endif surrounding use of PNG_DEBUG in png.h .
+  Changed "bad adaptive filter type" from error to warning in pngrutil.c .
+  Fixed a documentation error about default filtering with 8-bit indexed-color.
+  Separated the PNG_NO_STDIO macro into PNG_NO_STDIO and PNG_NO_CONSOLE_IO
+    (L. Peter Deutsch).
+  Added png_set_rgb_to_gray() and png_get_rgb_to_gray_status() functions.
+  Added png_get_copyright() and png_get_header_version() functions.
+  Revised comments on png_set_progressive_read_fn() in libpng.txt and example.c
+  Added information about debugging in libpng.txt and libpng.3 .
+  Changed "ln -sf" to "ln -s -f" in makefile.s2x, makefile.lnx, and makefile.sco.
+  Removed lines after Dynamic Dependencies" in makefile.aco .
+  Revised makefile.dec to make a shared library (Jeremie Petit).
+  Removed trailing blanks from all files. 
 
 Send comments/corrections/commendations to
 png-implement@dworkin.wustl.edu or to randeg@alumni.rpi.edu
 
-Glenn Randers-Pehrson
-libpng maintainer
-PNG Development Group
+Glenn R-P
diff --git a/CHANGES b/CHANGES
index 6243b1a..05c7c62 100644
--- a/CHANGES
+++ b/CHANGES
@@ -89,7 +89,7 @@
      so that people are aware that they need to change their code
   changed filter selection API to allow selection of multiple filters
      since it didn't work in previous versions of libpng anyways
-  optimized filter selection code    
+  optimized filter selection code
   fixed png_set_background() to allow using an arbitrary RGB color for
      paletted images
   fixed gamma and background correction for paletted images, so
@@ -256,7 +256,7 @@
   Minor changes to pngtest.c to suppress compiler warnings.
   Removed "beta" language from documentation.
 version 0.99h [March 6, 1998, evening]
-  Minor changes to previous minor changes to pngtest.c 
+  Minor changes to previous minor changes to pngtest.c
   Changed PNG_READ_NOT_FULLY_SUPPORTED to PNG_READ_TRANSFORMS_NOT_SUPPORTED
   and added PNG_PROGRESSIVE_READ_NOT_SUPPORTED macro
 version 1.00 [March 7, 1998]
@@ -334,7 +334,7 @@
     Bob Dellaca, to make a png32bd.dll with Borland C++ 4.5
   Fixed error in example.c with png_set_text: num_text is 3, not 2 (Guido V.)
   Changed several loops from count-down to count-up, for consistency.
-version 1.0.2 [June 14, 1998]
+version 1.0.1e [June 6, 1998]
   Revised libpng.txt and libpng.3 description of png_set_read|write_fn(), and
     added warnings when people try to set png_read_fn and png_write_fn in
     the same structure.
@@ -350,4 +350,29 @@
     and revised pngtest.c to demonstrate their use, replacing the
     PNGTEST_DEBUG_MEM feature.
   Added makefile.w32, for Microsoft C++ 4.0 and later (Tim Wegner).
-  Fixed two bugs in makefile.bor
+version 1.0.2 [June 14, 1998]
+  Fixed two bugs in makefile.bor .
+version 1.0.2a [December 29, 1998]
+  Replaced and extended code that was removed from png_set_filler() in 1.0.1a.
+  Fixed a bug in png_do_filler() that made it fail to write filler bytes in
+    the left-most pixel of each row (Kevin Bracey).
+  Changed "static pngcharp tIME_string" to "static char tIME_string[30]"
+    in pngtest.c (Duncan Simpson).
+  Fixed a problem in pngrutil.c: gray_to_rgb didn't always work with 16-bit.
+  Fixed a problem in png_read_push_finish_row(), which would not skip some
+    passes that it should skip, for images that are less than 3 pixels high.
+  Interchanged the order of calls to png_do_swap() and png_do_shift()
+    in pngwtran.c (John Cromer).
+  Added #ifdef PNG_DEBUG/#endif surrounding use of PNG_DEBUG in png.h .
+  Changed "bad adaptive filter type" from error to warning in pngrutil.c .
+  Fixed a documentation error about default filtering with 8-bit indexed-color.
+  Separated the PNG_NO_STDIO macro into PNG_NO_STDIO and PNG_NO_CONSOLE_IO
+    (L. Peter Deutsch).
+  Added png_set_rgb_to_gray() and png_get_rgb_to_gray_status() functions.
+  Added png_get_copyright() and png_get_header_version() functions.
+  Revised comments on png_set_progressive_read_fn() in libpng.txt and example.c
+  Added information about debugging in libpng.txt and libpng.3 .
+  Changed "ln -sf" to "ln -s -f" in makefile.s2x, makefile.lnx, and makefile.sco.
+  Removed lines after Dynamic Dependencies" in makefile.aco .
+  Revised makefile.dec to make a shared library (Jeremie Petit).
+  Removed trailing blanks from all files. 
diff --git a/INSTALL b/INSTALL
index 74b546b..69151ee 100644
--- a/INSTALL
+++ b/INSTALL
@@ -1,17 +1,17 @@
 
-Installing libpng version 1.0.2 - June 14, 1998
+Installing libpng version 1.0.2a - December 29, 1998
 
 Before installing libpng, you must first install zlib.  zlib
 can usually be found wherever you got libpng.  zlib can be
 placed in another directory, at the same level as libpng.
 Note that your system might already have a preinstalled
-zlib, but you will still need to have access to the 
+zlib, but you will still need to have access to the
 zlib.h and zconf.h include files that correspond to the
 version of zlib that's installed.
 
 You can rename the directories that you downloaded (they
-might be called "libpng-1.0.2" or "lpng102" and "zlib-1.1.2"
-or "zlib112") so that you have directories called "zlib" and "libpng".
+might be called "libpng-1.0.2a" or "lpng103" and "zlib-1.1.3"
+or "zlib113") so that you have directories called "zlib" and "libpng".
 
 Your directory structure should look like this:
 
diff --git a/KNOWNBUG b/KNOWNBUG
index 2ff29e6..57c3bb7 100644
--- a/KNOWNBUG
+++ b/KNOWNBUG
@@ -7,13 +7,80 @@
    Loops need to be optimized everywhere
 
    Make them count down instead of up -- Kevin Bracey
-   
+
    Optimizing compilers don't need this, and making
    the change would be error prone -- Tom Lane, Glenn R-P
 
    Question whether i-- or --i is better.
 
    STATUS: Under investigation, postponed until after
-   libpng-1.0.2.  About 160 loops turned around
-   in libpng-1.0.2a, for testing.
+   libpng-1.0.2.  About 160 loops will be turned around
+   in libpng-1.0.Nn, for testing.
+
+2. June 15, 1998 -- BUG -- Evan Light
+
+   Under some conditions, 16-bit grayscale images aren't properly
+   expanded to rgb, because png_read_start_row() returns too small a
+   value of max_pixel_depth when png_set_filler() has also been called.
+
+   STATUS: Fixed in libpng-1.0.2a
+
+3. June 16, 1998 -- BUG -- Matt McConnell
+
+   When reading interlaced images with a progressive reader, in some
+   cases (interlaced images less than 3 pixels high) some passes are
+   not skipped that should be.
+
+   STATUS: Fixed in libpng-1.0.2a
+
+4. July 3, 1998 -- ENHANCEMENT -- Glenn R-P
+
+   Added png_set_rgb_to_gray() and png_get_rgb_to_gray_status() functions.
+
+   STATUS: Done in libpng-1.0.2a, awaiting comment on resulting test
+   images.
+
+5. July 4, 1998 -- ENHANCEMENT -- Glenn R-P
+
+   libpng-1.0.2 and earlier transform colors to gamma=1.0 space for
+   merging with background, and then back to the image's gamma.  The
+   bit_depth of the intermediate (gamma=1.0) representation is probably
+   not sufficient.  In the typical gamma=1/2.2 situation, the linear
+   pixels need about 4 more bits than the gamma-encoded ones, to avoid
+   loss of precision.  A similar situation exists with the rgb_to_gray
+   operation.
+
+   STATUS: under development.
+
+6. August 20, 1998 -- BUG -- Kevin Bracey
+
+   When using the filler transformation, the filler isn't written
+   into the leftmost pixel of each row.
+
+   Also, the filler operation isn't being done right when writing
+   files.  Some code that was removed from png_set_filler() in
+   libpng 1.0.1a shouldn't have been removed.
+
+   STATUS: Fixed in libpng-1.0.2a
+
+7. August 23, 1998 -- BUG -- Duncan Simpson
+
+   "static pngcharp tIME_string" should be changed to
+   "static char tIME_string[30]" in pngtest.c
+
+   STATUS: Fixed in libpng-1.0.2a
+
+8. August 24, 1998 -- BUG -- John Cromer
+
+   Order of byte-swap and shift operations seems to be incorrect in
+   png_do_write_transformations()
+
+   STATUS: Under investigation.
+
+9. September 11, 1998 -- ENHANCEMENT -- L. Peter Deutsch
+
+   The PNG_NO_STDIO macro should be split into PNG_NO_STDIO
+   and PNG_NO_CONSOLE_STDIO.
+
+   STATUS: Done in libpng-1.0.2a
 
diff --git a/README b/README
index b5ece1d..5025c30 100644
--- a/README
+++ b/README
@@ -1,4 +1,4 @@
-README for libpng 1.0.2 - June 14, 1998 (shared library 2.1)
+README for libpng 1.0.2a - December 29, 1998 (shared library 2.1)
 See the note about version numbers near the top of png.h
 
 See INSTALL for instructions on how to install libpng.
diff --git a/TODO b/TODO
index 49bd5ff..52f32b8 100644
--- a/TODO
+++ b/TODO
@@ -1,23 +1,24 @@
-TODO - list of things to do for libpng
+TODO - list of things to do for libpng:
 
-fix problem with C++ and EXTERN "C"
-add "grayscale->palette" transformation and "palette->grayscale" detection
-color to gray transformation
-improved dithering
-multi-lingual error and warning message support
-sPLT chunk handling
-cHRM transformation
-complete sRGB transformation (presently it simply uses gamma=0.45)
-man pages for function calls
-high-level API for reading images
-final bug fixes
-better documentation
-better filter selection
-   (counting huffman bits/precompression?  filter inertia?  filter costs?)
-optional palette creation
-histogram creation
-support for application-defined chunk handlers
-keep up with public chunks
-better C++ wrapper/full C++ implementation?
-text conversion between different code pages (Latin-1 -> Mac and DOS)
-improve API by hiding the info_ptr
+Final bug fixes.
+Fix problem with C++ and EXTERN "C".
+Better C++ wrapper/full C++ implementation?
+Keep up with public chunks.
+sPLT chunk handling.
+cHRM transformation.
+Support for application-defined chunk handlers.
+Improve setjmp/longjmp usage or remove it in favor of returning error codes.
+High-level API for reading images.
+Add "grayscale->palette" transformation and "palette->grayscale" detection.
+Color to gray transformation.
+Improved dithering.
+Multi-lingual error and warning message support.
+Complete sRGB transformation (presently it simply uses gamma=0.45455).
+Man pages for function calls.
+Better documentation.
+Better filter selection
+   (counting huffman bits/precompression?  filter inertia?  filter costs?).
+Optional palette (sPLT) creation.
+Histogram creation.
+Text conversion between different code pages (Latin-1 -> Mac and DOS).
+Improve API by hiding the info_ptr.
diff --git a/example.c b/example.c
index b04d406..2a6d8a0 100644
--- a/example.c
+++ b/example.c
@@ -220,11 +220,14 @@
 
    if (png_get_sRGB(png_ptr, info_ptr, &intent))
       png_set_sRGB(png_ptr, intent, 0);
-   else 
+   else
+   {
+      double image_gamma;
       if (png_get_gAMA(png_ptr, info_ptr, &image_gamma))
          png_set_gamma(png_ptr, screen_gamma, image_gamma);
       else
-         png_set_gamma(png_ptr, screen_gamma, 0.50);
+         png_set_gamma(png_ptr, screen_gamma, 0.45455);
+   }
 
    /* Dither RGB files down to 8 bit palette or reduce palettes
     * to the number of colors available on your screen.
@@ -324,13 +327,13 @@
       {
 #ifdef sparkle /* Read the image using the "sparkle" effect. */
          png_read_rows(png_ptr, &row_pointers[y], NULL, number_of_rows);
-        
+
          png_read_rows(png_ptr, NULL, row_pointers[y], number_of_rows);
 #else no_sparkle /* Read the image using the "rectangle" effect */
          png_read_rows(png_ptr, NULL, &row_pointers[y], number_of_rows);
 #endif no_sparkle /* use only one of these two methods */
       }
-     
+
       /* if you want to display the image after every pass, do
          so here */
 #endif no_single /* use only one of these two methods */
@@ -384,8 +387,11 @@
       return ERROR;
    }
 
-   /* this one's new.  You will need to provide all three
+   /* This one's new.  You will need to provide all three
     * function callbacks, even if you aren't using them all.
+    * If you aren't using all functions, you can specify NULL
+    * parameters.  Even when all three functions are NULL,
+    * you need to call png_set_progressive_read_fn().
     * These functions shouldn't be dependent on global or
     * static variables if you are decoding several images
     * simultaneously.  You should store stream specific data
@@ -565,7 +571,7 @@
    sig_bit.alpha = true_alpha_bit_depth;
    png_set_sBIT(png_ptr, info_ptr, sig_bit);
 
-  
+
    /* Optional gamma chunk is strongly suggested if you have any guess
     * as to the correct gamma of the image.
     */
diff --git a/libpng.3 b/libpng.3
index 955a85c..08a4f1b 100644
--- a/libpng.3
+++ b/libpng.3
@@ -1,6 +1,6 @@
-.TH LIBPNG 3 "June 14, 1998"
+.TH LIBPNG 3 "December 29, 1998"
 .SH NAME
-libpng \- Portable Network Graphics (PNG) Reference Library 1.0.2 - June 14, 1998
+libpng \- Portable Network Graphics (PNG) Reference Library 1.0.2a - December 29, 1998
 .SH SYNOPSIS
 
 #include <png.h>
@@ -27,7 +27,7 @@
 user_png_ver, voidp error_ptr, png_error_ptr error_fn,
 png_error_ptr warn_fn);
 
-png_create_read_struct_2(png_const_charp user_png_ver,
+png_structp png_create_read_struct_2(png_const_charp user_png_ver,
 png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr
 warn_fn, png_voidp mem_ptr, png_malloc_ptr malloc_fn,
 png_free_ptr free_fn)
@@ -41,8 +41,11 @@
 png_error_ptr warn_fn, png_voidp mem_ptr,
 png_malloc_ptr malloc_fn, png_free_ptr free_fn)
 
-png_voidp png_debug_malloc (png_structp png_ptr, png_uint_32
-size);
+int png_debug(int level, png_const_charp message)
+
+int png_debug1(int level, png_const_charp message, p1)
+
+int png_debug2(int level, png_const_charp message, p1, p2)
 
 void png_destroy_info_struct (png_structp png_ptr, png_infopp
 info_ptr_ptr);
@@ -79,6 +82,8 @@
 png_byte png_get_compression_type (png_structp png_ptr,
 png_infop info_ptr);
 
+png_byte png_get_copyright (png_structp png_ptr);
+
 png_voidp png_get_error_ptr (png_structp png_ptr);
 
 png_byte png_get_filter_type (png_structp png_ptr, png_infop
@@ -87,6 +92,8 @@
 png_uint_32 png_get_gAMA (png_structp png_ptr, png_infop
 info_ptr, double *file_gamma);
 
+png_byte png_get_header_version (png_structp png_ptr);
+
 png_uint_32 png_get_hIST (png_structp png_ptr, png_infop
 info_ptr, png_uint_16p *hist);
 
@@ -131,6 +138,8 @@
 png_uint_32 png_get_PLTE (png_structp png_ptr, png_infop
 info_ptr, png_colorp *palette, int *num_palette);
 
+png_byte png_get_rgb_to_gray_status (png_structp png_ptr)
+
 png_uint_32 png_get_rowbytes (png_structp png_ptr, png_infop
 info_ptr);
 
@@ -325,7 +334,7 @@
 void png_set_read_user_transform_fn (png_structp png_ptr,
 png_user_transform_ptr read_user_transform_fn);
 
-void png_set_rgb_to_gray (png_structp png_ptr);
+void png_set_rgb_to_gray (png_structp png_ptr, int error_action);
 
 void png_set_sBIT (png_structp png_ptr, png_infop info_ptr,
 png_color_8p sig_bit);
@@ -414,7 +423,7 @@
 .SH LIBPNG.TXT
 libpng.txt - A description on how to use and modify libpng
 
- libpng version 1.0.2 - June 14, 1998
+ libpng version 1.0.2a - December 29, 1998
  Updated and distributed by Glenn Randers-Pehrson
  <randeg@alumni.rpi.edu>
  Copyright (c) 1998, Glenn Randers-Pehrson
@@ -424,7 +433,7 @@
  based on:
 
  libpng 1.0 beta 6  version 0.96 May 28, 1997
- Updated and distributed by Andreas Dilger 
+ Updated and distributed by Andreas Dilger
  Copyright (c) 1996, 1997 Andreas Dilger
 
  libpng 1.0 beta 2 - version 0.88  January 26, 1996
@@ -454,7 +463,8 @@
 W3C Recommendation <http://www.w3.org/TR/REC.png.html>. Some
 additional chunks are described in the special-purpose public chunks
 documents at <ftp://ftp.uu.net/graphics/png/documents/>.  Other information
-about PNG can be found at the PNG home page, <http://www.cdrom.com/pub/png/>.
+about PNG, and the latest version of libpng, can be found at the PNG home
+page, <http://www.cdrom.com/pub/png/>.
 
 Most users will not have to modify the library significantly; advanced
 users may want to modify it more.  All attempts were made to make it as
@@ -471,6 +481,8 @@
 majority of the needs of its users.
 
 Libpng uses zlib for its compression and decompression of PNG files.
+Further information about zlib, and the latest version of zlib, can
+be found at the zlib home page, <http://www.cdrom.com/pub/infozip/zlib/>.
 The zlib compression utility is a general purpose utility that is
 useful for more than PNG files, and can be used without libpng.
 See the documentation delivered with zlib for more details.
@@ -593,8 +605,8 @@
 handling and memory alloc/free functions.
 
 When libpng encounters an error, it expects to longjmp back
-to your routine.  Therefore, you will need to call setjmp and pass the
-jmpbuf field of your png_struct.  If you read the file from different
+to your routine.  Therefore, you will need to call setjmp and pass
+your png_ptr->jmpbuf.  If you read the file from different
 routines, you will need to update the jmpbuf field every time you enter
 a new routine that will call a png_ function.
 
@@ -667,7 +679,7 @@
 You must supply the function
 
     void read_transform_fn(png_ptr ptr, row_info_ptr
-       row_info, png_bytep data) 
+       row_info, png_bytep data)
 
 See pngtest.c for a working example.  Your function will be called
 after all of the other transformations have been processed.
@@ -696,7 +708,7 @@
                          are present.
                      PNG_COLOR_TYPE_GRAY
                         (bit depths 1, 2, 4, 8, 16)
-                     PNG_COLOR_TYPE_GRAY_ALPHA  
+                     PNG_COLOR_TYPE_GRAY_ALPHA
                         (bit depths 8, 16)
                      PNG_COLOR_TYPE_PALETTE
                         (bit depths 1, 2, 4, 8)
@@ -1002,6 +1014,63 @@
         color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
           png_set_gray_to_rgb(png_ptr);
 
+Conversely, you can convert an RGB or RGBA image to grayscale or grayscale
+with alpha.  This is intended for conversion of images that really are
+gray (red == green == blue), so the function simply strips out the red
+and blue channels, leaving the green channel in the gray position.
+
+    if (color_type == PNG_COLOR_TYPE_RGB ||
+        color_type == PNG_COLOR_TYPE_RGB_ALPHA)
+          png_set_rgb_to_gray(png_ptr, error_action,
+             float red_weight, float green_weight);
+
+    error_action = 1: silently do the conversion
+    error_action = 2: issue a warning if the original
+                      image has any pixel where
+                      red != green or red != blue
+    error_action = 3: issue an error and abort the
+                      conversion if the original
+                      image has any pixel where
+                      red != green or red != blue
+
+    red_weight:       weight of red component
+                      (NULL -> default 54/256)
+    green_weight:     weight of green component
+                      (NULL -> default 183/256)
+
+If you have set error_action = 1 or 2, you can
+later check whether the image really was gray, after processing
+the image rows, with the png_get_rgb_to_gray_status(png_ptr) function.
+It will return a png_byte that is zero if the image was gray or
+1 if there were any non-gray pixels.  bKGD and sBIT data
+will be silently converted to grayscale, using the green channel
+data, regardless of the error_action setting.
+
+With 0.0<=red_weight+green_weight<=1.0,
+the normalized graylevel is computed:
+
+    int rw = red_weight * 256;
+    int gw = green_weight * 256;
+    int bw = 256 - (rw + gw);
+    gray = (rw*red + gw*green + bw*blue)/256;
+
+The default values approximate those recommended in the Charles
+Poynton's Color FAQ, <http://www.inforamp.net/~poynton/>
+Copyright (c) 1998-01-04 Charles Poynton poynton@inforamp.net
+
+    Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
+
+Libpng approximates this with
+
+    Y = 0.211 * R    + 0.715 * G    + 0.074 * B
+
+which can be expressed with integers as
+
+    Y = (54 * R + 183 * G + 19 * B)/256
+
+The calculation is done in a linear colorspace, if the image gamma
+is known.
+
 If you have a grayscale and you are using png_set_expand() to change to
 a higher bit-depth, you must either supply the background color as a gray
 value at the original file bit-depth (need_expand = 1) or else supply the
@@ -1061,7 +1130,7 @@
 The png_set_gamma() function handles gamma transformations of the data.
 Pass both the file gamma and the current screen_gamma.  If the file does
 not have a gamma value, you can pass one anyway if you have an idea what
-it is (usually 0.50 is a good guess for GIF images on PCs).  Note
+it is (usually 0.45455 is a good guess for GIF images on PCs).  Note
 that file gammas are inverted from screen gammas.  See the discussions
 on gamma in the PNG specification for an excellent description of what
 gamma is, and why all applications should support it.  It is strongly
@@ -1070,7 +1139,7 @@
    if (png_get_gAMA(png_ptr, info_ptr, &gamma))
       png_set_gamma(png_ptr, screen_gamma, gamma);
    else
-      png_set_gamma(png_ptr, screen_gamma, 0.50);
+      png_set_gamma(png_ptr, screen_gamma, 0.45455);
 
 If you need to reduce an RGB file to a paletted file, or if a paletted
 file has more entries then will fit on your screen, png_set_dither()
@@ -1321,7 +1390,9 @@
        to be called when the header info is valid,
        when each row is completed, and when the image
        is finished.  If you aren't using all functions,
-       you can specify a NULL parameter.  You can use
+       you can specify NULL parameters.  Even when all
+       three functions are NULL, you need to call
+       png_set_progressive_read_fn().  You can use
        any struct as the user_ptr (cast to a void pointer
        for the function call), and retrieve the pointer
        from inside the callbacks using the function
@@ -1353,21 +1424,21 @@
        of data from the file stream (in order, of
        course).  On machines with segmented memory
        models machines, don't give it any more than
-       64K.  The library seems to run fine with sizes 
+       64K.  The library seems to run fine with sizes
        of 4K. Although you can give it much less if
        necessary (I assume you can give it chunks of
        1 byte, I haven't tried less then 256 bytes
        yet).  When this function returns, you may
        want to display any rows that were generated
        in the row callback if you don't already do
-       so there. 
+       so there.
      */
     png_process_data(png_ptr, info_ptr, buffer, length);
     return 0;
  }
 
  /* This function is called (as set by
-    png_set_progressive_fn() above) when enough data
+    png_set_progressive_read_fn() above) when enough data
     has been supplied so all of the header has been
     read.
  */
@@ -1424,7 +1495,7 @@
        for interlaced images), you will have to pass
        the current row, and the function will combine
        the old row and the new row.
-    */  
+    */
  }
 
  void
@@ -1454,7 +1525,7 @@
 so if it doesn't work, you don't have anything to undo. If you are not
 using the standard I/O functions, you will need to replace them with
 custom writing functions.  See the discussion under Customizing libpng.
-    
+
     FILE *fp = fopen(file_name, "wb");
     if (!fp)
     {
@@ -1495,20 +1566,22 @@
 After you have these structures, you will need to set up the
 error handling.  When libpng encounters an error, it expects to
 longjmp() back to your routine.  Therefore, you will need to call
-setjmp and pass the jmpbuf field of your png_struct.  If you
+setjmp() and pass the png_ptr->jmpbuf.  If you
 write the file from different routines, you will need to update
 the jmpbuf field every time you enter a new routine that will
 call a png_ function.  See your documentation of setjmp/longjmp
 for your compiler for more information on setjmp/longjmp.  See
 the discussion on libpng error handling in the Customizing Libpng
 section below for more information on the libpng error handling.
-    
+
     if (setjmp(png_ptr->jmpbuf))
-    {    
+    {
         png_destroy_write_struct(&png_ptr, &info_ptr);
         fclose(fp);
         return;
     }
+    ...
+    return;
 
 Now you need to set up the output code.  The default for libpng is to
 use the C function fwrite().  If you use this, you will need to pass a
@@ -1549,7 +1622,7 @@
 scanline.  See the Compression Library for details on the specific filter
 types.
 
-    
+
     /* turn on or off filtering, and/or choose
        specific filters */
     png_set_filter(png_ptr, 0,
@@ -1648,7 +1721,7 @@
                      PNG_SRGB_INTENT_PERCEPTUAL,
                      PNG_SRGB_INTENT_ABSOLUTE, or
                      PNG_SRGB_INTENT_RELATIVE.
-                        
+
 
     png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr,
        srgb_intent);
@@ -1739,7 +1812,7 @@
 The compression types have the same valid numbers as the compression
 types of the image data.  Currently, the only valid number is zero.
 However, you can store text either compressed or uncompressed, unlike
-images which always have to be compressed.  So if you don't want the
+images, which always have to be compressed.  So if you don't want the
 text compressed, set the compression type to PNG_TEXT_COMPRESSION_NONE.
 Until text gets around 1000 bytes, it is not worth compressing it.
 After the text has been written out to the file, the compression type
@@ -1799,7 +1872,7 @@
 created in a non-PNG format, a still photo from which the image was
 scanned, or possibly the subject matter itself.  In order to facilitate
 machine-readable dates, it is recommended that the "Creation Time"
-tEXt chunk use RFC 1123 format dates (e.g. 22 May 1997 18:07:10 GMT"),
+tEXt chunk use RFC 1123 format dates (e.g. "22 May 1997 18:07:10 GMT"),
 although this isn't a requirement.  Unlike the tIME chunk, the
 "Creation Time" tEXt chunk is not expected to be automatically changed
 by the software.  To facilitate the use of RFC 1123 dates, a function
@@ -1841,7 +1914,7 @@
 PNG files reduce possible bit depths to 1, 2, 4, 8, and 16.  If your
 data is of another bit depth, you can write an sBIT chunk into the
 file so that decoders can get the original data if desired.
-    
+
     /* Set the true bit depth of the image data */
     if (color_type & PNG_COLOR_MASK_COLOR)
     {
@@ -1902,7 +1975,7 @@
 You must supply the function
 
     void write_transform_fn(png_ptr ptr, row_info_ptr
-       row_info, png_bytep data) 
+       row_info, png_bytep data)
 
 See pngtest.c for a working example.  Your function will be called
 before any of the other transformations have been processed.
@@ -2017,7 +2090,7 @@
 adding new transformations, and generally changing how libpng works.
 
 All of the memory allocation, input/output, and error handling in libpng
-goes through callbacks which are user settable.  The default routines are
+goes through callbacks that are user settable.  The default routines are
 in pngmem.c, pngrio.c, pngwio.c, and pngerror.c respectively.  To change
 these functions, call the appropriate png_set_???_fn() function.
 
@@ -2059,7 +2132,7 @@
 
 Supplying NULL for the read, write, or flush functions sets them back
 to using the default C stream functions.  It is an error to read from
-a write stream, and vice versa. 
+a write stream, and vice versa.
 
 Error handling in libpng is done through png_error() and png_warning().
 Errors handled through png_error() are fatal, meaning that png_error()
@@ -2126,7 +2199,7 @@
 
 Configuring for DOS:
 
-For DOS users which only have access to the lower 640K, you will
+For DOS users who only have access to the lower 640K, you will
 have to limit zlib's memory usage via a png_set_compression_mem_level()
 call.  See zlib.h or zconf.h in the zlib library for more information.
 
@@ -2138,7 +2211,7 @@
 all set.  Everything in the library (except for zlib's structure) is
 expecting far data.  You must use the typedefs with the p or pp on
 the end for pointers (or at least look at them and be careful).  Make
-note that the row's of data are defined as png_bytepp which is a
+note that the row's of data are defined as png_bytepp, which is an
 unsigned char far * far *.
 
 Configuring for gui/windowing platforms:
@@ -2198,8 +2271,8 @@
 of row filters can have a significant impact on the size and
 encoding speed and a somewhat lesser impact on the decoding speed
 of an image.  Filtering is enabled by default for RGB and grayscale
-images (with and without alpha), and for 8-bit paletted images, but
-not for paletted images with bit depths less than 8 bits/pixel.
+images (with and without alpha), but not for paletted images nor
+for any images with bit depths less than 8 bits/pixel.
 
 The 'method' parameter sets the main filtering method, which is
 currently only '0' in the PNG 1.0 specification.  The 'filters'
@@ -2234,10 +2307,10 @@
        PNG_FILTER_SELECTION_WEIGHTED, 3,
        weights, costs);
 
-The weights are multiplying factors which indicate to libpng that row
-should be the same for successive rows unless another row filter is that
-many times better than the previous filter.  In the above example, if
-the previous 3 filters were SUB, SUB, NONE, the SUB filter could have a
+The weights are multiplying factors that indicate to libpng that the
+row filter should be the same for successive rows unless another row filter
+is that many times better than the previous filter.  In the above example,
+if the previous 3 filters were SUB, SUB, NONE, the SUB filter could have a
 "sum of absolute differences" 1.5 x 1.3 times higher than other filters
 and still be chosen, while the NONE filter could have a sum 1.1 times
 higher than other filters and still be chosen.  Unspecified weights are
@@ -2292,10 +2365,46 @@
 as this will cause applications linked with different versions of the
 library to fail if they call functions not available in your library.
 The size of the library itself should not be an issue, because only
-those sections which are actually used will be loaded into memory.
+those sections that are actually used will be loaded into memory.
 
+Requesting debug printout:
 
-Changes to Libpng from version 0.88
+The macro definition PNG_DEBUG can be used to request debugging
+printout.  Set it to an integer value in the range 0 to 3.  Higher
+numbers result in increasing amounts of debugging information.  The
+information is printed to the "stderr" file, unless another file
+name is specified in the PNG_DEBUG_FILE macro definition.
+
+When PNG_DEBUG > 0, the following functions (macros) become available:
+
+   png_debug(level, message)
+   png_debug1(level, message, p1)
+   png_debug2(level, message, p1, p2)
+
+in which "level" is compared to PNG_DEBUG to decide whether to print
+the message, "message" is the formatted string to be printed,
+and p1 and p2 are parameters that are to be embedded in the string
+according to printf-style formatting directives.  For example,
+
+   png_debug1(2, "foo=%d\n", foo);
+
+is expanded to
+
+   if(PNG_DEBUG > 2)
+     fprintf(PNG_DEBUG_FILE, "foo=%d\n", foo);
+
+When PNG_DEBUG is defined but is zero, the macros aren't defined, but you
+can still use PNG_DEBUG to control your own debugging:
+
+   #ifdef PNG_DEBUG
+       fprintf(stderr, ...
+   #endif
+
+When PNG_DEBUG = 1, the macros are defined, but only png_debug statements
+having level = 0 will be printed.  There aren't any such statements in
+this version of libpng, but if you insert some they will be printed.
+
+.SH VI.  Changes to Libpng from version 0.88
 
 It should be noted that versions of libpng later than 0.96 are not
 distributed by the original libpng author, Guy Schalnat, nor by
@@ -2321,7 +2430,7 @@
 
 Setting the error callbacks via png_set_message_fn() before
 png_read_init() as was suggested in libpng-0.88 is no longer supported
-because this caused applications which do not use custom error functions
+because this caused applications that do not use custom error functions
 to fail if the png_ptr was not initialized to zero.  It is still possible
 to set the error callbacks AFTER png_read_init(), or to change them with
 png_set_error_fn(), which is essentially the same function, but with a
@@ -2413,7 +2522,7 @@
 
 Thanks to Frank J. T. Wojcik for helping with the documentation.
   
-Libpng version 1.0.2 - June 14, 1998:
+Libpng version 1.0.2a - December 29, 1998:
 Initially created in 1995 by Guy Eric Schalnat, then of Group 42, Inc.
 Currently maintained by Glenn Randers-Pehrson (randeg@alumni.rpi.edu).
 
diff --git a/libpng.txt b/libpng.txt
index eac50e2..d256783 100644
--- a/libpng.txt
+++ b/libpng.txt
@@ -1,6 +1,6 @@
 libpng.txt - A description on how to use and modify libpng
 
- libpng version 1.0.2 - June 14, 1998
+ libpng version 1.0.2a - December 29, 1998
  Updated and distributed by Glenn Randers-Pehrson
  <randeg@alumni.rpi.edu>
  Copyright (c) 1998, Glenn Randers-Pehrson
@@ -10,7 +10,7 @@
  based on:
 
  libpng 1.0 beta 6  version 0.96 May 28, 1997
- Updated and distributed by Andreas Dilger 
+ Updated and distributed by Andreas Dilger
  Copyright (c) 1996, 1997 Andreas Dilger
 
  libpng 1.0 beta 2 - version 0.88  January 26, 1996
@@ -40,7 +40,8 @@
 W3C Recommendation <http://www.w3.org/TR/REC.png.html>. Some
 additional chunks are described in the special-purpose public chunks
 documents at <ftp://ftp.uu.net/graphics/png/documents/>.  Other information
-about PNG can be found at the PNG home page, <http://www.cdrom.com/pub/png/>.
+about PNG, and the latest version of libpng, can be found at the PNG home
+page, <http://www.cdrom.com/pub/png/>.
 
 Most users will not have to modify the library significantly; advanced
 users may want to modify it more.  All attempts were made to make it as
@@ -57,6 +58,8 @@
 majority of the needs of its users.
 
 Libpng uses zlib for its compression and decompression of PNG files.
+Further information about zlib, and the latest version of zlib, can
+be found at the zlib home page, <http://www.cdrom.com/pub/infozip/zlib/>.
 The zlib compression utility is a general purpose utility that is
 useful for more than PNG files, and can be used without libpng.
 See the documentation delivered with zlib for more details.
@@ -179,8 +182,8 @@
 handling and memory alloc/free functions.
 
 When libpng encounters an error, it expects to longjmp back
-to your routine.  Therefore, you will need to call setjmp and pass the
-jmpbuf field of your png_struct.  If you read the file from different
+to your routine.  Therefore, you will need to call setjmp and pass
+your png_ptr->jmpbuf.  If you read the file from different
 routines, you will need to update the jmpbuf field every time you enter
 a new routine that will call a png_ function.
 
@@ -253,7 +256,7 @@
 You must supply the function
 
     void read_transform_fn(png_ptr ptr, row_info_ptr
-       row_info, png_bytep data) 
+       row_info, png_bytep data)
 
 See pngtest.c for a working example.  Your function will be called
 after all of the other transformations have been processed.
@@ -282,7 +285,7 @@
                          are present.
                      PNG_COLOR_TYPE_GRAY
                         (bit depths 1, 2, 4, 8, 16)
-                     PNG_COLOR_TYPE_GRAY_ALPHA  
+                     PNG_COLOR_TYPE_GRAY_ALPHA
                         (bit depths 8, 16)
                      PNG_COLOR_TYPE_PALETTE
                         (bit depths 1, 2, 4, 8)
@@ -588,6 +591,63 @@
         color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
           png_set_gray_to_rgb(png_ptr);
 
+Conversely, you can convert an RGB or RGBA image to grayscale or grayscale
+with alpha.  This is intended for conversion of images that really are
+gray (red == green == blue), so the function simply strips out the red
+and blue channels, leaving the green channel in the gray position.
+
+    if (color_type == PNG_COLOR_TYPE_RGB ||
+        color_type == PNG_COLOR_TYPE_RGB_ALPHA)
+          png_set_rgb_to_gray(png_ptr, error_action,
+             float red_weight, float green_weight);
+
+    error_action = 1: silently do the conversion
+    error_action = 2: issue a warning if the original
+                      image has any pixel where
+                      red != green or red != blue
+    error_action = 3: issue an error and abort the
+                      conversion if the original
+                      image has any pixel where
+                      red != green or red != blue
+
+    red_weight:       weight of red component
+                      (NULL -> default 54/256)
+    green_weight:     weight of green component
+                      (NULL -> default 183/256)
+
+If you have set error_action = 1 or 2, you can
+later check whether the image really was gray, after processing
+the image rows, with the png_get_rgb_to_gray_status(png_ptr) function.
+It will return a png_byte that is zero if the image was gray or
+1 if there were any non-gray pixels.  bKGD and sBIT data
+will be silently converted to grayscale, using the green channel
+data, regardless of the error_action setting.
+
+With 0.0<=red_weight+green_weight<=1.0,
+the normalized graylevel is computed:
+
+    int rw = red_weight * 256;
+    int gw = green_weight * 256;
+    int bw = 256 - (rw + gw);
+    gray = (rw*red + gw*green + bw*blue)/256;
+
+The default values approximate those recommended in the Charles
+Poynton's Color FAQ, <http://www.inforamp.net/~poynton/>
+Copyright (c) 1998-01-04 Charles Poynton poynton@inforamp.net
+
+    Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
+
+Libpng approximates this with
+
+    Y = 0.211 * R    + 0.715 * G    + 0.074 * B
+
+which can be expressed with integers as
+
+    Y = (54 * R + 183 * G + 19 * B)/256
+
+The calculation is done in a linear colorspace, if the image gamma
+is known.
+
 If you have a grayscale and you are using png_set_expand() to change to
 a higher bit-depth, you must either supply the background color as a gray
 value at the original file bit-depth (need_expand = 1) or else supply the
@@ -647,7 +707,7 @@
 The png_set_gamma() function handles gamma transformations of the data.
 Pass both the file gamma and the current screen_gamma.  If the file does
 not have a gamma value, you can pass one anyway if you have an idea what
-it is (usually 0.50 is a good guess for GIF images on PCs).  Note
+it is (usually 0.45455 is a good guess for GIF images on PCs).  Note
 that file gammas are inverted from screen gammas.  See the discussions
 on gamma in the PNG specification for an excellent description of what
 gamma is, and why all applications should support it.  It is strongly
@@ -656,7 +716,7 @@
    if (png_get_gAMA(png_ptr, info_ptr, &gamma))
       png_set_gamma(png_ptr, screen_gamma, gamma);
    else
-      png_set_gamma(png_ptr, screen_gamma, 0.50);
+      png_set_gamma(png_ptr, screen_gamma, 0.45455);
 
 If you need to reduce an RGB file to a paletted file, or if a paletted
 file has more entries then will fit on your screen, png_set_dither()
@@ -907,7 +967,9 @@
        to be called when the header info is valid,
        when each row is completed, and when the image
        is finished.  If you aren't using all functions,
-       you can specify a NULL parameter.  You can use
+       you can specify NULL parameters.  Even when all
+       three functions are NULL, you need to call
+       png_set_progressive_read_fn().  You can use
        any struct as the user_ptr (cast to a void pointer
        for the function call), and retrieve the pointer
        from inside the callbacks using the function
@@ -939,21 +1001,21 @@
        of data from the file stream (in order, of
        course).  On machines with segmented memory
        models machines, don't give it any more than
-       64K.  The library seems to run fine with sizes 
+       64K.  The library seems to run fine with sizes
        of 4K. Although you can give it much less if
        necessary (I assume you can give it chunks of
        1 byte, I haven't tried less then 256 bytes
        yet).  When this function returns, you may
        want to display any rows that were generated
        in the row callback if you don't already do
-       so there. 
+       so there.
      */
     png_process_data(png_ptr, info_ptr, buffer, length);
     return 0;
  }
 
  /* This function is called (as set by
-    png_set_progressive_fn() above) when enough data
+    png_set_progressive_read_fn() above) when enough data
     has been supplied so all of the header has been
     read.
  */
@@ -1010,7 +1072,7 @@
        for interlaced images), you will have to pass
        the current row, and the function will combine
        the old row and the new row.
-    */  
+    */
  }
 
  void
@@ -1040,7 +1102,7 @@
 so if it doesn't work, you don't have anything to undo. If you are not
 using the standard I/O functions, you will need to replace them with
 custom writing functions.  See the discussion under Customizing libpng.
-    
+
     FILE *fp = fopen(file_name, "wb");
     if (!fp)
     {
@@ -1081,20 +1143,22 @@
 After you have these structures, you will need to set up the
 error handling.  When libpng encounters an error, it expects to
 longjmp() back to your routine.  Therefore, you will need to call
-setjmp and pass the jmpbuf field of your png_struct.  If you
+setjmp() and pass the png_ptr->jmpbuf.  If you
 write the file from different routines, you will need to update
 the jmpbuf field every time you enter a new routine that will
 call a png_ function.  See your documentation of setjmp/longjmp
 for your compiler for more information on setjmp/longjmp.  See
 the discussion on libpng error handling in the Customizing Libpng
 section below for more information on the libpng error handling.
-    
+
     if (setjmp(png_ptr->jmpbuf))
-    {    
+    {
         png_destroy_write_struct(&png_ptr, &info_ptr);
         fclose(fp);
         return;
     }
+    ...
+    return;
 
 Now you need to set up the output code.  The default for libpng is to
 use the C function fwrite().  If you use this, you will need to pass a
@@ -1135,7 +1199,7 @@
 scanline.  See the Compression Library for details on the specific filter
 types.
 
-    
+
     /* turn on or off filtering, and/or choose
        specific filters */
     png_set_filter(png_ptr, 0,
@@ -1234,7 +1298,7 @@
                      PNG_SRGB_INTENT_PERCEPTUAL,
                      PNG_SRGB_INTENT_ABSOLUTE, or
                      PNG_SRGB_INTENT_RELATIVE.
-                        
+
 
     png_set_sRGB_gAMA_and_cHRM(png_ptr, info_ptr,
        srgb_intent);
@@ -1325,7 +1389,7 @@
 The compression types have the same valid numbers as the compression
 types of the image data.  Currently, the only valid number is zero.
 However, you can store text either compressed or uncompressed, unlike
-images which always have to be compressed.  So if you don't want the
+images, which always have to be compressed.  So if you don't want the
 text compressed, set the compression type to PNG_TEXT_COMPRESSION_NONE.
 Until text gets around 1000 bytes, it is not worth compressing it.
 After the text has been written out to the file, the compression type
@@ -1385,7 +1449,7 @@
 created in a non-PNG format, a still photo from which the image was
 scanned, or possibly the subject matter itself.  In order to facilitate
 machine-readable dates, it is recommended that the "Creation Time"
-tEXt chunk use RFC 1123 format dates (e.g. 22 May 1997 18:07:10 GMT"),
+tEXt chunk use RFC 1123 format dates (e.g. "22 May 1997 18:07:10 GMT"),
 although this isn't a requirement.  Unlike the tIME chunk, the
 "Creation Time" tEXt chunk is not expected to be automatically changed
 by the software.  To facilitate the use of RFC 1123 dates, a function
@@ -1427,7 +1491,7 @@
 PNG files reduce possible bit depths to 1, 2, 4, 8, and 16.  If your
 data is of another bit depth, you can write an sBIT chunk into the
 file so that decoders can get the original data if desired.
-    
+
     /* Set the true bit depth of the image data */
     if (color_type & PNG_COLOR_MASK_COLOR)
     {
@@ -1488,7 +1552,7 @@
 You must supply the function
 
     void write_transform_fn(png_ptr ptr, row_info_ptr
-       row_info, png_bytep data) 
+       row_info, png_bytep data)
 
 See pngtest.c for a working example.  Your function will be called
 before any of the other transformations have been processed.
@@ -1603,7 +1667,7 @@
 adding new transformations, and generally changing how libpng works.
 
 All of the memory allocation, input/output, and error handling in libpng
-goes through callbacks which are user settable.  The default routines are
+goes through callbacks that are user settable.  The default routines are
 in pngmem.c, pngrio.c, pngwio.c, and pngerror.c respectively.  To change
 these functions, call the appropriate png_set_???_fn() function.
 
@@ -1645,7 +1709,7 @@
 
 Supplying NULL for the read, write, or flush functions sets them back
 to using the default C stream functions.  It is an error to read from
-a write stream, and vice versa. 
+a write stream, and vice versa.
 
 Error handling in libpng is done through png_error() and png_warning().
 Errors handled through png_error() are fatal, meaning that png_error()
@@ -1712,7 +1776,7 @@
 
 Configuring for DOS:
 
-For DOS users which only have access to the lower 640K, you will
+For DOS users who only have access to the lower 640K, you will
 have to limit zlib's memory usage via a png_set_compression_mem_level()
 call.  See zlib.h or zconf.h in the zlib library for more information.
 
@@ -1724,7 +1788,7 @@
 all set.  Everything in the library (except for zlib's structure) is
 expecting far data.  You must use the typedefs with the p or pp on
 the end for pointers (or at least look at them and be careful).  Make
-note that the row's of data are defined as png_bytepp which is a
+note that the row's of data are defined as png_bytepp, which is an
 unsigned char far * far *.
 
 Configuring for gui/windowing platforms:
@@ -1784,8 +1848,8 @@
 of row filters can have a significant impact on the size and
 encoding speed and a somewhat lesser impact on the decoding speed
 of an image.  Filtering is enabled by default for RGB and grayscale
-images (with and without alpha), and for 8-bit paletted images, but
-not for paletted images with bit depths less than 8 bits/pixel.
+images (with and without alpha), but not for paletted images nor
+for any images with bit depths less than 8 bits/pixel.
 
 The 'method' parameter sets the main filtering method, which is
 currently only '0' in the PNG 1.0 specification.  The 'filters'
@@ -1820,10 +1884,10 @@
        PNG_FILTER_SELECTION_WEIGHTED, 3,
        weights, costs);
 
-The weights are multiplying factors which indicate to libpng that row
-should be the same for successive rows unless another row filter is that
-many times better than the previous filter.  In the above example, if
-the previous 3 filters were SUB, SUB, NONE, the SUB filter could have a
+The weights are multiplying factors that indicate to libpng that the
+row filter should be the same for successive rows unless another row filter
+is that many times better than the previous filter.  In the above example,
+if the previous 3 filters were SUB, SUB, NONE, the SUB filter could have a
 "sum of absolute differences" 1.5 x 1.3 times higher than other filters
 and still be chosen, while the NONE filter could have a sum 1.1 times
 higher than other filters and still be chosen.  Unspecified weights are
@@ -1878,10 +1942,46 @@
 as this will cause applications linked with different versions of the
 library to fail if they call functions not available in your library.
 The size of the library itself should not be an issue, because only
-those sections which are actually used will be loaded into memory.
+those sections that are actually used will be loaded into memory.
 
+Requesting debug printout:
 
-Changes to Libpng from version 0.88
+The macro definition PNG_DEBUG can be used to request debugging
+printout.  Set it to an integer value in the range 0 to 3.  Higher
+numbers result in increasing amounts of debugging information.  The
+information is printed to the "stderr" file, unless another file
+name is specified in the PNG_DEBUG_FILE macro definition.
+
+When PNG_DEBUG > 0, the following functions (macros) become available:
+
+   png_debug(level, message)
+   png_debug1(level, message, p1)
+   png_debug2(level, message, p1, p2)
+
+in which "level" is compared to PNG_DEBUG to decide whether to print
+the message, "message" is the formatted string to be printed,
+and p1 and p2 are parameters that are to be embedded in the string
+according to printf-style formatting directives.  For example,
+
+   png_debug1(2, "foo=%d\n", foo);
+
+is expanded to
+
+   if(PNG_DEBUG > 2)
+     fprintf(PNG_DEBUG_FILE, "foo=%d\n", foo);
+
+When PNG_DEBUG is defined but is zero, the macros aren't defined, but you
+can still use PNG_DEBUG to control your own debugging:
+
+   #ifdef PNG_DEBUG
+       fprintf(stderr, ...
+   #endif
+
+When PNG_DEBUG = 1, the macros are defined, but only png_debug statements
+having level = 0 will be printed.  There aren't any such statements in
+this version of libpng, but if you insert some they will be printed.
+
+VI.  Changes to Libpng from version 0.88
 
 It should be noted that versions of libpng later than 0.96 are not
 distributed by the original libpng author, Guy Schalnat, nor by
@@ -1907,7 +2007,7 @@
 
 Setting the error callbacks via png_set_message_fn() before
 png_read_init() as was suggested in libpng-0.88 is no longer supported
-because this caused applications which do not use custom error functions
+because this caused applications that do not use custom error functions
 to fail if the png_ptr was not initialized to zero.  It is still possible
 to set the error callbacks AFTER png_read_init(), or to change them with
 png_set_error_fn(), which is essentially the same function, but with a
diff --git a/libpngpf.3 b/libpngpf.3
index 0b21c3a..a7c205c 100644
--- a/libpngpf.3
+++ b/libpngpf.3
@@ -1,6 +1,6 @@
-.TH LIBPNGPF 3 "June 14, 1998"
+.TH LIBPNGPF 3 "December 29, 1998"
 .SH NAME
-libpng \- Portable Network Graphics (PNG) Reference Library 1.0.2 - June 14, 1998
+libpng \- Portable Network Graphics (PNG) Reference Library 1.0.2a - December 29, 1998
 (private functions)
 .SH SYNOPSIS
 #include <png.h>
@@ -88,7 +88,7 @@
 
 void png_do_read_transformations (png_structp png_ptr);
 
-void png_do_rgb_to_gray (png_row_infop row_info, png_bytep
+int png_do_rgb_to_gray (png_row_infop row_info, png_bytep
 row);
 
 void png_do_shift (png_row_infop row_info, png_bytep row,
diff --git a/png.5 b/png.5
index c3aabb6..58596d8 100644
--- a/png.5
+++ b/png.5
@@ -1,4 +1,4 @@
-.TH PNG 5 "June 14, 1998"
+.TH PNG 5 "December 29, 1998"
 .SH NAME
 png \- Portable Network Graphics (PNG) format
 .SH DESCRIPTION
@@ -7,7 +7,7 @@
 a patent-free replacement for GIF and can also replace many
 common uses of TIFF. Indexed-color, grayscale, and truecolor images are
 supported, plus an optional alpha channel. Sample depths range from
-1 to 16 bits. 
+1 to 16 bits.
 .br
 
 PNG is designed to work well in online viewing applications, such as the
@@ -15,7 +15,7 @@
 option. PNG is robust, providing both full file integrity checking and
 fast, simple detection of common transmission errors. Also, PNG can store
 gamma and chromaticity data for improved color matching on heterogeneous
-platforms. 
+platforms.
 
 .SH "SEE ALSO"
 .IR libpng(3), zlib(3), deflate(5), and zlib(5)
diff --git a/png.c b/png.c
index 4601de0..4cd1429 100644
--- a/png.c
+++ b/png.c
@@ -1,11 +1,11 @@
 
 /* png.c - location for general purpose libpng functions
  *
- * libpng 1.0.2 - June 14, 1998
- * For conditions of distribution and use, see copyright notice in png.h
+ * libpng version 1.0.2a - December 29, 1998
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
  * Copyright (c) 1998, Glenn Randers-Pehrson
+ * 
  */
 
 #define PNG_INTERNAL
@@ -15,7 +15,8 @@
 /* Version information for C files.  This had better match the version
  * string defined in png.h.
  */
-char png_libpng_ver[12] = "1.0.2";
+
+char png_libpng_ver[12] = "1.0.2a";
 
 /* Place to hold the signature string for a PNG file. */
 png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
@@ -317,8 +318,8 @@
 png_convert_to_rfc1123(png_structp png_ptr, png_timep ptime)
 {
    static PNG_CONST char short_months[12][4] =
-	{"Jan", "Feb", "Mar", "Apr", "May", "Jun",
-	 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
+        {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
+         "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
 
    if (png_ptr->time_buffer == NULL)
    {
@@ -345,3 +346,13 @@
    return ((png_charp)png_ptr->time_buffer);
 }
 #endif /* PNG_TIME_RFC1123_SUPPORTED */
+
+png_charp
+png_get_copyright(png_structp png_ptr)
+{
+   return("\n libpng version 1.0.2a - December 29, 1998\n\
+   Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.\n\
+   Copyright (c) 1996, 1997 Andreas Dilger\n\
+   Copyright (c) 1998, Glenn Randers-Pehrson\n");
+}
+
diff --git a/png.h b/png.h
index 0183b49..93b55c9 100644
--- a/png.h
+++ b/png.h
@@ -1,11 +1,10 @@
 
 /* png.h - header file for PNG reference library
  *
- * libpng 1.0.2 - June 14, 1998
- * For conditions of distribution and use, see the COPYRIGHT NOTICE below.
+ * libpng version 1.0.2a - December 29, 1998
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
- * Copyright (c) 1998 Glenn Randers-Pehrson
+ * Copyright (c) 1998, Glenn Randers-Pehrson
  *
  * Note about libpng version numbers:
  *
@@ -31,6 +30,9 @@
  *    1.0.0                     1.0.0      100  2.1.0 [int should be 10000]
  *    1.0.1                     1.0.1    10001  2.1.0
  *    1.0.1a-e                  1.0.1a-e 10002  2.1.0.1a-e
+ *    1.0.2                     1.0.2    10002  2.1.0.2
+ *    1.0.2a                    1.0.2a   10003  2.1.0.2a
+ *    1.0.3                     1.0.3    10003  2.1.0.3
  *
  *    Henceforth the source version will match the shared-library minor
  *    and patch numbers; the shared-library major version number will be
@@ -117,14 +119,14 @@
  */
 
 /* Version information for png.h - this should match the version in png.c */
-#define PNG_LIBPNG_VER_STRING "1.0.2"
+#define PNG_LIBPNG_VER_STRING "1.0.2a"
 
 /* Careful here.  At one time, Guy wanted to use 082, but that would be octal.
  * We must not include leading zeros.
  * Versions 0.7 through 1.0.0 were in the range 0 to 100 here (only
  * version 1.0.0 was mis-numbered 100 instead of 10000).  From
  * version 1.0.1 it's    xxyyzz, where x=major, y=minor, z=bugfix */
-#define PNG_LIBPNG_VER    10002  /* 1.0.2 */
+#define PNG_LIBPNG_VER    10003  /* 1.0.3 */
 
 /* variables declared in png.c - only it needs to define PNG_NO_EXTERN */
 #if !defined(PNG_NO_EXTERN) || defined(PNG_ALWAYS_EXTERN)
@@ -144,6 +146,7 @@
 extern int FARDATA png_pass_width[7];
 extern int FARDATA png_pass_height[7];
 */
+
 #endif /* PNG_NO_EXTERN */
 
 /* Three color definitions.  The order of the red, green, and blue, (and the
@@ -453,7 +456,7 @@
 #define PNG_sRGB_INTENT_ABSOLUTE   2
 #define PNG_sRGB_INTENT_RELATIVE   3
 #define PNG_sRGB_INTENT_LAST       4 /* Not a valid value */
-                        
+
 
 
 /* These determine if an ancillary chunk's data has been successfully read
@@ -689,6 +692,12 @@
    png_malloc_ptr malloc_fn;         /* function for allocating memory */
    png_free_ptr free_fn;             /* function for freeing memory */
 #endif /* PNG_USER_MEM_SUPPORTED */
+#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
+   png_byte rgb_to_gray_status;
+   png_byte rgb_to_gray_red_coeff;
+   png_byte rgb_to_gray_green_coeff;
+   png_byte rgb_to_gray_blue_coeff;
+#endif
 };
 
 typedef png_struct FAR * FAR * png_structpp;
@@ -734,7 +743,7 @@
    png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
    png_malloc_ptr malloc_fn, png_free_ptr free_fn));
 extern PNG_EXPORT(png_structp,png_create_write_struct_2)
-   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr, 
+   PNGARG((png_const_charp user_png_ver, png_voidp error_ptr,
    png_error_ptr error_fn, png_error_ptr warn_fn, png_voidp mem_ptr,
    png_malloc_ptr malloc_fn, png_free_ptr free_fn));
 #endif
@@ -800,8 +809,11 @@
 #endif /* PNG_READ_GRAY_TO_RGB_SUPPORTED */
 
 #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
-/* Reduce RGB to grayscale. (Not yet implemented) */
-extern PNG_EXPORT(void,png_set_rgb_to_gray) PNGARG((png_structp png_ptr));
+/* Reduce RGB to grayscale. */
+extern PNG_EXPORT(void,png_set_rgb_to_gray) PNGARG((png_structp png_ptr,
+   int error_action, float red, float green ));
+extern PNG_EXPORT(png_byte,png_get_rgb_to_gray_status) PNGARG((png_structp
+   png_ptr));
 #endif /* PNG_READ_RGB_TO_GRAY_SUPPORTED */
 
 extern PNG_EXPORT(void,png_build_grayscale_palette) PNGARG((int bit_depth,
@@ -1105,6 +1117,7 @@
  * method of error handling.  If error_fn or warning_fn is NULL, the
  * default function will be used.
  */
+
 extern PNG_EXPORT(void,png_set_error_fn) PNGARG((png_structp png_ptr,
    png_voidp error_ptr, png_error_ptr error_fn, png_error_ptr warning_fn));
 
@@ -1343,7 +1356,7 @@
    png_infop info_ptr, png_uint_32 *width, png_uint_32 *height,
    int *bit_depth, int *color_type, int *interlace_type,
    int *compression_type, int *filter_type));
-  
+
 extern PNG_EXPORT(void,png_set_IHDR) PNGARG((png_structp png_ptr,
    png_infop info_ptr, png_uint_32 width, png_uint_32 height, int bit_depth,
    int color_type, int interlace_type, int compression_type, int filter_type));
@@ -1449,28 +1462,42 @@
  * only been added since version 0.95 so it is not implemented throughout
  * libpng yet, but more support will be added as needed.
  */
+#ifdef PNG_DEBUG
 #if (PNG_DEBUG > 0)
-#ifdef PNG_NO_STDIO
-#include <stdio.h>
-#endif
 #ifndef PNG_DEBUG_FILE
 #define PNG_DEBUG_FILE stderr
 #endif /* PNG_DEBUG_FILE */
 
 #define png_debug(l,m)        if (PNG_DEBUG > l) \
                                  fprintf(PNG_DEBUG_FILE,"%s"m,(l==1 ? "\t" : \
-                                    (l==2 ? "\t\t":(l==3 ? "\t\t\t":""))))
+                                    (l==2 ? "\t\t":(l>2 ? "\t\t\t":""))))
 #define png_debug1(l,m,p1)    if (PNG_DEBUG > l) \
                                  fprintf(PNG_DEBUG_FILE,"%s"m,(l==1 ? "\t" : \
-                                    (l==2 ? "\t\t":(l==3 ? "\t\t\t":""))),p1)
+                                    (l==2 ? "\t\t":(l>2 ? "\t\t\t":""))),p1)
 #define png_debug2(l,m,p1,p2) if (PNG_DEBUG > l) \
                                  fprintf(PNG_DEBUG_FILE,"%s"m,(l==1 ? "\t" : \
-                                    (l==2 ? "\t\t":(l==3 ? "\t\t\t":""))),p1,p2)
-#else
-#define png_debug(l, m)
-#define png_debug1(l, m, p1)
-#define png_debug2(l, m, p1, p2)
+                                    (l==2 ? "\t\t":(l>2 ? "\t\t\t":""))),p1,p2)
 #endif /* (PNG_DEBUG > 0) */
+#endif /* PNG_DEBUG */
+#ifndef png_debug
+#define png_debug(l, m)
+#endif
+#ifndef png_debug1
+#define png_debug1(l, m, p1)
+#endif
+#ifndef png_debug2
+#define png_debug2(l, m, p1, p2)
+#endif
+
+extern PNG_EXPORT(png_charp,png_get_copyright) PNGARG((png_structp png_ptr));
+#ifdef PNG_NO_EXTERN
+extern PNG_EXPORT(png_charp,png_get_header_version) PNGARG((png_structp png_ptr));
+png_charp
+png_get_header_version(png_structp png_ptr)
+{
+   return("\n libpng version 1.0.2a - December 29, 1998 (header)\n");
+}
+#endif
 
 #ifdef PNG_READ_COMPOSITE_NODIV_SUPPORTED
 /* With these routines we avoid an integer divide, which will be slower on
@@ -1485,28 +1512,30 @@
  * [Optimized code by Greg Roelofs and Mark Adler...blame us for bugs. :-) ]
  */
 
-   /* fg and bg should be in `gamma 1.0' space; alpha is the opacity */
-#  define png_composite(composite, fg, alpha, bg) \
-     { png_uint_16 temp = ((png_uint_16)(fg) * (png_uint_16)(alpha) + \
-                        (png_uint_16)(bg)*(png_uint_16)(255 - \
-                        (png_uint_16)(alpha)) + (png_uint_16)128); \
+ /* fg and bg should be in `gamma 1.0' space; alpha is the opacity          */
+
+#  define png_composite(composite, fg, alpha, bg)                            \
+     { png_uint_16 temp = ((png_uint_16)(fg) * (png_uint_16)(alpha) +        \
+                        (png_uint_16)(bg)*(png_uint_16)(255 -                \
+                        (png_uint_16)(alpha)) + (png_uint_16)128);           \
        (composite) = (png_byte)((temp + (temp >> 8)) >> 8); }
-#  define png_composite_16(composite, fg, alpha, bg) \
-     { png_uint_32 temp = ((png_uint_32)(fg) * (png_uint_32)(alpha) + \
-                        (png_uint_32)(bg)*(png_uint_32)(65535L - \
-                        (png_uint_32)(alpha)) + (png_uint_32)32768L); \
+
+#  define png_composite_16(composite, fg, alpha, bg)                         \
+     { png_uint_32 temp = ((png_uint_32)(fg) * (png_uint_32)(alpha) +        \
+                        (png_uint_32)(bg)*(png_uint_32)(65535L -             \
+                        (png_uint_32)(alpha)) + (png_uint_32)32768L);        \
        (composite) = (png_uint_16)((temp + (temp >> 16)) >> 16); }
 
 #else  /* standard method using integer division */
 
-   /* fg and bg should be in `gamma 1.0' space; alpha is the opacity */
-#  define png_composite(composite, fg, alpha, bg) \
-     (composite) = (png_byte)(((png_uint_16)(fg) * (png_uint_16)(alpha) + \
-       (png_uint_16)(bg) * (png_uint_16)(255 - (png_uint_16)(alpha)) + \
+#  define png_composite(composite, fg, alpha, bg)                            \
+     (composite) = (png_byte)(((png_uint_16)(fg) * (png_uint_16)(alpha) +    \
+       (png_uint_16)(bg) * (png_uint_16)(255 - (png_uint_16)(alpha)) +       \
        (png_uint_16)127) / 255)
-#  define png_composite_16(composite, fg, alpha, bg) \
+
+#  define png_composite_16(composite, fg, alpha, bg)                         \
      (composite) = (png_uint_16)(((png_uint_32)(fg) * (png_uint_32)(alpha) + \
-       (png_uint_32)(bg)*(png_uint_32)(65535L - (png_uint_32)(alpha)) + \
+       (png_uint_32)(bg)*(png_uint_32)(65535L - (png_uint_32)(alpha)) +      \
        (png_uint_32)32767) / (png_uint_32)65535L)
 
 #endif /* PNG_READ_COMPOSITE_NODIV_SUPPORTED */
@@ -1552,7 +1581,7 @@
 #define PNG_DITHER             0x0040
 #define PNG_BACKGROUND         0x0080
 #define PNG_BACKGROUND_EXPAND  0x0100
-#define PNG_RGB_TO_GRAY        0x0200 /* Not currently implemented */
+                          /*   0x0200 unused */
 #define PNG_16_TO_8            0x0400
 #define PNG_RGBA               0x0800
 #define PNG_EXPAND             0x1000
@@ -1564,6 +1593,9 @@
 #define PNG_STRIP_ALPHA       0x40000L
 #define PNG_INVERT_ALPHA      0x80000L
 #define PNG_USER_TRANSFORM   0x100000L
+#define PNG_RGB_TO_GRAY_ERR  0x200000L
+#define PNG_RGB_TO_GRAY_WARN 0x400000L
+#define PNG_RGB_TO_GRAY      0x600000L  /* two bits, RGB_TO_GRAY_ERR|WARN */
 
 /* flags for png_create_struct */
 #define PNG_STRUCT_PNG   0x0001
@@ -1725,8 +1757,8 @@
 #endif
 
 /* Place a 32-bit number into a buffer in PNG byte order (big-endian).
- * The only currently known PNG chunk that uses signed numbers is
- * the ancillary extension chunk, pCAL.
+ * The only currently known PNG chunks that use signed numbers are
+ * the ancillary extension chunks, oFFs and pCAL.
  */
 PNG_EXTERN void png_save_uint_32 PNGARG((png_bytep buf, png_uint_32 i));
 
@@ -1922,8 +1954,8 @@
 #endif
 
 #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
-PNG_EXTERN void png_do_rgb_to_gray PNGARG((png_row_infop row_info,
-   png_bytep row));
+PNG_EXTERN int png_do_rgb_to_gray PNGARG((png_structp png_ptr, png_row_infop
+   row_info, png_bytep row));
 #endif
 
 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
@@ -2128,11 +2160,16 @@
 
 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
 
+#endif
+ 
+
 #endif /* PNG_INTERNAL */
 
 #ifdef __cplusplus
 }
 #endif
 
+#ifdef PNG_NO_EXTERN
+
 /* do not put anything past this line */
 #endif /* _PNG_H */
diff --git a/pngconf.h b/pngconf.h
index e18ba19..e7ef28a 100644
--- a/pngconf.h
+++ b/pngconf.h
@@ -1,7 +1,7 @@
 
 /* pngconf.h - machine configurable file for libpng
  *
- * libpng 1.0.2 - June 14, 1998
+ * libpng 1.0.2a - December 29, 1998
  * For conditions of distribution and use, see copyright notice in png.h
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
@@ -17,6 +17,7 @@
 #ifndef PNGCONF_H
 #define PNGCONF_H
 
+
 /* This is the size of the compression buffer, and thus the size of
  * an IDAT chunk.  Make this whatever size you feel is best for your
  * machine.  One of these will be allocated per png_struct.  When this
@@ -49,13 +50,28 @@
 /* This protects us against compilers that run on a windowing system
  * and thus don't have or would rather us not use the stdio types:
  * stdin, stdout, and stderr.  The only one currently used is stderr
- * in png_error() and png_warning().  #defining PNG_NO_STDIO will
- * prevent these from being compiled and used.
+ * in png_error() and png_warning().  #defining PNG_NO_CONSOLE_IO will
+ * prevent these from being compiled and used. #defining PNG_NO_STDIO
+ * will also prevent these, plus will prevent the entire set of stdio
+ * macros and functions (FILE *, printf, etc.) from being compiled and used,
+ * unless PNG_DEBUG has been #defined.
+ *
+ * #define PNG_NO_CONSOLE_IO
  * #define PNG_NO_STDIO
  */
 
-#ifndef PNG_NO_STDIO 
-#include <stdio.h>
+#ifdef PNG_DEBUG
+#  if (PNG_DEBUG > 0)
+#    include <stdio.h>
+#  endif
+#else
+#  ifdef PNG_NO_STDIO
+#    ifndef PNG_NO_CONSOLE_IO
+#      define PNG_NO_CONSOLE_IO
+#    endif
+#  else
+#    include <stdio.h>
+#  endif
 #endif
 
 /* This macro protects us against machines that don't have function
@@ -307,11 +323,9 @@
 #ifndef PNG_NO_READ_USER_TRANSFORM
 #define PNG_READ_USER_TRANSFORM_SUPPORTED
 #endif
-/* the following aren't implemented yet
 #ifndef PNG_NO_READ_RGB_TO_GRAY
 #define PNG_READ_RGB_TO_GRAY_SUPPORTED
 #endif
- */
 #endif /* PNG_READ_TRANSFORMS_SUPPORTED */
 
 #if !defined(PNG_NO_PROGRESSIVE_READ) && \
@@ -631,7 +645,7 @@
  */
 typedef charf *         png_zcharp;
 typedef charf * FAR *   png_zcharpp;
-typedef z_stream FAR *  png_zstreamp; 
+typedef z_stream FAR *  png_zstreamp;
 
 /* allow for compilation as dll under MS Windows */
 #ifdef __WIN32DLL__
@@ -680,7 +694,7 @@
 /* End of memory model independent support */
 
 /* Just a double check that someone hasn't tried to define something
- * contradictory. 
+ * contradictory.
  */
 #if (PNG_ZBUF_SIZE > 65536) && defined(PNG_MAX_MALLOC_64K)
 #undef PNG_ZBUF_SIZE
diff --git a/pngerror.c b/pngerror.c
index e3772ca..ebbb0dc 100644
--- a/pngerror.c
+++ b/pngerror.c
@@ -1,7 +1,7 @@
 
 /* pngerror.c - stub functions for i/o and memory allocation
  *
- * libpng 1.0.2 - June 14, 1998
+ * libpng 1.0.2a - December 29, 1998
  * For conditions of distribution and use, see copyright notice in png.h
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
@@ -113,7 +113,7 @@
 static void
 png_default_error(png_structp png_ptr, png_const_charp message)
 {
-#ifndef PNG_NO_STDIO
+#ifndef PNG_NO_CONSOLE_IO
    fprintf(stderr, "libpng error: %s\n", message);
 #endif
 
@@ -139,7 +139,7 @@
    if (png_ptr == NULL)
       return;
 
-#ifndef PNG_NO_STDIO
+#ifndef PNG_NO_CONSOLE_IO
    fprintf(stderr, "libpng warning: %s\n", message);
 #endif
 }
diff --git a/pngget.c b/pngget.c
index c6aef6f..bb6fc3b 100644
--- a/pngget.c
+++ b/pngget.c
@@ -1,7 +1,7 @@
 
 /* pngget.c - retrieval of values from info struct
  *
- * libpng 1.0.2 - June 14, 1998
+ * libpng 1.0.2a - December 29, 1998
  * For conditions of distribution and use, see copyright notice in png.h
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
@@ -423,7 +423,7 @@
    png_uint_32 *width, png_uint_32 *height, int *bit_depth,
    int *color_type, int *interlace_type, int *compression_type,
    int *filter_type)
-   
+
 {
    if (png_ptr != NULL && info_ptr != NULL && width != NULL && height != NULL &&
       bit_depth != NULL && color_type != NULL)
@@ -641,3 +641,10 @@
 }
 #endif
 
+#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
+png_byte
+png_get_rgb_to_gray_status (png_structp png_ptr)
+{
+   return png_ptr->rgb_to_gray_status;
+}
+#endif
diff --git a/pngmem.c b/pngmem.c
index 0aeab68..b472e54 100644
--- a/pngmem.c
+++ b/pngmem.c
@@ -1,7 +1,7 @@
 
 /* pngmem.c - stub functions for memory allocation
  *
- * libpng 1.0.2 - June 14, 1998
+ * libpng 1.0.2a - December 29, 1998
  * For conditions of distribution and use, see copyright notice in png.h
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
@@ -130,7 +130,7 @@
 png_malloc_default(png_structp png_ptr, png_uint_32 size)
 {
    png_voidp ret;
-#endif PNG_USER_MEM_SUPPORTED
+#endif /* PNG_USER_MEM_SUPPORTED */
 
 #ifdef PNG_MAX_MALLOC_64K
    if (size > (png_uint_32)65536L)
@@ -245,7 +245,7 @@
 png_free_default(png_structp png_ptr, png_voidp ptr)
 {
 #endif /* PNG_USER_MEM_SUPPORTED */
-      
+
    if (png_ptr->offset_table != NULL)
    {
       int i;
@@ -468,7 +468,7 @@
    size = (png_size_t)length;
    if ((png_uint_32)size != length)
       png_error(png_ptr,"Overflow in png_memcpy_check.");
-  
+
    return(png_memcpy (s1, s2, size));
 }
 
diff --git a/pngpread.c b/pngpread.c
index dad6499..abf1dcb 100644
--- a/pngpread.c
+++ b/pngpread.c
@@ -1,7 +1,7 @@
 
 /* pngpread.c - read a png file in push mode
  *
- * libpng 1.0.2 - June 14, 1998
+ * libpng 1.0.2a - December 29, 1998
  * For conditions of distribution and use, see copyright notice in png.h
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
@@ -92,7 +92,7 @@
       num_to_check = png_ptr->buffer_size;
    }
 
-   png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]), 
+   png_push_fill_buffer(png_ptr, &(info_ptr->signature[num_checked]),
       num_to_check);
    png_ptr->sig_bytes += num_to_check;
 
@@ -466,7 +466,7 @@
 
       new_max = png_ptr->save_buffer_size + png_ptr->current_buffer_size + 256;
       old_buffer = png_ptr->save_buffer;
-      png_ptr->save_buffer = (png_bytep)png_malloc(png_ptr, 
+      png_ptr->save_buffer = (png_bytep)png_malloc(png_ptr,
          (png_uint_32)new_max);
       png_memcpy(png_ptr->save_buffer, old_buffer, png_ptr->save_buffer_size);
       png_free(png_ptr, old_buffer);
@@ -630,7 +630,7 @@
    png_ptr->row_info.channels = png_ptr->channels;
    png_ptr->row_info.bit_depth = png_ptr->bit_depth;
    png_ptr->row_info.pixel_depth = png_ptr->pixel_depth;
-   
+
    png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
       (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
 
@@ -781,6 +781,7 @@
          png_ptr->pass++;
          if (png_ptr->pass >= 7)
             break;
+
          png_ptr->iwidth = (png_ptr->width +
             png_pass_inc[png_ptr->pass] - 1 -
             png_pass_start[png_ptr->pass]) /
@@ -789,18 +790,15 @@
          png_ptr->irowbytes = ((png_ptr->iwidth *
             png_ptr->pixel_depth + 7) >> 3) + 1;
 
-         if (!(png_ptr->transformations & PNG_INTERLACE))
-         {
-            png_ptr->num_rows = (png_ptr->height +
-               png_pass_yinc[png_ptr->pass] - 1 -
-               png_pass_ystart[png_ptr->pass]) /
-               png_pass_yinc[png_ptr->pass];
-            if (!(png_ptr->num_rows))
-               continue;
-         }
          if (png_ptr->transformations & PNG_INTERLACE)
             break;
-      } while (png_ptr->iwidth == 0);
+
+         png_ptr->num_rows = (png_ptr->height +
+            png_pass_yinc[png_ptr->pass] - 1 -
+            png_pass_ystart[png_ptr->pass]) /
+            png_pass_yinc[png_ptr->pass];
+
+      } while (png_ptr->iwidth == 0 || png_ptr->num_rows == 0);
    }
 }
 
@@ -826,7 +824,7 @@
    }
 #endif
 
-   png_ptr->current_text = (png_charp)png_malloc(png_ptr, 
+   png_ptr->current_text = (png_charp)png_malloc(png_ptr,
          (png_uint_32)(length+1));
    png_ptr->current_text[length] = '\0';
    png_ptr->current_text_ptr = png_ptr->current_text;
diff --git a/pngread.c b/pngread.c
index f3097f4..597d9d5 100644
--- a/pngread.c
+++ b/pngread.c
@@ -1,7 +1,7 @@
 
 /* pngread.c - read a PNG file
  *
- * libpng 1.0.2 - June 14, 1998
+ * libpng 1.0.2a - December 29, 1998
  * For conditions of distribution and use, see copyright notice in png.h
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
@@ -515,7 +515,7 @@
  * image has alpha or transparency, and png_handle_alpha()[*] has been
  * called, the rows contents must be initialized to the contents of the
  * screen.
- * 
+ *
  * "row" holds the actual image, and pixels are placed in it
  * as they arrive.  If the image is displayed after each pass, it will
  * appear to "sparkle" in.  "display_row" can be used to display a
@@ -530,7 +530,7 @@
  * not called png_set_interlace_handling(), the display_row buffer will
  * be ignored, so pass NULL to it.
  *
- * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.2.
+ * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.2a.
  */
 
 void
@@ -550,7 +550,7 @@
       {
          png_bytep rptr = *rp++;
          png_bytep dptr = *dp++;
-   
+
          png_read_row(png_ptr, rptr, dptr);
       }
    else if(rp != NULL)
@@ -579,7 +579,7 @@
  * only call this function once.  If you desire to have an image for
  * each pass of a interlaced image, use png_read_rows() instead.
  *
- * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.2.
+ * [*] png_handle_alpha() does not exist yet, as of libpng version 1.0.2a.
  */
 void
 png_read_image(png_structp png_ptr, png_bytepp image)
@@ -820,9 +820,9 @@
       {
          png_free(png_ptr, png_ptr->gamma_16_table[i]);
       }
+   png_free(png_ptr, png_ptr->gamma_16_table);
    }
 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
-   png_free(png_ptr, png_ptr->gamma_16_table);
    if (png_ptr->gamma_16_from_1 != NULL)
    {
       int i;
@@ -831,8 +831,8 @@
       {
          png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
       }
-   }
    png_free(png_ptr, png_ptr->gamma_16_from_1);
+   }
    if (png_ptr->gamma_16_to_1 != NULL)
    {
       int i;
@@ -841,8 +841,8 @@
       {
          png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
       }
-   }
    png_free(png_ptr, png_ptr->gamma_16_to_1);
+   }
 #endif
 #endif
 #if defined(PNG_TIME_RFC1123_SUPPORTED)
diff --git a/pngrio.c b/pngrio.c
index b9a3645..a916b8c 100644
--- a/pngrio.c
+++ b/pngrio.c
@@ -1,7 +1,7 @@
 
 /* pngrio.c - functions for data input
  *
- * libpng 1.0.2 - June 14, 1998
+ * libpng 1.0.2a - December 29, 1998
  * For conditions of distribution and use, see copyright notice in png.h
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
@@ -60,10 +60,10 @@
    can't handle far buffers in the medium and small models, we have to copy
    the data.
 */
- 
+
 #define NEAR_BUF_SIZE 1024
 #define MIN(a,b) (a <= b ? a : b)
- 
+
 static void
 png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
 {
diff --git a/pngrtran.c b/pngrtran.c
index f426c92..fd4ec22 100644
--- a/pngrtran.c
+++ b/pngrtran.c
@@ -1,13 +1,13 @@
 
 /* pngrtran.c - transforms the data in a row for PNG readers
  *
- * libpng 1.0.2 - June 14, 1998
+ * libpng 1.0.2a - December 29, 1998
  * For conditions of distribution and use, see copyright notice in png.h
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
  * Copyright (c) 1998, Glenn Randers-Pehrson
  *
- * This file contains functions optionally called by an application 
+ * This file contains functions optionally called by an application
  * in order to tell libpng how to handle data when reading a PNG.
  * Transformations that are used in both reading and writing are
  * in pngtrans.c.
@@ -284,8 +284,8 @@
             don't always behave themselves with only a few colors
             as input.  So we will just find the closest two colors,
             and throw out one of them (chosen somewhat randomly).
-            [I don't understand this at all, so if someone wants to
-             work on improving it, be my guest - AED]
+            [We don't understand this at all, so if someone wants to
+             work on improving it, be our guest - AED, GRP]
             */
          int i;
          int max_d;
@@ -309,7 +309,7 @@
             palette_to_index[i] = (png_byte)i;
          }
 
-         hash = (png_dsortpp)png_malloc(png_ptr, (png_uint_32)(769 * 
+         hash = (png_dsortpp)png_malloc(png_ptr, (png_uint_32)(769 *
             sizeof (png_dsortp)));
          for (i = 0; i < 769; i++)
             hash[i] = NULL;
@@ -544,17 +544,49 @@
 #endif
 
 #if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
-/* Convert a RGB image to a grayscale of the given width.  This would
- * allow us, for example, to convert a 24 bpp RGB image into an 8 or
- * 16 bpp grayscale image. (Not yet implemented.)
+/* Convert a RGB image to a grayscale of the same width.  This allows us,
+ * for example, to convert a 24 bpp RGB image into an 8 bpp grayscale image.
  */
 void
-png_set_rgb_to_gray(png_structp png_ptr, int gray_bits)
+png_set_rgb_to_gray(png_structp png_ptr, int error_action, float red,
+  float green)
 {
    png_debug(1, "in png_set_rgb_to_gray\n");
-   png_ptr->transformations |= PNG_RGB_TO_GRAY;
-   /* Need to do something with gray_bits here. */
-   png_warning(png_ptr, "RGB to GRAY transformation is not yet implemented.");
+   switch(error_action)
+   {
+      case 1: png_ptr->transformations |= PNG_RGB_TO_GRAY;
+              break;
+      case 2: png_ptr->transformations |= PNG_RGB_TO_GRAY_WARN;
+              break;
+      case 3: png_ptr->transformations |= PNG_RGB_TO_GRAY_ERR;
+   }
+   if (png_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
+#if defined(PNG_READ_EXPAND_SUPPORTED)
+      png_ptr->transformations |= PNG_EXPAND;
+#else
+   {
+      png_warning(png_ptr, "Cannot do RGB_TO_GRAY without EXPAND_SUPPORTED.");
+      png_ptr->transformations &= ~PNG_RGB_TO_GRAY;
+   }
+#endif
+   {
+      png_byte red_byte = (png_byte)(red*255.0 + 0.5);
+      png_byte green_byte = (png_byte)(green*255.0 + 0.5);
+      if(red < 0.0 || green < 0.0)
+      {
+         red_byte = 54;
+         green_byte = 183;
+      }
+      else if(red_byte + green_byte > 255)
+      {
+         png_warning(png_ptr, "ignoring out of range rgb_to_gray coefficients");
+         red_byte = 54;
+         green_byte = 183;
+      }
+      png_ptr->rgb_to_gray_red_coeff   = red_byte;
+      png_ptr->rgb_to_gray_green_coeff = green_byte;
+      png_ptr->rgb_to_gray_blue_coeff  = 255 - red_byte - green_byte;
+   }
 }
 #endif
 
@@ -631,7 +663,7 @@
            if (!(png_ptr->transformations & PNG_EXPAND))
 #endif
            {
-           /* invert the alpha channel (in tRNS) unless the pixels are 
+           /* invert the alpha channel (in tRNS) unless the pixels are
               going to be expanded, in which case leave it for later */
               int i,istop;
               istop=(int)png_ptr->num_trans;
@@ -649,7 +681,7 @@
    png_ptr->background_1 = png_ptr->background;
 #endif
 #if defined(PNG_READ_GAMMA_SUPPORTED)
-   if (png_ptr->transformations & PNG_GAMMA)
+   if (png_ptr->transformations & (PNG_GAMMA | PNG_RGB_TO_GRAY))
    {
       png_build_gamma_table(png_ptr);
 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
@@ -958,6 +990,11 @@
       info_ptr->color_type |= PNG_COLOR_MASK_COLOR;
 #endif
 
+#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
+   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
+      info_ptr->color_type &= ~PNG_COLOR_MASK_COLOR;
+#endif
+
    if (info_ptr->color_type == PNG_COLOR_TYPE_PALETTE)
       info_ptr->channels = 1;
    else if (info_ptr->color_type & PNG_COLOR_MASK_COLOR)
@@ -1035,12 +1072,28 @@
          PNG_FLAG_FILLER_AFTER);
 #endif
 
+#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
+   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
+   {
+      int rgb_error =
+         png_do_rgb_to_gray(png_ptr, &(png_ptr->row_info), png_ptr->row_buf + 1);
+      if(rgb_error)
+      {
+         png_ptr->rgb_to_gray_status=1;
+         if(png_ptr->transformations == PNG_RGB_TO_GRAY_WARN)
+            png_warning(png_ptr, "png_do_rgb_to_gray found nongray pixel");
+         if(png_ptr->transformations == PNG_RGB_TO_GRAY_ERR)
+            png_error(png_ptr, "png_do_rgb_to_gray found nongray pixel");
+      }
+   }
+#endif
+
 /*
 From Andreas Dilger e-mail to png-implement, 26 March 1998:
 
   In most cases, the "simple transparency" should be done prior to doing
   gray-to-RGB, or you will have to test 3x as many bytes to check if a
-  pixel is transparent.  You would also need to make sure that the 
+  pixel is transparent.  You would also need to make sure that the
   transparency information is upgraded to RGB.
 
   To summarize, the current flow is:
@@ -1099,11 +1152,6 @@
          png_ptr->gamma_shift);
 #endif
 
-#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
-   if (png_ptr->transformations & PNG_RGB_TO_GRAY)
-      png_do_rgb_to_gray(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
-
 #if defined(PNG_READ_16_TO_8_SUPPORTED)
    if (png_ptr->transformations & PNG_16_TO_8)
       png_do_chop(&(png_ptr->row_info), png_ptr->row_buf + 1);
@@ -1359,7 +1407,6 @@
             }
             break;
          }
-#ifndef PNG_SLOW_SHIFT
          case 8:
          {
             png_bytep bp = row;
@@ -1387,43 +1434,6 @@
             }
             break;
          }
-#else
-         case 8:
-         {
-            png_bytep bp;
-            png_uint_32 i;
-            int cstop;
-
-            cstop=(int)row_info->channels;
-            for (bp = row, i = 0; i < row_width; i++)
-            {
-               for (c = 0; c < cstop; c++, bp++)
-               {
-                  *bp >>= shift[c];
-               }
-            }
-            break;
-         }
-         case 16:
-         {
-            png_bytep bp;
-            png_size_t i;
-            int cstop;
-
-            cstop=(int)row_info->channels;
-            for (bp = row, i = 0; i < row_width; i++)
-            {
-               for (c = 0; c < cstop; c++, bp += 2)
-               {
-                  value = (png_uint_16)((*bp << 8) + *(bp + 1));
-                  value >>= shift[c];
-                  *bp = (png_byte)(value >> 8);
-                  *(bp + 1) = (png_byte)(value & 0xff);
-               }
-            }
-            break;
-         }
-#endif
       }
    }
 }
@@ -1668,7 +1678,7 @@
    png_uint_32 row_width = row_info->width;
 
    png_byte hi_filler = (png_byte)((filler>>8) & 0xff);
-   png_byte low_filler = (png_byte)(filler & 0xff);
+   png_byte lo_filler = (png_byte)(filler & 0xff);
 
    png_debug(1, "in png_do_read_filler\n");
    if (
@@ -1686,9 +1696,10 @@
             png_bytep dp =  sp + (png_size_t)row_width;
             for (i = 1; i < row_width; i++)
             {
-               *(--dp) = low_filler;
+               *(--dp) = lo_filler;
                *(--dp) = *(--sp);
             }
+            *(--dp) = lo_filler;
             row_info->channels = 2;
             row_info->pixel_depth = 16;
             row_info->rowbytes = row_width * 2;
@@ -1701,7 +1712,7 @@
             for (i = 0; i < row_width; i++)
             {
                *(--dp) = *(--sp);
-               *(--dp) = low_filler;
+               *(--dp) = lo_filler;
             }
             row_info->channels = 2;
             row_info->pixel_depth = 16;
@@ -1718,10 +1729,12 @@
             for (i = 1; i < row_width; i++)
             {
                *(--dp) = hi_filler;
-               *(--dp) = low_filler;
+               *(--dp) = lo_filler;
                *(--dp) = *(--sp);
                *(--dp) = *(--sp);
             }
+            *(--dp) = hi_filler;
+            *(--dp) = lo_filler;
             row_info->channels = 2;
             row_info->pixel_depth = 32;
             row_info->rowbytes = row_width * 2;
@@ -1736,7 +1749,7 @@
                *(--dp) = *(--sp);
                *(--dp) = *(--sp);
                *(--dp) = hi_filler;
-               *(--dp) = low_filler;
+               *(--dp) = lo_filler;
             }
             row_info->channels = 2;
             row_info->pixel_depth = 16;
@@ -1755,11 +1768,12 @@
             png_bytep dp = sp  + (png_size_t)row_width;
             for (i = 1; i < row_width; i++)
             {
-               *(--dp) = low_filler;
+               *(--dp) = lo_filler;
                *(--dp) = *(--sp);
                *(--dp) = *(--sp);
                *(--dp) = *(--sp);
             }
+            *(--dp) = lo_filler;
             row_info->channels = 4;
             row_info->pixel_depth = 32;
             row_info->rowbytes = row_width * 4;
@@ -1774,7 +1788,7 @@
                *(--dp) = *(--sp);
                *(--dp) = *(--sp);
                *(--dp) = *(--sp);
-               *(--dp) = low_filler;
+               *(--dp) = lo_filler;
             }
             row_info->channels = 4;
             row_info->pixel_depth = 32;
@@ -1791,7 +1805,7 @@
             for (i = 1; i < row_width; i++)
             {
                *(--dp) = hi_filler;
-               *(--dp) = low_filler;
+               *(--dp) = lo_filler;
                *(--dp) = *(--sp);
                *(--dp) = *(--sp);
                *(--dp) = *(--sp);
@@ -1799,6 +1813,8 @@
                *(--dp) = *(--sp);
                *(--dp) = *(--sp);
             }
+            *(--dp) = hi_filler;
+            *(--dp) = lo_filler;
             row_info->channels = 4;
             row_info->pixel_depth = 64;
             row_info->rowbytes = row_width * 4;
@@ -1817,7 +1833,7 @@
                *(--dp) = *(--sp);
                *(--dp) = *(--sp);
                *(--dp) = hi_filler;
-               *(--dp) = low_filler;
+               *(--dp) = lo_filler;
             }
             row_info->channels = 4;
             row_info->pixel_depth = 64;
@@ -1918,6 +1934,261 @@
 }
 #endif
 
+#if defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
+/* reduce RGB files to grayscale, with or without alpha 
+ * using the equation given in Poynton's ColorFAQ at
+ * <http://www.inforamp.net/~poynton/>
+ * Copyright (c) 1998-01-04 Charles Poynton poynton@inforamp.net
+ *
+ *     Y = 0.212671 * R + 0.715160 * G + 0.072169 * B
+ *
+ *  We approximate this with
+ * 
+ *     Y = 0.211 * R    + 0.715 * G    + 0.074 * B
+ *
+ *  which can be expressed with integers as
+ *
+ *     Y = (54 * R + 183 * G + 19 * B)/256
+ *
+ *  The calculation is to be done in a linear colorspace.
+ *
+ *  Other integer coefficents can be used via png_set_rgb_to_gray().
+ */
+int
+png_do_rgb_to_gray(png_structp png_ptr, png_row_infop row_info, png_bytep row)
+
+{
+   png_uint_32 i;
+
+   png_uint_32 row_width = row_info->width;
+   int rgb_error = 0;
+
+   png_debug(1, "in png_do_rgb_to_gray\n");
+   if (
+#if defined(PNG_USELESS_TESTS_SUPPORTED)
+       row != NULL && row_info != NULL &&
+#endif
+      (row_info->color_type & PNG_COLOR_MASK_COLOR))
+   {
+      png_byte rc = png_ptr->rgb_to_gray_red_coeff;
+      png_byte gc = png_ptr->rgb_to_gray_green_coeff;
+      png_byte bc = png_ptr->rgb_to_gray_blue_coeff;
+
+      if (row_info->color_type == PNG_COLOR_TYPE_RGB)
+      {
+         if (row_info->bit_depth == 8)
+         {
+#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
+            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
+            {
+               png_bytep sp = row;
+               png_bytep dp = row;
+
+               for (i = 0; i < row_width; i++)
+               {
+                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
+                  png_byte green = png_ptr->gamma_to_1[*(sp++)];
+                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
+                  if(red != green || red != blue)
+                  {
+                     rgb_error |= 1;
+                     *(dp++) = png_ptr->gamma_from_1[
+                       (rc*red+gc*green+bc*blue)>>8];
+                  }
+                  else
+                     *(dp++) = *(sp-1);
+               }
+            }
+            else
+#endif
+            {
+               png_bytep sp = row;
+               png_bytep dp = row;
+               for (i = 0; i < row_width; i++)
+               {
+                  png_byte red   = *(sp++);
+                  png_byte green = *(sp++);
+                  png_byte blue  = *(sp++);
+                  if(red != green || red != blue)
+                  {
+                     rgb_error |= 1;
+                     *(dp++) = (rc*red+gc*green+bc*blue)>>8;
+                  }
+                  else
+                     *(dp++) = *(sp-1);
+               }
+            }
+         }
+ 
+         else /* RGB bit_depth == 16 */
+         {
+#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
+            if (png_ptr->gamma_16_to_1 != NULL &&
+                png_ptr->gamma_16_from_1 != NULL)
+            {
+               png_bytep sp = row;
+               png_bytep dp = row;
+               for (i = 0; i < row_width; i++)
+               {
+                  png_uint_16 red, green, blue, w;
+
+                  red   = ((*(sp))<<8) | *(sp+1); sp+=2;
+                  green = ((*(sp))<<8) | *(sp+1); sp+=2;
+                  blue  = ((*(sp))<<8) | *(sp+1); sp+=2;
+
+                  if(red == green && red == blue)
+                     w = red;
+                  else
+                  {
+                     png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
+                                  png_ptr->gamma_shift][red>>8];
+                     png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >>
+                                  png_ptr->gamma_shift][green>>8];
+                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >> 
+                                  png_ptr->gamma_shift][blue>>8];
+                     png_uint_16 gray16  =  (rc * red_1 + gc * green_1
+                                  + bc * blue_1)>>8;
+                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
+                         png_ptr->gamma_shift][gray16 >> 8];
+                     rgb_error |= 1;
+                  }
+                  
+                  *(dp++) = (w>>8) & 0xff;
+                  *(dp++) = w & 0xff;
+               }
+            }
+            else
+#endif
+            {
+               png_bytep sp = row;
+               png_bytep dp = row;
+               for (i = 0; i < row_width; i++)
+               {
+                  png_uint_16 red, green, blue, gray16;
+
+                  red   = ((*(sp))<<8) | *(sp+1); sp+=2;
+                  green = ((*(sp))<<8) | *(sp+1); sp+=2;
+                  blue  = ((*(sp))<<8) | *(sp+1); sp+=2;
+
+                  if(red != green || red != blue)
+                     rgb_error |= 1;
+                  gray16  =  (rc * red + gc * green + bc * blue)>>8;
+                  *(dp++) = (gray16>>8) & 0xff;
+                  *(dp++) = gray16 & 0xff;
+               }
+            }
+         }
+      }
+      if (row_info->color_type == PNG_COLOR_TYPE_RGB_ALPHA)
+      {
+         if (row_info->bit_depth == 8)
+         {
+#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
+            if (png_ptr->gamma_from_1 != NULL && png_ptr->gamma_to_1 != NULL)
+            {
+               png_bytep sp = row;
+               png_bytep dp = row;
+               for (i = 0; i < row_width; i++)
+               {
+                  png_byte red   = png_ptr->gamma_to_1[*(sp++)];
+                  png_byte green = png_ptr->gamma_to_1[*(sp++)];
+                  png_byte blue  = png_ptr->gamma_to_1[*(sp++)];
+                  if(red != green || red != blue)
+                     rgb_error |= 1;
+                  *(dp++) =  png_ptr->gamma_from_1
+                             [(rc*red + gc*green + bc*blue)>>8];
+                  *(dp++) = *(sp++);  /* alpha */
+               }
+            }
+            else
+#endif
+            {
+               png_bytep sp = row;
+               png_bytep dp = row;
+               for (i = 0; i < row_width; i++)
+               {
+                  png_byte red   = *(sp++);
+                  png_byte green = *(sp++);
+                  png_byte blue  = *(sp++);
+                  if(red != green || red != blue)
+                     rgb_error |= 1;
+                  *(dp++) =  (gc*red + gc*green + bc*blue)>>8;
+                  *(dp++) = *(sp++);  /* alpha */
+               }
+            }
+         }
+         else /* RGBA bit_depth == 16 */
+         {
+#if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
+            if (png_ptr->gamma_16_to_1 != NULL &&
+                png_ptr->gamma_16_from_1 != NULL)
+            {
+               png_bytep sp = row;
+               png_bytep dp = row;
+               for (i = 0; i < row_width; i++)
+               {
+                  png_uint_16 red, green, blue, w;
+
+                  red   = ((*(sp))<<8) | *(sp+1); sp+=2;
+                  green = ((*(sp))<<8) | *(sp+1); sp+=2;
+                  blue  = ((*(sp))<<8) | *(sp+1); sp+=2;
+
+                  if(red == green && red == blue)
+                     w = red;
+                  else
+                  {
+                     png_uint_16 red_1   = png_ptr->gamma_16_to_1[(red&0xff) >>
+                                  png_ptr->gamma_shift][red>>8];
+                     png_uint_16 green_1 = png_ptr->gamma_16_to_1[(green&0xff) >>
+                                  png_ptr->gamma_shift][green>>8];
+                     png_uint_16 blue_1  = png_ptr->gamma_16_to_1[(blue&0xff) >> 
+                                  png_ptr->gamma_shift][blue>>8];
+                     png_uint_16 gray16  =  (rc * red_1 + gc * green_1
+                                  + bc * blue_1)>>8;
+                     w = png_ptr->gamma_16_from_1[(gray16&0xff) >>
+                         png_ptr->gamma_shift][gray16 >> 8];
+                     rgb_error |= 1;
+                  }
+                  
+                  *(dp++) = (w>>8) & 0xff;
+                  *(dp++) = w & 0xff;
+                  *(dp++) = *(sp++);  /* alpha */
+                  *(dp++) = *(sp++);
+               }
+            }
+            else
+#endif
+            {
+               png_bytep sp = row;
+               png_bytep dp = row;
+               for (i = 0; i < row_width; i++)
+               {
+                  png_uint_16 red, green, blue, gray16;
+                  red   = (*(sp)<<8) | *(sp+1); sp+=2;
+                  green = (*(sp)<<8) | *(sp+1); sp+=2;
+                  blue  = (*(sp)<<8) | *(sp+1); sp+=2;
+                  if(red != green || red != blue)
+                     rgb_error |= 1;
+                  gray16  =  (rc * red + gc * green + bc * blue)>>8;
+                  *(dp++) = (gray16>>8) & 0xff;
+                  *(dp++) = gray16 & 0xff;
+                  *(dp++) = *(sp++);  /* alpha */
+                  *(dp++) = *(sp++);
+               }
+            }
+         }
+      }
+   row_info->channels -= (png_byte)2;
+      row_info->color_type &= ~PNG_COLOR_MASK_COLOR;
+      row_info->pixel_depth = (png_byte)(row_info->channels *
+         row_info->bit_depth);
+      row_info->rowbytes = ((row_width *
+         row_info->pixel_depth + 7) >> 3);
+   }
+   return rgb_error;
+}
+#endif
+
 /* Build a grayscale palette.  Palette is assumed to be 1 << bit_depth
  * large of png_color.  This lets grayscale images be treated as
  * paletted.  Most useful for gamma correction and simplification
@@ -1975,8 +2246,7 @@
 {
    png_debug(1, "in png_correct_palette\n");
 #if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_GAMMA_SUPPORTED)
-   if ((png_ptr->transformations & (PNG_GAMMA)) &&
-      (png_ptr->transformations & (PNG_BACKGROUND)))
+   if (png_ptr->transformations & (PNG_GAMMA | PNG_BACKGROUND))
    {
       png_color back, back_1;
 
@@ -3451,13 +3721,18 @@
 void
 png_build_gamma_table(png_structp png_ptr)
 {
-   png_debug(1, "in png_build_gamma_table\n");
+  png_debug(1, "in png_build_gamma_table\n");
+  if(png_ptr->gamma != 0.0)
+  {
    if (png_ptr->bit_depth <= 8)
    {
       int i;
       double g;
 
-      g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
+      if (png_ptr->screen_gamma > .000001)
+         g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
+      else
+         g = 1.0;
 
       png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr,
          (png_uint_32)256);
@@ -3468,9 +3743,11 @@
             g) * 255.0 + .5);
       }
 
-#if defined(PNG_READ_BACKGROUND_SUPPORTED)
-      if (png_ptr->transformations & PNG_BACKGROUND)
+#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
+    defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
+      if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY))
       {
+
          g = 1.0 / (png_ptr->gamma);
 
          png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr,
@@ -3482,18 +3759,23 @@
                g) * 255.0 + .5);
          }
 
-         g = 1.0 / (png_ptr->screen_gamma);
-
+         
          png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr,
             (png_uint_32)256);
 
+         if(png_ptr->screen_gamma > 0.000001)
+            g = 1.0 / png_ptr->screen_gamma;
+         else
+            g = png_ptr->gamma;   /* probably doing rgb_to_gray */
+
          for (i = 0; i < 256; i++)
          {
             png_ptr->gamma_from_1[i] = (png_byte)(pow((double)i / 255.0,
                g) * 255.0 + .5);
+
          }
       }
-#endif /* PNG_BACKGROUND_SUPPORTED */
+#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
    }
    else
    {
@@ -3535,13 +3817,15 @@
 
       num = (1 << (8 - shift));
 
-      g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
+      if (png_ptr->screen_gamma > .000001)
+         g = 1.0 / (png_ptr->gamma * png_ptr->screen_gamma);
+      else
+         g = 1.0;
 
       png_ptr->gamma_16_table = (png_uint_16pp)png_malloc(png_ptr,
          (png_uint_32)(num * sizeof (png_uint_16p)));
 
-      if ((png_ptr->transformations & PNG_16_TO_8) &&
-         !(png_ptr->transformations & PNG_BACKGROUND))
+      if (png_ptr->transformations & (PNG_16_TO_8 | PNG_BACKGROUND))
       {
          double fin, fout;
          png_uint_32 last, max;
@@ -3591,9 +3875,11 @@
          }
       }
 
-#if defined(PNG_READ_BACKGROUND_SUPPORTED)
-      if (png_ptr->transformations & PNG_BACKGROUND)
+#if defined(PNG_READ_BACKGROUND_SUPPORTED) || \
+    defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
+      if (png_ptr->transformations & (PNG_BACKGROUND | PNG_RGB_TO_GRAY))
       {
+
          g = 1.0 / (png_ptr->gamma);
 
          png_ptr->gamma_16_to_1 = (png_uint_16pp)png_malloc(png_ptr,
@@ -3613,7 +3899,11 @@
                      65535.0, g) * 65535.0 + .5);
             }
          }
-         g = 1.0 / (png_ptr->screen_gamma);
+
+         if(png_ptr->screen_gamma > 0.000001)
+            g = 1.0 / png_ptr->screen_gamma;
+         else
+            g = png_ptr->gamma;   /* probably doing rgb_to_gray */
 
          png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr,
             (png_uint_32)(num * sizeof (png_uint_16p)));
@@ -3633,8 +3923,9 @@
             }
          }
       }
-#endif /* PNG_BACKGROUND_SUPPORTED */
+#endif /* PNG_READ_BACKGROUND_SUPPORTED || PNG_RGB_TO_GRAY_SUPPORTED */
    }
+ }
 }
 #endif
 
diff --git a/pngrutil.c b/pngrutil.c
index a4ca3ab..880ab54 100644
--- a/pngrutil.c
+++ b/pngrutil.c
@@ -1,7 +1,7 @@
 
 /* pngrutil.c - utilities to read a PNG file
  *
- * libpng 1.0.2 - June 14, 1998
+ * libpng 1.0.2a - December 29, 1998
  * For conditions of distribution and use, see copyright notice in png.h
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
@@ -424,7 +424,7 @@
       {
          png_warning(png_ptr,
            "Ignoring incorrect gAMA value when sRGB is also present");
-#ifndef PNG_NO_STDIO
+#ifndef PNG_NO_CONSOLE_IO
          fprintf(stderr, "igamma = %lu\n", igamma);
 #endif
          return;
@@ -496,6 +496,9 @@
    else
    {
       png_ptr->sig_bit.gray = buf[0];
+      png_ptr->sig_bit.red = buf[0];
+      png_ptr->sig_bit.green = buf[0];
+      png_ptr->sig_bit.blue = buf[0];
       png_ptr->sig_bit.alpha = buf[1];
    }
    png_set_sBIT(png_ptr, info_ptr, &(png_ptr->sig_bit));
@@ -624,7 +627,7 @@
 
             png_warning(png_ptr,
               "Ignoring incorrect cHRM value when sRGB is also present");
-#ifndef PNG_NO_STDIO
+#ifndef PNG_NO_CONSOLE_IO
             fprintf(stderr,"wx=%f, wy=%f, rx=%f, ry=%f\n",
                white_x, white_y, red_x, red_y);
             fprintf(stderr,"gx=%f, gy=%f, bx=%f, by=%f\n",
@@ -693,7 +696,7 @@
       {
          png_warning(png_ptr,
            "Ignoring incorrect gAMA value when sRGB is also present");
-#ifndef PNG_NO_STDIO
+#ifndef PNG_NO_CONSOLE_IO
            fprintf(stderr,"gamma=%f\n",png_ptr->gamma);
 #endif
       }
@@ -1890,7 +1893,6 @@
 }
 #endif
 
-#ifndef PNG_READ_SLOW_FILTERING
 void
 png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,
    png_bytep prev_row, int filter)
@@ -1947,7 +1949,7 @@
                ((int)(*pp++) / 2)) & 0xff);
             rp++;
          }
-        
+
          for (i = 0; i < istop; i++)
          {
             *rp = (png_byte)(((int)(*rp) +
@@ -2010,118 +2012,11 @@
          break;
       }
       default:
-         png_error(png_ptr, "Bad adaptive filter type");
+         png_warning(png_ptr, "Ignoring bad adaptive filter type");
+         *row=0;
          break;
    }
 }
-#else /* PNG_READ_SLOW_FILTERING */
-void
-png_read_filter_row(png_structp png_ptr, png_row_infop row_info, png_bytep row,
-   png_bytep prev_row, int filter)
-{
-   png_debug(1, "in png_read_filter_row\n");
-   png_debug2(2,"row = %d, filter = %d\n", png_ptr->row_number, filter);
-
-
-   switch (filter)
-   {
-      case PNG_FILTER_VALUE_NONE:
-         break;
-      case PNG_FILTER_VALUE_SUB:
-      {
-         png_uint_32 i;
-         int bpp = (row_info->pixel_depth + 7) / 8;
-         png_bytep rp;
-         png_bytep lp;
-
-         for (i = (png_uint_32)bpp, rp = row + bpp, lp = row;
-            i < row_info->rowbytes; i++, rp++, lp++)
-         {
-            *rp = (png_byte)(((int)(*rp) + (int)(*lp)) & 0xff);
-         }
-         break;
-      }
-      case PNG_FILTER_VALUE_UP:
-      {
-         png_uint_32 i;
-         png_bytep rp;
-         png_bytep pp;
-
-         for (i = 0, rp = row, pp = prev_row;
-            i < row_info->rowbytes; i++, rp++, pp++)
-         {
-            *rp = (png_byte)(((int)(*rp) + (int)(*pp)) & 0xff);
-         }
-         break;
-      }
-      case PNG_FILTER_VALUE_AVG:
-      {
-         png_uint_32 i;
-         int bpp = (row_info->pixel_depth + 7) / 8;
-         png_bytep rp;
-         png_bytep pp;
-         png_bytep lp;
-
-         for (i = 0, rp = row, pp = prev_row;
-            i < (png_uint_32)bpp; i++, rp++, pp++)
-         {
-            *rp = (png_byte)(((int)(*rp) +
-               ((int)(*pp) / 2)) & 0xff);
-         }
-         for (lp = row; i < row_info->rowbytes; i++, rp++, lp++, pp++)
-         {
-            *rp = (png_byte)(((int)(*rp) +
-               (int)(*pp + *lp) / 2) & 0xff);
-         }
-         break;
-      }
-      case PNG_FILTER_VALUE_PAETH:
-      {
-         int bpp = (row_info->pixel_depth + 7) / 8;
-         png_uint_32 i;
-         png_bytep rp;
-         png_bytep pp;
-         png_bytep lp;
-         png_bytep cp;
-
-         for (i = 0, rp = row, pp = prev_row,
-            lp = row - bpp, cp = prev_row - bpp;
-            i < row_info->rowbytes; i++, rp++, pp++, lp++, cp++)
-         {
-            int a, b, c, pa, pb, pc, p;
-
-            b = *pp;
-            if (i >= (png_uint_32)bpp)
-            {
-               c = *cp;
-               a = *lp;
-            }
-            else
-            {
-               a = c = 0;
-            }
-            p = a + b - c;
-            pa = abs(p - a);
-            pb = abs(p - b);
-            pc = abs(p - c);
-
-            if (pa <= pb && pa <= pc)
-               p = a;
-            else if (pb <= pc)
-               p = b;
-            else
-               p = c;
-
-            *rp = (png_byte)(((int)(*rp) + p) & 0xff);
-         }
-         break;
-      }
-      default:
-         png_error(png_ptr, "Bad adaptive filter type");
-         break;
-   }
-}
-#endif /* PNG_READ_SLOW_FILTERING */
 
 void
 png_read_finish_row(png_structp png_ptr)
@@ -2320,19 +2215,25 @@
 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
    if (png_ptr->transformations & PNG_GRAY_TO_RGB)
    {
-      if ((png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
-         png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
+      if (
+#if defined(PNG_READ_EXPAND_SUPPORTED)
+        (png_ptr->num_trans && (png_ptr->transformations & PNG_EXPAND)) ||
+#endif
+#if defined(PNG_READ_FILLER_SUPPORTED)
+        (png_ptr->transformations & (PNG_FILLER)) ||
+#endif
+        png_ptr->color_type == PNG_COLOR_TYPE_GRAY_ALPHA)
       {
          if (max_pixel_depth <= 16)
             max_pixel_depth = 32;
-         else if (max_pixel_depth <= 32)
+         else
             max_pixel_depth = 64;
       }
       else
       {
          if (max_pixel_depth <= 8)
             max_pixel_depth = 24;
-         else if (max_pixel_depth <= 16)
+         else
             max_pixel_depth = 48;
       }
    }
diff --git a/pngset.c b/pngset.c
index 89b727b..4a1f196 100644
--- a/pngset.c
+++ b/pngset.c
@@ -1,7 +1,7 @@
 
 /* pngset.c - storage of image information into info struct
  *
- * libpng 1.0.2 - June 14, 1998
+ * libpng 1.0.2a - December 29, 1998
  * For conditions of distribution and use, see copyright notice in png.h
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
diff --git a/pngtest.c b/pngtest.c
index 9685474..e4300d6 100644
--- a/pngtest.c
+++ b/pngtest.c
@@ -1,7 +1,7 @@
 
 /* pngtest.c - a simple test program to test libpng
  *
- * libpng 1.0.2 -June 14, 1998
+ * libpng 1.0.2a -December 29, 1998
  * For conditions of distribution and use, see copyright notice in png.h
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
@@ -94,7 +94,7 @@
 count_zero_samples(png_structp png_ptr, png_row_infop row_info, png_bytep data)
 {
    png_bytep dp = data;
-   if(png_ptr == NULL)return; 
+   if(png_ptr == NULL)return;
 
    /* contents of row_info:
     *  png_uint_32 width      width of row
@@ -211,10 +211,10 @@
    can't handle far buffers in the medium and small models, we have to copy
    the data.
 */
- 
+
 #define NEAR_BUF_SIZE 1024
 #define MIN(a,b) (a <= b ? a : b)
- 
+
 static void
 png_default_read_data(png_structp png_ptr, png_bytep data, png_size_t length)
 {
@@ -474,7 +474,7 @@
 #ifdef USE_FAR_KEYWORD
    jmp_buf jmpbuf;
 #endif   
-   
+
    char inbuf[256], outbuf[256];
 
    row_buf = (png_bytep)NULL;
@@ -538,10 +538,12 @@
       fclose(fpout);
       return (1);
    }
+#ifdef USE_FAR_KEYWORD
+   png_memcpy(read_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
+#endif
 
    png_debug(0, "Setting jmpbuf for write struct\n");
 #ifdef USE_FAR_KEYWORD
-   png_memcpy(read_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
    if (setjmp(jmpbuf))
 #else
    if (setjmp(write_ptr->jmpbuf))
@@ -554,10 +556,10 @@
       fclose(fpout);
       return (1);
    }
-
 #ifdef USE_FAR_KEYWORD
    png_memcpy(write_ptr->jmpbuf,jmpbuf,sizeof(jmp_buf));
 #endif
+
    png_debug(0, "Initializing input and output streams\n");
 #if !defined(PNG_NO_STDIO)
    png_init_io(read_ptr, fpin);
@@ -764,7 +766,7 @@
    png_write_info(write_ptr, write_info_ptr);
 
    png_debug(0, "\nAllocating row buffer \n");
-   row_buf = (png_bytep)png_malloc(read_ptr, 
+   row_buf = (png_bytep)png_malloc(read_ptr,
       png_get_rowbytes(read_ptr, read_info_ptr));
    if (row_buf == NULL)
    {
@@ -793,7 +795,7 @@
    png_debug(0, "Reading and writing end_info data\n");
    png_read_end(read_ptr, end_info_ptr);
    png_write_end(write_ptr, end_info_ptr);
- 
+
 #ifdef PNG_EASY_ACCESS_SUPPORTED
    if(verbose)
    {
@@ -900,6 +902,7 @@
 
    fprintf(STDERR, "Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
    fprintf(STDERR, "   with zlib   version %s\n", ZLIB_VERSION);
+   fprintf(STDERR,"%s",png_get_copyright(NULL));
 
    /* Do some consistency checking on the memory allocation settings, I'm
       not sure this matters, but it is nice to know, the first of these
@@ -975,13 +978,13 @@
          int kerror;
          fprintf(STDERR, "Testing %s:",argv[i]);
          kerror = test_one_file(argv[i], outname);
-         if (kerror == 0) 
+         if (kerror == 0)
          {
 #if defined(PNG_WRITE_USER_TRANSFORM_SUPPORTED)
             fprintf(STDERR, " PASS (%lu zero samples)\n",zero_samples);
 #else
             fprintf(STDERR, " PASS\n");
-#endif 
+#endif
 #if defined(PNG_TIME_RFC1123_SUPPORTED)
          if(tIME_chunk_present != 0)
             fprintf(STDERR, " tIME = %s\n",tIME_string);
@@ -1055,7 +1058,7 @@
                current_allocation-allocation_now);
          if (current_allocation != 0) {
              memory_infop pinfo = pinformation;
-   
+
              fprintf(STDERR, "MEMORY ERROR: %d bytes still allocated\n",
                 current_allocation);
              while (pinfo != NULL) {
diff --git a/pngtrans.c b/pngtrans.c
index 3b23dfa..f8abd44 100644
--- a/pngtrans.c
+++ b/pngtrans.c
@@ -1,7 +1,7 @@
 
 /* pngtrans.c - transforms the data in a row (used by both readers and writers)
  *
- * libpng 1.0.2 - June 14, 1998
+ * libpng 1.0.2a - December 29, 1998
  * For conditions of distribution and use, see copyright notice in png.h
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
@@ -99,6 +99,24 @@
       png_ptr->flags |= PNG_FLAG_FILLER_AFTER;
    else
       png_ptr->flags &= ~PNG_FLAG_FILLER_AFTER;
+
+   /* This should probably go in the "do_filler" routine.
+    * I attempted to do that in libpng-1.0.1a but that caused problems
+    * so I restored it in libpng-1.0.2a
+   */
+
+   if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
+   {
+      png_ptr->usr_channels = 4;
+   }
+
+   /* Also I added this in libpng-1.0.2a (what happens when we expand
+    * a less-than-8-bit grayscale to GA? */
+
+   if (png_ptr->color_type == PNG_COLOR_TYPE_GRAY && png_ptr->bit_depth >= 8)
+   {
+      png_ptr->usr_channels = 2;
+   }
 }
 #endif
 
diff --git a/pngwio.c b/pngwio.c
index 5e47099..2d3243b 100644
--- a/pngwio.c
+++ b/pngwio.c
@@ -1,7 +1,7 @@
 
 /* pngwio.c - functions for data output
  *
- * libpng 1.0.2 - June 14, 1998
+ * libpng 1.0.2a - December 29, 1998
  * For conditions of distribution and use, see copyright notice in png.h
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
@@ -184,11 +184,11 @@
    }
 }
 
-#if defined(USE_FAR_KEYWORD) 
-#if defined(_MSC_VER)   
+#if defined(USE_FAR_KEYWORD)
+#if defined(_MSC_VER)
 void *png_far_to_near(png_structp png_ptr,png_voidp ptr, int check)
 {
-   void *near_ptr;   
+   void *near_ptr;
    void FAR *far_ptr;
    FP_OFF(near_ptr) = FP_OFF(ptr);
    far_ptr = (void FAR *)near_ptr;
@@ -200,7 +200,7 @@
 #  else
 void *png_far_to_near(png_structp png_ptr,png_voidp ptr, int check)
 {
-   void *near_ptr;   
+   void *near_ptr;
    void FAR *far_ptr;
    near_ptr = (void FAR *)ptr;
    far_ptr = (void FAR *)near_ptr;
diff --git a/pngwrite.c b/pngwrite.c
index a47b5a6..5966c9b 100644
--- a/pngwrite.c
+++ b/pngwrite.c
@@ -1,7 +1,7 @@
-   
+
 /* pngwrite.c - general routines to write a PNG file
  *
- * libpng 1.0.2 - June 14, 1998
+ * libpng 1.0.2a - December 29, 1998
  * For conditions of distribution and use, see copyright notice in png.h
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
diff --git a/pngwtran.c b/pngwtran.c
index 896d324..d14c659 100644
--- a/pngwtran.c
+++ b/pngwtran.c
@@ -1,7 +1,7 @@
 
 /* pngwtran.c - transforms the data in a row for PNG writers
  *
- * libpng 1.0.2 - June 14, 1998
+ * libpng 1.0.2a - December 29, 1998
  * For conditions of distribution and use, see copyright notice in png.h
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
@@ -47,15 +47,15 @@
       png_do_pack(&(png_ptr->row_info), png_ptr->row_buf + 1,
          (png_uint_32)png_ptr->bit_depth);
 #endif
+#if defined(PNG_WRITE_SWAP_SUPPORTED)
+   if (png_ptr->transformations & PNG_SWAP_BYTES)
+      png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
+#endif
 #if defined(PNG_WRITE_SHIFT_SUPPORTED)
    if (png_ptr->transformations & PNG_SHIFT)
       png_do_shift(&(png_ptr->row_info), png_ptr->row_buf + 1,
          &(png_ptr->shift));
 #endif
-#if defined(PNG_WRITE_SWAP_SUPPORTED)
-   if (png_ptr->transformations & PNG_SWAP_BYTES)
-      png_do_swap(&(png_ptr->row_info), png_ptr->row_buf + 1);
-#endif
 #if defined(PNG_WRITE_INVERT_ALPHA_SUPPORTED)
    if (png_ptr->transformations & PNG_INVERT_ALPHA)
       png_do_write_invert_alpha(&(png_ptr->row_info), png_ptr->row_buf + 1);
@@ -102,7 +102,7 @@
             dp = row;
             mask = 0x80;
             v = 0;
-           
+
             for (i = 0; i < row_width; i++)
             {
                if (*sp != 0)
diff --git a/pngwutil.c b/pngwutil.c
index 8d0c6ee..ea078f6 100644
--- a/pngwutil.c
+++ b/pngwutil.c
@@ -1,7 +1,7 @@
 
 /* pngwutil.c - utilities to write a PNG file
  *
- * libpng 1.0.2 - June 14, 1998
+ * libpng 1.0.2a - December 29, 1998
  * For conditions of distribution and use, see copyright notice in png.h
  * Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
  * Copyright (c) 1996, 1997 Andreas Dilger
@@ -376,9 +376,9 @@
    {
       png_byte maxbits;
 
-      maxbits = color_type==PNG_COLOR_TYPE_PALETTE ? 8:png_ptr->usr_bit_depth;
-      if (sbit->red == 0 || sbit->red > maxbits || 
-          sbit->green == 0 || sbit->green > maxbits || 
+      maxbits = color_type==PNG_COLOR_TYPE_PALETTE ? 8 : png_ptr->usr_bit_depth;
+      if (sbit->red == 0 || sbit->red > maxbits ||
+          sbit->green == 0 || sbit->green > maxbits ||
           sbit->blue == 0 || sbit->blue > maxbits)
       {
          png_warning(png_ptr, "Invalid sBIT depth specified");
@@ -952,7 +952,7 @@
 png_write_pCAL(png_structp png_ptr, png_charp purpose, png_int_32 X0,
    png_int_32 X1, int type, int nparams, png_charp units, png_charpp params)
 {
-   png_size_t purpose_len, units_len, total_len; 
+   png_size_t purpose_len, units_len, total_len;
    png_uint_32p params_len;
    png_byte buf[10];
    png_charp new_purpose;
@@ -1180,7 +1180,7 @@
       if (png_ptr->pass < 7)
       {
          if (png_ptr->prev_row != NULL)
-            png_memset(png_ptr->prev_row, 0, 
+            png_memset(png_ptr->prev_row, 0,
                (png_size_t) (((png_uint_32)png_ptr->usr_channels *
                (png_uint_32)png_ptr->usr_bit_depth *
                png_ptr->width + 7) >> 3) + 1);
@@ -1772,7 +1772,7 @@
       }
       for (lp = row_buf + 1; i < row_bytes; i++)
       {
-         v = *dp++ = 
+         v = *dp++ =
           (png_byte)(((int)*rp++ - (((int)*pp++ + (int)*lp++) / 2)) & 0xff);
 
          sum += (v < 128) ? v : 256 - v;
diff --git a/scripts/makefile.aco b/scripts/makefile.aco
index 2cb0898..7fa6974 100644
--- a/scripts/makefile.aco
+++ b/scripts/makefile.aco
@@ -49,173 +49,3 @@
 
 
 # Dynamic dependencies:
-o.pngtest:	c.pngtest
-o.pngtest:	h.png
-o.pngtest:	Zlib:h.zlib
-o.pngtest:	Zlib:h.zconf
-o.pngtest:	h.pngconf
-mm.png:		LibPng:c.png
-mm.png:		LibPng:h.png
-mm.png:		Zlib:h.zlib
-mm.png:		Zlib:h.zconf
-mm.png:		LibPng:h.pngconf
-mm.png:		MemCheck:ANSI.h.stdio
-mm.pngerror:	LibPng:c.pngerror
-mm.pngerror:	LibPng:h.png
-mm.pngerror:	Zlib:h.zlib
-mm.pngerror:	Zlib:h.zconf
-mm.pngerror:	LibPng:h.pngconf
-mm.pngerror:	MemCheck:ANSI.h.stdio
-mm.pngrio:	LibPng:c.pngrio
-mm.pngrio:	LibPng:h.png
-mm.pngrio:	Zlib:h.zlib
-mm.pngrio:	Zlib:h.zconf
-mm.pngrio:	LibPng:h.pngconf
-mm.pngrio:	MemCheck:ANSI.h.stdio
-mm.pngwio:	LibPng:c.pngwio
-mm.pngwio:	LibPng:h.png
-mm.pngwio:	Zlib:h.zlib
-mm.pngwio:	Zlib:h.zconf
-mm.pngwio:	LibPng:h.pngconf
-mm.pngwio:	MemCheck:ANSI.h.stdio
-mm.pngmem:	LibPng:c.pngmem
-mm.pngmem:	LibPng:h.png
-mm.pngmem:	Zlib:h.zlib
-mm.pngmem:	Zlib:h.zconf
-mm.pngmem:	LibPng:h.pngconf
-mm.pngmem:	MemCheck:ANSI.h.stdio
-mm.pngpread:	LibPng:c.pngpread
-mm.pngpread:	LibPng:h.png
-mm.pngpread:	Zlib:h.zlib
-mm.pngpread:	Zlib:h.zconf
-mm.pngpread:	LibPng:h.pngconf
-mm.pngpread:	MemCheck:ANSI.h.stdio
-mm.pngset:	LibPng:c.pngset
-mm.pngset:	LibPng:h.png
-mm.pngset:	Zlib:h.zlib
-mm.pngset:	Zlib:h.zconf
-mm.pngset:	LibPng:h.pngconf
-mm.pngset:	MemCheck:ANSI.h.stdio
-mm.pngget:	LibPng:c.pngget
-mm.pngget:	LibPng:h.png
-mm.pngget:	Zlib:h.zlib
-mm.pngget:	Zlib:h.zconf
-mm.pngget:	LibPng:h.pngconf
-mm.pngget:	MemCheck:ANSI.h.stdio
-mm.pngread:	LibPng:c.pngread
-mm.pngread:	LibPng:h.png
-mm.pngread:	Zlib:h.zlib
-mm.pngread:	Zlib:h.zconf
-mm.pngread:	LibPng:h.pngconf
-mm.pngread:	MemCheck:ANSI.h.stdio
-mm.pngrtran:	LibPng:c.pngrtran
-mm.pngrtran:	LibPng:h.png
-mm.pngrtran:	Zlib:h.zlib
-mm.pngrtran:	Zlib:h.zconf
-mm.pngrtran:	LibPng:h.pngconf
-mm.pngrtran:	MemCheck:ANSI.h.stdio
-mm.pngrutil:	LibPng:c.pngrutil
-mm.pngrutil:	LibPng:h.png
-mm.pngrutil:	Zlib:h.zlib
-mm.pngrutil:	Zlib:h.zconf
-mm.pngrutil:	LibPng:h.pngconf
-mm.pngrutil:	MemCheck:ANSI.h.stdio
-mm.pngtrans:	LibPng:c.pngtrans
-mm.pngtrans:	LibPng:h.png
-mm.pngtrans:	Zlib:h.zlib
-mm.pngtrans:	Zlib:h.zconf
-mm.pngtrans:	LibPng:h.pngconf
-mm.pngtrans:	MemCheck:ANSI.h.stdio
-mm.pngwrite:	LibPng:c.pngwrite
-mm.pngwrite:	LibPng:h.png
-mm.pngwrite:	Zlib:h.zlib
-mm.pngwrite:	Zlib:h.zconf
-mm.pngwrite:	LibPng:h.pngconf
-mm.pngwrite:	MemCheck:ANSI.h.stdio
-mm.pngwtran:	LibPng:c.pngwtran
-mm.pngwtran:	LibPng:h.png
-mm.pngwtran:	Zlib:h.zlib
-mm.pngwtran:	Zlib:h.zconf
-mm.pngwtran:	LibPng:h.pngconf
-mm.pngwtran:	MemCheck:ANSI.h.stdio
-mm.pngwutil:	LibPng:c.pngwutil
-mm.pngwutil:	LibPng:h.png
-mm.pngwutil:	Zlib:h.zlib
-mm.pngwutil:	Zlib:h.zconf
-mm.pngwutil:	LibPng:h.pngconf
-mm.pngwutil:	MemCheck:ANSI.h.stdio
-o.png:		c.png
-o.png:		h.png
-o.png:		Zlib:h.zlib
-o.png:		Zlib:h.zconf
-o.png:		h.pngconf
-o.pngerror:	c.pngerror
-o.pngerror:	h.png
-o.pngerror:	Zlib:h.zlib
-o.pngerror:	Zlib:h.zconf
-o.pngerror:	h.pngconf
-o.pngrio:	c.pngrio
-o.pngrio:	h.png
-o.pngrio:	Zlib:h.zlib
-o.pngrio:	Zlib:h.zconf
-o.pngrio:	h.pngconf
-o.pngwio:	c.pngwio
-o.pngwio:	h.png
-o.pngwio:	Zlib:h.zlib
-o.pngwio:	Zlib:h.zconf
-o.pngwio:	h.pngconf
-o.pngmem:	c.pngmem
-o.pngmem:	h.png
-o.pngmem:	Zlib:h.zlib
-o.pngmem:	Zlib:h.zconf
-o.pngmem:	h.pngconf
-o.pngpread:	c.pngpread
-o.pngpread:	h.png
-o.pngpread:	Zlib:h.zlib
-o.pngpread:	Zlib:h.zconf
-o.pngpread:	h.pngconf
-o.pngset:	c.pngset
-o.pngset:	h.png
-o.pngset:	Zlib:h.zlib
-o.pngset:	Zlib:h.zconf
-o.pngset:	h.pngconf
-o.pngget:	c.pngget
-o.pngget:	h.png
-o.pngget:	Zlib:h.zlib
-o.pngget:	Zlib:h.zconf
-o.pngget:	h.pngconf
-o.pngread:	c.pngread
-o.pngread:	h.png
-o.pngread:	Zlib:h.zlib
-o.pngread:	Zlib:h.zconf
-o.pngread:	h.pngconf
-o.pngrtran:	c.pngrtran
-o.pngrtran:	h.png
-o.pngrtran:	Zlib:h.zlib
-o.pngrtran:	Zlib:h.zconf
-o.pngrtran:	h.pngconf
-o.pngrutil:	c.pngrutil
-o.pngrutil:	h.png
-o.pngrutil:	Zlib:h.zlib
-o.pngrutil:	Zlib:h.zconf
-o.pngrutil:	h.pngconf
-o.pngtrans:	c.pngtrans
-o.pngtrans:	h.png
-o.pngtrans:	Zlib:h.zlib
-o.pngtrans:	Zlib:h.zconf
-o.pngtrans:	h.pngconf
-o.pngwrite:	c.pngwrite
-o.pngwrite:	h.png
-o.pngwrite:	Zlib:h.zlib
-o.pngwrite:	Zlib:h.zconf
-o.pngwrite:	h.pngconf
-o.pngwtran:	c.pngwtran
-o.pngwtran:	h.png
-o.pngwtran:	Zlib:h.zlib
-o.pngwtran:	Zlib:h.zconf
-o.pngwtran:	h.pngconf
-o.pngwutil:	c.pngwutil
-o.pngwutil:	h.png
-o.pngwutil:	Zlib:h.zlib
-o.pngwutil:	Zlib:h.zconf
-o.pngwutil:	h.pngconf
diff --git a/scripts/makefile.atr b/scripts/makefile.atr
index d490ce1..2d14ff8 100644
--- a/scripts/makefile.atr
+++ b/scripts/makefile.atr
@@ -9,6 +9,7 @@
 LDFLAGS=-lpng -lz -lm
 
 # where make install puts libpng.a and png.h
+prefix=/usr/local
 
 OBJS = $(LBR)(png.o) $(LBR)(pngset.o) $(LBR)(pngget.o) $(LBR)(pngrutil.o)\
 	$(LBR)(pngtrans.o) $(LBR)(pngwutil.o)\
@@ -28,4 +29,5 @@
 	-@mkdir $(prefix)/lib
 	cp png.h $(prefix)/include
 	cp pngconf.h $(prefix)/include
-	chmod 644 $(prefix)/include/p
+	chmod 644 $(prefix)/include/png.h
+	chmod 644 $(prefix)/include/pngconf.h
diff --git a/scripts/makefile.bor b/scripts/makefile.bor
index 0b7c0e6..13432c1 100644
--- a/scripts/makefile.bor
+++ b/scripts/makefile.bor
@@ -100,7 +100,7 @@
  +pngtrans.$(O) \
  +pngwrite.$(O) \
  +pngwtran.$(O) \
- +pngwio.$(O)
+ +pngwio.$(O) \
  +pngwutil.$(O)
 
 LIBNAME=libpng$(MODEL).lib
@@ -121,7 +121,7 @@
 
 pngtest: pngtest$(MODEL).exe
 
-test:
+test: pngtest$(MODEL).exe
 	pngtest$(MODEL)
 
 
diff --git a/scripts/makefile.dec b/scripts/makefile.dec
index 328799a..0fb800a 100644
--- a/scripts/makefile.dec
+++ b/scripts/makefile.dec
@@ -2,12 +2,21 @@
 # Copyright (C) 1995 Guy Eric Schalnat, Group 42, Inc.
 # For conditions of distribution and use, see copyright notice in png.h
 
+# where make install puts libpng.a and png.h
+prefix=/usr/local
+
 # Where the zlib library and include files are located
 #ZLIBLIB=/usr/local/lib
 #ZLIBINC=/usr/local/include
 ZLIBLIB=../zlib
 ZLIBINC=../zlib
 
+# read libpng.txt or png.h to see why PNGMAJ is 2.  You should not
+# have to change it.
+PNGMAJ = 2
+PNGMIN = 1.0.2a
+PNGVER = $(PNGMAJ).$(PNGMIN)
+
 CC=cc
 CFLAGS=-std -w1 -I$(ZLIBINC) -O # -g -DPNG_DEBUG=1
 LDFLAGS=-L. -L$(ZLIBLIB) -lpng -lz -lm
@@ -15,26 +24,33 @@
 #RANLIB=echo
 RANLIB=ranlib
 
-# where make install puts libpng.a and png.h
-prefix=/usr/local
 
 OBJS = png.o pngset.o pngget.o pngrutil.o pngtrans.o pngwutil.o \
 	pngread.o pngrio.o pngwio.o pngwrite.o pngrtran.o \
 	pngwtran.o pngmem.o pngerror.o pngpread.o
 
-all: libpng.a pngtest
+all: libpng.so libpng.a pngtest
 
 libpng.a: $(OBJS)
 	ar rc $@  $(OBJS)
 	$(RANLIB) $@
 
+libpng.so: libpng.so.$(PNGMAJ)
+	ln -s -f libpng.so.$(PNGMAJ) libpng.so
+
+libpng.so.$(PNGMAJ): libpng.so.$(PNGVER)
+	ln -s -f libpng.so.$(PNGVER) libpng.so.$(PNGMAJ)
+
+libpng.so.$(PNGVER): $(OBJS)
+	$(CC) -shared -o $@ $(OBJS) -L$(ZLIBLIB) -lz -lm
+
 pngtest: pngtest.o libpng.a
 	$(CC) -o pngtest $(CFLAGS) pngtest.o $(LDFLAGS)
 
 test: pngtest
 	./pngtest
 
-install: libpng.a
+install: libpng.a libpng.so.$(PNGVER)
 	-@mkdir $(prefix)/include
 	-@mkdir $(prefix)/lib
 	cp png.h $(prefix)/include
@@ -42,10 +58,15 @@
 	chmod 644 $(prefix)/include/png.h
 	chmod 644 $(prefix)/include/pngconf.h
 	cp libpng.a $(prefix)/lib
+	cp libpng.so.$(PNGVER) $(prefix)/lib
 	chmod 644 $(prefix)/lib/libpng.a
+	chmod 644 $(prefix)/lib/libpng.so.$(PNGVER)
+	-@/bin/rm -f $(LIBPATH)/libpng.so.$(PNGMAJ) $(LIBPATH)/libpng.so
+	(cd $(LIBPATH); ln -s -f libpng.so.$(PNGVER) libpng.so.$(PNGMAJ); \
+	 ln -s -f libpng.so.$(PNGMAJ) libpng.so)
 
 clean:
-	rm -f *.o libpng.a pngtest pngout.png
+	rm -f *.o libpng.a pngtest pngout.png libpng.so*
 
 # DO NOT DELETE THIS LINE -- make depend depends on it.
 
diff --git a/scripts/makefile.dj2 b/scripts/makefile.dj2
index e80f5cc..8ba1770 100644
--- a/scripts/makefile.dj2
+++ b/scripts/makefile.dj2
@@ -2,16 +2,16 @@
 # Copyright (C) 1995 Guy Eric Schalnat, Group 42, Inc.
 # For conditions of distribution and use, see copyright notice in png.h
 
+# where make install puts libpng.a and png.h
+#prefix=/usr/local
+prefix=.
+
 CC=gcc
 CFLAGS=-I../zlib -O
 LDFLAGS=-L. -L../zlib/ -lpng -lz -lm
 
 RANLIB=ranlib
 
-# where make install puts libpng.a and png.h
-#prefix=/usr/local
-prefix=.
-
 OBJS = png.o pngset.o pngget.o pngrutil.o pngtrans.o pngwutil.o \
 	pngread.o pngrio.o pngwio.o pngwrite.o pngrtran.o pngwtran.o \
 	pngmem.o pngerror.o pngpread.o
diff --git a/scripts/makefile.knr b/scripts/makefile.knr
index 81129ae..8300422 100644
--- a/scripts/makefile.knr
+++ b/scripts/makefile.knr
@@ -2,6 +2,9 @@
 # Copyright (C) 1995 Guy Eric Schalnat, Group 42, Inc.
 # For conditions of distribution and use, see copyright notice in png.h
 
+# where make install puts libpng.a and png.h
+prefix=/usr/local
+
 CC=cc
 CFLAGS=-I../zlib -O
 LDFLAGS=-L. -L../zlib/ -lpng -lz -lm
@@ -11,9 +14,6 @@
 RANLIB=ranlib
 #RANLIB=echo
 
-# where make install puts libpng.a and png.h
-prefix=/usr/local
-
 OBJS = png.o pngset.o pngget.o pngrutil.o pngtrans.o pngwutil.o \
 	pngread.o pngrio.o pngwio.o pngwrite.o pngrtran.o \
 	pngwtran.o pngmem.o pngerror.o pngpread.o
diff --git a/scripts/makefile.lnx b/scripts/makefile.lnx
index 1049de9..c070cf9 100644
--- a/scripts/makefile.lnx
+++ b/scripts/makefile.lnx
@@ -5,6 +5,9 @@
 
 CC=gcc
 
+# where make install puts libpng.a, libpng.so*, and png.h
+prefix=/usr/local
+
 # Where the zlib library and include files are located
 #ZLIBLIB=/usr/local/lib
 #ZLIBINC=/usr/local/include
@@ -29,11 +32,9 @@
 # read libpng.txt or png.h to see why PNGMAJ is 2.  You should not
 # have to change it.
 PNGMAJ = 2
-PNGMIN = 1.0.2
+PNGMIN = 1.0.2a
 PNGVER = $(PNGMAJ).$(PNGMIN)
 
-# where make install puts libpng.a, libpng.so*, and png.h
-prefix=/usr/local
 INCPATH=$(prefix)/include
 LIBPATH=$(prefix)/lib
 
@@ -55,14 +56,14 @@
 	$(RANLIB) $@
 
 libpng.so: libpng.so.$(PNGMAJ)
-	ln -sf libpng.so.$(PNGMAJ) libpng.so
+	ln -s -f libpng.so.$(PNGMAJ) libpng.so
 
 libpng.so.$(PNGMAJ): libpng.so.$(PNGVER)
-	ln -sf libpng.so.$(PNGVER) libpng.so.$(PNGMAJ)
+	ln -s -f libpng.so.$(PNGVER) libpng.so.$(PNGMAJ)
 
 libpng.so.$(PNGVER): $(OBJSDLL)
 	$(CC) -shared -Wl,-soname,libpng.so.$(PNGMAJ) -o libpng.so.$(PNGVER) \
-	 $(OBJSDLL)
+	 $(OBJSDLL) -L$(ZLIBLIB) -lz -lm -lc
 
 pngtest: pngtest.o libpng.so
 	$(CC) -o pngtest $(CFLAGS) pngtest.o $(LDFLAGS)
@@ -77,8 +78,8 @@
 	cp libpng.a libpng.so.$(PNGVER) $(LIBPATH)
 	chmod 755 $(LIBPATH)/libpng.so.$(PNGVER)
 	-@/bin/rm -f $(LIBPATH)/libpng.so.$(PNGMAJ) $(LIBPATH)/libpng.so
-	(cd $(LIBPATH); ln -sf libpng.so.$(PNGVER) libpng.so.$(PNGMAJ); \
-	 ln -sf libpng.so.$(PNGMAJ) libpng.so)
+	(cd $(LIBPATH); ln -s -f libpng.so.$(PNGVER) libpng.so.$(PNGMAJ); \
+	 ln -s -f libpng.so.$(PNGMAJ) libpng.so)
 
 clean:
 	/bin/rm -f *.o libpng.a libpng.so* pngtest pngout.png
diff --git a/scripts/makefile.mip b/scripts/makefile.mip
index 2b3cccd..06ca02f 100644
--- a/scripts/makefile.mip
+++ b/scripts/makefile.mip
@@ -2,6 +2,9 @@
 # Copyright (C) 1995 Guy Eric Schalnat, Group 42, Inc.
 # For conditions of distribution and use, see copyright notice in png.h
 
+# where make install puts libpng.a and png.h
+prefix=/usr/local
+
 CC=cc
 CFLAGS=-I../zlib -O -systype sysv -DSYSV -w -Dmips
 #CFLAGS=-O
@@ -10,9 +13,6 @@
 #RANLIB=ranlib
 RANLIB=echo
 
-# where make install puts libpng.a and png.h
-prefix=/usr/local
-
 OBJS = png.o pngset.o pngget.o pngrutil.o pngtrans.o pngwutil.o \
 	pngread.o pngrio.o pngwio.o pngwrite.o pngrtran.o \
 	pngwtran.o pngmem.o pngerror.o pngpread.o
diff --git a/scripts/makefile.s2x b/scripts/makefile.s2x
index c54c021..3b39897 100644
--- a/scripts/makefile.s2x
+++ b/scripts/makefile.s2x
@@ -6,6 +6,9 @@
 
 CC=gcc
 
+# where make install puts libpng.a, libpng.so*, and png.h
+prefix=/usr/local
+
 # Where the zlib library and include files are located
 # Changing these to ../zlib poses a security risk.  If you want
 # to have zlib in an adjacent directory, specify the full path instead of "..".
@@ -15,7 +18,6 @@
 ZLIBLIB=/usr/local/lib
 ZLIBINC=/usr/local/include
 
-
 WARNMORE=-Wwrite-strings -Wpointer-arith -Wshadow \
          -Wmissing-declarations -Wtraditional -Wcast-align \
          -Wstrict-prototypes -Wmissing-prototypes #-Wconversion
@@ -29,11 +31,9 @@
 # read libpng.txt or png.h to see why PNGMAJ is 2.  You should not
 # have to change it.
 PNGMAJ = 2
-PNGMIN = 1.0.2
+PNGMIN = 1.0.2a
 PNGVER = $(PNGMAJ).$(PNGMIN)
 
-# where make install puts libpng.a, libpng.so*, and png.h
-prefix=/usr/local
 INCPATH=$(prefix)/include
 LIBPATH=$(prefix)/lib
 
@@ -55,10 +55,10 @@
 	$(RANLIB) $@
 
 libpng.so: libpng.so.$(PNGMAJ)
-	ln -sf libpng.so.$(PNGMAJ) libpng.so
+	ln -s -f libpng.so.$(PNGMAJ) libpng.so
 
 libpng.so.$(PNGMAJ): libpng.so.$(PNGVER)
-	ln -sf libpng.so.$(PNGVER) libpng.so.$(PNGMAJ)
+	ln -s -f libpng.so.$(PNGVER) libpng.so.$(PNGMAJ)
 
 libpng.so.$(PNGVER): $(OBJSDLL)
 	$(LD) -G -L$(ZLIBLIB) -R$(ZLIBLIB) -h libpng.so.$(PNGMAJ) \
@@ -77,8 +77,8 @@
 	cp libpng.a libpng.so.$(PNGVER) $(LIBPATH)
 	chmod 755 $(LIBPATH)/libpng.so.$(PNGVER)
 	-@/bin/rm -f $(LIBPATH)/libpng.so.$(PNGMAJ) $(LIBPATH)/libpng.so
-	(cd $(LIBPATH); ln -sf libpng.so.$(PNGVER) libpng.so.$(PNGMAJ); \
-	 ln -sf libpng.so.$(PNGMAJ) libpng.so)
+	(cd $(LIBPATH); ln -s -f libpng.so.$(PNGVER) libpng.so.$(PNGMAJ); \
+	 ln -s -f libpng.so.$(PNGMAJ) libpng.so)
 
 clean:
 	/bin/rm -f *.o libpng.a libpng.so* pngtest pngout.png
diff --git a/scripts/makefile.sco b/scripts/makefile.sco
index 323e378..c6913e2 100644
--- a/scripts/makefile.sco
+++ b/scripts/makefile.sco
@@ -7,6 +7,9 @@
 
 CC=cc
 
+# where make install puts libpng.a, libpng.so*, and png.h
+prefix=/usr/local
+
 # Where the zlib library and include files are located
 #ZLIBLIB=/usr/local/lib
 #ZLIBINC=/usr/local/include
@@ -22,11 +25,9 @@
 # read libpng.txt or png.h to see why PNGMAJ is 2.  You should not
 # have to change it.
 PNGMAJ = 2
-PNGMIN = 1.0.2
+PNGMIN = 1.0.2a
 PNGVER = $(PNGMAJ).$(PNGMIN)
 
-# where make install puts libpng.a, libpng.so*, and png.h
-prefix=/usr/local
 INCPATH=$(prefix)/include
 LIBPATH=$(prefix)/lib
 
@@ -48,10 +49,10 @@
 	$(RANLIB) $@
 
 libpng.so: libpng.so.$(PNGMAJ)
-	ln -sf libpng.so.$(PNGMAJ) libpng.so
+	ln -s -f libpng.so.$(PNGMAJ) libpng.so
 
 libpng.so.$(PNGMAJ): libpng.so.$(PNGVER)
-	ln -sf libpng.so.$(PNGVER) libpng.so.$(PNGMAJ)
+	ln -s -f libpng.so.$(PNGVER) libpng.so.$(PNGMAJ)
 
 libpng.so.$(PNGVER): $(OBJSDLL)
 	$(CC) -G  -Wl,-h,libpng.so.$(PNGMAJ) -o libpng.so.$(PNGVER) \
@@ -70,8 +71,8 @@
 	cp libpng.a libpng.so.$(PNGVER) $(LIBPATH)
 	chmod 755 $(LIBPATH)/libpng.so.$(PNGVER)
 	-@/bin/rm -f $(LIBPATH)/libpng.so.$(PNGMAJ) $(LIBPATH)/libpng.so
-	(cd $(LIBPATH); ln -sf libpng.so.$(PNGVER) libpng.so.$(PNGMAJ); \
-	 ln -sf libpng.so.$(PNGMAJ) libpng.so)
+	(cd $(LIBPATH); ln -s -f libpng.so.$(PNGVER) libpng.so.$(PNGMAJ); \
+	 ln -s -f libpng.so.$(PNGMAJ) libpng.so)
 
 clean:
 	/bin/rm -f *.o libpng.a libpng.so* pngtest pngout.png
diff --git a/scripts/makefile.sgi b/scripts/makefile.sgi
index ec7fd30..715624b 100644
--- a/scripts/makefile.sgi
+++ b/scripts/makefile.sgi
@@ -2,6 +2,9 @@
 # Copyright (C) 1995 Guy Eric Schalnat, Group 42, Inc.
 # For conditions of distribution and use, see copyright notice in png.h
 
+# where make install puts libpng.a and png.h
+prefix=/usr/local
+
 # Where the zlib library and include files are located
 #ZLIBLIB=/usr/local/lib
 #ZLIBINC=/usr/local/include
@@ -17,9 +20,6 @@
 RANLIB=echo
 #RANLIB=ranlib
 
-# where make install puts libpng.a and png.h
-prefix=/usr/local
-
 OBJS = png.o pngset.o pngget.o pngrutil.o pngtrans.o pngwutil.o \
 	pngread.o pngrio.o pngwio.o pngwrite.o pngrtran.o \
 	pngwtran.o pngmem.o pngerror.o pngpread.o
diff --git a/scripts/makefile.std b/scripts/makefile.std
index ddb5a29..6afe481 100644
--- a/scripts/makefile.std
+++ b/scripts/makefile.std
@@ -2,6 +2,9 @@
 # Copyright (C) 1995 Guy Eric Schalnat, Group 42, Inc.
 # For conditions of distribution and use, see copyright notice in png.h
 
+# where make install puts libpng.a and png.h
+prefix=/usr/local
+
 # Where the zlib library and include files are located
 #ZLIBLIB=/usr/local/lib
 #ZLIBINC=/usr/local/include
@@ -15,9 +18,6 @@
 #RANLIB=echo
 RANLIB=ranlib
 
-# where make install puts libpng.a and png.h
-prefix=/usr/local
-
 OBJS = png.o pngset.o pngget.o pngrutil.o pngtrans.o pngwutil.o \
 	pngread.o pngrio.o pngwio.o pngwrite.o pngrtran.o \
 	pngwtran.o pngmem.o pngerror.o pngpread.o
diff --git a/scripts/makefile.sun b/scripts/makefile.sun
index 1f96f22..db91d21 100644
--- a/scripts/makefile.sun
+++ b/scripts/makefile.sun
@@ -2,6 +2,9 @@
 # Copyright (C) 1995 Guy Eric Schalnat, Group 42, Inc.
 # For conditions of distribution and use, see copyright notice in png.h
 
+# where make install puts libpng.a and png.h
+prefix=/usr/local
+
 # Where the zlib library and include files are located
 #ZLIBLIB=/usr/local/lib
 #ZLIBINC=/usr/local/include
@@ -19,9 +22,6 @@
 RANLIB=ranlib
 #RANLIB=echo
 
-# where make install puts libpng.a and png.h
-prefix=/usr/local
-
 OBJS = png.o pngset.o pngget.o pngrutil.o pngtrans.o pngwutil.o \
 	pngread.o pngrio.o pngwio.o pngwrite.o pngrtran.o \
 	pngwtran.o pngmem.o pngerror.o pngpread.o
diff --git a/scripts/makefile.w32 b/scripts/makefile.w32
index 3893600..109f3a3 100644
--- a/scripts/makefile.w32
+++ b/scripts/makefile.w32
@@ -14,7 +14,7 @@
 O=.obj
 
 #uncomment next to put error messages in a file
-ERRFILE= >> pngerrs
+#ERRFILE= >> pngerrs
 
 # variables
 OBJS1 = png$(O) pngset$(O) pngget$(O) pngrutil$(O) pngtrans$(O) pngwutil$(O)
diff --git a/scripts/pngdef.pas b/scripts/pngdef.pas
index d948aae..92ddb8a 100644
--- a/scripts/pngdef.pas
+++ b/scripts/pngdef.pas
@@ -3,7 +3,7 @@
 interface
 
 const
-  PNG_LIBPNG_VER_STRING = '1.0.2';
+  PNG_LIBPNG_VER_STRING = '1.0.2a';
   PNG_LIBPNG_VER        =  10001;
 
 type
@@ -259,6 +259,10 @@
              error_ptr: user_error_ptr; error_fn: png_error_ptr;
              warn_fn: png_error_ptr): png_structp;
              stdcall;
+function png_get_copyright(png_ptr: png_structp): png_charp;
+             stdcall;
+function png_get_header_version(png_ptr: png_structp): png_charp;
+             stdcall;
 function png_create_write_struct(user_png_ver: png_charp;
              error_ptr: user_error_ptr; error_fn: png_error_ptr;
              warn_fn: png_error_ptr): png_structp;
@@ -343,6 +347,8 @@
              stdcall;
 function png_get_progressive_ptr(png_ptr: png_structp): png_voidp;
              stdcall;
+function png_get_rgb_to_gray_status(png_ptr: png_structp);
+             stdcall;
 function png_get_rowbytes(png_ptr: png_structp; info_ptr: png_infop):
              png_uint_32;
              stdcall;
@@ -510,6 +516,8 @@
 procedure png_set_read_user_transform_fn(png_ptr: png_structp;
              read_user_transform_fn: png_user_transform_ptr);
              stdcall;
+procedure png_set_rgb_to_gray(png_ptr: png_structp; int: error_action);
+             stdcall;
 procedure png_set_sBIT(png_ptr: png_structp; info_ptr: png_infop;
              sig_bits: png_color_8p);
              stdcall;
diff --git a/scripts/pngos2.def b/scripts/pngos2.def
index 72620de..d12d498 100644
--- a/scripts/pngos2.def
+++ b/scripts/pngos2.def
@@ -29,7 +29,7 @@
   png_set_expand
   png_set_bgr
   png_set_gray_to_rgb
-; png_set_rgb_to_gray
+  png_set_rgb_to_gray
   png_build_grayscale_palette
   png_set_strip_alpha
   png_set_swap_alpha
@@ -72,8 +72,10 @@
   png_set_compression_window_bits
   png_set_compression_method
   png_init_io
+  png_get_copyright
   png_set_error_fn
   png_get_error_ptr
+  png_get_header_version
 ; png_get_mem_fn
 ; png_set_mem_fn
   png_set_write_fn
@@ -112,6 +114,7 @@
   png_get_compression_type
   png_get_pixels_per_meter
   png_get_pixel_aspect_ratio
+  png_get_rgb_to_gray_status
   png_get_x_offset_pixels
   png_get_y_offset_pixels
   png_get_x_offset_microns
@@ -202,7 +205,7 @@
   png_do_strip_filler
   png_do_swap
   png_do_packswap
-; png_do_rgb_to_gray
+  png_do_rgb_to_gray
   png_do_gray_to_rgb
   png_do_unpack
   png_do_unshift