Imported from libpng-0.87.tar
diff --git a/libpng.txt b/libpng.txt
index d708f65..6e367c4 100644
--- a/libpng.txt
+++ b/libpng.txt
@@ -1,9 +1,9 @@
 libpng.txt - a description on how to use and modify libpng
 
-	libpng 1.0 beta 2 - version 0.86
+	libpng 1.0 beta 2 - version 0.87
 	For conditions of distribution and use, see copyright notice in png.h
 	Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
-	January 10, 1996
+	January 15, 1996
 	Updated/rewritten per request in the libpng FAQ
 	Copyright (c) 1995 Frank J. T. Wojcik
 	December 18, 1995
@@ -13,7 +13,7 @@
 This file describes how to use and modify the PNG reference library
 (known as libpng) for your own use.  There are five sections to this
 file: introduction, structures, reading, writing, and modification and
-configuration notes for various special platforms.  Other then this
+configuration notes for various special platforms.  In addition to this
 file, example.c is a good starting point for using the library, as
 it is heavily commented and should include everything most people
 will need.
@@ -22,7 +22,7 @@
 way to reduce the amount of time and effort it takes to support
 the PNG file format in application programs.  Most users will not
 have to modify the library significantly; advanced users may want
-to modify it more.  The library was coded for both kind of users.
+to modify it more.  The library was coded for both kinds of users.
 All attempts were made to make it as complete as possible, while
 keeping the code easy to understand.  Currently, this library
 only supports C.  Support for other languages is being considered.
@@ -37,9 +37,15 @@
 
 Libpng uses zlib for its compression and decompression of PNG files.
 The zlib compression utility is a general purpose utility that is
-useful for more then PNG files, and can be used without libpng.
+useful for more than PNG files, and can be used without libpng.
 See the documentation delivered with zlib for more details.
 
+Libpng is thread safe provided the threads are using different
+instances of the structures.  Each thread should have its own
+png_struct and png_info instances, and thus its own image.
+Libpng does not protect itself against two threads using the
+same instance of a structure.
+
 
 
 II. Structures
@@ -287,8 +293,8 @@
    if (info_ptr->bit_depth == 16)
       png_set_strip_16(png_ptr);
 
-If you need to reduce an rgb file to a paletted file (perhaps because
-a paletted file has more entries then will fit on your screen)
+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() will do that.  Note that this is a simple match
 dither, that merely finds the closest color available.  This should
 work fairly well with optimized palettes, and fairly badly with linear
@@ -799,7 +805,7 @@
 these, but if you wish to fill in the png_time structure directly,
 you should provide the time in universal time (GMT) if possible
 instead of your local time.  Note that the year number is the full
-year (ie 1996, rather than 96)
+year (ie 1996, rather than 96).
 
 It is possible to have libpng flush any pending output, either manually,
 or automatically after a certain number of lines have been written.  To
@@ -1024,8 +1030,9 @@
 		png_uint_32 length);
 	void user_flush_data(png_structp png_ptr);
 
-Note that you can pass NULL for the flush function if you are not doing
-flushing of the output data.
+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.
 
 Error handling in libpng is done through png_error() and png_warning().
 Errors handled through png_error() are fatal, meaning that png_error()
@@ -1073,13 +1080,19 @@
 
 Configuring for 16 bit platforms:
 
-You will probably need to change the png_large_malloc() and
+You may need to change the png_large_malloc() and
 png_large_free() routines in pngmem.c, as these are requred
 to allocate 64K.  Also, you will want to look into zconf.h to tell
 zlib (and thus libpng) that it cannot allocate more then 64K at a
 time.  Even if you can, the memory won't be accessable.  So limit zlib
 and libpng to 64K by defining MAXSEG_64K.
 
+Configuring for DOS:
+
+For DOS users which 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.
+
 Configuring for Medium Model:
 
 Libpng's support for medium model has been tested on most of the popular
@@ -1089,7 +1102,7 @@
 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
-unsigned char far * far *
+unsigned char far * far *.
 
 Configuring for gui/windowing platforms:
 
@@ -1099,14 +1112,47 @@
 like png_message().  On some compliers, you may have to change the
 memory allocators (png_malloc, etc.).
 
-
 Configuring for compiler xxx:
 
 All includes for libpng are in pngconf.h.  If you need to add/change/delete
 an include, this is the place to do it.  The includes that are not
 needed outside libpng are protected by the PNG_INTERNAL definition,
 which is only defined for those routines inside libpng itself.  The
-files in libpng proper only include png.h.
+files in libpng proper only include png.h, which includes pngconf.h.
+
+Configuring zlib:
+
+There are special functions to configure the compression.  Perhaps
+the most useful one changes the compression level.  The library
+normally uses the default compression level, but for maximum
+compression (9) or maximum speed (1), you may desire to change the
+level.  You do this by calling:
+
+	png_set_compression_mem_level(png_ptr, level);
+
+Another useful one is to reduce the memory level used by the library.
+The memory level defaults to 8, but it can be lowered if you are
+short on memory (running DOS, for example, where you only have 640K).
+
+	png_set_compression_mem_level(png_ptr, level);
+
+If you want to control whether libpng uses filtering or not, you
+can call this function.  I recommend not changing the default unless
+you are experimenting with compression ratios.
+
+	png_set_filtering(png_ptr, use_filter);
+
+The other functions are for configuring zlib.  They are not recommended
+for normal use and may result in writing an invalid png file.  See
+zlib.h for more information on what these mean.
+
+	png_set_compression_strategy(png_ptr, strategy);
+	png_set_compression_window_bits(png_ptr, window_bits);
+	png_set_compression_method(png_ptr, method);
+
+Except for png_set_filtering(), all of these are just controlling
+zlib, so see the zlib documentation (zlib.h and zconf.h) for more
+information.
 
 Removing unwanted object code:
 
diff --git a/makefile.elf b/makefile.elf
new file mode 100644
index 0000000..1533f5b
--- /dev/null
+++ b/makefile.elf
@@ -0,0 +1,73 @@
+# makefile for libpng on (linux) ELF
+# Copyright (C) 1995 Guy Eric Schalnat, Group 42, Inc.
+# For conditions of distribution and use, see copyright notice in png.h
+
+CC=gcc
+CFLAGS=-I../zlib -O2 -Wall -fPIC
+LDFLAGS=-L. -L../zlib/ -lpng -lz -lm
+
+RANLIB=ranlib
+#RANLIB=echo
+
+PNGVER = 0.87
+
+# where make install puts libpng.a, libpng.so*, and png.h
+prefix=/usr/local
+
+OBJS = png.o pngrcb.o pngrutil.o pngtrans.o pngwutil.o \
+       pngread.o pngio.o pngwrite.o pngrtran.o pngwtran.o \
+       pngmem.o pngerror.o pngpread.o
+
+all: libpng.so pngtest
+
+libpng.a: $(OBJS)
+	ar rc $@  $(OBJS)
+	$(RANLIB) $@
+
+libpng.so: libpng.so.1
+	ln -sf libpng.so.1 libpng.so
+
+libpng.so.1: libpng.so.1.$(PNGVER)
+	ln -sf libpng.so.1.$(PNGVER) libpng.so.1
+
+libpng.so.1.$(PNGVER): $(OBJS)
+	gcc -shared -Wl,-soname,libpng.so.1 -o libpng.so.1.$(PNGVER) $(OBJS)
+
+pngtest: pngtest.o libpng.so
+	$(CC) -o pngtest $(CCFLAGS) pngtest.o $(LDFLAGS)
+
+test: pngtest
+	./pngtest
+
+install: libpng.so.1.$(PNGVER)
+	-@mkdir $(prefix)/include
+	-@mkdir $(prefix)/lib
+	cp png.h $(prefix)/include
+	cp pngconf.h $(prefix)/include
+	chmod 644 $(prefix)/include/png.h
+	chmod 644 $(prefix)/include/pngconf.h
+	cp libpng.so.1.$(PNGVER) $(prefix)/lib
+	chmod 755 $(prefix)/lib/libpng.so.1.$(PNGVER)
+	-@/bin/rm $(prefix)/lib/libpng.so.1 $(prefix)/lib/libpng.so
+	(cd $(prefix)/lib; ln -sf libpng.so.1.$(PNGVER) libpng.so.1; \
+	 ln -sf libpng.so.1 libpng.so)
+
+clean:
+	rm -f *.o libpng.so.1.$(PNGVER) libpng.so.1 libpng.so pngtest pngout.png
+
+# DO NOT DELETE THIS LINE -- make depend depends on it.
+
+png.o: png.h pngconf.h
+pngerror.o: png.h pngconf.h
+pngio.o: png.h pngconf.h
+pngmem.o: png.h pngconf.h
+pngrcb.o: png.h pngconf.h
+pngread.o: png.h pngconf.h
+pngrtran.o: png.h pngconf.h
+pngrutil.o: png.h pngconf.h
+pngtest.o: png.h pngconf.h
+pngtrans.o: png.h pngconf.h
+pngwrite.o: png.h pngconf.h
+pngwtran.o: png.h pngconf.h
+pngwutil.o: png.h pngconf.h
+pngpread.o: png.h pngconf.h
diff --git a/makefile b/makefile.sun
similarity index 85%
rename from makefile
rename to makefile.sun
index b7d7c26..ad3a9ff 100644
--- a/makefile
+++ b/makefile.sun
@@ -2,15 +2,15 @@
 # Copyright (C) 1995 Guy Eric Schalnat, Group 42, Inc.
 # For conditions of distribution and use, see copyright notice in png.h
 
-CC=cc
-CFLAGS=-I../zlib -O
+CC=gcc
+CFLAGS=-I../zlib -O2 -Wall
 LDFLAGS=-L. -L../zlib/ -lpng -lz -lm
 
-#RANLIB=ranlib
-RANLIB=echo
+RANLIB=ranlib
+#RANLIB=echo
 
 # where make install puts libpng.a and png.h
-prefix=/usr/local
+prefix=/home/munet-d2/sun/local
 
 OBJS = png.o pngrcb.o pngrutil.o pngtrans.o pngwutil.o \
 	pngread.o pngio.o pngwrite.o pngrtran.o pngwtran.o \
@@ -31,11 +31,12 @@
 install: libpng.a
 	-@mkdir $(prefix)/include
 	-@mkdir $(prefix)/lib
-	cp png.h $(prefix)/include
-	cp pngconf.h $(prefix)/include
+	cp png.h pngconf.h $(prefix)/include
+	rcp png.h pngconf.h vlsi:bin/include
 	chmod 644 $(prefix)/include/png.h
 	chmod 644 $(prefix)/include/pngconf.h
 	cp libpng.a $(prefix)/lib
+	rcp libpng.a vlsi:bin/lib
 	chmod 644 $(prefix)/lib/libpng.a
 
 clean:
@@ -57,3 +58,4 @@
 pngwtran.o: png.h pngconf.h
 pngwutil.o: png.h pngconf.h
 pngpread.o: png.h pngconf.h
+
diff --git a/png.c b/png.c
index e07c001..4663e1a 100644
--- a/png.c
+++ b/png.c
@@ -1,10 +1,10 @@
 
 /* png.c - location for general purpose png functions
 
-	libpng 1.0 beta 2 - version 0.86
+	libpng 1.0 beta 2 - version 0.87
    For conditions of distribution and use, see copyright notice in png.h
 	Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
-   January 10, 1996
+   January 15, 1996
    */
 
 #define PNG_INTERNAL
@@ -13,7 +13,7 @@
 
 /* version information for c files.  This better match the version
    string defined in png.h */
-char FARDATA png_libpng_ver[] = "0.86";
+char png_libpng_ver[] = "0.87";
 
 /* place to hold the signiture string for a png file. */
 png_byte FARDATA png_sig[8] = {137, 80, 78, 71, 13, 10, 26, 10};
@@ -107,11 +107,22 @@
 voidpf
 png_zalloc(voidpf png_ptr, uInt items, uInt size)
 {
-	voidp ptr;
+	png_voidp ptr;
+	png_uint_32 num_bytes;
 
-	ptr = ((voidp)png_large_malloc((png_structp)png_ptr,
-		(png_uint_32)items * (png_uint_32)size));
-	png_memset(ptr, 0, (png_uint_32)items * (png_uint_32)size);
+	ptr = png_large_malloc((png_structp)png_ptr,
+		(png_uint_32)items * (png_uint_32)size);
+	num_bytes = (png_uint_32)items * (png_uint_32)size;
+	if (num_bytes > (png_uint_32)0x7fff)
+	{
+		png_memset(ptr, 0, (png_size_t)0x8000L);
+		png_memset((png_bytep)ptr + (png_size_t)0x8000L, 0,
+			(png_size_t)(num_bytes - (png_uint_32)0x8000L));
+	}
+	else
+	{
+		png_memset(ptr, 0, (png_size_t)num_bytes);
+	}
    return (voidpf)(ptr);
 }
 
@@ -119,7 +130,7 @@
 void
 png_zfree(voidpf png_ptr, voidpf ptr)
 {
-	png_large_free((png_structp)png_ptr, (voidp)ptr);
+	png_large_free((png_structp)png_ptr, (png_voidp)ptr);
 }
 
 /* reset the crc variable to 32 bits of 1's.  Care must be taken
diff --git a/png.h b/png.h
index 67fb984..112bbd8 100644
--- a/png.h
+++ b/png.h
@@ -1,31 +1,31 @@
 
 /* png.h - header file for png reference library
 
-	libpng 1.0 beta 2 - version 0.86
-	Jan 10, 1996
+   libpng 1.0 beta 2 - version 0.87
+   Jan 15, 1996
 
-	Note: This is a beta version.  It reads and writes valid files
-	on the platforms I have, but it has had limited portability
-	testing.  Furthermore, you will may have to modify the
-	includes below to get it to work on your system, and you
-	may have to supply the correct compiler flags in the makefile.
-	Read the readme.txt for more information, and how to contact
-	me if you have any problems, or if you want your compiler/
-	platform to be supported in the next official libpng release.
+   Note: This is a beta version.  It reads and writes valid files
+   on the platforms I have, but it has had limited portability
+   testing.  Furthermore, you may have to modify the
+   includes below to get it to work on your system, and you
+   may have to supply the correct compiler flags in the makefile.
+   Read the readme.txt for more information, and how to contact
+   me if you have any problems, or if you want your compiler/
+   platform to be supported in the next official libpng release.
 
-	See readme.txt for more information
+   See readme.txt for more information
 
    Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
-	Contributing Authors:
+   Contributing Authors:
       Andreas Dilger
       Dave Martindale
-		Guy Eric Schalnat
-		Paul Schmidt
-		Tim Wegner
+      Guy Eric Schalnat
+      Paul Schmidt
+      Tim Wegner
 
-	The contributing authors would like to thank all those who helped
-	with testing, bug fixes, and patience.  You know who you are.  This
-	wouldn't have been possible without all of you.
+   The contributing authors would like to thank all those who helped
+   with testing, bug fixes, and patience.  You know who you are.  This
+   wouldn't have been possible without all of you.
 
    Thanks to Frank J. T. Wojcik for reviewing the documentation
 
@@ -41,16 +41,16 @@
    to the following restrictions:
    1. The origin of this source code must not be misrepresented.
    2. Altered versions must be plainly marked as such and must not be
-	  misrepresented as being the original source.
-	3. This Copyright notice may not be removed or altered from any source or
-	  altered source distribution.
+      misrepresented as being the original source.
+   3. This Copyright notice may not be removed or altered from any source or
+      altered source distribution.
 
-	The Contributing Authors and Group 42, Inc. specifically permit, without
-	fee, and encourage the use of this source code as a component to
-	supporting the PNG file format in commercial products. If you use this
-	source code in a product, acknowledgment is not required but would be
-	appreciated.
-	*/
+   The Contributing Authors and Group 42, Inc. specifically permit, without
+   fee, and encourage the use of this source code as a component to
+   supporting the PNG file format in commercial products. If you use this
+   source code in a product, acknowledgment is not required but would be
+   appreciated.
+   */
 
 #ifndef _PNG_H
 #define _PNG_H
@@ -70,14 +70,14 @@
    the functions most users will use.  The third section describes the
    stub files that users will most likely need to change.  The last
    section contains functions used internally by the code.
-	*/
+   */
 
 /* version information for png.h - this should match the version
    number in png.c */
-#define PNG_LIBPNG_VER_STRING "0.86"
-/* careful here.  I wanted to use 086, but that would be octal.  Version
-	1.0 will be 100 here, etc. */
-#define PNG_LIBPNG_VER 86
+#define PNG_LIBPNG_VER_STRING "0.87"
+/* careful here.  I wanted to use 087, but that would be octal.  Version
+   1.0 will be 100 here, etc. */
+#define PNG_LIBPNG_VER 87
 
 /* variables defined in png.c - only it needs to define PNG_NO_EXTERN */
 #ifndef PNG_NO_EXTERN
@@ -90,16 +90,16 @@
    exact size) is not important, although the size of the fields need to
    be png_byte or png_uint_16 (as defined below).  While png_color_8 and
    png_color_16 have more fields then they need, they are never used in
-	arrays, so the size isn't that important.  I thought about using
+   arrays, so the size isn't that important.  I thought about using
    unions, but it looked too clumsy, so I left it. If you're using C++,
    you can union red, index, and gray, if you really want too. */
 typedef struct png_color_struct
 {
-	png_byte red;
+   png_byte red;
    png_byte green;
    png_byte blue;
 } png_color;
-typedef png_color       FAR *   	png_colorp;
+typedef png_color       FAR *      png_colorp;
 typedef png_color       FAR * FAR * png_colorpp;
 
 typedef struct png_color_16_struct
@@ -108,9 +108,9 @@
    png_uint_16 red; /* for use in red green blue files */
    png_uint_16 green;
    png_uint_16 blue;
-	png_uint_16 gray; /* for use in grayscale files */
+   png_uint_16 gray; /* for use in grayscale files */
 } png_color_16;
-typedef png_color_16    FAR *   	png_color_16p;
+typedef png_color_16    FAR *      png_color_16p;
 typedef png_color_16    FAR * FAR * png_color_16pp;
 
 typedef struct png_color_8_struct
@@ -121,7 +121,7 @@
    png_byte gray; /* for use in grayscale files */
    png_byte alpha; /* for alpha channel files */
 } png_color_8;
-typedef png_color_8     FAR *   	png_color_8p;
+typedef png_color_8     FAR *      png_color_8p;
 typedef png_color_8     FAR * FAR * png_color_8pp;
 
 /* png_text holds the text in a png file, and whether they are compressed
@@ -129,11 +129,11 @@
 typedef struct png_text_struct
 {
    int compression; /* compression value, -1 if uncompressed */
-	png_charp key; /* keyword */
-	png_charp text; /* comment */
-	png_uint_32 text_length; /* length of text field */
+   png_charp key; /* keyword */
+   png_charp text; /* comment */
+   png_uint_32 text_length; /* length of text field */
 } png_text;
-typedef png_text        FAR *   	png_textp;
+typedef png_text        FAR *      png_textp;
 typedef png_text        FAR * FAR * png_textpp;
 
 /* png_time is a way to hold the time in an machine independent way.
@@ -148,9 +148,9 @@
    png_byte day; /* day of month, 1 - 31 */
    png_byte hour; /* hour of day, 0 - 23 */
    png_byte minute; /* minute of hour, 0 - 59 */
-	png_byte second; /* second of minute, 0 - 60 (for leap seconds) */
+   png_byte second; /* second of minute, 0 - 60 (for leap seconds) */
 } png_time;
-typedef png_time        FAR *   	png_timep;
+typedef png_time        FAR *      png_timep;
 typedef png_time        FAR * FAR * png_timepp;
 
 /* png_info is a structure that holds the information in a png file.
@@ -162,12 +162,12 @@
    about the meaning of each field. */
 typedef struct png_info_struct
 {
-	/* the following are necessary for every png file */
+   /* the following are necessary for every png file */
    png_uint_32 width; /* with of file */
    png_uint_32 height; /* height of file */
    png_byte bit_depth; /* 1, 2, 4, 8, or 16 */
-	png_byte color_type; /* use the PNG_COLOR_TYPE_ defines */
-	png_byte compression_type; /* must be 0 */
+   png_byte color_type; /* use the PNG_COLOR_TYPE_ defines */
+   png_byte compression_type; /* must be 0 */
    png_byte filter_type; /* must be 0 */
    png_byte interlace_type; /* 0 for non-interlaced, 1 for interlaced */
    png_uint_32 valid; /* the PNG_INFO_ defines, OR'd together */
@@ -180,15 +180,15 @@
    /* the rest are optional.  If you are reading, check the valid
       field to see if the information in these are valid.  If you
       are writing, set the valid field to those chunks you want
-		written, and initialize the appropriate fields below */
+      written, and initialize the appropriate fields below */
 #if defined(PNG_READ_gAMA_SUPPORTED) || defined(PNG_WRITE_gAMA_SUPPORTED)
    float gamma; /* gamma value of file, if gAMA chunk is valid */
 #endif
 #if defined(PNG_READ_sBIT_SUPPORTED) || defined(PNG_WRITE_sBIT_SUPPORTED)
-	png_color_8 sig_bit; /* significant bits */
+   png_color_8 sig_bit; /* significant bits */
 #endif
 #if defined(PNG_READ_cHRM_SUPPORTED) || defined(PNG_WRITE_cHRM_SUPPORTED)
-	float x_white; /* cHRM chunk values */
+   float x_white; /* cHRM chunk values */
    float y_white;
    float x_red;
    float y_red;
@@ -198,21 +198,21 @@
    float y_blue;
 #endif
    png_colorp palette; /* palette of file */
-	png_uint_16 num_palette; /* number of values in palette */
+   png_uint_16 num_palette; /* number of values in palette */
    png_uint_16 num_trans; /* number of trans values */
 #if defined(PNG_READ_tRNS_SUPPORTED) || defined(PNG_WRITE_tRNS_SUPPORTED)
    png_bytep trans; /* tRNS values for palette image */
-	png_color_16 trans_values; /* tRNS values for non-palette image */
+   png_color_16 trans_values; /* tRNS values for non-palette image */
 #endif
 #if defined(PNG_READ_bKGD_SUPPORTED) || defined(PNG_WRITE_bKGD_SUPPORTED)
 
-	png_color_16 background; /* background color of image */
+   png_color_16 background; /* background color of image */
 #endif
 #if defined(PNG_READ_hIST_SUPPORTED) || defined(PNG_WRITE_hIST_SUPPORTED)
-	png_uint_16p hist; /* histogram of palette usage */
+   png_uint_16p hist; /* histogram of palette usage */
 #endif
 #if defined(PNG_READ_pHYs_SUPPORTED) || defined(PNG_WRITE_pHYs_SUPPORTED)
-	png_uint_32 x_pixels_per_unit; /* x resolution */
+   png_uint_32 x_pixels_per_unit; /* x resolution */
    png_uint_32 y_pixels_per_unit; /* y resolution */
    png_byte phys_unit_type; /* resolution type */
 #endif
@@ -225,13 +225,13 @@
    png_time mod_time; /* modification time */
 #endif
 #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_WRITE_tEXt_SUPPORTED) || \
-	 defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
-	int num_text; /* number of comments */
+    defined(PNG_READ_zTXt_SUPPORTED) || defined(PNG_WRITE_zTXt_SUPPORTED)
+   int num_text; /* number of comments */
    int max_text; /* size of text array */
    png_textp text; /* array of comments */
 #endif
 } png_info;
-typedef png_info        FAR *   	png_infop;
+typedef png_info        FAR *      png_infop;
 typedef png_info        FAR * FAR * png_infopp;
 
 #define PNG_RESOLUTION_UNKNOWN 0
@@ -257,7 +257,7 @@
 #define PNG_COLOR_TYPE_GRAY_ALPHA (PNG_COLOR_MASK_ALPHA)
 
 /* These determine if a chunks information is present in a read operation, or
-	if the chunk should be written in a write operation.  */
+   if the chunk should be written in a write operation.  */
 #define PNG_INFO_gAMA 0x0001
 #define PNG_INFO_sBIT 0x0002
 #define PNG_INFO_cHRM 0x0004
@@ -269,25 +269,25 @@
 #define PNG_INFO_oFFs 0x0100
 #define PNG_INFO_tIME 0x0200
 
-/* these determine if a function in the info needs freed */
+/* these determine if a function in the info needs to be freed */
 #define PNG_FREE_PALETTE 0x0001
 #define PNG_FREE_HIST 0x0002
 #define PNG_FREE_TRANS 0x0004
 
 /* this is used for the transformation routines, as some of them
-	change these values for the row.  It also should enable using
+   change these values for the row.  It also should enable using
    the routines for other uses. */
 typedef struct png_row_info_struct
 {
-	png_uint_32 width; /* width of row */
-	png_uint_32 rowbytes; /* number of bytes in row */
-	png_byte color_type; /* color type of row */
-	png_byte bit_depth; /* bit depth of row */
-	png_byte channels; /* number of channels (1, 2, 3, or 4) */
-	png_byte pixel_depth; /* bits per pixel (depth * channels) */
+   png_uint_32 width; /* width of row */
+   png_uint_32 rowbytes; /* number of bytes in row */
+   png_byte color_type; /* color type of row */
+   png_byte bit_depth; /* bit depth of row */
+   png_byte channels; /* number of channels (1, 2, 3, or 4) */
+   png_byte pixel_depth; /* bits per pixel (depth * channels) */
 } png_row_info;
 
-typedef png_row_info    FAR *   	png_row_infop;
+typedef png_row_info    FAR *      png_row_infop;
 typedef png_row_info    FAR * FAR * png_row_infopp;
 
 /* These are the function types for the I/O functions, and the functions which
@@ -305,135 +305,143 @@
 typedef void (*png_progressive_info_ptr) PNGARG((png_structp, png_infop));
 typedef void (*png_progressive_end_ptr) PNGARG((png_structp, png_infop));
 typedef void (*png_progressive_row_ptr) PNGARG((png_structp, png_bytep,
-	png_uint_32, int));
+   png_uint_32, int));
 #endif
 
 /* The structure that holds the information to read and write png files.
-	The only people who need to care about what is inside of this are the
-	people who will be modifying the library for their own special needs.
-	*/
+   The only people who need to care about what is inside of this are the
+   people who will be modifying the library for their own special needs.
+   */
 
 struct png_struct_def
 {
-	jmp_buf jmpbuf; /* used in png_error */
-	png_byte mode; /* used to determine where we are in the png file */
+   jmp_buf jmpbuf; /* used in png_error */
+   png_byte mode; /* used to determine where we are in the png file */
    png_byte read_mode;
    png_byte color_type; /* color type of file */
    png_byte bit_depth; /* bit depth of file */
    png_byte interlaced; /* interlace type of file */
    png_byte compession; /* compression type of file */
    png_byte filter; /* filter type */
-	png_byte channels; /* number of channels in file */
+   png_byte channels; /* number of channels in file */
    png_byte pixel_depth; /* number of bits per pixel */
    png_byte usr_bit_depth; /* bit depth of users row */
    png_byte usr_channels; /* channels at start of write */
 #if defined(PNG_READ_GAMMA_SUPPORTED)
    png_byte gamma_shift; /* amount of shift for 16 bit gammas */
 #endif
-	png_byte pass; /* current pass (0 - 6) */
+   png_byte pass; /* current pass (0 - 6) */
    png_byte row_init; /* 1 if png_read_start_row() has been called */
 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
-	png_byte background_gamma_type;
+   png_byte background_gamma_type;
    png_byte background_expand;
 #endif
    png_byte zlib_finished;
    png_byte user_palette;
 #if defined(PNG_READ_FILLER_SUPPORTED) || defined(PNG_WRITE_FILLER_SUPPORTED)
    png_byte filler;
-	png_byte filler_loc;
+   png_byte filler_loc;
 #endif
-	png_byte zlib_custom_level; /* one if custom compression level */
-	png_byte zlib_custom_method; /* one if custom compression method */
-	png_byte zlib_custom_window_bits; /* one if custom compression window bits */
-	png_byte zlib_custom_mem_level; /* one if custom compression memory level */
-	png_byte zlib_custom_strategy; /* one if custom compression strategy */
-	png_byte do_filter; /* one if filtering, zero if not */
-	png_byte do_custom_filter; /* one if filtering, zero if not */
+   png_byte zlib_custom_level; /* one if custom compression level */
+   png_byte zlib_custom_method; /* one if custom compression method */
+   png_byte zlib_custom_window_bits; /* one if custom compression window bits */
+   png_byte zlib_custom_mem_level; /* one if custom compression memory level */
+   png_byte zlib_custom_strategy; /* one if custom compression strategy */
+   png_byte do_filter; /* one if filtering, zero if not */
+   png_byte do_custom_filter; /* one if filtering, zero if not */
 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
-	png_byte have_chunk_header;
+   png_byte have_chunk_header;
 #endif
    png_uint_16 num_palette; /* number of entries in palette */
    png_uint_16 num_trans; /* number of transparency values */
    int zlib_level; /* holds zlib compression level */
    int zlib_method; /* holds zlib compression method */
-	int zlib_window_bits; /* holds zlib compression window bits */
+   int zlib_window_bits; /* holds zlib compression window bits */
    int zlib_mem_level; /* holds zlib compression memory level */
-	int zlib_strategy; /* holds zlib compression strategy */
+   int zlib_strategy; /* holds zlib compression strategy */
 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
-	int process_mode;
-	int cur_palette;
+   int process_mode;
+   int cur_palette;
 #endif
-	png_uint_32 transformations; /* which transformations to perform */
-	png_uint_32 crc; /* current crc value */
-	png_uint_32 width; /* width of file */
-	png_uint_32 height; /* height of file */
-	png_uint_32 num_rows; /* number of rows in current pass */
-	png_uint_32 rowbytes; /* size of row in bytes */
-	png_uint_32 usr_width; /* width of row at start of write */
-	png_uint_32 iwidth; /* interlaced width */
+   png_uint_32 transformations; /* which transformations to perform */
+   png_uint_32 crc; /* current crc value */
+   png_uint_32 width; /* width of file */
+   png_uint_32 height; /* height of file */
+   png_uint_32 num_rows; /* number of rows in current pass */
+   png_uint_32 rowbytes; /* size of row in bytes */
+   png_uint_32 usr_width; /* width of row at start of write */
+   png_uint_32 iwidth; /* interlaced width */
    png_uint_32 irowbytes; /* interlaced rowbytes */
    png_uint_32 row_number; /* current row in pass */
-	png_uint_32 idat_size; /* current idat size for read */
-	png_uint_32 zbuf_size; /* size of zbuf */
-	png_uint_32 do_free; /* flags indicating if libpng should free memory */
+   png_uint_32 idat_size; /* current idat size for read */
+   png_uint_32 zbuf_size; /* size of zbuf */
+   png_uint_32 do_free; /* flags indicating if libpng should free memory */
 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
-	png_uint_32 flush_dist;  /* how many rows apart to flush, 0 for no flush */
-	png_uint_32 flush_rows;  /* number of rows written since last flush */
+   png_uint_32 flush_dist;  /* how many rows apart to flush, 0 for no flush */
+   png_uint_32 flush_rows;  /* number of rows written since last flush */
 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
-	png_uint_32 push_length;
-	png_uint_32 skip_length;
-	png_uint_32 save_buffer_size;
-	png_uint_32 save_buffer_max;
-	png_uint_32 buffer_size;
-	png_uint_32 current_buffer_size;
+   png_uint_32 push_length;
+   png_uint_32 skip_length;
+   png_uint_32 save_buffer_size;
+   png_uint_32 save_buffer_max;
+   png_uint_32 buffer_size;
+   png_uint_32 current_buffer_size;
 #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED)
-	png_uint_32 current_text_size;
-	png_uint_32 current_text_left;
-	png_charp current_text;
-	png_charp current_text_ptr;
+   png_uint_32 current_text_size;
+   png_uint_32 current_text_left;
+   png_charp current_text;
+   png_charp current_text_ptr;
 #endif
-	png_byte push_chunk_name[4];
-	png_bytep save_buffer_ptr;
-	png_bytep save_buffer;
-	png_bytep current_buffer_ptr;
-	png_bytep current_buffer;
+#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
+/* for the Borland special 64K segment handler */
+   png_bytepp offset_table_ptr;
+   png_bytep offset_table;
+   png_uint_16 offset_table_number;
+   png_uint_16 offset_table_count;
+   png_uint_16 offset_table_count_free;
 #endif
-	png_colorp palette; /* files palette */
+   png_byte push_chunk_name[4];
+   png_bytep save_buffer_ptr;
+   png_bytep save_buffer;
+   png_bytep current_buffer_ptr;
+   png_bytep current_buffer;
+#endif
+   png_colorp palette; /* files palette */
 #if defined(PNG_READ_DITHER_SUPPORTED)
-	png_bytep palette_lookup; /* lookup table for dithering */
+   png_bytep palette_lookup; /* lookup table for dithering */
 #endif
 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
-	png_bytep gamma_table; /* gamma table for 8 bit depth files */
+   png_bytep gamma_table; /* gamma table for 8 bit depth files */
 #endif
 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
-	png_bytep gamma_from_1; /* converts from 1.0 to screen */
-	png_bytep gamma_to_1; /* converts from file to 1.0 */
+   png_bytep gamma_from_1; /* converts from 1.0 to screen */
+   png_bytep gamma_to_1; /* converts from file to 1.0 */
 #endif
 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
-	png_bytep trans; /* transparency values for paletted files */
+   png_bytep trans; /* transparency values for paletted files */
 #endif
 #if defined(PNG_READ_DITHER_SUPPORTED)
-	png_bytep dither_index; /* index translation for palette files */
+   png_bytep dither_index; /* index translation for palette files */
 #endif
 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined(PNG_READ_BACKGROUND_SUPPORTED)
-	png_uint_16pp gamma_16_table; /* gamma table for 16 bit depth files */
+   png_uint_16pp gamma_16_table; /* gamma table for 16 bit depth files */
 #endif
 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
-	png_uint_16pp gamma_16_from_1; /* converts from 1.0 to screen */
-	png_uint_16pp gamma_16_to_1; /* converts from file to 1.0 */
+   png_uint_16pp gamma_16_from_1; /* converts from 1.0 to screen */
+   png_uint_16pp gamma_16_to_1; /* converts from file to 1.0 */
 #endif
 #if defined(PNG_READ_DITHER_SUPPORTED)
-	png_uint_16p hist; /* histogram */
+   png_uint_16p hist; /* histogram */
 #endif
-	png_bytep zbuf; /* buffer for zlib */
-	png_bytep row_buf; /* row buffer */
-	png_bytep prev_row; /* previous row */
-	png_bytep save_row; /* place to save row before filtering */
+   png_bytep zbuf; /* buffer for zlib */
+   png_bytep row_buf; /* row buffer */
+   png_bytep prev_row; /* previous row */
+   png_bytep save_row; /* place to save row before filtering */
    z_stream * zstream; /* pointer to decompression structure (below) */
 #if defined(PNG_READ_GAMMA_SUPPORTED)
-	float gamma; /* file gamma value */
-	float display_gamma; /* display gamma value */
+   float gamma; /* file gamma value */
+   float display_gamma; /* display gamma value */
 #endif
 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
    float background_gamma;
@@ -442,40 +450,40 @@
    png_color_8 shift; /* shift for significant bit tranformation */
 #endif
 #if defined(PNG_READ_GAMMA_SUPPORTED) || defined (PNG_READ_sBIT_SUPPORTED)
-	png_color_8 sig_bit; /* significant bits in file */
+   png_color_8 sig_bit; /* significant bits in file */
 #endif
 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
    png_color_16 trans_values; /* transparency values for non-paletted files */
-	png_color_16 background; /* background color, gamma corrected for screen */
+   png_color_16 background; /* background color, gamma corrected for screen */
 #if defined(PNG_READ_GAMMA_SUPPORTED)
    png_color_16 background_1; /* background normalized to gamma 1.0 */
 #endif
 #endif
    png_row_info row_info; /* used for transformation routines */
-	FILE *fp; /* used for default png_read and png_write */
-	png_msg_ptr error_fn;         /* Function for printing errors and aborting */
-	png_msg_ptr warning_fn;       /* Function for printing warnings */
-	png_rw_ptr write_data_fn;     /* Function for writing output data */
-	png_rw_ptr read_data_fn;      /* Function for reading input data */
+   FILE *fp; /* used for default png_read and png_write */
+   png_msg_ptr error_fn;         /* Function for printing errors and aborting */
+   png_msg_ptr warning_fn;       /* Function for printing warnings */
+   png_rw_ptr write_data_fn;     /* Function for writing output data */
+   png_rw_ptr read_data_fn;      /* Function for reading input data */
 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
-	png_flush_ptr output_flush_fn;/* Function for flushing output */
+   png_flush_ptr output_flush_fn;/* Function for flushing output */
 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
-	png_progressive_info_ptr info_fn;
-	png_progressive_row_ptr row_fn;
-	png_progressive_end_ptr end_fn;
-	png_voidp push_ptr;
+   png_progressive_info_ptr info_fn;
+   png_progressive_row_ptr row_fn;
+   png_progressive_end_ptr end_fn;
+   png_voidp push_ptr;
 #endif
-	png_voidp io_ptr;  /* Pointer to user supplied struct for I/O functions */
-	png_voidp msg_ptr;  /* Pointer to user supplied struct for message functions */
+   png_voidp io_ptr;  /* Pointer to user supplied struct for I/O functions */
+   png_voidp msg_ptr;  /* Pointer to user supplied struct for message functions */
 };
 
 typedef png_struct      FAR * FAR * png_structpp;
 
 /* Here are the function definitions most commonly used.  This is not
-	the place to find out how to use libpng.  See libpng.txt for the
-	full explanation, see example.c for the summary.  This just provides
-	a simple one line of the use of each function. */
+   the place to find out how to use libpng.  See libpng.txt for the
+   full explanation, see example.c for the summary.  This just provides
+   a simple one line of the use of each function. */
 
 /* check the first 1 - 8 bytes to see if it is a png file */
 extern int png_check_sig PNGARG((png_bytep sig, int num));
@@ -571,7 +579,7 @@
 #define PNG_BACKGROUND_GAMMA_UNIQUE 2
 #define PNG_BACKGROUND_GAMMA_UNKNOWN 3
 extern void png_set_background PNGARG((png_structp png_ptr,
-	png_color_16p background_color, int background_gamma_code,
+   png_color_16p background_color, int background_gamma_code,
    int need_expand, double background_gamma));
 #endif
 
@@ -588,8 +596,8 @@
 #if defined(PNG_READ_DITHER_SUPPORTED)
 /* Turn on dithering, and reduce the palette to the number of colors available. */
 extern void png_set_dither PNGARG((png_structp png_ptr, png_colorp palette,
-	int num_palette, int maximum_colors, png_uint_16p histogram,
-	int full_dither));
+   int num_palette, int maximum_colors, png_uint_16p histogram,
+   int full_dither));
 #endif
 
 #if defined(PNG_READ_GAMMA_SUPPORTED)
@@ -611,25 +619,25 @@
 
 /* optional call to update the users info structure */
 extern void png_read_update_info PNGARG((png_structp png_ptr,
-	png_infop info_ptr));
+   png_infop info_ptr));
 
 /* read a one or more rows of image data.*/
 extern void png_read_rows PNGARG((png_structp png_ptr,
-	png_bytepp row,
-	png_bytepp display_row, png_uint_32 num_rows));
+   png_bytepp row,
+   png_bytepp display_row, png_uint_32 num_rows));
 
 /* read a row of data.*/
 extern void png_read_row PNGARG((png_structp png_ptr,
-	png_bytep row,
-	png_bytep display_row));
+   png_bytep row,
+   png_bytep display_row));
 
 /* read the whole image into memory at once. */
 extern void png_read_image PNGARG((png_structp png_ptr,
-	png_bytepp image));
+   png_bytepp image));
 
 /* write a row of image data */
 extern void png_write_row PNGARG((png_structp png_ptr,
-	png_bytep row));
+   png_bytep row));
 
 /* write a few rows of image data */
 extern void png_write_rows PNGARG((png_structp png_ptr,
@@ -647,7 +655,7 @@
 
 /* free all memory used by the read */
 extern void png_read_destroy PNGARG((png_structp png_ptr, png_infop info,
-	png_infop end_info));
+   png_infop end_info));
 
 /* free any memory used in png struct */
 extern void png_write_destroy PNGARG((png_structp png_ptr));
@@ -655,7 +663,7 @@
 /* These functions give the user control over the filtering and
    compression libraries used by zlib.  These functions are mainly
    useful for testing, as the defaults should work with most users.
-	Those users who are tight on memory, or are wanting faster
+   Those users who are tight on memory, or are wanting faster
    performance at the expense of compression can modify them.
    See the compression library header file for an explination
    of these functions */
@@ -677,10 +685,10 @@
    int method));
 
 /* These next functions are stubs of typical c functions for input/output,
-	memory, and error handling.  They are in the file pngio.c, and pngerror.c.
-	These functions can be replaced at run time for those applications that
-	need to handle I/O in a different manner.  See the file libpng.txt for
-	more information */
+   memory, and error handling.  They are in the file pngio.c, and pngerror.c.
+   These functions can be replaced at run time for those applications that
+   need to handle I/O in a different manner.  See the file libpng.txt for
+   more information */
 
 /* Write the data to whatever output you are using. */
 extern void png_write_data PNGARG((png_structp png_ptr, png_bytep data,
@@ -688,58 +696,62 @@
 
 /* Read data from whatever input you are using */
 extern void png_read_data PNGARG((png_structp png_ptr, png_bytep data,
-	png_uint_32 length));
+   png_uint_32 length));
 
 /* Initialize the input/output for the png file to the default functions. */
 extern void png_init_io PNGARG((png_structp png_ptr, FILE *fp));
 
 /* Replace the error message and abort, and warning functions with user
-	supplied functions.   If no messages are to be printed, NULL can be
-	supplied for error_fn and warning_fn, although error_fn will still do
-	a longjmp to the last setjmp location. */
+   supplied functions.  If no messages are to be printed then you must
+   supply replacement message functions. The replacement error_fn should
+   still do a longjmp to the last setjmp location if you are using this
+   method of error handling.  If error_fn or warning_fn is NULL, the
+   default functions will be used. */
 extern void png_set_message_fn PNGARG((png_structp png_ptr, png_voidp msg_ptr,
-	png_msg_ptr error_fn, png_msg_ptr warning_fn));
+   png_msg_ptr error_fn, png_msg_ptr warning_fn));
 
 /* Return the user pointer associated with the message functions */
 extern png_voidp png_get_msg_ptr PNGARG((png_structp png_ptr));
 
 /* Replace the default data output functions with a user supplied one(s).
-	If buffered output is not used, then output_flush_fn can be set to NULL.
-	If PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile time
-	output_flush_fn will be ignored (and thus can be NULL). */
+   If buffered output is not used, then output_flush_fn can be set to NULL.
+   If PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile time
+   output_flush_fn will be ignored (and thus can be NULL). */
 extern void png_set_write_fn PNGARG((png_structp png_ptr, png_voidp io_ptr,
-	png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn));
+   png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn));
 
 /* Replace the default data input function with a user supplied one. */
-extern void png_set_read_fn PNGARG((png_structp png_ptr, void *io_ptr,
-	png_rw_ptr read_data_fn));
+extern void png_set_read_fn PNGARG((png_structp png_ptr, png_voidp io_ptr,
+   png_rw_ptr read_data_fn));
 
 /* Return the user pointer associated with the I/O functions */
 extern png_voidp png_get_io_ptr PNGARG((png_structp png_ptr));
 
+#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 /* Replace the default push model read functions */
 extern void png_set_push_fn PNGARG((png_structp png_ptr, png_voidp push_ptr,
-	png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
-	png_progressive_end_ptr end_fn));
+   png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
+   png_progressive_end_ptr end_fn));
 
-/* returns the user pointer assiciated with the push read functions */
+/* returns the user pointer associated with the push read functions */
 extern png_voidp png_get_progressive_ptr PNGARG((png_structp png_ptr));
+#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
 
 extern png_voidp png_large_malloc PNGARG((png_structp png_ptr,
-	png_uint_32 size));
+   png_uint_32 size));
 
 /* free's a pointer allocated by png_large_malloc() */
 extern void png_large_free PNGARG((png_structp png_ptr, png_voidp ptr));
 
 /* Allocate memory. */
-extern png_voidp png_malloc PNGARG((png_structp png_ptr, png_uint_32 size));
+extern void * png_malloc PNGARG((png_structp png_ptr, png_uint_32 size));
 
 /* Reallocate memory. */
-extern png_voidp png_realloc PNGARG((png_structp png_ptr, png_voidp ptr,
+extern void * png_realloc PNGARG((png_structp png_ptr, void * ptr,
    png_uint_32 size, png_uint_32 old_size));
 
 /* free's a pointer allocated by png_malloc() */
-extern void png_free PNGARG((png_structp png_ptr, png_voidp ptr));
+extern void png_free PNGARG((png_structp png_ptr, void * ptr));
 
 /* Fatal error in libpng - can't continue */ 
 extern void png_error PNGARG((png_structp png_ptr, png_const_charp error));
@@ -880,19 +892,21 @@
    would need to use huge pointers to access all that data.  See the
    code in png.c for more information. */
 extern void png_calculate_crc PNGARG((png_structp png_ptr, png_bytep ptr,
-	png_uint_32 length));
+   png_uint_32 length));
 
 /* default error and warning functions if user doesn't supply them */
-extern void png_default_warning PNGARG((png_structp png_ptr, png_const_charp message));
-extern void png_default_error PNGARG((png_structp png_ptr, png_const_charp error));
+extern void png_default_warning PNGARG((png_structp png_ptr,
+   png_const_charp message));
+extern void png_default_error PNGARG((png_structp png_ptr,
+   png_const_charp error));
 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
-extern void png_flush PNGARG((png_struct *png_ptr));
-extern void png_default_flush PNGARG((png_struct *png_ptr));
+extern void png_flush PNGARG((png_structp png_ptr));
+extern void png_default_flush PNGARG((png_structp png_ptr));
 #endif
 
 /* place a 32 bit number into a buffer in png byte order.  We work
-	with unsigned numbers for convenience, you may have to cast
-	signed numbers (if you use any, most png data is unsigned). */
+   with unsigned numbers for convenience, you may have to cast
+   signed numbers (if you use any, most png data is unsigned). */
 extern void png_save_uint_32 PNGARG((png_bytep buf, png_uint_32 i));
 
 /* place a 16 bit number into a buffer in png byte order */
@@ -906,15 +920,15 @@
 
 /* Write a png chunk.  */
 extern void png_write_chunk PNGARG((png_structp png_ptr, png_bytep type,
-	png_bytep data, png_uint_32 length));
+   png_bytep data, png_uint_32 length));
 
 /* Write the start of a png chunk. */
 extern void png_write_chunk_start PNGARG((png_structp png_ptr, png_bytep type,
-	png_uint_32 total_length));
+   png_uint_32 total_length));
 
 /* write the data of a png chunk started with png_write_chunk_start(). */
 extern void png_write_chunk_data PNGARG((png_structp png_ptr, png_bytep data,
-	png_uint_32 length));
+   png_uint_32 length));
 
 /* finish a chunk started with png_write_chunk_start() */
 extern void png_write_chunk_end PNGARG((png_structp png_ptr));
@@ -925,17 +939,17 @@
 /* write various chunks */
 
 /* Write the IHDR chunk, and update the png_struct with the necessary
-	information. */
+   information. */
 extern void png_write_IHDR PNGARG((png_structp png_ptr, png_uint_32 width,
-	png_uint_32 height,
-	int bit_depth, int color_type, int compression_type, int filter_type,
-	int interlace_type));
+   png_uint_32 height,
+   int bit_depth, int color_type, int compression_type, int filter_type,
+   int interlace_type));
 
 extern void png_write_PLTE PNGARG((png_structp png_ptr, png_colorp palette,
-	int number));
+   int number));
 
 extern void png_write_IDAT PNGARG((png_structp png_ptr, png_bytep data,
-	png_uint_32 length));
+   png_uint_32 length));
 
 extern void png_write_IEND PNGARG((png_structp png_ptr));
 
@@ -945,19 +959,19 @@
 
 #if defined(PNG_WRITE_sBIT_SUPPORTED)
 extern void png_write_sBIT PNGARG((png_structp png_ptr, png_color_8p sbit,
-	int color_type));
+   int color_type));
 #endif
 
 #if defined(PNG_WRITE_cHRM_SUPPORTED)
 extern void png_write_cHRM PNGARG((png_structp png_ptr,
-	double white_x, double white_y,
-	double red_x, double red_y, double green_x, double green_y,
-	double blue_x, double blue_y));
+   double white_x, double white_y,
+   double red_x, double red_y, double green_x, double green_y,
+   double blue_x, double blue_y));
 #endif
 
 #if defined(PNG_WRITE_tRNS_SUPPORTED)
 extern void png_write_tRNS PNGARG((png_structp png_ptr, png_bytep trans,
-	png_color_16p values, int number, int color_type));
+   png_color_16p values, int number, int color_type));
 #endif
 
 #if defined(PNG_WRITE_bKGD_SUPPORTED)
@@ -967,29 +981,29 @@
 
 #if defined(PNG_WRITE_hIST_SUPPORTED)
 extern void png_write_hIST PNGARG((png_structp png_ptr, png_uint_16p hist,
-	int number));
+   int number));
 #endif
 
 #if defined(PNG_WRITE_tEXt_SUPPORTED)
 extern void png_write_tEXt PNGARG((png_structp png_ptr, png_charp key,
-	png_charp text, png_uint_32 text_len));
+   png_charp text, png_uint_32 text_len));
 #endif
 
 #if defined(PNG_WRITE_zTXt_SUPPORTED)
 extern void png_write_zTXt PNGARG((png_structp png_ptr, png_charp key,
-	png_charp text, png_uint_32 text_len, int compression));
+   png_charp text, png_uint_32 text_len, int compression));
 #endif
 
 #if defined(PNG_WRITE_pHYs_SUPPORTED)
 extern void png_write_pHYs PNGARG((png_structp png_ptr,
-	png_uint_32 x_pixels_per_unit,
-	png_uint_32 y_pixels_per_unit,
-	int unit_type));
+   png_uint_32 x_pixels_per_unit,
+   png_uint_32 y_pixels_per_unit,
+   int unit_type));
 #endif
 
 #if defined(PNG_WRITE_oFFs_SUPPORTED)
 extern void png_write_oFFs PNGARG((png_structp png_ptr,
-	png_uint_32 x_offset,
+   png_uint_32 x_offset,
    png_uint_32 y_offset,
    int unit_type));
 #endif
@@ -1007,66 +1021,66 @@
 /* callbacks for png chunks */
 extern void png_read_IHDR PNGARG((png_structp png_ptr, png_infop info,
    png_uint_32 width, png_uint_32 height, int bit_depth,
-	int color_type, int compression_type, int filter_type,
+   int color_type, int compression_type, int filter_type,
    int interlace_type));
 
 extern void png_read_PLTE PNGARG((png_structp png_ptr, png_infop info,
-	png_colorp palette, int num));
+   png_colorp palette, int num));
 
 #if defined(PNG_READ_gAMA_SUPPORTED)
 extern void png_read_gAMA PNGARG((png_structp png_ptr, png_infop info,
-	double gamma));
+   double gamma));
 #endif
 
 #if defined(PNG_READ_sBIT_SUPPORTED)
 extern void png_read_sBIT PNGARG((png_structp png_ptr, png_infop info,
-	png_color_8p sig_bit));
+   png_color_8p sig_bit));
 #endif
 
 #if defined(PNG_READ_cHRM_SUPPORTED)
 extern void png_read_cHRM PNGARG((png_structp png_ptr, png_infop info,
-	double white_x, double white_y, double red_x, double red_y,
-	double green_x, double green_y, double blue_x, double blue_y));
+   double white_x, double white_y, double red_x, double red_y,
+   double green_x, double green_y, double blue_x, double blue_y));
 #endif
 
 #if defined(PNG_READ_tRNS_SUPPORTED)
 extern void png_read_tRNS PNGARG((png_structp png_ptr, png_infop info,
-	png_bytep trans, int num_trans,   png_color_16p trans_values));
+   png_bytep trans, int num_trans,   png_color_16p trans_values));
 #endif
 
 #if defined(PNG_READ_bKGD_SUPPORTED)
 extern void png_read_bKGD PNGARG((png_structp png_ptr, png_infop info,
-	png_color_16p background));
+   png_color_16p background));
 #endif
 
 #if defined(PNG_READ_hIST_SUPPORTED)
 extern void png_read_hIST PNGARG((png_structp png_ptr, png_infop info,
-	png_uint_16p hist));
+   png_uint_16p hist));
 #endif
 
 #if defined(PNG_READ_pHYs_SUPPORTED)
 extern void png_read_pHYs PNGARG((png_structp png_ptr, png_infop info,
-	png_uint_32 res_x, png_uint_32 res_y, int unit_type));
+   png_uint_32 res_x, png_uint_32 res_y, int unit_type));
 #endif
 
 #if defined(PNG_READ_oFFs_SUPPORTED)
 extern void png_read_oFFs PNGARG((png_structp png_ptr, png_infop info,
-	png_uint_32 offset_x, png_uint_32 offset_y, int unit_type));
+   png_uint_32 offset_x, png_uint_32 offset_y, int unit_type));
 #endif
 
 #if defined(PNG_READ_tIME_SUPPORTED)
 extern void png_read_tIME PNGARG((png_structp png_ptr, png_infop info,
-	png_timep mod_time));
+   png_timep mod_time));
 #endif
 
 #if defined(PNG_READ_tEXt_SUPPORTED)
 extern void png_read_tEXt PNGARG((png_structp png_ptr, png_infop info,
-	png_charp key, png_charp text, png_uint_32 text_len));
+   png_charp key, png_charp text, png_uint_32 text_len));
 #endif
 
 #if defined(PNG_READ_zTXt_SUPPORTED)
 extern void png_read_zTXt PNGARG((png_structp png_ptr, png_infop info,
-	png_charp key, png_charp text, png_uint_32 text_len, int compression));
+   png_charp key, png_charp text, png_uint_32 text_len, int compression));
 #endif
 
 #if defined(PNG_READ_GAMMA_SUPPORTED)
@@ -1081,38 +1095,38 @@
 #if defined(PNG_READ_INTERLACING_SUPPORTED)
 /* expand an interlaced row */
 extern void png_do_read_interlace PNGARG((png_row_infop row_info,
-	png_bytep row, int pass));
+   png_bytep row, int pass));
 #endif
 
 #if defined(PNG_WRITE_INTERLACING_SUPPORTED)
 /* grab pixels out of a row for an interlaced pass */
 extern void png_do_write_interlace PNGARG((png_row_infop row_info,
-	png_bytep row, int pass));
+   png_bytep row, int pass));
 #endif
 
 /* unfilter a row */
 extern void png_read_filter_row PNGARG((png_row_infop row_info,
-	png_bytep row, png_bytep prev_row, int filter));
+   png_bytep row, png_bytep prev_row, int filter));
 /* filter a row, and place the correct filter byte in the row */
 extern void png_write_filter_row PNGARG((png_row_infop row_info,
-	png_bytep row, png_bytep prev_row));
+   png_bytep row, png_bytep prev_row));
 /* finish a row while reading, dealing with interlacing passes, etc. */
 extern void png_read_finish_row PNGARG((png_structp png_ptr));
 /* initialize the row buffers, etc. */
 extern void png_read_start_row PNGARG((png_structp png_ptr));
 /* optional call to update the users info structure */
 extern void png_read_transform_info PNGARG((png_structp png_ptr,
-	png_infop info_ptr));
+   png_infop info_ptr));
 
 /* these are the functions that do the transformations */
 #if defined(PNG_READ_FILLER_SUPPORTED)
 extern void png_do_read_filler PNGARG((png_row_infop row_info,
-	png_bytep row, png_byte filler, png_byte filler_loc));
+   png_bytep row, png_byte filler, png_byte filler_loc));
 #endif
 
 #if defined(PNG_WRITE_FILLER_SUPPORTED)
 extern void png_do_write_filler PNGARG((png_row_infop row_info,
-	png_bytep row, png_byte filler_loc));
+   png_bytep row, png_byte filler_loc));
 #endif
 
 #if defined(PNG_READ_SWAP_SUPPORTED) || defined(PNG_WRITE_SWAP_SUPPORTED)
@@ -1133,11 +1147,11 @@
 #endif
 
 extern void png_build_grayscale_palette PNGARG((int bit_depth,
-	png_colorp palette));
+   png_colorp palette));
 
 #if defined(PNG_READ_GRAY_TO_RGB_SUPPORTED)
 extern void png_do_gray_to_rgb PNGARG((png_row_infop row_info,
-	png_bytep row));
+   png_bytep row));
 #endif
 
 #if defined(PNG_READ_16_TO_8_SUPPORTED)
@@ -1146,7 +1160,7 @@
 
 #if defined(PNG_READ_DITHER_SUPPORTED)
 extern void png_do_dither PNGARG((png_row_infop row_info,
-	png_bytep row, png_bytep palette_lookup, png_bytep dither_lookup));
+   png_bytep row, png_bytep palette_lookup, png_bytep dither_lookup));
 #endif
 
 #if defined(PNG_READ_BGR_SUPPORTED) || defined(PNG_WRITE_BGR_SUPPORTED)
@@ -1155,7 +1169,7 @@
 
 #if defined(PNG_WRITE_PACK_SUPPORTED)
 extern void png_do_pack PNGARG((png_row_infop row_info,
-	png_bytep row, png_byte bit_depth));
+   png_bytep row, png_byte bit_depth));
 #endif
 
 #if defined(PNG_WRITE_SHIFT_SUPPORTED)
@@ -1166,23 +1180,23 @@
 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
 extern void png_do_background PNGARG((png_row_infop row_info, png_bytep row,
    png_color_16p trans_values, png_color_16p background,
-	png_color_16p background_1,
-	png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
-	png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
-	png_uint_16pp gamma_16_to_1, int gamma_shift));
+   png_color_16p background_1,
+   png_bytep gamma_table, png_bytep gamma_from_1, png_bytep gamma_to_1,
+   png_uint_16pp gamma_16, png_uint_16pp gamma_16_from_1,
+   png_uint_16pp gamma_16_to_1, int gamma_shift));
 #endif
 
 #if defined(PNG_READ_GAMMA_SUPPORTED)
 extern void png_do_gamma PNGARG((png_row_infop row_info, png_bytep row,
-	png_bytep gamma_table, png_uint_16pp gamma_16_table,
+   png_bytep gamma_table, png_uint_16pp gamma_16_table,
    int gamma_shift));
 #endif
 
 #if defined(PNG_READ_EXPAND_SUPPORTED)
 extern void png_do_expand_palette PNGARG((png_row_infop row_info,
-	png_bytep row, png_colorp palette, png_bytep trans, int num_trans));
+   png_bytep row, png_colorp palette, png_bytep trans, int num_trans));
 extern void png_do_expand PNGARG((png_row_infop row_info,
-	png_bytep row, png_color_16p trans_value));
+   png_bytep row, png_color_16p trans_value));
 #endif
 
 /* unpack 16 and 32 bit values from a string */
@@ -1200,7 +1214,7 @@
 
 /* decode the IHDR chunk */
 extern void png_handle_IHDR PNGARG((png_structp png_ptr, png_infop info,
-	png_uint_32 length));
+   png_uint_32 length));
 extern void png_handle_PLTE PNGARG((png_structp png_ptr, png_infop info,
    png_uint_32 length));
 #if defined(PNG_READ_gAMA_SUPPORTED)
@@ -1271,40 +1285,36 @@
 extern void png_push_crc_skip PNGARG((png_structp png_ptr, png_uint_32 length));
 extern void png_push_skip PNGARG((png_structp png_ptr));
 extern void png_push_fill_buffer PNGARG((png_structp png_ptr, png_bytep buffer,
-	png_uint_32 length));
+   png_uint_32 length));
 extern void png_push_save_buffer PNGARG((png_structp png_ptr));
 extern void png_push_restore_buffer PNGARG((png_structp png_ptr, png_bytep buffer,
-	png_uint_32 buffer_length));
+   png_uint_32 buffer_length));
 extern void png_push_read_idat PNGARG((png_structp png_ptr));
 extern void png_process_IDAT_data PNGARG((png_structp png_ptr,
-	png_bytep buffer, png_uint_32 buffer_length));
+   png_bytep buffer, png_uint_32 buffer_length));
 extern void png_push_process_row PNGARG((png_structp png_ptr));
 extern void png_push_handle_PLTE PNGARG((png_structp png_ptr,
-	png_infop info, png_uint_32 length));
+   png_uint_32 length));
 extern void png_push_read_plte PNGARG((png_structp png_ptr, png_infop info));
 extern void png_push_handle_tRNS PNGARG((png_structp png_ptr, png_infop info,
-	png_uint_32 length));
+   png_uint_32 length));
 extern void png_push_handle_hIST PNGARG((png_structp png_ptr, png_infop info,
-	png_uint_32 length));
-extern void png_push_handle_tEXt PNGARG((png_structp png_ptr, png_infop info,
-	png_uint_32 length));
-extern void png_push_handle_zTXt PNGARG((png_structp png_ptr, png_infop info,
-	png_uint_32 length));
+   png_uint_32 length));
 extern void png_push_have_info PNGARG((png_structp png_ptr, png_infop info));
 extern void png_push_have_end PNGARG((png_structp png_ptr, png_infop info));
 extern void png_push_have_row PNGARG((png_structp png_ptr, png_bytep row));
 extern void png_push_read_end PNGARG((png_structp png_ptr, png_infop info));
 extern void png_process_some_data PNGARG((png_structp png_ptr,
-	png_infop info));
+   png_infop info));
 extern void png_read_push_finish_row PNGARG((png_structp png_ptr));
 #if defined(PNG_READ_tEXt_SUPPORTED)
-extern void png_push_handle_tEXt PNGARG((png_structp png_ptr, png_infop info,
-	png_uint_32 length));
+extern void png_push_handle_tEXt PNGARG((png_structp png_ptr,
+   png_uint_32 length));
 extern void png_push_read_text PNGARG((png_structp png_ptr, png_infop info));
 #endif
 #if defined(PNG_READ_zTXt_SUPPORTED)
-extern void png_push_handle_zTXt PNGARG((png_structp png_ptr, png_infop info,
-	png_uint_32 length));
+extern void png_push_handle_zTXt PNGARG((png_structp png_ptr,
+   png_uint_32 length));
 extern void png_push_read_ztxt PNGARG((png_structp png_ptr, png_infop info));
 #endif
 
@@ -1314,13 +1324,13 @@
 
 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
 extern void png_process_data PNGARG((png_structp png_ptr, png_infop info,
-	png_bytep buffer, png_uint_32 buffer_size));
+   png_bytep buffer, png_uint_32 buffer_size));
 extern void png_set_progressive_read_fn PNGARG((png_structp png_ptr,
-	png_voidp progressive_ptr,
-	png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
-	png_progressive_end_ptr end_fn));
+   png_voidp progressive_ptr,
+   png_progressive_info_ptr info_fn, png_progressive_row_ptr row_fn,
+   png_progressive_end_ptr end_fn));
 extern void png_progressive_combine_row PNGARG((png_structp png_ptr,
-	png_bytep old_row, png_bytep new_row));
+   png_bytep old_row, png_bytep new_row));
 #endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
 
 /* do not put anything past this line */
diff --git a/pngchang.txt b/pngchang.txt
index 404b528..c0ef5a5 100644
--- a/pngchang.txt
+++ b/pngchang.txt
@@ -65,5 +65,8 @@
 version 0.86
    fixed bugs
    improved documentation
-
+version 0.87
+	fixed medium model bugs
+	fixed other bugs introduced in 0.85 and 0.86
+   added some minor documentation
 
diff --git a/pngconf.h b/pngconf.h
index 157e5d2..99c21cd 100644
--- a/pngconf.h
+++ b/pngconf.h
@@ -1,10 +1,10 @@
 
 /* pngconf.c - machine configurable file for libpng
 
-	libpng 1.0 beta 2 - version 0.86
+	libpng 1.0 beta 2 - version 0.87
    For conditions of distribution and use, see copyright notice in png.h
 	Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
-   January 10, 1996
+   January 15, 1996
    */
 
 /* Any machine specific code is near the front of this file, so if you
@@ -27,7 +27,7 @@
    Unless this gets smaller then the size of a row (compressed),
    it should not make much difference how big this is.  */
 
-#define PNG_ZBUF_SIZE 8192
+#define PNG_ZBUF_SIZE 32768
 
 /* While libpng currently uses zlib for it's compression, it has been designed
    to stand on it's own.  Towards this end, there are two defines that are
diff --git a/pngerror.c b/pngerror.c
index 5301695..522cbb7 100644
--- a/pngerror.c
+++ b/pngerror.c
@@ -1,10 +1,10 @@
 
 /* pngerror.c - stub functions for i/o and memory allocation
 
-	libpng 1.0 beta 2 - version 0.86
+	libpng 1.0 beta 2 - version 0.87
    For conditions of distribution and use, see copyright notice in png.h
 	Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
-   January 10, 1996
+   January 15, 1996
 
 	This file provides a location for all error handling.  Users which
    need special error handling are expected to write replacement functions
@@ -42,6 +42,10 @@
 		png_default_warning(png_ptr, message);
 }
 
+/* This is the default error handling function.  Note that replacements for
+	this function MUST NOT RETURN, or the program will likely crash.  This
+	function is used by default, or if the program supplies NULL for the
+	error function pointer in png_set_message_fn(). */
 void
 png_default_error(png_structp png_ptr, png_const_charp message)
 {
diff --git a/pngio.c b/pngio.c
index b6181f6..b399a92 100644
--- a/pngio.c
+++ b/pngio.c
@@ -1,10 +1,10 @@
 
 /* pngio.c - stub functions for i/o and memory allocation
 
-	libpng 1.0 beta 2 - version 0.86
+	libpng 1.0 beta 2 - version 0.87
    For conditions of distribution and use, see copyright notice in png.h
 	Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
-   January 10, 1996
+   January 15, 1996
 
    This file provides a location for all input/output.  Users which need
 	special handling are expected to write functions which have the same
@@ -164,7 +164,7 @@
 #else
    /* this works in MSC also but with lost segment warning */
    n_data = (png_byte *)data;
-   if ((PNG_BYTEP )n_data == data)
+   if ((png_bytep)n_data == data)
 #endif
    {
       check = fread(n_data, 1, (size_t)length, png_ptr->fp);
@@ -196,16 +196,19 @@
 }
 #endif
 
+/* This function is called to output any data pending writing (normally
+	to disk.  After png_flush is called, there should be no data pending
+	writing in any buffers. */
 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
 void
-png_flush(png_struct *png_ptr)
+png_flush(png_structp png_ptr)
 {
 	if (png_ptr->output_flush_fn)
 		(*(png_ptr->output_flush_fn))(png_ptr);
 }
 
 void
-png_default_flush(png_struct *png_ptr)
+png_default_flush(png_structp png_ptr)
 {
 	if (png_ptr->fp)
 		fflush(png_ptr->fp);
@@ -223,19 +226,20 @@
 						 arguments a pointer to a png_struct, a pointer to
 						 data to be written, and a 32-bit unsigned int which is
 						 the number of bytes to be written.  The new write
-						 function should call png_error("Error msg")
+						 function should call png_error(png_ptr, "Error msg")
 						 to exit and output any fatal error messages.
 	flush_data_fn - pointer to a new flush function which takes as its
 						 arguments a pointer to a png_struct.  After a call to
 						 the flush function, there should be no data in any buffers
 						 or pending transmission.  If the output method doesn't do
-						 any buffering of ouput, this parameter can be NULL.  If
-						 PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng
-						 compile time, output_flush_fn will be ignored, although
-						 it must be supplied for compatibility. */
+						 any buffering of ouput, a function prototype must still be
+						 supplied although it doesn't have to do anything.  If
+						 PNG_WRITE_FLUSH_SUPPORTED is not defined at libpng compile
+						 time, output_flush_fn will be ignored, although it must be
+						 supplied for compatibility. */
 void
-png_set_write_fn(png_structp png_ptr, png_voidp io_ptr, png_rw_ptr write_data_fn,
-	png_flush_ptr output_flush_fn)
+png_set_write_fn(png_structp png_ptr, png_voidp io_ptr,
+	png_rw_ptr write_data_fn, png_flush_ptr output_flush_fn)
 {
 	png_ptr->io_ptr = io_ptr;
 
@@ -245,10 +249,10 @@
 		png_ptr->write_data_fn = png_default_write_data;
 
 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
-	if (output_flush_fn == NULL)
-		png_ptr->output_flush_fn = png_default_flush;
-	else
+	if (output_flush_fn)
 		png_ptr->output_flush_fn = output_flush_fn;
+	else
+		png_ptr->output_flush_fn = png_default_flush;
 #endif /* PNG_WRITE_FLUSH_SUPPORTED */
 
 	/* It is an error to read while writing a png file */
@@ -270,7 +274,8 @@
 						To exit and output any fatal error messages the new write
                   function should call png_error(png_ptr, "Error msg"). */
 void
-png_set_read_fn(png_struct *png_ptr, void *io_ptr, png_rw_ptr read_data_fn)
+png_set_read_fn(png_structp png_ptr, png_voidp io_ptr,
+	png_rw_ptr read_data_fn)
 {
 	png_ptr->io_ptr = io_ptr;
 
@@ -291,8 +296,8 @@
 /* This function returns a pointer to the io_ptr associated with the user
 	functions.  The application should free any memory associated with this
 	pointer before png_write_destroy and png_read_destroy are called. */
-void *
-png_get_io_ptr(png_struct *png_ptr)
+png_voidp
+png_get_io_ptr(png_structp png_ptr)
 {
 	return png_ptr->io_ptr;
 }
diff --git a/pngmem.c b/pngmem.c
index c7bc5d4..83b8528 100644
--- a/pngmem.c
+++ b/pngmem.c
@@ -1,41 +1,199 @@
 
 /* pngmem.c - stub functions for memory allocation
 
-	libpng 1.0 beta 2 - version 0.86
+	libpng 1.0 beta 2 - version 0.87
    For conditions of distribution and use, see copyright notice in png.h
 	Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
-   January 10, 1996
+   January 15, 1996
 
    This file provides a location for all memory allocation.  Users which
 	need special memory handling are expected to modify the code in this file
-   to meet their needs.  See the instructions at each function. */
+	to meet their needs.  See the instructions at each function. */
 
 #define PNG_INTERNAL
 #include "png.h"
 
+/* Borland DOS special memory handler */
+#if defined(__TURBOC__) && !defined(_Windows) && !defined(__FLAT__)
+/* if you change this, be sure to change the one in png.h also */
+
 /* Allocate memory.  For reasonable files, size should never exceed
-   64K.  However, zlib may allocate more then 64K if you don't tell
-   it not to.  See zconf.h and png.h for more information. zlib does
-   need to allocate exactly 64K, so whatever you call here must
-   have the ability to do that. */
+	64K.  However, zlib may allocate more then 64K if you don't tell
+	it not to.  See zconf.h and png.h for more information. zlib does
+	need to allocate exactly 64K, so whatever you call here must
+	have the ability to do that. */
+
+/* Borland seems to have a problem in DOS mode for exactly 64K.
+	It gives you a segment with an offset of 8 (perhaps to store it's
+	memory stuff).  zlib doesn't like this at all, so we have to
+	detect and deal with it.  This code should not be needed in
+   Windows or OS/2 modes, and only in 16 bit mode.
+*/
+
+png_voidp
+png_large_malloc(png_structp png_ptr, png_uint_32 size)
+{
+	png_voidp ret;
+	if (!png_ptr || !size)
+		return ((voidp)0);
+
+#ifdef PNG_MAX_MALLOC_64K
+	if (size > (png_uint_32)65536L)
+		png_error(png_ptr, "Cannot Allocate > 64K");
+#endif
+
+	if (size == (png_uint_32)(65536L))
+	{
+		if (!png_ptr->offset_table)
+		{
+			/* try to see if we need to do any of this fancy stuff */
+			ret = farmalloc(size);
+			if (!ret || ((long)ret & 0xffff))
+			{
+				int num_blocks;
+				png_uint_32 total_size;
+				png_bytep table;
+				int i;
+            png_byte huge * hptr;
+
+				if (ret)
+					farfree(ret);
+				ret = 0;
+
+				num_blocks = (int)(1 << (png_ptr->zlib_window_bits - 14));
+				if (num_blocks < 1)
+					num_blocks = 1;
+				if (png_ptr->zlib_mem_level >= 7)
+					num_blocks += (int)(1 << (png_ptr->zlib_mem_level - 7));
+				else
+            	num_blocks++;
+
+				total_size = ((png_uint_32)65536L) * (png_uint_32)num_blocks;
+
+				table = farmalloc(total_size);
+
+				if (!table)
+				{
+					png_error(png_ptr, "Out of Memory");
+				}
+
+				if ((long)table & 0xffff)
+				{
+					farfree(table);
+					total_size += (png_uint_32)65536L;
+				}
+
+				table = farmalloc(total_size);
+
+				if (!table)
+				{
+					png_error(png_ptr, "Out of Memory");
+				}
+
+				png_ptr->offset_table = table;
+				png_ptr->offset_table_ptr = farmalloc(
+					num_blocks * sizeof (png_bytep));
+				hptr = (png_byte huge *)table;
+				if ((long)hptr & 0xffff)
+				{
+					hptr = (png_byte huge *)((long)(hptr) & 0xffff0000L);
+					hptr += 65536L;
+				}
+				for (i = 0; i < num_blocks; i++)
+				{
+					png_ptr->offset_table_ptr[i] = (png_bytep)hptr;
+					hptr += 65536L;
+				}
+
+				png_ptr->offset_table_number = num_blocks;
+				png_ptr->offset_table_count = 0;
+				png_ptr->offset_table_count_free = 0;
+			}
+
+			if (png_ptr->offset_table_count >= png_ptr->offset_table_number)
+				png_error(png_ptr, "Out of Memory");
+
+         ret = png_ptr->offset_table_ptr[png_ptr->offset_table_count++];
+		}
+	}
+	else
+		ret = farmalloc(size);
+
+	if (ret == NULL)
+	{
+		png_error(png_ptr, "Out of Memory");
+	}
+
+	return ret;
+}
+
+/* free a pointer allocated by png_large_malloc().  In the default
+  configuration, png_ptr is not used, but is passed in case it
+  is needed.  If ptr is NULL, return without taking any action. */
+void
+png_large_free(png_structp png_ptr, png_voidp ptr)
+{
+	if (!png_ptr)
+		return;
+
+	if (ptr != NULL)
+	{
+		if (png_ptr->offset_table)
+		{
+			int i;
+
+			for (i = 0; i < png_ptr->offset_table_count; i++)
+			{
+				if (ptr == png_ptr->offset_table_ptr[i])
+            {
+					ptr = 0;
+               png_ptr->offset_table_count_free++;
+					break;
+				}
+			}
+			if (png_ptr->offset_table_count_free == png_ptr->offset_table_count)
+			{
+				farfree(png_ptr->offset_table);
+				farfree(png_ptr->offset_table_ptr);
+				png_ptr->offset_table = 0;
+				png_ptr->offset_table_ptr = 0;
+         }
+		}
+
+		if (ptr)
+			farfree(ptr);
+	}
+}
+
+#else /* Not the Borland DOS special memory handler */
+
+/* Allocate memory.  For reasonable files, size should never exceed
+	64K.  However, zlib may allocate more then 64K if you don't tell
+	it not to.  See zconf.h and png.h for more information. zlib does
+	need to allocate exactly 64K, so whatever you call here must
+	have the ability to do that. */
 
 
 png_voidp
 png_large_malloc(png_structp png_ptr, png_uint_32 size)
 {
-   png_voidp ret;
-   if (!png_ptr || !size)
+	png_voidp ret;
+	if (!png_ptr || !size)
 		return ((voidp)0);
 
 #ifdef PNG_MAX_MALLOC_64K
-   if (size > (png_uint_32)65536L)
-      png_error(png_ptr, "Cannot Allocate > 64K");
+	if (size > (png_uint_32)65536L)
+		png_error(png_ptr, "Cannot Allocate > 64K");
 #endif
 
 #if defined(__TURBOC__) && !defined(__FLAT__)
 	ret = farmalloc(size);
 #else
+# if defined(_MSC_VER) && defined(MAXSEG_64K)
+	ret = halloc(size, 1);
+# else
 	ret = malloc(size);
+# endif
 #endif
 
    if (ret == NULL)
@@ -43,7 +201,7 @@
       png_error(png_ptr, "Out of Memory");
    }
 
-   return ret;
+	return ret;
 }
 
 /* free a pointer allocated by png_large_malloc().  In the default
@@ -60,15 +218,22 @@
 #if defined(__TURBOC__) && !defined(__FLAT__)
 		farfree(ptr);
 #else
+# if defined(_MSC_VER) && defined(MAXSEG_64K)
+		hfree(ptr);
+# else
 		free(ptr);
+# endif
 #endif
-   }
+	}
 }
 
+#endif /* Not Borland DOS special memory handler */
 
 /* Allocate memory.  This is called for smallish blocks only  It
 	should not get anywhere near 64K.  On segmented machines, this
-	must come from the local heap (for zlib). */
+	must come from the local heap (for zlib).  Currently, zlib is
+	the only one that uses this, so you should only get one call
+	to this, and that a small block. */
 void *
 png_malloc(png_structp png_ptr, png_uint_32 size)
 {
@@ -96,7 +261,8 @@
 }
 
 /* Reallocate memory.  This will not get near 64K on a
-	even marginally reasonable file. */
+	even marginally reasonable file.  This is not used in
+	the current version of the library. */
 void *
 png_realloc(png_structp png_ptr, void * ptr, png_uint_32 size,
 	png_uint_32 old_size)
diff --git a/pngpread.c b/pngpread.c
index 9b03126..f23f326 100644
--- a/pngpread.c
+++ b/pngpread.c
@@ -1,15 +1,17 @@
 
 /* pngpread.c - read a png file in push mode
 
-	libpng 1.0 beta 2 - version 0.86
+	libpng 1.0 beta 2 - version 0.87
    For conditions of distribution and use, see copyright notice in png.h
 	Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
-   January 10, 1996
+   January 15, 1996
    */
 
 #define PNG_INTERNAL
 #include "png.h"
 
+#ifdef PNG_PROGRESSIVE_READ_SUPPORTED
+
 void
 png_process_data(png_structp png_ptr, png_infop info,
 	png_bytep buffer, png_uint_32 buffer_size)
@@ -110,7 +112,7 @@
 
 		png_push_fill_buffer(png_ptr, chunk_start, 8);
 		png_ptr->push_length = png_get_uint_32(chunk_start);
-		memcpy(png_ptr->push_chunk_name, chunk_start + 4, 4);
+		png_memcpy(png_ptr->push_chunk_name, (png_voidp)(chunk_start + 4), 4);
 		png_ptr->have_chunk_header = 1;
 		png_reset_crc(png_ptr);
 		png_calculate_crc(png_ptr, chunk_start + 4, 4);
@@ -142,7 +144,7 @@
 		else
 #else
 		{
-			png_push_handle_PLTE(png_ptr, info, png_ptr->push_length);
+			png_push_handle_PLTE(png_ptr, png_ptr->push_length);
 		}
 #endif
 		png_ptr->mode = PNG_HAVE_PLTE;
@@ -316,7 +318,7 @@
 			png_ptr->mode == PNG_AFTER_IEND)
 			png_error(png_ptr, "Out of Place tEXt");
 
-		png_push_handle_tEXt(png_ptr, info, png_ptr->push_length);
+		png_push_handle_tEXt(png_ptr, png_ptr->push_length);
 	}
 #endif
 #if defined(PNG_READ_zTXt_SUPPORTED)
@@ -326,7 +328,7 @@
 			png_ptr->mode == PNG_AFTER_IEND)
 			png_error(png_ptr, "Out of Place zTXt");
 
-		png_push_handle_zTXt(png_ptr, info, png_ptr->push_length);
+		png_push_handle_zTXt(png_ptr, png_ptr->push_length);
 	}
 #endif
 	else
@@ -376,7 +378,7 @@
 		png_ptr->skip_length -= save_size;
 		png_ptr->buffer_size -= save_size;
 		png_ptr->save_buffer_size -= save_size;
-		png_ptr->save_buffer_ptr += save_size;
+		png_ptr->save_buffer_ptr += (png_size_t)save_size;
 	}
 	if (png_ptr->skip_length && png_ptr->current_buffer_size)
 	{
@@ -392,7 +394,7 @@
 		png_ptr->skip_length -= save_size;
 		png_ptr->buffer_size -= save_size;
 		png_ptr->current_buffer_size -= save_size;
-		png_ptr->current_buffer_ptr += save_size;
+		png_ptr->current_buffer_ptr += (png_size_t)save_size;
 	}
 	if (!png_ptr->skip_length && png_ptr->buffer_size >= 4)
 	{
@@ -405,7 +407,7 @@
 png_push_fill_buffer(png_structp png_ptr, png_bytep buffer,
 	png_uint_32 length)
 {
-	png_byte * ptr;
+	png_bytep ptr;
 
 	ptr = buffer;
 	if (png_ptr->save_buffer_size)
@@ -417,12 +419,12 @@
 		else
 			save_size = png_ptr->save_buffer_size;
 
-		memcpy(ptr, png_ptr->save_buffer_ptr, save_size);
+		png_memcpy(ptr, png_ptr->save_buffer_ptr, (png_size_t)save_size);
 		length -= save_size;
-		ptr += save_size;
+		ptr += (png_size_t)save_size;
 		png_ptr->buffer_size -= save_size;
 		png_ptr->save_buffer_size -= save_size;
-		png_ptr->save_buffer_ptr += save_size;
+		png_ptr->save_buffer_ptr += (png_size_t)save_size;
 	}
 	if (length && png_ptr->current_buffer_size)
 	{
@@ -433,10 +435,10 @@
 		else
 			save_size = png_ptr->current_buffer_size;
 
-		memcpy(ptr, png_ptr->current_buffer_ptr, save_size);
+		png_memcpy(ptr, png_ptr->current_buffer_ptr, (png_size_t)save_size);
 		png_ptr->buffer_size -= save_size;
 		png_ptr->current_buffer_size -= save_size;
-		png_ptr->current_buffer_ptr += save_size;
+		png_ptr->current_buffer_ptr += (png_size_t)save_size;
 	}
 }
 
@@ -448,8 +450,8 @@
 		if (png_ptr->save_buffer_ptr != png_ptr->save_buffer)
 		{
 			int i;
-			png_byte * sp;
-			png_byte * dp;
+			png_bytep sp;
+			png_bytep dp;
 
 			for (i = 0, sp = png_ptr->save_buffer_ptr, dp = png_ptr->save_buffer;
 				i < png_ptr->save_buffer_size;
@@ -463,22 +465,24 @@
 		png_ptr->save_buffer_max)
 	{
 		int new_max;
-		png_byte * old_buffer;
+		png_bytep old_buffer;
 
-		new_max = png_ptr->save_buffer_size +
-			png_ptr->current_buffer_size + 256;
+		new_max = (int)(png_ptr->save_buffer_size +
+			png_ptr->current_buffer_size + 256);
 		old_buffer = png_ptr->save_buffer;
-		png_ptr->save_buffer = (png_byte *)
+		png_ptr->save_buffer = (png_bytep)
 			png_large_malloc(png_ptr, new_max);
-		memcpy(png_ptr->save_buffer, old_buffer,
-			png_ptr->save_buffer_size);
+		png_memcpy(png_ptr->save_buffer, old_buffer,
+			(png_size_t)png_ptr->save_buffer_size);
 		png_large_free(png_ptr, old_buffer);
 		  png_ptr->save_buffer_max = new_max;
 	}
 	if (png_ptr->current_buffer_size)
 	{
-		memcpy(png_ptr->save_buffer + png_ptr->save_buffer_size,
-			png_ptr->current_buffer_ptr, png_ptr->current_buffer_size);
+		png_memcpy(png_ptr->save_buffer +
+			(png_size_t)png_ptr->save_buffer_size,
+			png_ptr->current_buffer_ptr,
+			(png_size_t)png_ptr->current_buffer_size);
 		png_ptr->save_buffer_size += png_ptr->current_buffer_size;
 		png_ptr->current_buffer_size = 0;
 	}
@@ -511,7 +515,8 @@
 
 		png_push_fill_buffer(png_ptr, chunk_start, 8);
 		png_ptr->push_length = png_get_uint_32(chunk_start);
-		memcpy(png_ptr->push_chunk_name, chunk_start + 4, 4);
+		png_memcpy(png_ptr->push_chunk_name,
+			(png_voidp)(chunk_start + 4), 4);
 		png_ptr->have_chunk_header = 1;
 		png_reset_crc(png_ptr);
 		png_calculate_crc(png_ptr, chunk_start + 4, 4);
@@ -540,7 +545,7 @@
 		png_ptr->idat_size -= save_size;
 		png_ptr->buffer_size -= save_size;
 		png_ptr->save_buffer_size -= save_size;
-		png_ptr->save_buffer_ptr += save_size;
+		png_ptr->save_buffer_ptr += (png_size_t)save_size;
 	}
 	if (png_ptr->idat_size && png_ptr->current_buffer_size)
 	{
@@ -557,7 +562,7 @@
 		png_ptr->idat_size -= save_size;
 		png_ptr->buffer_size -= save_size;
 		png_ptr->current_buffer_size -= save_size;
-		png_ptr->current_buffer_ptr += save_size;
+		png_ptr->current_buffer_ptr += (png_size_t)save_size;
 	}
 	if (!png_ptr->idat_size && png_ptr->buffer_size >= 4)
 	{
@@ -785,14 +790,14 @@
 
 
 void
-png_push_handle_PLTE(png_structp png_ptr, png_infop info, png_uint_32 length)
+png_push_handle_PLTE(png_structp png_ptr, png_uint_32 length)
 {
 	if (length % 3)
 		png_error(png_ptr, "Invalid Palette Chunk");
 
-	png_ptr->num_palette = length / 3;
+	png_ptr->num_palette = (png_uint_16)(length / 3);
 	png_ptr->cur_palette = 0;
-	png_ptr->palette = (png_colorp)png_malloc(png_ptr,
+	png_ptr->palette = (png_colorp)png_large_malloc(png_ptr,
 		png_ptr->num_palette * sizeof (png_color));
 	png_ptr->process_mode = PNG_READ_PLTE_MODE;
 }
@@ -828,8 +833,7 @@
 
 #if defined(PNG_READ_tEXt_SUPPORTED)
 void
-png_push_handle_tEXt(png_structp png_ptr, png_infop info,
-	png_uint_32 length)
+png_push_handle_tEXt(png_structp png_ptr, png_uint_32 length)
 {
 	png_ptr->current_text = (png_charp)png_large_malloc(png_ptr, length + 1);
 	png_ptr->current_text[(png_size_t)length] = '\0';
@@ -855,7 +859,7 @@
 		png_calculate_crc(png_ptr, (png_bytep)png_ptr->current_text_ptr,
 			text_size);
 		png_ptr->current_text_left -= text_size;
-		png_ptr->current_text_ptr += text_size;
+		png_ptr->current_text_ptr += (png_size_t)text_size;
 	}
 	if (!(png_ptr->current_text_left) && png_ptr->buffer_size >= 4)
 	{
@@ -882,7 +886,7 @@
 
 #if defined(PNG_READ_zTXt_SUPPORTED)
 void
-png_push_handle_zTXt(png_structp png_ptr, png_infop info,
+png_push_handle_zTXt(png_structp png_ptr,
 	png_uint_32 length)
 {
 	png_ptr->current_text = (png_charp)png_large_malloc(png_ptr, length + 1);
@@ -909,7 +913,7 @@
 		png_calculate_crc(png_ptr, (png_bytep)png_ptr->current_text_ptr,
 			text_size);
 		png_ptr->current_text_left -= text_size;
-		png_ptr->current_text_ptr += text_size;
+		png_ptr->current_text_ptr += (png_size_t)text_size;
 	}
 	if (!(png_ptr->current_text_left) && png_ptr->buffer_size >= 4)
 	{
@@ -923,8 +927,6 @@
 		key = png_ptr->current_text;
 		png_ptr->current_text = 0;
 
-		text = NULL;
-
 		for (text = key; *text; text++)
 			/* empty loop */ ;
 
@@ -954,7 +956,7 @@
 		key_size = text - key;
 		text_size = 0;
 		text = NULL;
-      ret = Z_STREAM_END;
+		ret = Z_STREAM_END;
 
 		while (png_ptr->zstream->avail_in)
 		{
@@ -971,7 +973,7 @@
 			{
 				if (!text)
 				{
-					text = (png_charp)png_malloc(png_ptr,
+					text = (png_charp)png_large_malloc(png_ptr,
 						png_ptr->zbuf_size - png_ptr->zstream->avail_out +
 							key_size + 1);
 					png_memcpy(text + (png_size_t)key_size, png_ptr->zbuf,
@@ -1073,7 +1075,7 @@
 
 		png_push_fill_buffer(png_ptr, chunk_start, 8);
 		png_ptr->push_length = png_get_uint_32(chunk_start);
-		memcpy(png_ptr->push_chunk_name, chunk_start + 4, 4);
+		png_memcpy(png_ptr->push_chunk_name, (png_voidp)(chunk_start + 4), 4);
 		png_ptr->have_chunk_header = 1;
 		png_reset_crc(png_ptr);
 		png_calculate_crc(png_ptr, chunk_start + 4, 4);
@@ -1173,7 +1175,7 @@
 			png_ptr->mode == PNG_AFTER_IEND)
 			png_error(png_ptr, "Out of Place tEXt");
 
-		png_push_handle_tEXt(png_ptr, info, png_ptr->push_length);
+		png_push_handle_tEXt(png_ptr, png_ptr->push_length);
 	}
 #endif
 #if defined(PNG_READ_zTXt_SUPPORTED)
@@ -1183,7 +1185,7 @@
 			png_ptr->mode == PNG_AFTER_IEND)
 			png_error(png_ptr, "Out of Place zTXt");
 
-		png_push_handle_zTXt(png_ptr, info, png_ptr->push_length);
+		png_push_handle_zTXt(png_ptr, png_ptr->push_length);
 	}
 #endif
 	else
@@ -1214,6 +1216,9 @@
 png_progressive_combine_row (png_structp png_ptr,
 	png_bytep old_row, png_bytep new_row)
 {
-	png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
+	if (new_row)
+		png_combine_row(png_ptr, old_row, png_pass_dsp_mask[png_ptr->pass]);
 }
 
+#endif /* PNG_PROGRESSIVE_READ_SUPPORTED */
+
diff --git a/pngrcb.c b/pngrcb.c
index d6bcd3b..c85e138 100644
--- a/pngrcb.c
+++ b/pngrcb.c
@@ -1,9 +1,9 @@
 /* pngrcb.c - callbacks while reading a png file
 
-	libpng 1.0 beta 2 - version 0.86
+	libpng 1.0 beta 2 - version 0.87
    For conditions of distribution and use, see copyright notice in png.h
 	Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
-   January 10, 1996
+   January 15, 1996
    */
 
 #define PNG_INTERNAL
@@ -20,11 +20,11 @@
 
    info->width = width;
    info->height = height;
-   info->bit_depth = bit_depth;
-   info->color_type = color_type;
-   info->compression_type = compression_type;
-   info->filter_type = filter_type;
-	info->interlace_type = interlace_type;
+	info->bit_depth = (png_byte)bit_depth;
+	info->color_type =(png_byte) color_type;
+	info->compression_type = (png_byte)compression_type;
+	info->filter_type = (png_byte)filter_type;
+	info->interlace_type = (png_byte)interlace_type;
    if (info->color_type == PNG_COLOR_TYPE_PALETTE)
       info->channels = 1;
    else if (info->color_type & PNG_COLOR_MASK_COLOR)
@@ -33,7 +33,7 @@
       info->channels = 1;
    if (info->color_type & PNG_COLOR_MASK_ALPHA)
       info->channels++;
-   info->pixel_depth = info->channels * info->bit_depth;
+	info->pixel_depth = (png_byte)(info->channels * info->bit_depth);
    info->rowbytes = ((info->width * info->pixel_depth + 7) >> 3);
 }
 
@@ -45,7 +45,7 @@
       return;
 
    info->palette = palette;
-   info->num_palette = num;
+	info->num_palette = (png_uint_16)num;
    info->valid |= PNG_INFO_PLTE;
 }
 
@@ -112,7 +112,7 @@
       png_memcpy(&(info->trans_values), trans_values,
          sizeof(png_color_16));
    }
-   info->num_trans = num_trans;
+	info->num_trans = (png_uint_16)num_trans;
    info->valid |= PNG_INFO_tRNS;
 }
 #endif
@@ -152,7 +152,7 @@
 
    info->x_pixels_per_unit = res_x;
    info->y_pixels_per_unit = res_y;
-   info->phys_unit_type = unit_type;
+	info->phys_unit_type = (png_byte)unit_type;
    info->valid |= PNG_INFO_pHYs;
 }
 #endif
@@ -167,7 +167,7 @@
 
    info->x_offset = offset_x;
    info->y_offset = offset_y;
-   info->offset_unit_type = unit_type;
+   info->offset_unit_type = (png_byte)unit_type;
    info->valid |= PNG_INFO_oFFs;
 }
 #endif
@@ -200,16 +200,22 @@
          png_uint_32 old_max;
 
          old_max = info->max_text;
-         info->max_text = info->num_text + 16;
-			info->text = (png_textp)png_realloc(png_ptr,
-            info->text,
-            info->max_text * sizeof (png_text),
-				old_max * sizeof (png_text));
+			info->max_text = info->num_text + 16;
+			{
+				png_textp old_text;
+
+				old_text = info->text;
+				info->text = (png_textp)png_large_malloc(png_ptr,
+					info->max_text * sizeof (png_text));
+				png_memcpy(info->text, old_text,
+					(png_size_t)(old_max * sizeof (png_text)));
+				png_large_free(png_ptr, old_text);
+			}
       }
       else
       {
          info->max_text = info->num_text + 16;
-         info->text = (png_textp)png_malloc(png_ptr,
+         info->text = (png_textp)png_large_malloc(png_ptr,
             info->max_text * sizeof (png_text));
          info->num_text = 0;
       }
diff --git a/pngread.c b/pngread.c
index f0b363c..9e00833 100644
--- a/pngread.c
+++ b/pngread.c
@@ -1,10 +1,10 @@
 
 /* pngread.c - read a png file
 
-	libpng 1.0 beta 2 - version 0.86
+   libpng 1.0 beta 2 - version 0.87
    For conditions of distribution and use, see copyright notice in png.h
-	Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
-   January 10, 1996
+   Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
+   January 15, 1996
    */
 
 #define PNG_INTERNAL
@@ -14,30 +14,30 @@
 void
 png_read_init(png_structp png_ptr)
 {
-	jmp_buf tmp_jmp;
-	png_msg_ptr error_fn;
-	png_msg_ptr warning_fn;
-	png_voidp msg_ptr;
+   jmp_buf tmp_jmp;
+   png_msg_ptr error_fn;
+   png_msg_ptr warning_fn;
+   png_voidp msg_ptr;
 
-	png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
-	error_fn = png_ptr->error_fn;
-	warning_fn = png_ptr->warning_fn;
-	msg_ptr = png_ptr->msg_ptr;
+   png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
+   error_fn = png_ptr->error_fn;
+   warning_fn = png_ptr->warning_fn;
+   msg_ptr = png_ptr->msg_ptr;
 
-	png_memset(png_ptr, 0, sizeof (png_struct));
+   png_memset(png_ptr, 0, sizeof (png_struct));
 
-	png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
-	png_ptr->error_fn = error_fn;
-	png_ptr->warning_fn = warning_fn;
-	png_ptr->msg_ptr = msg_ptr;
+   png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
+   png_ptr->error_fn = error_fn;
+   png_ptr->warning_fn = warning_fn;
+   png_ptr->msg_ptr = msg_ptr;
 
    png_ptr->zbuf_size = PNG_ZBUF_SIZE;
    png_ptr->zbuf = png_large_malloc(png_ptr, png_ptr->zbuf_size);
-	png_ptr->zstream = (z_stream *)png_malloc(png_ptr, sizeof (z_stream));
-	png_ptr->zstream->zalloc = png_zalloc;
+   png_ptr->zstream = (z_stream *)png_malloc(png_ptr, sizeof (z_stream));
+   png_ptr->zstream->zalloc = png_zalloc;
    png_ptr->zstream->zfree = png_zfree;
-   png_ptr->zstream->opaque = (voidp)png_ptr;
-	inflateInit(png_ptr->zstream);
+   png_ptr->zstream->opaque = (voidpf)png_ptr;
+   inflateInit(png_ptr->zstream);
    png_ptr->zstream->next_out = png_ptr->zbuf;
    png_ptr->zstream->avail_out = (uInt)png_ptr->zbuf_size;
 }
@@ -63,7 +63,7 @@
       png_calculate_crc(png_ptr, chunk_start + 4, 4);
       if (!png_memcmp(chunk_start + 4, png_IHDR, 4))
       {
-			if (png_ptr->mode != PNG_BEFORE_IHDR)
+         if (png_ptr->mode != PNG_BEFORE_IHDR)
             png_error(png_ptr, "Out of Place IHDR");
 
          png_handle_IHDR(png_ptr, info, length);
@@ -81,7 +81,7 @@
 #else
          {
             png_handle_PLTE(png_ptr, info, length);
-			}
+         }
 #endif
          png_ptr->mode = PNG_HAVE_PLTE;
       }
@@ -99,7 +99,7 @@
       else if (!png_memcmp(chunk_start + 4, png_gAMA, 4))
       {
          if (png_ptr->mode != PNG_HAVE_IHDR)
-				png_error(png_ptr, "Out of Place PLTE");
+            png_error(png_ptr, "Out of Place PLTE");
 
          png_handle_gAMA(png_ptr, info, length);
       }
@@ -117,7 +117,7 @@
       else if (!png_memcmp(chunk_start + 4, png_cHRM, 4))
       {
          if (png_ptr->mode != PNG_HAVE_IHDR)
-				png_error(png_ptr, "Out of Place cHRM");
+            png_error(png_ptr, "Out of Place cHRM");
 
          png_handle_cHRM(png_ptr, info, length);
       }
@@ -135,7 +135,7 @@
 #if defined(PNG_READ_bKGD_SUPPORTED)
       else if (!png_memcmp(chunk_start + 4, png_bKGD, 4))
       {
-			if (png_ptr->mode != PNG_HAVE_IHDR &&
+         if (png_ptr->mode != PNG_HAVE_IHDR &&
             png_ptr->mode != PNG_HAVE_PLTE)
             png_error(png_ptr, "Out of Place bKGD");
 
@@ -153,7 +153,7 @@
 #endif
 #if defined(PNG_READ_pHYs_SUPPORTED)
       else if (!png_memcmp(chunk_start + 4, png_pHYs, 4))
-		{
+      {
          if (png_ptr->mode != PNG_HAVE_IHDR &&
             png_ptr->mode != PNG_HAVE_PLTE)
             png_error(png_ptr, "Out of Place pHYs");
@@ -189,7 +189,7 @@
             png_error(png_ptr, "Out of Place tEXt");
 
          png_handle_tEXt(png_ptr, info, length);
-		}
+      }
 #endif
 #if defined(PNG_READ_zTXt_SUPPORTED)
       else if (!png_memcmp(chunk_start + 4, png_zTXt, 4))
@@ -255,7 +255,7 @@
                if (dsp_row)
                   png_combine_row(png_ptr, dsp_row,
                      png_pass_dsp_mask[png_ptr->pass]);
-					png_read_finish_row(png_ptr);
+               png_read_finish_row(png_ptr);
                return;
             }
             break;
@@ -273,7 +273,7 @@
             if ((png_ptr->row_number & 7) != 4)
             {
                if (dsp_row && (png_ptr->row_number & 4))
-						png_combine_row(png_ptr, dsp_row,
+                  png_combine_row(png_ptr, dsp_row,
                      png_pass_dsp_mask[png_ptr->pass]);
                png_read_finish_row(png_ptr);
                return;
@@ -291,7 +291,7 @@
             break;
          case 4:
             if ((png_ptr->row_number & 3) != 2)
-				{
+            {
                if (dsp_row && (png_ptr->row_number & 2))
                   png_combine_row(png_ptr, dsp_row,
                      png_pass_dsp_mask[png_ptr->pass]);
@@ -309,7 +309,7 @@
                return;
             }
             break;
-			case 6:
+         case 6:
             if (!(png_ptr->row_number & 1))
             {
                png_read_finish_row(png_ptr);
@@ -327,7 +327,7 @@
    png_ptr->zstream->avail_out = (uInt)png_ptr->irowbytes;
    do
    {
-		if (!(png_ptr->zstream->avail_in))
+      if (!(png_ptr->zstream->avail_in))
       {
          while (!png_ptr->idat_size)
          {
@@ -345,7 +345,7 @@
             png_reset_crc(png_ptr);
 
             png_crc_read(png_ptr, buf, 4);
-				if (png_memcmp(buf, png_IDAT, 4))
+            if (png_memcmp(buf, png_IDAT, 4))
                png_error(png_ptr, "Not enough image data");
 
          }
@@ -356,14 +356,14 @@
          png_crc_read(png_ptr, png_ptr->zbuf, png_ptr->zstream->avail_in);
          png_ptr->idat_size -= png_ptr->zstream->avail_in;
       }
-		ret = inflate(png_ptr->zstream, Z_PARTIAL_FLUSH);
+      ret = inflate(png_ptr->zstream, Z_PARTIAL_FLUSH);
       if (ret == Z_STREAM_END)
       {
          if (png_ptr->zstream->avail_out || png_ptr->zstream->avail_in ||
             png_ptr->idat_size)
             png_error(png_ptr, "Extra compressed data");
          png_ptr->mode = PNG_AT_LAST_IDAT;
-			break;
+         break;
       }
       if (ret != Z_OK)
          png_error(png_ptr, "Compression Error");
@@ -381,7 +381,7 @@
    png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
       (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
 
-	if (png_ptr->row_buf[0])
+   if (png_ptr->row_buf[0])
       png_read_filter_row(&(png_ptr->row_info),
          png_ptr->row_buf + 1, png_ptr->prev_row + 1,
          (int)(png_ptr->row_buf[0]));
@@ -421,7 +421,7 @@
 /* read a one or more rows of image data.   If the image is interlaced,
    and png_set_interlace_handling() has been called, the rows need to
    to contain the contents of the rows from the previous pass.  If
-	the image has alpha or transparency, and png_handle_alpha() has been
+   the 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
@@ -435,22 +435,22 @@
    rows.  In this case, you do not have to provide a display_rows buffer
    also, but you may.  If the image is not interlaced, or if you have
    not called png_set_interlace_handling(), the display_row buffer will
-	be ignored, so pass NULL to it. */
+   be ignored, so pass NULL to it. */
 
 void
 png_read_rows(png_structp png_ptr, png_bytepp row,
-	png_bytepp display_row, png_uint_32 num_rows)
+   png_bytepp display_row, png_uint_32 num_rows)
 {
-	png_uint_32 i;
-	png_bytepp rp;
-	png_bytepp dp;
+   png_uint_32 i;
+   png_bytepp rp;
+   png_bytepp dp;
 
    rp = row;
    dp = display_row;
    for (i = 0; i < num_rows; i++)
    {
-		png_bytep rptr;
-		png_bytep dptr;
+      png_bytep rptr;
+      png_bytep dptr;
 
       if (rp)
          rptr = *rp;
@@ -461,7 +461,7 @@
       else
          dptr = NULL;
       png_read_row(png_ptr, rptr, dptr);
-		if (row)
+      if (row)
          rp++;
       if (display_row)
          dp++;
@@ -479,7 +479,7 @@
 {
    png_uint_32 i;
    int pass, j;
-	png_bytepp rp;
+   png_bytepp rp;
 
    pass = png_set_interlace_handling(png_ptr);
    for (j = 0; j < pass; j++)
@@ -533,7 +533,7 @@
          png_error(png_ptr, "invalid chunk after IDAT");
       }
       else if (!png_memcmp(chunk_start + 4, png_cHRM, 4))
-		{
+      {
          png_error(png_ptr, "invalid chunk after IDAT");
       }
       else if (!png_memcmp(chunk_start + 4, png_tRNS, 4))
@@ -551,7 +551,7 @@
       else if (!png_memcmp(chunk_start + 4, png_IDAT, 4))
       {
          if (length > 0 || png_ptr->mode != PNG_AT_LAST_IDAT)
-				png_error(png_ptr, "too many IDAT's found");
+            png_error(png_ptr, "too many IDAT's found");
       }
       else if (!png_memcmp(chunk_start + 4, png_pHYs, 4))
       {
@@ -569,7 +569,7 @@
             png_error(png_ptr, "Out of Place tIME");
 
          if (info)
-				png_handle_tIME(png_ptr, info, length);
+            png_handle_tIME(png_ptr, info, length);
          else
             png_crc_skip(png_ptr, length);
       }
@@ -605,7 +605,7 @@
          png_ptr->mode = PNG_AFTER_IEND;
       }
       else
-		{
+      {
          if ((chunk_start[4] & 0x20) == 0)
             png_error(png_ptr, "Unknown Critical Chunk");
 
@@ -616,7 +616,7 @@
       if (((crc ^ 0xffffffffL) & 0xffffffffL) !=
          (png_ptr->crc & 0xffffffffL))
          png_error(png_ptr, "Bad CRC value");
-		if (png_ptr->mode == PNG_AT_LAST_IDAT)
+      if (png_ptr->mode == PNG_AT_LAST_IDAT)
          png_ptr->mode = PNG_AFTER_IDAT;
    } while (png_ptr->mode != PNG_AFTER_IEND);
 }
@@ -629,37 +629,27 @@
    jmp_buf tmp_jmp;
 
    if (info)
-	{
-		if (png_ptr->do_free & PNG_FREE_PALETTE)
-			png_free(png_ptr, info->palette);
-#if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_bKGD_SUPPORTED)
-		if (png_ptr->do_free & PNG_FREE_TRANS)
-			png_free(png_ptr, info->trans);
-#endif
-#if defined(PNG_READ_hIST_SUPPORTED)
-		if (png_ptr->do_free & PNG_FREE_HIST)
-			png_free(png_ptr, info->hist);
-#endif
+   {
 #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED)
-		for (i = 0; i < info->num_text; i++)
-		{
-			png_large_free(png_ptr, info->text[i].key);
-		}
+      for (i = 0; i < info->num_text; i++)
+      {
+         png_large_free(png_ptr, info->text[i].key);
+      }
 
-		png_free(png_ptr, info->text);
+      png_large_free(png_ptr, info->text);
 #endif
-		png_memset(info, 0, sizeof(png_info));
-	}
+      png_memset(info, 0, sizeof(png_info));
+   }
 
    if (end_info)
    {
 #if defined(PNG_READ_tEXt_SUPPORTED) || defined(PNG_READ_zTXt_SUPPORTED)
-		for (i = 0; i < end_info->num_text; i++)
+      for (i = 0; i < end_info->num_text; i++)
       {
-			png_large_free(png_ptr, end_info->text[i].key);
+         png_large_free(png_ptr, end_info->text[i].key);
       }
 
-      png_free(png_ptr, end_info->text);
+      png_large_free(png_ptr, end_info->text);
 #endif
       png_memset(end_info, 0, sizeof(png_info));
    }
@@ -669,59 +659,61 @@
    png_large_free(png_ptr, png_ptr->prev_row);
 #if defined(PNG_READ_DITHER_SUPPORTED)
    png_large_free(png_ptr, png_ptr->palette_lookup);
-   png_free(png_ptr, png_ptr->dither_index);
+   png_large_free(png_ptr, png_ptr->dither_index);
 #endif
 #if defined(PNG_READ_GAMMA_SUPPORTED)
-	png_free(png_ptr, png_ptr->gamma_table);
+   png_large_free(png_ptr, png_ptr->gamma_table);
 #endif
 #if defined(PNG_READ_BACKGROUND_SUPPORTED)
-   png_free(png_ptr, png_ptr->gamma_from_1);
-   png_free(png_ptr, png_ptr->gamma_to_1);
+   png_large_free(png_ptr, png_ptr->gamma_from_1);
+   png_large_free(png_ptr, png_ptr->gamma_to_1);
+#endif
+   if (png_ptr->do_free & PNG_FREE_PALETTE)
+      png_large_free(png_ptr, png_ptr->palette);
+#if defined(PNG_READ_BACKGROUND_SUPPORTED) && defined(PNG_READ_bKGD_SUPPORTED)
+   if (png_ptr->do_free & PNG_FREE_TRANS)
+      png_large_free(png_ptr, png_ptr->trans);
+#endif
+#if defined(PNG_READ_hIST_SUPPORTED)
+   if (png_ptr->do_free & PNG_FREE_HIST)
+      png_large_free(png_ptr, png_ptr->hist);
 #endif
 #if defined(PNG_READ_GAMMA_SUPPORTED)
    if (png_ptr->gamma_16_table)
    {
       for (i = 0; i < (1 << (8 - png_ptr->gamma_shift)); i++)
       {
-         png_free(png_ptr, png_ptr->gamma_16_table[i]);
-      }
-	}
-#endif
-#if defined(PNG_READ_BACKGROUND_SUPPORTED)
-   png_free(png_ptr, png_ptr->gamma_16_table);
-   if (png_ptr->gamma_16_from_1)
-	{
-      for (i = 0; i < (1 << (8 - png_ptr->gamma_shift)); i++)
-		{
-         png_free(png_ptr, png_ptr->gamma_16_from_1[i]);
+         png_large_free(png_ptr, png_ptr->gamma_16_table[i]);
       }
    }
-   png_free(png_ptr, png_ptr->gamma_16_from_1);
+#endif
+#if defined(PNG_READ_BACKGROUND_SUPPORTED)
+   png_large_free(png_ptr, png_ptr->gamma_16_table);
+   if (png_ptr->gamma_16_from_1)
+   {
+      for (i = 0; i < (1 << (8 - png_ptr->gamma_shift)); i++)
+      {
+         png_large_free(png_ptr, png_ptr->gamma_16_from_1[i]);
+      }
+   }
+   png_large_free(png_ptr, png_ptr->gamma_16_from_1);
    if (png_ptr->gamma_16_to_1)
    {
       for (i = 0; i < (1 << (8 - png_ptr->gamma_shift)); i++)
       {
-         png_free(png_ptr, png_ptr->gamma_16_to_1[i]);
+         png_large_free(png_ptr, png_ptr->gamma_16_to_1[i]);
       }
-	}
-   png_free(png_ptr, png_ptr->gamma_16_to_1);
+   }
+   png_large_free(png_ptr, png_ptr->gamma_16_to_1);
 #endif
-#if defined(PNG_READ_BACKGROUND_SUPPORTED)
-   png_free(png_ptr, png_ptr->trans);
-#endif
-#if defined(PNG_READ_DITHER_SUPPORTED)
-	png_free(png_ptr, png_ptr->hist);
-#endif
-	if (!png_ptr->user_palette)
-		png_free(png_ptr, png_ptr->palette);
 
-	inflateEnd(png_ptr->zstream);
-	png_free(png_ptr, png_ptr->zstream);
+   inflateEnd(png_ptr->zstream);
+   png_free(png_ptr, png_ptr->zstream);
 #ifdef PNG_PROGRESSIVE_READ_SUPPORTED
-	png_free(png_ptr, png_ptr->save_buffer);
+   png_large_free(png_ptr, png_ptr->save_buffer);
 #endif
-	png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
-	png_memset(png_ptr, 0, sizeof (png_struct));
-	png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
+   png_memcpy(tmp_jmp, png_ptr->jmpbuf, sizeof (jmp_buf));
+   png_memset(png_ptr, 0, sizeof (png_struct));
+   png_memcpy(png_ptr->jmpbuf, tmp_jmp, sizeof (jmp_buf));
 }
 
diff --git a/pngrtran.c b/pngrtran.c
index d3e8938..93913a0 100644
--- a/pngrtran.c
+++ b/pngrtran.c
@@ -1,10 +1,10 @@
 
 /* pngrtran.c - transforms the data in a row for png readers
 
-	libpng 1.0 beta 2 - version 0.86
+	libpng 1.0 beta 2 - version 0.87
    For conditions of distribution and use, see copyright notice in png.h
 	Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
-   January 10, 1996
+   January 15, 1996
    */
 
 #define PNG_INTERNAL
@@ -21,8 +21,8 @@
    png_memcpy(&(png_ptr->background), background_color,
       sizeof(png_color_16));
    png_ptr->background_gamma = (float)background_gamma;
-   png_ptr->background_gamma_type = background_gamma_code;
-   png_ptr->background_expand = need_expand;
+	png_ptr->background_gamma_type = (png_byte)(background_gamma_code);
+	png_ptr->background_expand = (png_byte)need_expand;
 }
 #endif
 
@@ -59,14 +59,14 @@
 {
    png_ptr->transformations |= PNG_DITHER;
 
-   if (!full_dither)
+	if (!full_dither)
    {
       int i;
 
-      png_ptr->dither_index = (png_bytep)png_malloc(png_ptr,
+      png_ptr->dither_index = (png_bytep)png_large_malloc(png_ptr,
          num_palette * sizeof (png_byte));
       for (i = 0; i < num_palette; i++)
-         png_ptr->dither_index[i] = i;
+			png_ptr->dither_index[i] = (png_byte)i;
    }
 
    if (num_palette > maximum_colors)
@@ -77,14 +77,14 @@
             perhaps not the best solution, but good enough */
 
          int i;
-         png_bytep sort;
+			png_bytep sort;
 
          /* initialize an array to sort colors */
-         sort = (png_bytep)png_malloc(png_ptr, num_palette * sizeof (png_byte));
+         sort = (png_bytep)png_large_malloc(png_ptr, num_palette * sizeof (png_byte));
 
          /* initialize the sort array */
          for (i = 0; i < num_palette; i++)
-            sort[i] = i;
+				sort[i] = (png_byte)i;
 
          /* find the least used palette entries by starting a
             bubble sort, and running it until we have sorted
@@ -95,7 +95,7 @@
          for (i = num_palette - 1; i >= maximum_colors; i--)
          {
             int done; /* to stop early if the list is pre-sorted */
-            int j;
+				int j;
 
             done = 1;
             for (j = 0; j < i; j++)
@@ -131,13 +131,13 @@
                   while (sort[j] >= maximum_colors);
                   palette[i] = palette[j];
                }
-            }
+				}
          }
          else
          {
             int j;
 
-            /* move all the used colors inside the max limit, and
+				/* move all the used colors inside the max limit, and
                develop a translation table */
             j = num_palette;
             for (i = 0; i < maximum_colors; i++)
@@ -149,14 +149,14 @@
 
                   do
                      j--;
-                  while (sort[j] >= maximum_colors);
+						while (sort[j] >= maximum_colors);
 
                   tmp_color = palette[j];
                   palette[j] = palette[i];
                   palette[i] = tmp_color;
                   /* indicate where the color went */
-                  png_ptr->dither_index[j] = i;
-                  png_ptr->dither_index[i] = j;
+						png_ptr->dither_index[j] = (png_byte)i;
+						png_ptr->dither_index[i] = (png_byte)j;
                }
             }
             /* find closest color for those colors we are not
@@ -167,7 +167,7 @@
                {
                   int min_d, j, min_j, index;
 
-                  /* find the closest color to one we threw out */
+						/* find the closest color to one we threw out */
                   index = png_ptr->dither_index[i];
                   min_d = PNG_COLOR_DIST(palette[index],
                         palette[0]);
@@ -185,12 +185,12 @@
                         min_j = j;
                      }
                   }
-                  /* point to closest color */
-                  png_ptr->dither_index[i] = min_j;
+						/* point to closest color */
+						png_ptr->dither_index[i] = (png_byte)min_j;
                }
             }
          }
-         png_free(png_ptr, sort);
+         png_large_free(png_ptr, sort);
       }
       else
       {
@@ -203,26 +203,26 @@
          int i;
          int max_d;
          int num_new_palette;
-         png_dsortpp hash;
+			png_dsortpp hash;
 			png_bytep index_to_palette;
             /* where the original index currently is in the palette */
          png_bytep palette_to_index;
             /* which original index points to this palette color */
 
          /* initialize palette index arrays */
-         index_to_palette = (png_bytep)png_malloc(png_ptr,
+         index_to_palette = (png_bytep)png_large_malloc(png_ptr,
             num_palette * sizeof (png_byte));
-         palette_to_index = (png_bytep)png_malloc(png_ptr,
+         palette_to_index = (png_bytep)png_large_malloc(png_ptr,
             num_palette * sizeof (png_byte));
 
          /* initialize the sort array */
          for (i = 0; i < num_palette; i++)
          {
-            index_to_palette[i] = i;
-            palette_to_index[i] = i;
+				index_to_palette[i] = (png_byte)i;
+				palette_to_index[i] = (png_byte)i;
          }
 
-         hash = (png_dsortpp)png_malloc(png_ptr, 769 * sizeof (png_dsortp));
+         hash = (png_dsortpp)png_large_malloc(png_ptr, 769 * sizeof (png_dsortp));
          for (i = 0; i < 769; i++)
             hash[i] = (png_dsortp)0;
 /*         png_memset(hash, 0, 769 * sizeof (png_dsortp)); */
@@ -239,7 +239,7 @@
             */
          max_d = 96;
 
-         while (num_new_palette > maximum_colors)
+			while (num_new_palette > maximum_colors)
          {
             for (i = 0; i < num_new_palette - 1; i++)
             {
@@ -255,10 +255,10 @@
                   {
                      png_dsortp t;
 
-                     t = png_malloc(png_ptr, sizeof (png_dsort));
+                     t = png_large_malloc(png_ptr, sizeof (png_dsort));
                      t->next = hash[d];
-                     t->left = i;
-                     t->right = j;
+							t->left = (png_byte)i;
+							t->right = (png_byte)j;
                      hash[d] = t;
                   }
                }
@@ -313,8 +313,8 @@
                         palette_to_index[index_to_palette[j]] =
                            palette_to_index[num_new_palette];
 
-                        index_to_palette[j] = num_new_palette;
-                        palette_to_index[num_new_palette] = j;
+								index_to_palette[j] = (png_byte)num_new_palette;
+								palette_to_index[num_new_palette] = (png_byte)j;
                      }
                      if (num_new_palette <= maximum_colors)
                         break;
@@ -336,7 +336,7 @@
 							png_dsortp t;
 
                      t = p->next;
-                     png_free(png_ptr, p);
+                     png_large_free(png_ptr, p);
                      p = t;
                   }
                }
@@ -344,9 +344,9 @@
             }
             max_d += 96;
          }
-         png_free(png_ptr, hash);
-         png_free(png_ptr, palette_to_index);
-         png_free(png_ptr, index_to_palette);
+         png_large_free(png_ptr, hash);
+         png_large_free(png_ptr, palette_to_index);
+         png_large_free(png_ptr, index_to_palette);
       }
       num_palette = maximum_colors;
    }
@@ -355,7 +355,7 @@
       png_ptr->palette = palette;
       png_ptr->user_palette = 1;
    }
-   png_ptr->num_palette = num_palette;
+	png_ptr->num_palette = (png_uint_16)num_palette;
 
    if (full_dither)
    {
@@ -397,7 +397,7 @@
             dr = abs(ir - r);
             index_r = (ir << (PNG_DITHER_BLUE_BITS + PNG_DITHER_GREEN_BITS));
             for (ig = 0; ig < num_green; ig++)
-            {
+				{
                int dg, dt, dm, index_g;
 
                dg = abs(ig - g);
@@ -415,8 +415,8 @@
 
                   if (d < distance[index])
                   {
-                     distance[index] = d;
-                     png_ptr->palette_lookup[index] = i;
+							distance[index] = (png_byte)d;
+							png_ptr->palette_lookup[index] = (png_byte)i;
                   }
                }
             }
@@ -485,13 +485,13 @@
          switch (png_ptr->bit_depth)
          {
             case 1:
-               png_ptr->background.gray *= 0xff;
+					png_ptr->background.gray *= (png_byte)0xff;
                break;
             case 2:
-               png_ptr->background.gray *= 0x55;
+					png_ptr->background.gray *= (png_byte)0x55;
                break;
             case 4:
-               png_ptr->background.gray *= 0x11;
+					png_ptr->background.gray *= (png_byte)0x11;
                break;
          }
       }
@@ -672,7 +672,8 @@
       info_ptr->channels = 1;
    if (info_ptr->color_type & PNG_COLOR_MASK_ALPHA)
       info_ptr->channels++;
-   info_ptr->pixel_depth = info_ptr->channels * info_ptr->bit_depth;
+	info_ptr->pixel_depth = (png_byte)(info_ptr->channels *
+		info_ptr->bit_depth);
    info_ptr->rowbytes = ((info_ptr->width * info_ptr->pixel_depth + 7) >> 3);
 }
 
@@ -796,7 +797,7 @@
             shift = 7 - (int)((row_info->width + 7) & 7);
             for (i = 0; i < row_info->width; i++)
             {
-               *dp = (*sp >> shift) & 0x1;
+					*dp = (png_byte)((*sp >> shift) & 0x1);
                if (shift == 7)
                {
                   shift = 0;
@@ -817,7 +818,7 @@
             shift = (int)((3 - ((row_info->width + 3) & 3)) << 1);
             for (i = 0; i < row_info->width; i++)
             {
-               *dp = (*sp >> shift) & 0x3;
+					*dp = (png_byte)((*sp >> shift) & 0x3);
                if (shift == 6)
                {
                   shift = 0;
@@ -837,7 +838,7 @@
             shift = (int)((1 - ((row_info->width + 1) & 1)) << 2);
             for (i = 0; i < row_info->width; i++)
             {
-               *dp = (*sp >> shift) & 0xf;
+					*dp = (png_byte)((*sp >> shift) & 0xf);
                if (shift == 4)
                {
                   shift = 0;
@@ -852,7 +853,7 @@
          }
       }
       row_info->bit_depth = 8;
-      row_info->pixel_depth = 8 * row_info->channels;
+		row_info->pixel_depth = (png_byte)(8 * row_info->channels);
       row_info->rowbytes = row_info->width * row_info->channels;
    }
 }
@@ -896,8 +897,8 @@
 
 		for (i = 0; i < channels; i++)
 		{
-			if (shift[i] <= 0)
-				shift[i] = 0;
+			if (shift[(png_size_t)i] <= 0)
+				shift[(png_size_t)i] = 0;
 			else
 				value = 1;
 		}
@@ -922,7 +923,7 @@
          {
             png_byte  mask;
 				mask = (png_byte)(((int)0xf0 >> shift[0]) & (int)0xf0) |
-               ((int)0xf >> shift[0]);
+					(png_byte)((int)0xf >> shift[0]);
             for (bp = row, i = 0;
                i < row_info->rowbytes;
                i++, bp++)
@@ -955,10 +956,10 @@
 
                for (c = 0; c < row_info->channels; c++, bp += 2)
                {
-                  value = (*bp << 8) + *(bp + 1);
+						value = (png_uint_16)((*bp << 8) + *(bp + 1));
                   value >>= shift[c];
-						*bp = value >> 8;
-                  *(bp + 1) = value & 0xff;
+						*bp = (png_byte)(value >> 8);
+                  *(bp + 1) = (png_byte)(value & 0xff);
                }
             }
             break;
@@ -991,7 +992,7 @@
          dp++;
       }
       row_info->bit_depth = 8;
-      row_info->pixel_depth = 8 * row_info->channels;
+		row_info->pixel_depth = (png_byte)(8 * row_info->channels);
       row_info->rowbytes = row_info->width * row_info->channels;
    }
 }
@@ -1125,9 +1126,10 @@
             }
          }
       }
-      row_info->channels += 2;
+		row_info->channels += (png_byte)2;
       row_info->color_type |= PNG_COLOR_MASK_COLOR;
-      row_info->pixel_depth = row_info->channels * row_info->bit_depth;
+		row_info->pixel_depth = (png_byte)(row_info->channels *
+			row_info->bit_depth);
       row_info->rowbytes = ((row_info->width *
          row_info->pixel_depth + 7) >> 3);
    }
@@ -1175,9 +1177,9 @@
 
    for (i = 0, v = 0; i < num_palette; i++, v += color_inc)
    {
-      palette[i].red = v;
-      palette[i].green = v;
-      palette[i].blue = v;
+		palette[i].red = (png_byte)v;
+		palette[i].green = (png_byte)v;
+		palette[i].blue = (png_byte)v;
    }
 }
 
@@ -1262,9 +1264,9 @@
          {
             if (palette[i].red == png_ptr->trans_values.gray)
             {
-               palette[i].red = back;
-               palette[i].green = back;
-               palette[i].blue = back;
+					palette[i].red = (png_byte)back;
+					palette[i].green = (png_byte)back;
+					palette[i].blue = (png_byte)back;
             }
             else
             {
@@ -1384,8 +1386,8 @@
                      if (((*sp >> shift) & 0x1) ==
                         trans_values->gray)
                      {
-                        *sp &= ((0x7f7f >> (7 - shift)) & 0xff);
-                        *sp |= (background->gray << shift);
+								*sp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
+								*sp |= (png_byte)(background->gray << shift);
                      }
                      if (!shift)
                      {
@@ -1406,8 +1408,8 @@
                      if (((*sp >> shift) & 0x3) ==
                         trans_values->gray)
                      {
-                        *sp &= ((0x3f3f >> (6 - shift)) & 0xff);
-                        *sp |= (background->gray << shift);
+								*sp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
+								*sp |= (png_byte)(background->gray << shift);
                      }
                      if (!shift)
                      {
@@ -1428,8 +1430,8 @@
                      if (((*sp >> shift) & 0xf) ==
                         trans_values->gray)
                      {
-                        *sp &= ((0xf0f >> (4 - shift)) & 0xff);
-                        *sp |= (background->gray << shift);
+								*sp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
+								*sp |= (png_byte)(background->gray << shift);
                      }
                      if (!shift)
                      {
@@ -1484,19 +1486,19 @@
                      {
                         png_uint_16 v;
 
-                        v = ((png_uint_16)(*sp) << 8) +
-                           (png_uint_16)(*(sp + 1));
+								v = (png_uint_16)(((png_uint_16)(*sp) << 8) +
+									(png_uint_16)(*(sp + 1)));
                         if (v == trans_values->gray)
                         {
-                           *sp = (background->gray >> 8) & 0xff;
-                           *(sp + 1) = background->gray & 0xff;
+									*sp = (png_byte)((background->gray >> 8) & 0xff);
+									*(sp + 1) = (png_byte)(background->gray & 0xff);
                         }
                         else
                         {
                            v = gamma_16[
 										*(sp + 1) >> gamma_shift][*sp];
-                           *sp = (v >> 8) & 0xff;
-                           *(sp + 1) = v & 0xff;
+									*sp = (png_byte)((v >> 8) & 0xff);
+                           *(sp + 1) = (png_byte)(v & 0xff);
                         }
 							}
                   }
@@ -1508,12 +1510,12 @@
                      {
                         png_uint_16 v;
 
-                        v = ((png_uint_16)(*sp) << 8) +
-                           (png_uint_16)(*(sp + 1));
+								v = (png_uint_16)(((png_uint_16)(*sp) << 8) +
+									(png_uint_16)(*(sp + 1)));
                         if (v == trans_values->gray)
                         {
-                           *sp = (background->gray >> 8) & 0xff;
-                           *(sp + 1) = background->gray & 0xff;
+									*sp = (png_byte)((background->gray >> 8) & 0xff);
+									*(sp + 1) = (png_byte)(background->gray & 0xff);
                         }
                      }
                   }
@@ -1571,42 +1573,42 @@
                if (gamma_16)
                {
                   for (i = 0, sp = row;
-                     i < row_info->width; i++, sp += 6)
-                  {
-                     png_uint_16 r, g, b;
+							i < row_info->width; i++, sp += 6)
+						{
+							png_uint_16 r, g, b;
 
-                     r = ((png_uint_16)(*sp) << 8) +
-                        (png_uint_16)(*(sp + 1));
-                     g = ((png_uint_16)(*(sp + 2)) << 8) +
-                        (png_uint_16)(*(sp + 3));
-                     b = ((png_uint_16)(*(sp + 4)) << 8) +
-                        (png_uint_16)(*(sp + 5));
+							r = (png_uint_16)(((png_uint_16)(*sp) << 8) +
+								(png_uint_16)(*(sp + 1)));
+							g = (png_uint_16)(((png_uint_16)(*(sp + 2)) << 8) +
+								(png_uint_16)(*(sp + 3)));
+							b = (png_uint_16)(((png_uint_16)(*(sp + 4)) << 8) +
+								(png_uint_16)(*(sp + 5)));
                      if (r == trans_values->red &&
                         g == trans_values->green &&
                         b == trans_values->blue)
                      {
-                        *sp = (background->red >> 8) & 0xff;
-                        *(sp + 1) = background->red & 0xff;
-                        *(sp + 2) = (background->green >> 8) & 0xff;
-                        *(sp + 3) = background->green & 0xff;
-                        *(sp + 4) = (background->blue >> 8) & 0xff;
-                        *(sp + 5) = background->blue & 0xff;
+								*sp = (png_byte)((background->red >> 8) & 0xff);
+								*(sp + 1) = (png_byte)(background->red & 0xff);
+								*(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
+								*(sp + 3) = (png_byte)(background->green & 0xff);
+								*(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
+								*(sp + 5) = (png_byte)(background->blue & 0xff);
                      }
                      else
                      {
                         png_uint_16 v;
                         v = gamma_16[
                            *(sp + 1) >> gamma_shift][*sp];
-                        *sp = (v >> 8) & 0xff;
-                        *(sp + 1) = v & 0xff;
+								*sp = (png_byte)((v >> 8) & 0xff);
+                        *(sp + 1) = (png_byte)(v & 0xff);
                         v = gamma_16[
                            *(sp + 3) >> gamma_shift][*(sp + 2)];
-                        *(sp + 2) = (v >> 8) & 0xff;
-                        *(sp + 3) = v & 0xff;
-                        v = gamma_16[
+								*(sp + 2) = (png_byte)((v >> 8) & 0xff);
+								*(sp + 3) = (png_byte)(v & 0xff);
+								v = gamma_16[
                            *(sp + 5) >> gamma_shift][*(sp + 4)];
-                        *(sp + 4) = (v >> 8) & 0xff;
-                        *(sp + 5) = v & 0xff;
+								*(sp + 4) = (png_byte)((v >> 8) & 0xff);
+								*(sp + 5) = (png_byte)(v & 0xff);
                      }
                   }
                }
@@ -1618,29 +1620,29 @@
                   {
                      png_uint_16 r, g, b;
 
-                     r = ((png_uint_16)(*sp) << 8) +
-                        (png_uint_16)(*(sp + 1));
-                     g = ((png_uint_16)(*(sp + 2)) << 8) +
-                        (png_uint_16)(*(sp + 3));
-                     b = ((png_uint_16)(*(sp + 4)) << 8) +
-                        (png_uint_16)(*(sp + 5));
+							r = (png_uint_16)(((png_uint_16)(*sp) << 8) +
+								(png_uint_16)(*(sp + 1)));
+							g = (png_uint_16)(((png_uint_16)(*(sp + 2)) << 8) +
+								(png_uint_16)(*(sp + 3)));
+							b = (png_uint_16)(((png_uint_16)(*(sp + 4)) << 8) +
+								(png_uint_16)(*(sp + 5)));
                      if (r == trans_values->red &&
                         g == trans_values->green &&
                         b == trans_values->blue)
-                     {
-                        *sp = (background->red >> 8) & 0xff;
-                        *(sp + 1) = background->red & 0xff;
-                        *(sp + 2) = (background->green >> 8) & 0xff;
-                        *(sp + 3) = background->green & 0xff;
-                        *(sp + 4) = (background->blue >> 8) & 0xff;
-                        *(sp + 5) = background->blue & 0xff;
-                     }
-                  }
-               }
-            }
-            break;
-         }
-         case PNG_COLOR_TYPE_GRAY_ALPHA:
+							{
+								*sp = (png_byte)((background->red >> 8) & 0xff);
+								*(sp + 1) = (png_byte)(background->red & 0xff);
+								*(sp + 2) = (png_byte)((background->green >> 8) & 0xff);
+								*(sp + 3) = (png_byte)(background->green & 0xff);
+								*(sp + 4) = (png_byte)((background->blue >> 8) & 0xff);
+								*(sp + 5) = (png_byte)(background->blue & 0xff);
+							}
+						}
+					}
+				}
+				break;
+			}
+			case PNG_COLOR_TYPE_GRAY_ALPHA:
          {
             switch (row_info->bit_depth)
             {
@@ -1669,9 +1671,9 @@
                            png_uint_16 v;
 
                            v = gamma_to_1[*sp];
-                           v = ((png_uint_16)(v) * a +
-                              (png_uint_16)background_1->gray *
-                              (255 - a) + 127) / 255;
+									v = (png_uint_16)(((png_uint_16)(v) * a +
+										(png_uint_16)background_1->gray *
+                              (255 - a) + 127) / 255);
                            *dp = gamma_from_1[v];
                         }
                      }
@@ -1696,9 +1698,9 @@
                         }
                         else
                         {
-                           *dp = ((png_uint_16)(*sp) * a +
-                              (png_uint_16)background_1->gray *
-                              (255 - a) + 127) / 255;
+									*dp = (png_byte)(((png_uint_16)(*sp) * a +
+										(png_uint_16)background_1->gray *
+										(255 - a) + 127) / 255);
                         }
                      }
                   }
@@ -1715,8 +1717,8 @@
                      {
                         png_uint_16 a;
 
-                        a = ((png_uint_16)(*(sp + 2)) << 8) +
-                           (png_uint_16)(*(sp + 3));
+								a = (png_uint_16)(((png_uint_16)(*(sp + 2)) << 8) +
+									(png_uint_16)(*(sp + 3)));
                         if (a == (png_uint_16)0xffff)
                         {
                            png_uint_32 v;
@@ -1728,8 +1730,8 @@
                         }
                         else if (a == 0)
                         {
-                           *dp = (background->gray >> 8) & 0xff;
-                           *(dp + 1) = background->gray & 0xff;
+                           *dp = (png_byte)((background->gray >> 8) & 0xff);
+                           *(dp + 1) = (png_byte)(background->gray & 0xff);
                         }
                         else
                         {
@@ -1757,18 +1759,18 @@
                      {
                         png_uint_16 a;
 
-                        a = ((png_uint_16)(*(sp + 2)) << 8) +
-                           (png_uint_16)(*(sp + 3));
+								a = (png_uint_16)(((png_uint_16)(*(sp + 2)) << 8) +
+									(png_uint_16)(*(sp + 3)));
                         if (a == (png_uint_16)0xffff)
                         {
                            png_memcpy(dp, sp, 2);
                         }
                         else if (a == 0)
                         {
-                           *dp = (background->gray >> 8) & 0xff;
-                           *(dp + 1) = background->gray & 0xff;
-                        }
-                        else
+									*dp = (png_byte)((background->gray >> 8) & 0xff);
+									*(dp + 1) = (png_byte)(background->gray & 0xff);
+								}
+								else
                         {
                            png_uint_32 g, v;
 
@@ -1819,19 +1821,19 @@
                         png_uint_16 v;
 
                         v = gamma_to_1[*sp];
-                        v = ((png_uint_16)(v) * a +
-                           (png_uint_16)background_1->red *
-                           (255 - a) + 127) / 255;
+								v = (png_uint_16)(((png_uint_16)(v) * a +
+									(png_uint_16)background_1->red *
+									(255 - a) + 127) / 255);
                         *dp = gamma_from_1[v];
                         v = gamma_to_1[*(sp + 1)];
-                        v = ((png_uint_16)(v) * a +
-                           (png_uint_16)background_1->green *
-                           (255 - a) + 127) / 255;
+								v = (png_uint_16)(((png_uint_16)(v) * a +
+									(png_uint_16)background_1->green *
+									(255 - a) + 127) / 255);
                         *(dp + 1) = gamma_from_1[v];
                         v = gamma_to_1[*(sp + 2)];
-                        v = ((png_uint_16)(v) * a +
-                           (png_uint_16)background_1->blue *
-                           (255 - a) + 127) / 255;
+								v = (png_uint_16)(((png_uint_16)(v) * a +
+									(png_uint_16)background_1->blue *
+									(255 - a) + 127) / 255);
                         *(dp + 2) = gamma_from_1[v];
                      }
                   }
@@ -1860,15 +1862,15 @@
                      }
                      else
                      {
-                        *dp = ((png_uint_16)(*sp) * a +
-                           (png_uint_16)background->red *
-                           (255 - a) + 127) / 255;
-                        *(dp + 1) = ((png_uint_16)(*(sp + 1)) * a +
-                           (png_uint_16)background->green *
-                           (255 - a) + 127) / 255;
-                        *(dp + 2) = ((png_uint_16)(*(sp + 2)) * a +
-                           (png_uint_16)background->blue *
-                           (255 - a) + 127) / 255;
+								*dp = (png_byte)(((png_uint_16)(*sp) * a +
+									(png_uint_16)background->red *
+									(255 - a) + 127) / 255);
+								*(dp + 1) = (png_byte)(((png_uint_16)(*(sp + 1)) * a +
+									(png_uint_16)background->green *
+									(255 - a) + 127) / 255);
+								*(dp + 2) = (png_byte)(((png_uint_16)(*(sp + 2)) * a +
+									(png_uint_16)background->blue *
+									(255 - a) + 127) / 255);
                      }
                   }
                }
@@ -1884,33 +1886,33 @@
                   {
                      png_uint_16 a;
 
-                     a = ((png_uint_16)(*(sp + 6)) << 8) +
-                        (png_uint_16)(*(sp + 7));
+							a = (png_uint_16)(((png_uint_16)(*(sp + 6)) << 8) +
+								(png_uint_16)(*(sp + 7)));
                      if (a == (png_uint_16)0xffff)
                      {
                         png_uint_16 v;
 
                         v = gamma_16[
                            *(sp + 1) >> gamma_shift][*sp];
-                        *dp = (v >> 8) & 0xff;
-                        *(dp + 1) = v & 0xff;
+								*dp = (png_byte)((v >> 8) & 0xff);
+								*(dp + 1) = (png_byte)(v & 0xff);
                         v = gamma_16[
                            *(sp + 3) >> gamma_shift][*(sp + 2)];
-                        *(dp + 2) = (v >> 8) & 0xff;
-                        *(dp + 3) = v & 0xff;
-                        v = gamma_16[
-                           *(sp + 5) >> gamma_shift][*(sp + 4)];
-                        *(dp + 4) = (v >> 8) & 0xff;
-                        *(dp + 5) = v & 0xff;
+								*(dp + 2) = (png_byte)((v >> 8) & 0xff);
+								*(dp + 3) = (png_byte)(v & 0xff);
+								v = gamma_16[
+									*(sp + 5) >> gamma_shift][*(sp + 4)];
+								*(dp + 4) = (png_byte)((v >> 8) & 0xff);
+								*(dp + 5) = (png_byte)(v & 0xff);
                      }
                      else if (a == 0)
                      {
-                        *dp = (background->red >> 8) & 0xff;
-                        *(dp + 1) = background->red & 0xff;
-                        *(dp + 2) = (background->green >> 8) & 0xff;
-                        *(dp + 3) = background->green & 0xff;
-                        *(dp + 4) = (background->blue >> 8) & 0xff;
-                        *(dp + 5) = background->blue & 0xff;
+								*dp = (png_byte)((background->red >> 8) & 0xff);
+								*(dp + 1) = (png_byte)(background->red & 0xff);
+								*(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
+								*(dp + 3) = (png_byte)(background->green & 0xff);
+								*(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
+								*(dp + 5) = (png_byte)(background->blue & 0xff);
                      }
                      else
                      {
@@ -1958,22 +1960,22 @@
                   {
                      png_uint_16 a;
 
-                     a = ((png_uint_16)(*(sp + 6)) << 8) +
-                        (png_uint_16)(*(sp + 7));
+							a = (png_uint_16)(((png_uint_16)(*(sp + 6)) << 8) +
+								(png_uint_16)(*(sp + 7)));
                      if (a == (png_uint_16)0xffff)
                      {
                         png_memcpy(dp, sp, 6);
                      }
                      else if (a == 0)
                      {
-                        *dp = (background->red >> 8) & 0xff;
-                        *(dp + 1) = background->red & 0xff;
-                        *(dp + 2) = (background->green >> 8) & 0xff;
-                        *(dp + 3) = background->green & 0xff;
-                        *(dp + 4) = (background->blue >> 8) & 0xff;
-                        *(dp + 5) = background->blue & 0xff;
-                     }
-                     else
+								*dp = (png_byte)((background->red >> 8) & 0xff);
+								*(dp + 1) = (png_byte)(background->red & 0xff);
+								*(dp + 2) = (png_byte)((background->green >> 8) & 0xff);
+								*(dp + 3) = (png_byte)(background->green & 0xff);
+								*(dp + 4) = (png_byte)((background->blue >> 8) & 0xff);
+								*(dp + 5) = (png_byte)(background->blue & 0xff);
+							}
+							else
                      {
                         png_uint_32 r, g, b, v;
 
@@ -2011,9 +2013,9 @@
       if (row_info->color_type & PNG_COLOR_MASK_ALPHA)
       {
          row_info->color_type &= ~PNG_COLOR_MASK_ALPHA;
-         row_info->channels -= 1;
-         row_info->pixel_depth = row_info->channels *
-            row_info->bit_depth;
+			row_info->channels -= (png_byte)1;
+			row_info->pixel_depth = (png_byte)(row_info->channels *
+				row_info->bit_depth);
          row_info->rowbytes = ((row_info->width *
             row_info->pixel_depth + 7) >> 3);
       }
@@ -2065,30 +2067,30 @@
 
                   v = gamma_16_table[*(sp + 1) >>
                      gamma_shift][*sp];
-                  *sp = (v >> 8) & 0xff;
-                  *(sp + 1) = v & 0xff;
+						*sp = (png_byte)((v >> 8) & 0xff);
+						*(sp + 1) = (png_byte)(v & 0xff);
+						sp += 2;
+						v = gamma_16_table[*(sp + 1) >>
+							gamma_shift][*sp];
+						*sp = (png_byte)((v >> 8) & 0xff);
+						*(sp + 1) = (png_byte)(v & 0xff);
                   sp += 2;
                   v = gamma_16_table[*(sp + 1) >>
                      gamma_shift][*sp];
-                  *sp = (v >> 8) & 0xff;
-                  *(sp + 1) = v & 0xff;
-                  sp += 2;
-                  v = gamma_16_table[*(sp + 1) >>
-                     gamma_shift][*sp];
-                  *sp = (v >> 8) & 0xff;
-                  *(sp + 1) = v & 0xff;
-                  sp += 2;
-               }
-            }
-            break;
-         }
-         case PNG_COLOR_TYPE_RGB_ALPHA:
-         {
-            if (row_info->bit_depth == 8)
-            {
-               for (i = 0, sp = row;
-                  i < row_info->width; i++)
-               {
+						*sp = (png_byte)((v >> 8) & 0xff);
+						*(sp + 1) = (png_byte)(v & 0xff);
+						sp += 2;
+					}
+				}
+				break;
+			}
+			case PNG_COLOR_TYPE_RGB_ALPHA:
+			{
+				if (row_info->bit_depth == 8)
+				{
+					for (i = 0, sp = row;
+						i < row_info->width; i++)
+					{
                   *sp = gamma_table[*sp];
                   sp++;
                   *sp = gamma_table[*sp];
@@ -2107,21 +2109,21 @@
 
                   v = gamma_16_table[*(sp + 1) >>
                      gamma_shift][*sp];
-                  *sp = (v >> 8) & 0xff;
-                  *(sp + 1) = v & 0xff;
+						*sp = (png_byte)((v >> 8) & 0xff);
+						*(sp + 1) = (png_byte)(v & 0xff);
                   sp += 2;
                   v = gamma_16_table[*(sp + 1) >>
                      gamma_shift][*sp];
-                  *sp = (v >> 8) & 0xff;
-                  *(sp + 1) = v & 0xff;
-                  sp += 2;
-                  v = gamma_16_table[*(sp + 1) >>
-                     gamma_shift][*sp];
-                  *sp = (v >> 8) & 0xff;
-                  *(sp + 1) = v & 0xff;
-                  sp += 4;
-               }
-            }
+						*sp = (png_byte)((v >> 8) & 0xff);
+						*(sp + 1) = (png_byte)(v & 0xff);
+						sp += 2;
+						v = gamma_16_table[*(sp + 1) >>
+							gamma_shift][*sp];
+						*sp = (png_byte)((v >> 8) & 0xff);
+						*(sp + 1) = (png_byte)(v & 0xff);
+						sp += 4;
+					}
+				}
             break;
          }
          case PNG_COLOR_TYPE_GRAY_ALPHA:
@@ -2145,8 +2147,8 @@
 
                   v = gamma_16_table[*(sp + 1) >>
                      gamma_shift][*sp];
-                  *sp = (v >> 8) & 0xff;
-                  *(sp + 1) = v & 0xff;
+						*sp = (png_byte)((v >> 8) & 0xff);
+						*(sp + 1) = (png_byte)(v & 0xff);
                   sp += 4;
                }
             }
@@ -2172,8 +2174,8 @@
 
                   v = gamma_16_table[*(sp + 1) >>
                      gamma_shift][*sp];
-                  *sp = (v >> 8) & 0xff;
-                  *(sp + 1) = v & 0xff;
+						*sp = (png_byte)((v >> 8) & 0xff);
+						*(sp + 1) = (png_byte)(v & 0xff);
                   sp += 2;
                }
             }
@@ -2233,7 +2235,7 @@
                for (i = 0; i < row_info->width; i++)
                {
                   value = (*sp >> shift) & 0x3;
-                  *dp = value;
+						*dp = (png_byte)value;
                   if (shift == 6)
                   {
                      shift = 0;
@@ -2254,7 +2256,7 @@
                for (i = 0; i < row_info->width; i++)
                {
                   value = (*sp >> shift) & 0xf;
-                  *dp = value;
+						*dp = (png_byte)value;
                   if (shift == 4)
                   {
                      shift = 0;
@@ -2370,8 +2372,8 @@
                for (i = 0; i < row_info->width; i++)
                {
                   value = (*sp >> shift) & 0x3;
-                  *dp = (value | (value << 2) | (value << 4) |
-                     (value << 6));
+						*dp = (png_byte)(value | (value << 2) | (value << 4) |
+							(value << 6));
                   if (shift == 6)
                   {
                      shift = 0;
@@ -2392,7 +2394,7 @@
                for (i = 0; i < row_info->width; i++)
                {
                   value = (*sp >> shift) & 0xf;
-                  *dp = (value | (value << 4));
+						*dp = (png_byte)(value | (value << 4));
                   if (shift == 4)
                   {
                      shift = 0;
@@ -2448,7 +2450,7 @@
          }
          row_info->color_type = PNG_COLOR_TYPE_GRAY_ALPHA;
          row_info->channels = 2;
-         row_info->pixel_depth = (row_info->bit_depth << 1);
+			row_info->pixel_depth = (png_byte)(row_info->bit_depth << 1);
          row_info->rowbytes =
             ((row_info->width * row_info->pixel_depth) >> 3);
       }
@@ -2502,7 +2504,7 @@
          }
          row_info->color_type = PNG_COLOR_TYPE_RGB_ALPHA;
          row_info->channels = 4;
-         row_info->pixel_depth = (row_info->bit_depth << 2);
+			row_info->pixel_depth = (png_byte)(row_info->bit_depth << 2);
          row_info->rowbytes =
             ((row_info->width * row_info->pixel_depth) >> 3);
       }
@@ -2613,7 +2615,7 @@
 
       g = 1.0 / (png_ptr->gamma * png_ptr->display_gamma);
 
-      png_ptr->gamma_table = (png_bytep)png_malloc(png_ptr,
+      png_ptr->gamma_table = (png_bytep)png_large_malloc(png_ptr,
          (png_uint_32)256);
 
       for (i = 0; i < 256; i++)
@@ -2626,7 +2628,7 @@
       {
          g = 1.0 / (png_ptr->gamma);
 
-         png_ptr->gamma_to_1 = (png_bytep)png_malloc(png_ptr,
+         png_ptr->gamma_to_1 = (png_bytep)png_large_malloc(png_ptr,
             (png_uint_32)256);
 
          for (i = 0; i < 256; i++)
@@ -2637,7 +2639,7 @@
 
          g = 1.0 / (png_ptr->display_gamma);
 
-			png_ptr->gamma_from_1 = (png_bytep)png_malloc(png_ptr,
+			png_ptr->gamma_from_1 = (png_bytep)png_large_malloc(png_ptr,
             (png_uint_32)256);
 
          for (i = 0; i < 256; i++)
@@ -2683,13 +2685,13 @@
       if (shift < 0)
          shift = 0;
 
-		png_ptr->gamma_shift = shift;
+		png_ptr->gamma_shift = (png_byte)shift;
 
 		num = (1 << (8 - shift));
 
       g = 1.0 / (png_ptr->gamma * png_ptr->display_gamma);
 
-      png_ptr->gamma_16_table = (png_uint_16pp)png_malloc(png_ptr,
+      png_ptr->gamma_16_table = (png_uint_16pp)png_large_malloc(png_ptr,
          num * sizeof (png_uint_16p ));
 
 		if ((png_ptr->transformations & PNG_16_TO_8) &&
@@ -2700,7 +2702,7 @@
 
 			for (i = 0; i < num; i++)
 			{
-				png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
+				png_ptr->gamma_16_table[i] = (png_uint_16p)png_large_malloc(png_ptr,
 					256 * sizeof (png_uint_16));
 			}
 
@@ -2714,8 +2716,8 @@
 				while (last <= max)
 				{
 					png_ptr->gamma_16_table[(int)(last & (0xff >> shift))]
-						[(int)(last >> (8 - shift))] =
-						(png_uint_16)i | ((png_uint_16)i << 8);
+						[(int)(last >> (8 - shift))] = (png_uint_16)(
+						(png_uint_16)i | ((png_uint_16)i << 8));
 					last++;
 				}
 			}
@@ -2731,7 +2733,7 @@
 		{
 			for (i = 0; i < num; i++)
 			{
-				png_ptr->gamma_16_table[i] = (png_uint_16p)png_malloc(png_ptr,
+				png_ptr->gamma_16_table[i] = (png_uint_16p)png_large_malloc(png_ptr,
 					256 * sizeof (png_uint_16));
 
 				ig = (((png_uint_32)i *
@@ -2749,12 +2751,12 @@
       {
          g = 1.0 / (png_ptr->gamma);
 
-			png_ptr->gamma_16_to_1 = (png_uint_16pp)png_malloc(png_ptr,
+			png_ptr->gamma_16_to_1 = (png_uint_16pp)png_large_malloc(png_ptr,
             num * sizeof (png_uint_16p ));
 
          for (i = 0; i < num; i++)
          {
-            png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_malloc(png_ptr,
+            png_ptr->gamma_16_to_1[i] = (png_uint_16p)png_large_malloc(png_ptr,
                256 * sizeof (png_uint_16));
 
             ig = (((png_uint_32)i *
@@ -2768,12 +2770,12 @@
          }
          g = 1.0 / (png_ptr->display_gamma);
 
-         png_ptr->gamma_16_from_1 = (png_uint_16pp)png_malloc(png_ptr,
+         png_ptr->gamma_16_from_1 = (png_uint_16pp)png_large_malloc(png_ptr,
             num * sizeof (png_uint_16p));
 
          for (i = 0; i < num; i++)
          {
-				png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_malloc(png_ptr,
+				png_ptr->gamma_16_from_1[i] = (png_uint_16p)png_large_malloc(png_ptr,
                256 * sizeof (png_uint_16));
 
             ig = (((png_uint_32)i *
diff --git a/pngrutil.c b/pngrutil.c
index 39c342c..523d8bf 100644
--- a/pngrutil.c
+++ b/pngrutil.c
@@ -1,10 +1,10 @@
 
 /* pngrutil.c - utilities to read a png file
 
-	libpng 1.0 beta 2 - version 0.86
+	libpng 1.0 beta 2 - version 0.87
    For conditions of distribution and use, see copyright notice in png.h
 	Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
-   January 10, 1996
+   January 15, 1996
    */
 
 #define PNG_INTERNAL
@@ -30,8 +30,8 @@
 {
    png_uint_16 i;
 
-	i = ((png_uint_16)(*buf) << 8) +
-      (png_uint_16)(*(buf + 1));
+	i = (png_uint_16)(((png_uint_16)(*buf) << 8) +
+		(png_uint_16)(*(buf + 1)));
 
    return i;
 }
@@ -121,9 +121,9 @@
    /* set internal variables */
    png_ptr->width = width;
    png_ptr->height = height;
-   png_ptr->bit_depth = bit_depth;
-   png_ptr->interlaced = interlace_type;
-   png_ptr->color_type = color_type;
+	png_ptr->bit_depth = (png_byte)bit_depth;
+	png_ptr->interlaced = (png_byte)interlace_type;
+	png_ptr->color_type = (png_byte)color_type;
 
    /* find number of channels */
    switch (png_ptr->color_type)
@@ -143,8 +143,8 @@
          break;
    }
    /* set up other useful info */
-   png_ptr->pixel_depth = png_ptr->bit_depth *
-      png_ptr->channels;
+	png_ptr->pixel_depth = (png_byte)(png_ptr->bit_depth *
+		png_ptr->channels);
    png_ptr->rowbytes = ((png_ptr->width *
       (png_uint_32)png_ptr->pixel_depth + 7) >> 3);
    /* call the IHDR callback (which should just set up info) */
@@ -163,7 +163,7 @@
       png_error(png_ptr, "Invalid Palette Chunk");
 
    num = (int)length / 3;
-   palette = (png_colorp)png_malloc(png_ptr, num * sizeof (png_color));
+   palette = (png_colorp)png_large_malloc(png_ptr, num * sizeof (png_color));
    png_ptr->do_free |= PNG_FREE_PALETTE;
 	for (i = 0; i < num; i++)
    {
@@ -176,7 +176,7 @@
       palette[i].blue = buf[2];
    }
    png_ptr->palette = palette;
-   png_ptr->num_palette = num;
+	png_ptr->num_palette = (png_uint_16)num;
    png_read_PLTE(png_ptr, info, palette, num);
 }
 
@@ -310,10 +310,10 @@
          return;
       }
 
-      png_ptr->trans = (png_bytep)png_malloc(png_ptr, length);
+      png_ptr->trans = (png_bytep)png_large_malloc(png_ptr, length);
       png_ptr->do_free |= PNG_FREE_TRANS;
 		png_crc_read(png_ptr, png_ptr->trans, length);
-		png_ptr->num_trans = (int)length;
+		png_ptr->num_trans = (png_uint_16)length;
    }
    else if (png_ptr->color_type == PNG_COLOR_TYPE_RGB)
    {
@@ -406,7 +406,7 @@
 	}
 
    num = (int)length / 2;
-	png_ptr->hist = (png_uint_16p)png_malloc(png_ptr,
+	png_ptr->hist = (png_uint_16p)png_large_malloc(png_ptr,
 		num * sizeof (png_uint_16));
    png_ptr->do_free |= PNG_FREE_HIST;
 	for (i = 0; i < num; i++)
@@ -503,9 +503,7 @@
 	png_charp key;
    png_charp text;
 
-   text = NULL;
-
-   key = (png_charp )png_large_malloc(png_ptr, length + 1);
+	key = (png_charp )png_large_malloc(png_ptr, length + 1);
    png_crc_read(png_ptr, (png_bytep )key, length);
 	key[(png_size_t)length] = '\0';
 
@@ -529,10 +527,8 @@
    int ret;
    png_uint_32 text_size, key_size;
 
-   text = NULL;
-
-   key = png_large_malloc(png_ptr, length + 1);
-   png_crc_read(png_ptr, (png_bytep )key, length);
+	key = png_large_malloc(png_ptr, length + 1);
+	png_crc_read(png_ptr, (png_bytep )key, length);
    key[(png_size_t)length] = '\0';
 
    for (text = key; *text; text++)
@@ -585,7 +581,7 @@
       {
          if (!text)
          {
-            text = (png_charp)png_malloc(png_ptr,
+            text = (png_charp)png_large_malloc(png_ptr,
                png_ptr->zbuf_size - png_ptr->zstream->avail_out +
                   key_size + 1);
             png_memcpy(text + (png_size_t)key_size, png_ptr->zbuf,
@@ -687,7 +683,7 @@
                {
                   value = (*sp >> shift) & 0x1;
                   *dp &= (png_byte)((0x7f7f >> (7 - shift)) & 0xff);
-                  *dp |= (value << shift);
+						*dp |= (png_byte)(value << shift);
                }
 
                if (shift == 0)
@@ -725,7 +721,7 @@
                {
                   value = (*sp >> shift) & 0x3;
                   *dp &= (png_byte)((0x3f3f >> (6 - shift)) & 0xff);
-                  *dp |= (value << shift);
+						*dp |= (png_byte)(value << shift);
                }
 
                if (shift == 0)
@@ -762,7 +758,7 @@
                {
                   value = (*sp >> shift) & 0xf;
                   *dp &= (png_byte)((0xf0f >> (4 - shift)) & 0xff);
-                  *dp |= (value << shift);
+						*dp |= (png_byte)(value << shift);
                }
 
                if (shift == 0)
@@ -839,7 +835,7 @@
             dshift = 7 - (int)((final_width + 7) & 7);
             for (i = row_info->width; i; i--)
             {
-               v = (*sp >> sshift) & 0x1;
+					v = (png_byte)((*sp >> sshift) & 0x1);
                for (j = 0; j < png_pass_inc[pass]; j++)
                {
                   *dp &= (png_byte)((0x7f7f >> (7 - dshift)) & 0xff);
@@ -875,11 +871,11 @@
             dshift = (png_size_t)((3 - ((final_width + 3) & 3)) << 1);
             for (i = row_info->width; i; i--)
             {
-               v = (*sp >> sshift) & 0x3;
+					v = (png_byte)((*sp >> sshift) & 0x3);
                for (j = 0; j < png_pass_inc[pass]; j++)
                {
                   *dp &= (png_byte)((0x3f3f >> (6 - dshift)) & 0xff);
-                  *dp |= (v << dshift);
+						*dp |= (png_byte)(v << dshift);
                   if (dshift == 6)
                   {
                      dshift = 0;
@@ -912,11 +908,11 @@
             dshift = (png_size_t)((1 - ((final_width + 1) & 1)) << 2);
             for (i = row_info->width; i; i--)
             {
-               v = (*sp >> sshift) & 0xf;
+					v = (png_byte)((*sp >> sshift) & 0xf);
                for (j = 0; j < png_pass_inc[pass]; j++)
                {
                   *dp &= (png_byte)((0xf0f >> (4 - dshift)) & 0xff);
-                  *dp |= (v << dshift);
+                  *dp |= (png_byte)(v << dshift);
                   if (dshift == 4)
                   {
                      dshift = 0;
diff --git a/pngtest.c b/pngtest.c
index 073cf17..92f501e 100644
--- a/pngtest.c
+++ b/pngtest.c
@@ -1,10 +1,10 @@
-/* pngtest.c - a simple test program to test libpng 
+/* pngtest.c - a simple test program to test libpng
 
-   libpng 1.0 beta 2 - version 0.86
+	libpng 1.0 beta 2 - version 0.87
    For conditions of distribution and use, see copyright notice in png.h
-   Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
-   January 10, 1996
-   */
+	Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
+   January 15, 1996
+*/
 
 #include <stdio.h>
 #include <stdlib.h>
@@ -32,12 +32,14 @@
 int main()
 {
    FILE *fpin, *fpout;
-   png_bytep row_buf;
+	png_bytep row_buf;
+   png_byte * near_row_buf;
    png_uint_32 rowbytes;
    png_uint_32 y;
    int channels, num_pass, pass;
 
-   row_buf = (png_bytep )0;
+	row_buf = (png_bytep)0;
+   near_row_buf = (png_byte *)0;
 
    fprintf(STDERR, "Testing libpng version %s\n", PNG_LIBPNG_VER_STRING);
 
@@ -73,7 +75,7 @@
    }
 
    if (setjmp(write_ptr.jmpbuf))
-   {
+	{
       fprintf(STDERR, "libpng write error\n");
       fclose(fpin);
       fclose(fpout);
@@ -91,7 +93,7 @@
    png_read_info(&read_ptr, &info_ptr);
    png_write_info(&write_ptr, &info_ptr);
 
-   if ((info_ptr.color_type & 3) == 2)
+	if ((info_ptr.color_type & 3) == 2)
       channels = 3;
    else
       channels = 1;
@@ -99,7 +101,8 @@
       channels++;
 
    rowbytes = ((info_ptr.width * info_ptr.bit_depth * channels + 7) >> 3);
-   row_buf = (png_bytep )malloc((size_t)rowbytes);
+	near_row_buf = (png_byte *)malloc((size_t)rowbytes);
+   row_buf = (png_bytep)near_row_buf;
    if (!row_buf)
    {
       fprintf(STDERR, "no memory to allocate row buffer\n");
@@ -124,8 +127,11 @@
    {
       for (y = 0; y < info_ptr.height; y++)
       {
-         png_read_rows(&read_ptr, (png_bytepp )&row_buf, (png_bytepp )0, 1);
-         png_write_rows(&write_ptr, (png_bytepp )&row_buf, 1);
+#ifdef TESTING
+         fprintf(STDERR, "Processing line #%ld\n", y);
+#endif
+			png_read_rows(&read_ptr, (png_bytepp)&row_buf, (png_bytepp)0, 1);
+			png_write_rows(&write_ptr, (png_bytepp)&row_buf, 1);
       }
    }
 
@@ -138,7 +144,7 @@
    fclose(fpin);
    fclose(fpout);
 
-   free((void *)row_buf);
+   free((void *)near_row_buf);
 
    fpin = fopen(inname, "rb");
 
diff --git a/pngtodo.txt b/pngtodo.txt
index ff7e1fe..4ab649c 100644
--- a/pngtodo.txt
+++ b/pngtodo.txt
@@ -3,7 +3,8 @@
 for 0.9
    improved dithering
    final bug fixes
-   cHRM transformation
+	cHRM transformation
+   better documentation
 
 after 1.0
    overlaying one image on top of another
diff --git a/pngtrans.c b/pngtrans.c
index 2d33122..db1cbf9 100644
--- a/pngtrans.c
+++ b/pngtrans.c
@@ -2,10 +2,10 @@
 /* pngtrans.c - transforms the data in a row
    routines used by both readers and writers
 
-	libpng 1.0 beta 2 - version 0.86
+	libpng 1.0 beta 2 - version 0.87
    For conditions of distribution and use, see copyright notice in png.h
 	Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
-   January 10, 1996
+   January 15, 1996
    */
 
 #define PNG_INTERNAL
@@ -113,7 +113,7 @@
          i < row_info->rowbytes;
          i++, rp++)
       {
-         *rp = ~(*rp);
+         *rp = (png_byte)(~(*rp));
       }
    }
 }
diff --git a/pngwrite.c b/pngwrite.c
index ff7b804..e6d590f 100644
--- a/pngwrite.c
+++ b/pngwrite.c
@@ -1,10 +1,10 @@
 
 /* pngwrite.c - general routines to write a png file
 
-	libpng 1.0 beta 2 - version 0.86
+	libpng 1.0 beta 2 - version 0.87
    For conditions of distribution and use, see copyright notice in png.h
 	Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
-   January 10, 1996
+   January 15, 1996
    */
 
 /* get internal access to png.h */
@@ -161,12 +161,12 @@
 void
 png_convert_from_struct_tm(png_timep ptime, struct tm FAR * ttime)
 {
-	ptime->year = 1900 + ttime->tm_year;
-	ptime->month = ttime->tm_mon + 1;
-	ptime->day = ttime->tm_mday;
-	ptime->hour = ttime->tm_hour;
-	ptime->minute = ttime->tm_min;
-	ptime->second = ttime->tm_sec;
+	ptime->year = (png_uint_16)(1900 + ttime->tm_year);
+	ptime->month = (png_byte)(ttime->tm_mon + 1);
+	ptime->day = (png_byte)ttime->tm_mday;
+	ptime->hour = (png_byte)ttime->tm_hour;
+	ptime->minute = (png_byte)ttime->tm_min;
+	ptime->second = (png_byte)ttime->tm_sec;
 }
 
 void
@@ -322,8 +322,8 @@
    png_ptr->row_info.width = png_ptr->usr_width;
    png_ptr->row_info.channels = png_ptr->usr_channels;
    png_ptr->row_info.bit_depth = png_ptr->usr_bit_depth;
-   png_ptr->row_info.pixel_depth = png_ptr->row_info.bit_depth *
-      png_ptr->row_info.channels;
+	png_ptr->row_info.pixel_depth = (png_byte)(png_ptr->row_info.bit_depth *
+		png_ptr->row_info.channels);
    png_ptr->row_info.rowbytes = ((png_ptr->row_info.width *
       (png_uint_32)png_ptr->row_info.pixel_depth + 7) >> 3);
 
@@ -421,14 +421,14 @@
 #if defined(PNG_WRITE_FLUSH_SUPPORTED)
 /* Set the automatic flush interval or 0 to turn flushing off */
 void
-png_set_flush(png_struct *png_ptr, int nrows)
+png_set_flush(png_structp png_ptr, int nrows)
 {
 	png_ptr->flush_dist = (nrows < 0 ? 0 : nrows);
 }
 
 /* flush the current output buffers now */
 void
-png_write_flush(png_struct *png_ptr)
+png_write_flush(png_structp png_ptr)
 {
 	int wrote_IDAT;
 
@@ -501,7 +501,7 @@
 png_set_filtering(png_structp png_ptr, int filter)
 {
    png_ptr->do_custom_filter = 1;
-   png_ptr->do_filter = filter;
+   png_ptr->do_filter = (png_byte)filter;
 }
 
 void
diff --git a/pngwtran.c b/pngwtran.c
index cd21162..3638761 100644
--- a/pngwtran.c
+++ b/pngwtran.c
@@ -1,10 +1,10 @@
 
 /* pngwtran.c - transforms the data in a row for png writers
 
-	libpng 1.0 beta 2 - version 0.86
+	libpng 1.0 beta 2 - version 0.87
    For conditions of distribution and use, see copyright notice in png.h
 	Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
-   January 10, 1996
+   January 15, 1996
    */
 
 #define PNG_INTERNAL
@@ -78,13 +78,13 @@
                else
                {
                   mask = 0x80;
-                  *dp = v;
+						*dp = (png_byte)v;
 						dp++;
                   v = 0;
                }
             }
             if (mask != 0x80)
-               *dp = v;
+					*dp = (png_byte)v;
             break;
          }
          case 2:
@@ -94,7 +94,7 @@
             int shift;
             png_int_32 i;
             int v;
-            png_byte value;
+				png_byte value;
 
             sp = row;
             dp = row;
@@ -102,12 +102,12 @@
             v = 0;
             for (i = 0; i < row_info->width; i++)
             {
-               value = *sp & 0x3;
+					value = (png_byte)(*sp & 0x3);
                v |= (value << shift);
                if (shift == 0)
                {
                   shift = 6;
-                  *dp = v;
+						*dp = (png_byte)v;
                   dp++;
                   v = 0;
                }
@@ -116,7 +116,7 @@
                sp++;
             }
             if (shift != 6)
-                   *dp = v;
+					*dp = (png_byte)v;
             break;
          }
          case 4:
@@ -134,13 +134,13 @@
             v = 0;
             for (i = 0; i < row_info->width; i++)
             {
-               value = *sp & 0xf;
+					value = (png_byte)(*sp & 0xf);
                v |= (value << shift);
 
                if (shift == 0)
                {
                   shift = 4;
-                  *dp = v;
+						*dp = (png_byte)v;
                   dp++;
                   v = 0;
                }
@@ -150,12 +150,12 @@
                sp++;
             }
             if (shift != 4)
-               *dp = v;
+					*dp = (png_byte)v;
             break;
          }
       }
       row_info->bit_depth = bit_depth;
-      row_info->pixel_depth = bit_depth * row_info->channels;
+		row_info->pixel_depth = (png_uint_16)(bit_depth * row_info->channels);
       row_info->rowbytes =
          ((row_info->width * row_info->pixel_depth + 7) >> 3);
    }
@@ -276,7 +276,8 @@
             {
                png_uint_16 value, v;
 
-               v = ((png_uint_16)(*bp) << 8) + (png_uint_16)(*(bp + 1));
+					v = (png_uint_16)(((png_uint_16)(*bp) << 8) +
+						(png_uint_16)(*(bp + 1)));
                value = 0;
                for (j = shift_start[c]; j > -shift_dec[c]; j -= shift_dec[c])
                {
@@ -285,8 +286,8 @@
                   else
                      value |= (png_uint_16)((v >> (-j)) & (png_uint_16)0xffff);
                }
-               *bp = value >> 8;
-               *(bp + 1) = value & 0xff;
+					*bp = (png_byte)(value >> 8);
+               *(bp + 1) = (png_byte)(value & 0xff);
             }
          }
       }
diff --git a/pngwutil.c b/pngwutil.c
index 1a877f1..39a950d 100644
--- a/pngwutil.c
+++ b/pngwutil.c
@@ -1,10 +1,10 @@
 
 /* pngwutil.c - utilities to write a png file
 
-	libpng 1.0 beta 2 - version 0.86
+	libpng 1.0 beta 2 - version 0.87
    For conditions of distribution and use, see copyright notice in png.h
 	Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.
-   January 10, 1996
+   January 15, 1996
    */
 #define PNG_INTERNAL
 #include "png.h"
@@ -142,15 +142,15 @@
    /* pack the header information into the buffer */
    png_save_uint_32(buf, width);
    png_save_uint_32(buf + 4, height);
-   buf[8] = bit_depth;
-   buf[9] = color_type;
-   buf[10] = compression_type;
-   buf[11] = filter_type;
-   buf[12] = interlace_type;
-   /* save off the relevent information */
-   png_ptr->bit_depth = bit_depth;
-   png_ptr->color_type = color_type;
-   png_ptr->interlaced = interlace_type;
+	buf[8] = (png_byte)bit_depth;
+	buf[9] = (png_byte)color_type;
+	buf[10] = (png_byte)compression_type;
+	buf[11] = (png_byte)filter_type;
+	buf[12] = (png_byte)interlace_type;
+	/* save off the relevent information */
+	png_ptr->bit_depth = (png_byte)bit_depth;
+	png_ptr->color_type = (png_byte)color_type;
+	png_ptr->interlaced = (png_byte)interlace_type;
    png_ptr->width = width;
    png_ptr->height = height;
 
@@ -170,7 +170,7 @@
          png_ptr->channels = 4;
          break;
    }
-   png_ptr->pixel_depth = bit_depth * png_ptr->channels;
+	png_ptr->pixel_depth = (png_byte)(bit_depth * png_ptr->channels);
    png_ptr->rowbytes = ((width * (png_uint_32)png_ptr->pixel_depth + 7) >> 3);
    /* set the usr info, so any transformations can modify it */
    png_ptr->usr_width = png_ptr->width;
@@ -184,7 +184,7 @@
    png_ptr->zstream = (z_stream *)png_malloc(png_ptr, sizeof (z_stream));
    png_ptr->zstream->zalloc = png_zalloc;
    png_ptr->zstream->zfree = png_zfree;
-   png_ptr->zstream->opaque = (voidp)png_ptr;
+   png_ptr->zstream->opaque = (voidpf)png_ptr;
    if (!png_ptr->do_custom_filter)
    {
       if (png_ptr->color_type == 3 || png_ptr->bit_depth < 8)
@@ -476,12 +476,19 @@
 
             old_max = max_output_ptr;
             max_output_ptr = num_output_ptr + 4;
-            if (output_ptr)
-					output_ptr = (png_charpp)png_realloc(png_ptr, output_ptr,
-						max_output_ptr * sizeof (png_charpp),
-						old_max * sizeof (png_charp));
-            else
-					output_ptr = (png_charpp)png_malloc(png_ptr,
+				if (output_ptr)
+				{
+					png_charpp old_ptr;
+
+					old_ptr = output_ptr;
+					output_ptr = (png_charpp)png_large_malloc(png_ptr,
+						max_output_ptr * sizeof (png_charpp));
+					png_memcpy(output_ptr, old_ptr,
+						(png_size_t)(old_max * sizeof (png_charp)));
+					png_large_free(png_ptr, old_ptr);
+				}
+				else
+					output_ptr = (png_charpp)png_large_malloc(png_ptr,
 						max_output_ptr * sizeof (png_charp));
          }
 
@@ -516,7 +523,7 @@
       /* check to see if we need more room */
       if (!png_ptr->zstream->avail_out && ret == Z_OK)
       {
-         /* check to make sure our output array has room */
+			/* check to make sure our output array has room */
 			if (num_output_ptr >= max_output_ptr)
          {
             png_uint_32 old_max;
@@ -524,17 +531,24 @@
             old_max = max_output_ptr;
             max_output_ptr = num_output_ptr + 4;
             if (output_ptr)
-					output_ptr = (png_charpp)png_realloc(png_ptr, output_ptr,
-						max_output_ptr * sizeof (png_charp),
-						old_max * sizeof (png_charp));
-            else
-					output_ptr = (png_charpp)png_malloc(png_ptr,
-						max_output_ptr * sizeof (png_charp));
-         }
+				{
+					png_charpp old_ptr;
 
-         /* save off the data */
-         output_ptr[num_output_ptr] = png_large_malloc(png_ptr,
-            png_ptr->zbuf_size);
+					old_ptr = output_ptr;
+					output_ptr = (png_charpp)png_large_malloc(png_ptr,
+						max_output_ptr * sizeof (png_charpp));
+					png_memcpy(output_ptr, old_ptr,
+						(png_size_t)(old_max * sizeof (png_charp)));
+					png_large_free(png_ptr, old_ptr);
+				}
+				else
+					output_ptr = (png_charpp)png_large_malloc(png_ptr,
+						max_output_ptr * sizeof (png_charp));
+			}
+
+			/* save off the data */
+			output_ptr[num_output_ptr] = png_large_malloc(png_ptr,
+				png_ptr->zbuf_size);
 			png_memcpy(output_ptr[num_output_ptr], png_ptr->zbuf,
             (png_size_t)png_ptr->zbuf_size);
          num_output_ptr++;
@@ -556,7 +570,7 @@
       (png_uint_32)(key_len + text_len + 2));
    /* write key */
    png_write_chunk_data(png_ptr, (png_bytep )key, (png_uint_32)(key_len + 1));
-   buf[0] = compression;
+	buf[0] = (png_byte)compression;
    /* write compression */
    png_write_chunk_data(png_ptr, (png_bytep )buf, (png_uint_32)1);
 
@@ -567,7 +581,7 @@
 		png_large_free(png_ptr, output_ptr[i]);
    }
    if (max_output_ptr)
-      png_free(png_ptr, output_ptr);
+      png_large_free(png_ptr, output_ptr);
    /* write anything left in zbuf */
    if (png_ptr->zstream->avail_out < png_ptr->zbuf_size)
       png_write_chunk_data(png_ptr, png_ptr->zbuf,
@@ -591,7 +605,7 @@
 
    png_save_uint_32(buf, x_pixels_per_unit);
    png_save_uint_32(buf + 4, y_pixels_per_unit);
-   buf[8] = unit_type;
+	buf[8] = (png_byte)unit_type;
 
    png_write_chunk(png_ptr, png_pHYs, buf, (png_uint_32)9);
 }
@@ -608,7 +622,7 @@
 
    png_save_uint_32(buf, x_offset);
    png_save_uint_32(buf + 4, y_offset);
-   buf[8] = unit_type;
+	buf[8] = (png_byte)unit_type;
 
    png_write_chunk(png_ptr, png_oFFs, buf, (png_uint_32)9);
 }
@@ -804,7 +818,7 @@
                if (shift == 0)
                {
                   shift = 7;
-                  *dp++ = d;
+						*dp++ = (png_byte)d;
                   d = 0;
                }
                else
@@ -812,7 +826,7 @@
 
             }
             if (shift != 7)
-               *dp = d;
+					*dp = (png_byte)d;
             break;
          }
          case 2:
@@ -838,14 +852,14 @@
                if (shift == 0)
                {
                   shift = 6;
-                  *dp++ = d;
+						*dp++ = (png_byte)d;
                   d = 0;
                }
                else
                   shift -= 2;
             }
             if (shift != 6)
-                   *dp = d;
+						 *dp = (png_byte)d;
             break;
          }
          case 4:
@@ -871,14 +885,14 @@
                if (shift == 0)
                {
 						shift = 4;
-                  *dp++ = d;
+						*dp++ = (png_byte)d;
                   d = 0;
                }
                else
                   shift -= 4;
             }
             if (shift != 4)
-               *dp = d;
+					*dp = (png_byte)d;
             break;
          }
          default:
@@ -1038,7 +1052,7 @@
 
    if (s1 < mins)
    {
-      mins = s1;
+		mins = s1;
       minf = 1;
    }
 
@@ -1056,12 +1070,11 @@
 
    if (s4 < mins)
    {
-      mins = s4;
       minf = 4;
    }
 
    /* set filter byte */
-   row[0] = minf;
+	row[0] = (png_byte)minf;
 
    /* do filter */
    switch (minf)
@@ -1074,7 +1087,7 @@
          {
             *rp = (png_byte)(((int)*rp - (int)*lp) & 0xff);
          }
-         break;
+			break;
       /* up filter */
       case 2:
          for (i = 0, rp = row + (png_size_t)row_info->rowbytes,
diff --git a/readme.txt b/readme.txt
index 0c444bc..f7ce8e0 100644
--- a/readme.txt
+++ b/readme.txt
@@ -1,4 +1,4 @@
-readme.txt - for libpng 0.86
+readme.txt - for libpng 0.87
 
 This is a bug fix for the second beta version of libpng 1.0, and
 a first try at a progressive (push) reader.  It hasn't been
@@ -60,6 +60,11 @@
 internet: schalnat@group42.com
 CompuServe: 75501,1625
 
+I tend to check my CompuServe account very infrequently, so you may
+want to use the internet account.  If I don't answer your email
+immediately, please be patient.  If you don't receive a reply within
+a week, you may want to write and ask if I got the first email.
+
 Please do not send me general questions about PNG.  Send them to
 the address in the specification.  At the same time, please do
 not send libpng questions to that address, send them to me.  I'll
@@ -70,7 +75,7 @@
 to others, if necessary.
 
 Please do not send suggestions on how to change PNG.  We have
-been discussing PNG for 9 months now, and it is official and
+been discussing PNG for over a year now, and it is official and
 finished.  If you have suggestions for libpng, however, I'll
 gladly listen.  Even if your suggestion is not used for version
 1.0, it may be used later.