[master] Imported from libpng-1.6.1.tar
diff --git a/ANNOUNCE b/ANNOUNCE
index b719f66..cc2064e 100644
--- a/ANNOUNCE
+++ b/ANNOUNCE
@@ -1,5 +1,5 @@
 
-Libpng 1.6.0 - February 14, 2013
+Libpng 1.6.1 - March 28, 2013
 
 This is a public release of libpng, intended for use in production codes.
 
@@ -8,452 +8,89 @@
 Source files with LF line endings (for Unix/Linux) and with a
 "configure" script
 
-   libpng-1.6.0.tar.xz (LZMA-compressed, recommended)
-   libpng-1.6.0.tar.gz
+   libpng-1.6.1.tar.xz (LZMA-compressed, recommended)
+   libpng-1.6.1.tar.gz
 
 Source files with CRLF line endings (for Windows), without the
 "configure" script
 
-   lpng160.7z  (LZMA-compressed, recommended)
-   lpng160.zip
+   lpng161.7z  (LZMA-compressed, recommended)
+   lpng161.zip
 
 Other information:
 
-   libpng-1.6.0-README.txt
-   libpng-1.6.0-LICENSE.txt
+   libpng-1.6.1-README.txt
+   libpng-1.6.1-LICENSE.txt
 
-Changes since the last public release (1.5.7):
+Changes since the last public release (1.6.0):
 
-  Removed machine-generated configure files from the GIT repository (they will
-    continue to appear in the tarball distributions and in the libpng15 and
-    earlier GIT branches).
-  Restored the new 'simplified' API, which was started in libpng-1.5.7beta02
-    but later deleted from libpng-1.5.7beta05.
-  Added example programs and test program pngstest for the new 'simplified' API.
-  Added ANSI-C (C90) headers and require them, and take advantage of the
-    change. Also fixed some of the projects/* and contrib/* files that needed
-    updates for libpng16 and the move of pngvalid.c.
-    With this change the required ANSI-C header files are assumed to exist: the
-    implementation must provide float.h, limits.h, stdarg.h and stddef.h and
-    libpng relies on limits.h and stddef.h existing and behaving as defined
-    (the other two required headers aren't used).  Non-ANSI systems that don't
-    have stddef.h or limits.h will have to provide an appropriate fake
-    containing the relevant types and #defines.
-  The use of FAR/far has been eliminated and the definition of png_alloc_size_t
-    is now controlled by a flag so that 'small size_t' systems can select it
-    if necessary.  Libpng 1.6 may not currently work on such systems -- it
-    seems likely that it will ask 'malloc' for more than 65535 bytes with any
-    image that has a sufficiently large row size (rather than simply failing
-    to read such images).
-  New tools directory containing tools used to generate libpng code.
-  Fixed race conditions in parallel make builds. With higher degrees of
-    parallelism during 'make' the use of the same temporary file names such
-    as 'dfn*' can result in a race where a temporary file from one arm of the
-    build is deleted or overwritten in another arm.  This changes the
-    temporary files for suffix rules to always use $* and ensures that the
-    non-suffix rules use unique file names.
-  Correct configure builds where build and source directories are separate.
-    The include path of 'config.h' was erroneously made relative in pngvalid.c
-    in libpng 1.5.7.
-  Start-up code size improvements, error handler flexibility. These changes
-    alter how the tricky allocation of the initial png_struct and png_info
-    structures are handled. png_info is now handled in pretty much the same
-    way as everything else, except that the allocations handle NULL return
-    silently.  png_struct is changed in a similar way on allocation and on
-    deallocation a 'safety' error handler is put in place (which should never
-    be required).  The error handler itself is changed to permit mismatches
-    in the application and libpng error buffer size; however, this means a
-    silent change to the API to return the jmp_buf if the size doesn't match
-    the size from the libpng compilation; libpng now allocates the memory and
-    this may fail.  Overall these changes result in slight code size
-    reductions; however, this is a reduction in code that is always executed
-    so is particularly valuable.  Overall on a 64-bit system the libpng DLL
-    decreases in code size by 1733 bytes.  pngerror.o increases in size by
-    about 465 bytes because of the new functionality.
-  Added png_convert_to_rfc1123_buffer() and deprecated png_convert_to_rfc1123()
-    to avoid including a spurious buffer in the png_struct.
-  Regenerated configure scripts with automake-1.11.2
-  Eliminated png_info_destroy(). It is now used only in png.c and only calls
-    one other internal function and memset().
-  Enabled png_get_sCAL_fixed() if floating point APIs are enabled. Previously
-    it was disabled whenever internal fixed point arithmetic was selected,
-    which meant it didn't exist even on systems where FP was available but not
-    preferred.
-  Added pngvalid.c compile time checks for const APIs.
-  Implemented 'restrict' for png_info and png_struct. Because of the way
-    libpng works both png_info and png_struct are always accessed via a
-    single pointer.  This means adding C99 'restrict' to the pointer gives
-    the compiler some opportunity to optimize the code.  This change allows
-    that.
-  Moved AC_MSG_CHECKING([if libraries can be versioned]) later to the proper
-    location in configure.ac (Gilles Espinasse).
-  Changed png_memcpy to C assignment where appropriate. Changed all those
-    uses of png_memcpy that were doing a simple assignment to assignments
-    (all those cases where the thing being copied is a non-array C L-value).
-  Added some error checking to png_set_*() routines.
-  Removed the reference to the non-exported function png_memcpy() from
-    example.c.
-  Fixed the Visual C 64-bit build - it requires jmp_buf to be aligned, but
-    it had become misaligned.
-  Revised contrib/pngminus/pnm2png.c to avoid warnings when png_uint_32
-    and unsigned long are of different sizes.
-  Updated manual with description of the simplified API.
-  Fix bug in pngerror.c: some long warnings were being improperly truncated
-    (CVE-2011-3464, bug introduced in libpng-1.5.4).
-  Increased the formatted warning buffer to 192 bytes.
-  Fixed Min/GW uninstall to remove libpng.dll.a
-  Eliminated Intel icc/icl compiler warnings. The Intel (GCC derived)
-    compiler issues slightly different warnings from those issued by the
-    current vesions of GCC. This eliminates those warnings by
-    adding/removing casts and small code rewrites.
-  Updated configure.ac from autoupdate: added --enable-werror option.
-    Also some layout regularization and removal of introduced tab characters
-    (replaced with 3-character indentation).  Obsolete macros identified by
-    autoupdate have been removed; the replacements are all in 2.59 so
-    the pre-req hasn't been changed.  --enable-werror checks for support
-    for -Werror (or the given argument) in the compiler.  This mimics the
-    gcc configure option by allowing -Werror to be turned on safely; without
-    the option the tests written in configure itself fail compilation because
-    they cause compiler warnings.
-  Rewrote autogen.sh to run autoreconf instead of running tools one-by-one.
-  Conditionalize the install rules for MINGW and CYGWIN in CMakeLists.txt and
-    set CMAKE_LIBRARY_OUTPUT_DIRECTORY to "lib" on all platforms (C. Yapp).
-  Freeze libtool files in the 'scripts' directory. This version of autogen.sh
-    attempts to dissuade people from running it when it is not, or should not,
-    be necessary.  In fact, autogen.sh does not work when run in a libpng
-    directory extracted from a tar distribution anymore. You must run it in
-    a GIT clone instead.
-  Added two images to contrib/pngsuite (1-bit and 2-bit transparent grayscale),
-    and renamed three whose names were inconsistent with those in
-    pngsuite/README.txt.
-  Check libtool/libtoolize version number (2.4.2) in configure.ac
-  Moved automake options to AM_INIT_AUTOMAKE in configure.ac
-  Added color-tests, silent-rules (Not yet implemented in Makefile.am) and
-    version checking to configure.ac
-  Cleaned up pngpriv.h to consistently declare all functions and data.
-    Also eliminated PNG_CONST_DATA, which is apparently not needed but we
-    can't be sure until it is gone.
-  Added symbol prefixing that allows all the libpng external symbols
-    to be prefixed (suggested by Reuben Hawkins).
-  Updated "ftbb*.png" list in the owatcom and vstudio projects.
-  Added Z_SOLO for zlib-1.2.6+ and correct pngstest tests
-  Revised INSTALL information about autogen.sh.
-  Updated list of test images in CMakeLists.txt
-  Corrected estimate of error in libpng png_set_rgb_to_gray API.  The API is
-    extremely inaccurate for sRGB conversions because it uses an 8-bit
-    intermediate linear value and it does not use the sRGB transform, so it
-    suffers from the known instability in gamma transforms for values close
-    to 0 (see Poynton).  The net result is that the calculation has a maximum
-    error of 14.99/255; 0.5/255^(1/2.2).  pngstest now uses 15 for the
-    permitted 8-bit error. This may still not be enough because of arithmetic
-    error.
-  Removed some unused arrays (with #ifdef) from png_read_push_finish_row().
-  Fixed a memory overwrite bug in simplified read of RGB PNG with
-    non-linear gamma Also bugs in the error checking in pngread.c and changed
-    quite a lot of the checks in pngstest.c to be correct; either correctly
-    written or not over-optimistic.  The pngstest changes are insufficient to
-    allow all possible RGB transforms to be passed; pngstest cmppixel needs
-    to be rewritten to make it clearer which errors it allows and then changed
-    to permit known inaccuracies.
-  Removed tests for no-longer-used *_EMPTY_PLTE_SUPPORTED from pngstruct.h
-  Fixed fixed/float API export conditionals. 1) If FIXED_POINT or
-    FLOATING_POINT options were switched off, png.h ended up with lone ';'
-    characters.  This is not valid ANSI-C outside a function.  The ';'
-    characters have been moved inside the definition of PNG_FP_EXPORT and
-    PNG_FIXED_EXPORT. 2) If either option was switched off, the declaration
-    of the corresponding functions were completely omitted, even though some
-    of them are still used internally.  The result is still valid, but
-    produces warnings from gcc with some warning options (including -Wall). The
-    fix is to cause png.h to declare the functions with PNG_INTERNAL_FUNCTION
-    when png.h is included from pngpriv.h.
-  Check for invalid palette index while reading paletted PNG.  When one is
-    found, issue a warning.
-  Fixed CVE-2011-3026 buffer overrun bug.  Deal more correctly with the test
-    on iCCP chunk length. Also removed spurious casts that may hide problems
-    on 16-bit systems.
-  Eliminated redundant png_push_read_tEXt|zTXt|iTXt|unknown code from
-    pngpread.c and use the sequential png_handle_tEXt, etc., in pngrutil.c;
-    now that png_ptr->buffer is inaccessible to applications, the special
-    handling is no longer useful.
-  Added PNG_SAFE_LIMITS feature to pnglibconf.dfa, pngpriv.h, and new
-    pngusr.dfa to reset the user limits to safe ones if PNG_SAFE_LIMITS is
-    defined.  To enable, use "CPPFLAGS=-DPNG_SAFE_LIMITS_SUPPORTED=1" on the
-    configure command or put #define PNG_SAFE_LIMITS_SUPPORTED in
-    pnglibconf.h.prebuilt and pnglibconf.h.
-  Added information about the new limits in the manual.
-  Removed unused "current_text" members of png_struct and the png_free()
-    of png_ptr->current_text from pngread.c
-  Added PNG_IMAGE_FLAG_FAST for the benefit of applications that store
-    intermediate files, or intermediate in-memory data, while processing
-    image data with the simplified API.  The option makes the files larger
-    but faster to write and read.  pngstest now uses this by default; this
-    can be disabled with the --slow option.
-  Improved pngstest fine tuning of error numbers, new test file generator.
-    The generator generates images that test the full range of sample values,
-    allow the error numbers in pngstest to be tuned and checked.  makepng
-    also allows generation of images with extra chunks, although this is
-    still work-in-progress.
-  Fixed some bugs in ICC profile writing. The code should now accept
-    all potentially valid ICC profiles and reject obviously invalid ones.
-    It now uses png_error() to do so rather than casually writing a PNG
-    without the necessary color data.
-  Removed whitespace from the end of lines in all source files and scripts.
-  Added tests for invalid palette index while reading and writing.
-  Changed png_inflate() and calling routines to avoid overflow problems.
-    This is an intermediate check-in that solves the immediate problems and
-    introduces one performance improvement (avoiding a copy via png_ptr->zbuf.)
-    Further changes will be made to make ICC profile handling more secure.
-  Added contrib/libtests/tarith.c to test internal arithmetic functions from
-    png.c. This is a libpng maintainer program used to validate changes to the
-    internal arithmetic functions.
-  Made read 'inflate' handling like write 'deflate' handling. The read
-    code now claims and releases png_ptr->zstream, like the write code.
-    The bug whereby the progressive reader failed to release the zstream
-    is now fixed, all initialization is delayed, and the code checks for
-    changed parameters on deflate rather than always calling
-    deflatedEnd/deflateInit.
-  Validate the zTXt strings in pngvalid.
-  Added code to validate the windowBits value passed to deflateInit2().
-    If the call to deflateInit2() is wrong a png_warning will be issued
-    (in fact this is harmless, but the PNG data produced may be sub-optimal).
-  Fixed PNG_LIBPNG_BUILD_BASE_TYPE definition. 
-  Reject all iCCP chunks after the first, even if the first one is invalid.
-  Deflate/inflate was reworked to move common zlib calls into single
-    functions [rw]util.c.  A new shared keyword check routine was also added
-    and the 'zbuf' is no longer allocated on progressive read.  It is now
-    possible to call png_inflate() incrementally.
-  If benign errors are disabled use maximum window on ancilliary inflate.
-    This works round a bug introduced in 1.5.4 where compressed ancillary
-    chunks could end up with a too-small windowBits value in the deflate
-    header.
-  Issue a png_benign_error() instead of png_warning() about bad palette index.
-  In pngtest, treat benign errors as errors if "-strict" is present.
-  Fixed a compiler warning under Cygwin (Windows-7, 32-bit system)
-  Revised example.c to put text strings in a temporary character array
-    instead of directly assigning string constants to png_textp members.
-    This avoids compiler warnings when -Wwrite-strings is enabled.
-  Added output flushing to aid debugging under Visual Studio. Unfortunately
-    this is necessary because the VS2010 output window otherwise simply loses
-    the error messages on error (they weren't flushed to the window before
-    the process exited, apparently!)
-  Added configuration support for benign errors and changed the read
-    default. Also changed some warnings in the iCCP and sRGB handling
-    from to benign errors. Configuration now makes read benign
-    errors warnings and write benign errors to errors by default (thus
-    changing the behavior on read).  The simplified API always forces
-    read benign errors to warnings (regardless of the system default, unless
-    this is disabled in which case the simplified API can't be built.)
-  Work around for duplicate row start calls; added warning messages.
-    This turns on PNG_FLAG_DETECT_UNINITIALIZED to detect app code that
-    fails to call one of the 'start' routines (not enabled in libpng-1.5
-    because it is technically an API change, since it did normally work
-    before.)  It also makes duplicate calls to png_read_start_row (an
-    internal function called at the start of the image read) benign, as
-    they were before changes to use png_inflate_claim. Somehow webkit is
-    causing this to happen; this is probably a mis-feature in the zlib
-    changes so this commit is only a work-round.
-  Removed erroneous setting of DETECT_UNINITIALIZED and added more
-    checks. The code now does a png_error if an attempt is made to do the
-    row initialization twice; this is an application error and it has
-    serious consequences because the transform data in png_struct is
-    changed by each call.
-  Added application error reporting and added chunk names to read
-    benign errors; also added --strict to pngstest - not enabled
-    yet because a warning is produced.
-  Avoid the double gamma correction warning in the simplified API.
-    This allows the --strict option to pass in the pngstest checks
-  Changed chunk handler warnings into benign errors, incrementally load iCCP
-  Added checksum-icc.c to contrib/tools
-  Prevent PNG_EXPAND+PNG_SHIFT doing the shift twice.
-  Recognize known sRGB ICC profiles while reading; prefer writing the
-    iCCP profile over writing the sRGB chunk, controlled by the
-    PNG_sRGB_PROFILE_CHECKS option.
-  Revised png_set_text_2() to avoid potential memory corruption (fixes
-    CVE-2011-3048).
-  Revised scripts/makefile.darwin: use system zlib; remove quotes around
-    architecture list; add missing ppc architecture; add architecture options
-    to shared library link; don't try to create a shared lib based on missing
-    RELEASE variable.
-  Enable png_set_check_for_invalid_index() for both read and write.
-  Removed #ifdef PNG_HANDLE_AS_UNKNOWN_SUPPORTED in pngpriv.h around
-    declaration of png_handle_unknown().
-  Added -lssp_nonshared in a comment in scripts/makefile.freebsd
-    and changed deprecated NOOBJ and NOPROFILE to NO_OBJ and NO_PROFILE.
-  Removed need for -Wno-cast-align with clang.  clang correctly warns on
-    alignment increasing pointer casts when -Wcast-align is passed. This
-    fixes the cases that clang warns about either by eliminating the
-    casts from png_bytep to png_uint_16p (pngread.c), or, for pngrutil.c
-    where the cast is previously verified or pngstest.c where it is OK, by
-    introducing new png_aligncast macros to do the cast in a way that clang
-    accepts.
-  Revised CMakeLists.txt to not attempt to make a symlink under mingw.
-  Made fixes for new optimization warnings from gcc 4.7.0. The compiler
-    performs an optimization which is safe; however it then warns about it.
-    Changing the type of 'palette_number' in pngvalid.c removes the warning.
-  Do not depend upon a GCC feature macro being available for use in generating
-    the linker mapfile symbol prefix.
-  Improved performance of new do_check_palette_indexes() function (only
-    update the value when it actually increases, move test for whether
-    the check is wanted out of the function.
-  Don't check palette indexes if num_palette is 0 (as it can be in MNG files).
-  Revised png_set_keep_unknown_chunks() so num_chunks < 0 means ignore all
-    unknown chunks and all known chunks except for IHDR, PLTE, tRNS, IDAT,
-    and IEND.  Previously it only meant ignore all unknown chunks, the
-    same as num_chunks == 0. Revised png_image_skip_unused_chunks() to
-    provide a list of chunks to be processed instead of a list of chunks to
-    ignore.  Revised contrib/gregbook/readpng2.c accordingly.
-  Removed scripts/makefile.cegcc from the *.zip and *.7z distributions; it
-    depends on configure, which is not included in those archives.
-  Moved scripts/chkfmt to contrib/tools.
-  Changed "a+w" to "u+w" in Makefile.in to fix CVE-2012-3386.
-  Do not compile PNG_DEPRECATED, PNG_ALLOC and PNG_PRIVATE when __GNUC__ < 3.
-  Do not use __restrict when GNUC is <= 3.1
-  Removed references to png_zalloc() and png_zfree() from the manual.
-  Fixed configurations where floating point is completely disabled.  Because
-    of the changes to support symbol prefixing PNG_INTERNAL_FUNCTION declares
-    floating point APIs during libpng builds even if they are completely
-    disabled. This requires the png floating point types (png_double*) to be
-    declared even though the functions are never actually defined.  This
-    change provides a dummy definition so that the declarations work, yet any
-    implementation will fail to compile because of an incomplete type.
-  Re-eliminated the use of strcpy() in pngtest.c.  An unncessary use of
-    strcpy() was accidentally re-introduced in libpng16; this change replaces
-    it with strncpy().
-  Eliminated use of png_sizeof(); use sizeof() instead.
-  Use a consistent style for (sizeof type) and (sizeof (array))
-  Cleanup of png_set_filler().  This function does very different things on
-    read and write.  In libpng 1.6 the two cases can be distinguished and
-    considerable code cleanup, and extra error checking, is possible.  This
-    makes calls on the write side that have no effect be ignored with a
-    png_app_error(), which can be disabled in the app using
-    png_set_benign_errors(), and removes the spurious use of usr_channels
-    on the read side.
-  Insist on autotools 1.12.1 for git builds because there are security issues
-    with 1.12 and insisting on anything less would allow 1.12 to be used.
-  Removed info_ptr->signature[8] from WRITE-only builds.
-  Add some conditions for compiling png_fixed().  This is a small function
-    but it requires "-lm" on some platforms.
-  Cause pngtest --strict to fail on any warning from libpng (not just errors)
-    and cause it not to fail at the comparison step if libpng lacks support
-    for writing chunks that it reads from the input (currently only implemented
-    for compressed text chunks).
-  Make all three "make check" test programs work without READ or WRITE support.
-    Now "make check" will succeed even if libpng is compiled with -DPNG_NO_READ
-    or -DPNG_NO_WRITE.  The tests performed are reduced, but the basic reading
-    and writing of a PNG file is always tested by one or more of the tests.
-  Consistently use strlen(), memset(), memcpy(), and memcmp() instead of the
-    png_strlen(), png_memset(), png_memcpy(), and png_memcmp() macros.
-  Removed the png_sizeof(), png_strlen(), png_memset(), png_memcpy(), and
-    png_memcmp() macros.
-  Work around gcc 3.x and Microsoft Visual Studio 2010 complaints. Both object
-    to the split initialization of num_chunks.
-  Unknown handling fixes and clean up. This adds more correct option
-    control of the unknown handling, corrects the pre-existing bug where
-    the per-chunk 'keep' setting is ignored and makes it possible to skip
-    IDAT chunks in the sequential reader (broken in earlier 1.6 versions).
-    There is a new test program, test-unknown.c, which is a work in progress
-    (not currently part of the test suite).  Comments in the header files now
-    explain how the unknown handling works.
-  Allow fine grain control of unknown chunk APIs. This change allows
-    png_set_keep_unknown_chunks() to be turned off if not required and causes
-    both read and write to behave appropriately (on read this is only possible
-    if the user callback is used to handle unknown chunks).  The change
-    also removes the support for storing unknown chunks in the info_struct
-    if the only unknown handling enabled is via the callback, allowing libpng
-    to be configured with callback reading and none of the unnecessary code.
-  Corrected fix for unknown handling in pngtest. This reinstates the
-    libpng handling of unknown chunks other than vpAg and sTER (including
-    unsafe-to-copy chunks which were dropped before) and eliminates the
-    repositioning of vpAg and sTER in pngtest.png by changing pngtest.png
-    (so the chunks are where libpng would put them).
-  Added "tunknown" test and corrected a logic error in png_handle_unknown()
-    when SAVE support is absent.  Moved the shell test scripts for
-    contrib/libtests from the libpng top directory to contrib/libtests.
-    png_handle_unknown() must always read or skip the chunk, if
-    SAVE_UNKNOWN_CHUNKS is turned off *and* the application does not set
-    a user callback an unknown chunk will not be read, leading to a read
-    error, which was revealed by the "tunknown" test.
-  Cleaned up and corrected ICC profile handling.
-    contrib/libtests/makepng: corrected 'rgb' and 'gray' cases.  profile_error
-    messages could be truncated; made a correct buffer size calculation and
-    adjusted pngerror.c appropriately. png_icc_check_* checking improved;
-    changed the functions to receive the correct color type of the PNG on read
-    or write and check that it matches the color space of the profile (despite
-    what the comments said before, there is danger in assuming the app will
-    cope correctly with an RGB profile on a grayscale image and, since it
-    violates the PNG spec, allowing it is certain to produce inconsistent
-    app behavior and might even cause app crashes.) Check that profiles
-    contain the tags needed to process the PNG (tags all required by the ICC
-    spec). Removed unused PNG_STATIC from pngpriv.h.
-  Fixed the simplified API example programs and improved the error message
-    if the version field is not set.
-  Updated simplified API synopses and description of the png_image structure
-    in the manual.
-  Made makepng and pngtest produce identical PNGs, add "--relaxed" option
-    to pngtest. The "--relaxed" option turns off the benign errors that are
-    enabled by default in pre-RC builds. makepng can now write ICC profiles
-    where the length has not been extended to a multiple of 4, and pngtest
-    now intercepts all libpng errors, allowing the previously-introduced
-    "--strict test" on no warnings to actually work.
-  Added a work around for GCC 4.2 optimization bug.
-  Marked the broken (bad white point) original HP sRGB profiles correctly and
-    correct comments.
-  Added -DZ_SOLO to contrib/pngminim/*/makefile to work with zlib-1.2.7
-  Use /MDd for vstudio debug builds. Also added pngunkown to the vstudio
-    builds, fixed build errors and corrected a minor exit code error in
-    pngvalid if the 'touch' file name is invalid.
-  Add updated WARNING file to projects/vstudio from libpng 1.5/vstudio
-  Fixed build when using #define PNG_NO_READ_GAMMA in png_do_compose() in
-    pngrtran.c (Domani Hannes).
-  Made pngvalid so that it will build outside the libpng source tree.
-  Made PNG_NO_READ_GAMMA switch off interfaces that depend on READ_GAMMA.
-    Prior to 1.6.0 switching off READ_GAMMA did unpredictable things to the
-    interfaces that use it (specifically, png_do_background in 1.4 would
-    simply display composite for grayscale images but do composition
-    with the incorrect arithmetic for color ones). In 1.6 the semantic
-    of -DPNG_NO_READ_GAMMA is changed to simply disable any interface that
-    depends on it; this obliges people who set it to consider whether they
-    really want it off if they happen to use any of the interfaces in
-    question (typically most users who disable it won't).
-  Added the ability for contrib/libtests/makepng.c to make a PNG with just one
-    color. This is useful for debugging pngstest color inaccuracy reports.
-  Made png_user_version_check() ok to use with libpng version 1.10.x and later.
-  Fixed typo in png.c (PNG_SET_CHUNK_MALLOC_MAX should be PNG_CHUNK_MALLOC_MAX)
-    that causes the MALLOC_MAX limit not to work (John Bowler)
-  Change png_warning() to png_app_error() in pngwrite.c and comment the
-    fall-through condition.
-  Change png_warning() to png_app_warning() in png_write_tRNS().
-  Rearranged the ARM-NEON optimizations: Isolated the machine specific code
-    to the hardware subdirectory and added comments to pngrutil.c so that
-    implementors of other optimizations know what to do.
-  Fixed cases of unquoted DESTDIR in Makefile.am
-  Cleaned up whitespace in the synopsis portion of the manpage "libpng.3"
-  Disassembled the version number in scripts/options.awk (necessary for
-    building on SunOs).
-  Made default Zlib compression settings be configurable. This adds #defines to
-    pnglibconf.h to control the defaults.
-  Fixed Windows build issues, enabled ARM compilation. Various warnings issued
-    by earlier versions of GCC fixed for Cygwin and Min/GW (which both use old
-    GCCs.) ARM support is enabled by default in zlib.props (unsupported by
-    Microsoft) and ARM compilation is made possible by deleting the check for
-    x86. The test programs cannot be run because they are not signed.
-  Discontinued distributing libpng-1.x.x.tar.bz2.
-  Discontinued distributing libpng-1.7.0-1.6.0-diff.txt and similar.
-  Rebuilt Makefile.in, configure, etc., with autoconf-2.69 and automake-1.12.5.
-  Fixed 'make distcheck' on SUN OS - libpng.so was not being removed
-  Fixed conceivable but difficult to repro overflow. Also added two test
-    programs to generate and test a PNG which should have the problem.
-  Added overflow detection in png_set_sPLT() and png_set_text_2().
-  Use consistent handling of overflows in text, sPLT and unknown png_set_* APIs
-  Added png_get_palette_max() function.
-  Fixed missing dependency in --prefix builds The intermediate
-    internal 'prefix.h' file can only be generated correctly after
-    pnglibconf.h, however the dependency was not in Makefile.am.  The
-    symptoms are unpredictable depending on the order make chooses to
-    build pngprefix.h and pnglibconf.h, often the error goes unnoticed
-    because there is a system pnglibconf.h to use instead.
-  Enclosed the new png_get_palette_max in #ifdef PNG_GET_PALETTE_MAX_SUPPORTED
-    block, and revised pnglibconf.h and pnglibconf.h.prebuilt accordingly.
+  Made symbol prefixing work with the ARM neon optimizations. Also allow
+    pngpriv.h to be included for preprocessor definitions only, so it can
+    be used in non-C/C++ files. Back ported from libpng 1.7.
+  Made sRGB check numbers consistent.
+  Ported libpng 1.5 options.awk/dfn file handling to 1.6, fixed one bug.
+  Removed cc -E workround, corrected png_get_palette_max API Tested on
+    SUN OS cc 5.9, which demonstrates the tokenization problem previously
+    avoided by using /lib/cpp.  Since all .dfn output is now protected in
+    double quotes unless it is to be macro substituted the fix should
+    work everywhere.
+  Enabled parallel tests - back ported from libpng-1.7.
+  scripts/pnglibconf.dfa formatting improvements back ported from libpng17.
+  Fixed a race condition in the creation of the build 'scripts' directory
+    while building with a parallel make.
+  Use approved/supported Android method to check for NEON, use Linux/POSIX
+    1003.1 API to check /proc/self/auxv avoiding buffer allocation and other
+    library calls (ported from libpng15).
+  Use parentheses more consistently in "#if defined(MACRO)" tests.
+  Folded long lines.
+  Reenabled code to allow zero length PLTE chunks for MNG.
+  Fixed ALIGNED_MEMORY support.
+  Allow run-time ARM NEON checking to be disabled. A new configure option:
+    --enable-arm-neon=always will stop the run-time checks. New checks
+    within arm/arm_init.c will cause the code not to be compiled unless
+    __ARM_NEON__ is set. This should make it fail safe (if someone asks
+    for it on then the build will fail if it can't be done.)
+  Updated the INSTALL document.
+  Revised INSTALL to recommend using CPPFLAGS instead of INCLUDES.
+  Revised scripts/makefile.freebsd to respect ZLIBLIB and ZLIBINC.
+  Revised scripts/dfn.awk to work with the buggy MSYS awk that has trouble
+    with CRLF line endings.
+  Avoid a possible memory leak in contrib/gregbook/readpng.c
+  Better documentation of unknown handling API interactions.
+  Corrected Android builds and corrected libpng.vers with symbol
+    prefixing This adds an API to set optimization options externally,
+    providing an alternative and general solution for the non-portable
+    run-time tests used by the ARM Neon code.  It also makes those tests
+    compile and link on Android.
+  The order of settings vs options in pnglibconf.h is reversed to allow
+    settings to depend on options and options can now set (or override) the
+    defaults for settings.
+  Corrected simplified API default gamma for color-mapped output, added
+    a flag to change default. In 1.6.0 when the simplified API was used
+    to produce color-mapped output from an input image with no gamma
+    information the gamma assumed for the input could be different from
+    that assumed for non-color-mapped output.  In particular 16-bit depth
+    input files were assumed to be sRGB encoded, whereas in the 'direct'
+    case they were assumed to have linear data.  This was an error.  The
+    fix makes the simplified API treat all input files the same way and
+    adds a new flag to the png_image::flags member to allow the
+    application/user to specify that 16-bit files contain sRGB data
+    rather than the default linear.
+  Fixed bugs in the pngpixel and makepng test programs.
+  Fixed CMakelists.txt to allow building a single variant of the library
+    (Claudio Bley):
+  Introduced a PNG_LIB_TARGETS variable that lists all activated library
+    targets.  It is an error if this variable ends up empty, ie. you have
+    to build at least one library variant.
+  Made the *_COPY targets only depend on library targets actually being build.
+  Use PNG_LIB_TARGETS to unify a code path.
+  Changed the CREATE_SYMLINK macro to expect the full path to a file as the
+    first argument. When symlinking the filename component of that path is
+    determined and used as the link target.
+  Use copy_if_different in the CREATE_SYMLINK macro.
+  Eliminated two warnings from the Intel C compiler. The warnings are
+    technically valid, although a reasonable treatment of division would
+    show it to be incorrect.
 
 Send comments/corrections/commendations to png-mng-implement at lists.sf.net
 (subscription required; visit
diff --git a/CHANGES b/CHANGES
index b7e8cca..3825408 100644
--- a/CHANGES
+++ b/CHANGES
@@ -3672,7 +3672,8 @@
   No changes.
 
 Version 1.5.7beta01 [November 4, 2011]
-  Added support for ARM processor (Mans Rullgard)
+  Added support for ARM processor, when decoding all PNG up-filtered rows
+    and any other-filtered rows with 3 or 4 bytes per pixel (Mans Rullgard).
   Fixed bug in pngvalid on early allocation failure; fixed type cast in
     pngmem.c; pngvalid would attempt to call png_error() if the allocation
     of a png_struct or png_info failed. This would probably have led to a
@@ -4381,6 +4382,97 @@
 Version 1.6.0 [February 14, 2013]
   No changes.
 
+Version 1.6.1beta01 [February 16, 2013]
+  Made symbol prefixing work with the ARM neon optimizations. Also allow
+    pngpriv.h to be included for preprocessor definitions only, so it can
+    be used in non-C/C++ files. Back ported from libpng 1.7.
+  Made sRGB check numbers consistent.
+  Ported libpng 1.5 options.awk/dfn file handling to 1.6, fixed one bug.
+  Removed cc -E workround, corrected png_get_palette_max API Tested on
+    SUN OS cc 5.9, which demonstrates the tokenization problem previously
+    avoided by using /lib/cpp.  Since all .dfn output is now protected in
+    double quotes unless it is to be macro substituted the fix should
+    work everywhere.
+  Enabled parallel tests - back ported from libpng-1.7.
+  scripts/pnglibconf.dfa formatting improvements back ported from libpng17.
+  Fixed a race condition in the creation of the build 'scripts' directory
+    while building with a parallel make.
+  Use approved/supported Android method to check for NEON, use Linux/POSIX
+    1003.1 API to check /proc/self/auxv avoiding buffer allocation and other
+    library calls (ported from libpng15).
+
+Version 1.6.1beta02 [February 19, 2013]
+  Use parentheses more consistently in "#if defined(MACRO)" tests.
+  Folded long lines.
+  Reenabled code to allow zero length PLTE chunks for MNG.
+
+Version 1.6.1beta03 [February 22, 2013]
+  Fixed ALIGNED_MEMORY support.
+  Allow run-time ARM NEON checking to be disabled. A new configure option:
+    --enable-arm-neon=always will stop the run-time checks. New checks
+    within arm/arm_init.c will cause the code not to be compiled unless
+    __ARM_NEON__ is set. This should make it fail safe (if someone asks
+    for it on then the build will fail if it can't be done.)
+  Updated the INSTALL document.
+
+Version 1.6.1beta04 [February 27, 2013]
+  Revised INSTALL to recommend using CPPFLAGS instead of INCLUDES.
+  Revised scripts/makefile.freebsd to respect ZLIBLIB and ZLIBINC.
+  Revised scripts/dfn.awk to work with the buggy MSYS awk that has trouble
+    with CRLF line endings.
+
+Version 1.6.1beta05 [March 1, 2013]
+  Avoid a possible memory leak in contrib/gregbook/readpng.c
+
+Version 1.6.1beta06 [March 4, 2013]
+  Better documentation of unknown handling API interactions.
+  Corrected Android builds and corrected libpng.vers with symbol
+    prefixing This adds an API to set optimization options externally,
+    providing an alternative and general solution for the non-portable
+    run-time tests used by the ARM Neon code.  It also makes those tests
+    compile and link on Android.
+  The order of settings vs options in pnglibconf.h is reversed to allow
+    settings to depend on options and options can now set (or override) the
+    defaults for settings.
+
+Version 1.6.1beta07 [March 7, 2013]
+  Corrected simplified API default gamma for color-mapped output, added
+    a flag to change default. In 1.6.0 when the simplified API was used
+    to produce color-mapped output from an input image with no gamma
+    information the gamma assumed for the input could be different from
+    that assumed for non-color-mapped output.  In particular 16-bit depth
+    input files were assumed to be sRGB encoded, whereas in the 'direct'
+    case they were assumed to have linear data.  This was an error.  The
+    fix makes the simplified API treat all input files the same way and
+    adds a new flag to the png_image::flags member to allow the
+    application/user to specify that 16-bit files contain sRGB data
+    rather than the default linear.
+  Fixed bugs in the pngpixel and makepng test programs.
+
+Version 1.6.1beta08 [March 7, 2013]
+  Fixed CMakelists.txt to allow building a single variant of the library
+    (Claudio Bley):
+  Introduced a PNG_LIB_TARGETS variable that lists all activated library
+    targets.  It is an error if this variable ends up empty, ie. you have
+    to build at least one library variant.
+  Made the *_COPY targets only depend on library targets actually being build.
+  Use PNG_LIB_TARGETS to unify a code path.
+  Changed the CREATE_SYMLINK macro to expect the full path to a file as the
+    first argument. When symlinking the filename component of that path is
+    determined and used as the link target.
+  Use copy_if_different in the CREATE_SYMLINK macro.
+
+Version 1.6.1beta09 [March 13, 2013]
+  Eliminated two warnings from the Intel C compiler. The warnings are
+    technically valid, although a reasonable treatment of division would
+    show it to be incorrect.
+
+Version 1.6.1rc01 [March 21, 2013]
+  No changes.
+
+Version 1.6.1 [March 28, 2013]
+  No changes.
+
 Send comments/corrections/commendations to png-mng-implement at lists.sf.net
 (subscription required; visit
 https://lists.sourceforge.net/lists/listinfo/png-mng-implement
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 728a419..1ebf0da 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -35,7 +35,7 @@
 
 set(PNGLIB_MAJOR 1)
 set(PNGLIB_MINOR 6)
-set(PNGLIB_RELEASE 0)
+set(PNGLIB_RELEASE 1)
 set(PNGLIB_NAME libpng${PNGLIB_MAJOR}${PNGLIB_MINOR})
 set(PNGLIB_VERSION ${PNGLIB_MAJOR}.${PNGLIB_MINOR}.${PNGLIB_RELEASE})
 
@@ -139,8 +139,11 @@
 # NOW BUILD OUR TARGET
 include_directories(${CMAKE_CURRENT_SOURCE_DIR} ${ZLIB_INCLUDE_DIR})
 
+unset(PNG_LIB_TARGETS)
+
 if(PNG_SHARED)
   add_library(${PNG_LIB_NAME} SHARED ${libpng_sources})
+  set(PNG_LIB_TARGETS ${PNG_LIB_NAME})
   if(MSVC)
     # msvc does not append 'lib' - do it here to have consistent name
     set_target_properties(${PNG_LIB_NAME} PROPERTIES PREFIX "lib")
@@ -153,6 +156,7 @@
 # does not work without changing name
   set(PNG_LIB_NAME_STATIC ${PNG_LIB_NAME}_static)
   add_library(${PNG_LIB_NAME_STATIC} STATIC ${libpng_sources})
+  list(APPEND PNG_LIB_TARGETS ${PNG_LIB_NAME_STATIC})
   if(MSVC)
     # msvc does not append 'lib' - do it here to have consistent name
     set_target_properties(${PNG_LIB_NAME_STATIC} PROPERTIES PREFIX "lib")
@@ -160,6 +164,12 @@
   target_link_libraries(${PNG_LIB_NAME_STATIC} ${ZLIB_LIBRARY} ${M_LIBRARY})
 endif()
 
+if(NOT PNG_LIB_TARGETS)
+  message(SEND_ERROR
+    "No library variant selected to build. "
+    "Please enable at least one of the following options: PNG_STATIC, PNG_SHARED")
+endif()
+
 if(PNG_SHARED AND WIN32)
   set_target_properties(${PNG_LIB_NAME} PROPERTIES DEFINE_SYMBOL PNG_BUILD_DLL)
 endif()
@@ -222,14 +232,15 @@
   if(WIN32 AND NOT CYGWIN AND NOT MSYS)
     ADD_CUSTOM_COMMAND(
         OUTPUT ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE}   ${CMAKE_CURRENT_BINARY_DIR}/${DEST_FILE}
-        COMMAND ${CMAKE_COMMAND} -E copy        ${CMAKE_CURRENT_BINARY_DIR}/${SRC_FILE} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE}
-        COMMAND ${CMAKE_COMMAND} -E copy        ${CMAKE_CURRENT_BINARY_DIR}/${SRC_FILE} ${CMAKE_CURRENT_BINARY_DIR}/${DEST_FILE}
-        DEPENDS ${PNG_LIB_NAME} ${PNG_LIB_NAME_STATIC}
+        COMMAND ${CMAKE_COMMAND} -E copy_if_different  "${SRC_FILE}" ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE}
+        COMMAND ${CMAKE_COMMAND} -E copy_if_different  "${SRC_FILE}" ${CMAKE_CURRENT_BINARY_DIR}/${DEST_FILE}
+        DEPENDS ${PNG_LIB_TARGETS}
         )
     ADD_CUSTOM_TARGET(${DEST_FILE}_COPY ALL DEPENDS ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE})
   else(WIN32 AND NOT CYGWIN AND NOT MSYS)
-    execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${SRC_FILE} ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
-    execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink ${SRC_FILE} ${DEST_FILE} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
+    get_filename_component(LINK_TARGET "${SRC_FILE}" NAME)
+    execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${LINK_TARGET}" ${CMAKE_LIBRARY_OUTPUT_DIRECTORY}/${DEST_FILE} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
+    execute_process(COMMAND ${CMAKE_COMMAND} -E create_symlink "${LINK_TARGET}" ${DEST_FILE} WORKING_DIRECTORY ${CMAKE_CURRENT_BINARY_DIR})
   endif(WIN32 AND NOT CYGWIN AND NOT MSYS)
 endmacro()
 
@@ -260,7 +271,7 @@
 # SET UP LINKS
 if(PNG_SHARED)
   set_target_properties(${PNG_LIB_NAME} PROPERTIES
-#   VERSION 16.${PNGLIB_RELEASE}.1.6.0
+#   VERSION 16.${PNGLIB_RELEASE}.1.6.1
     VERSION 16.${PNGLIB_RELEASE}.0
     SOVERSION 16
     CLEAN_DIRECT_OUTPUT 1)
@@ -287,40 +298,33 @@
 
 # INSTALL
 if(NOT SKIP_INSTALL_LIBRARIES AND NOT SKIP_INSTALL_ALL )
-  if(PNG_SHARED)
-    install(TARGETS ${PNG_LIB_NAME}
-        ${PNG_EXPORT_RULE}
-        RUNTIME DESTINATION bin
-        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
-        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
+  install(TARGETS ${PNG_LIB_TARGETS}
+      ${PNG_EXPORT_RULE}
+      RUNTIME DESTINATION bin
+      LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
+      ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
 
+  if(PNG_SHARED)
     # Create a symlink for libpng.dll.a => libpng16.dll.a on Cygwin
     if(CYGWIN OR MINGW)
        get_target_property(BUILD_TARGET_LOCATION ${PNG_LIB_NAME} LOCATION_${CMAKE_BUILD_TYPE})
-       get_filename_component(BUILD_TARGET_FILE ${BUILD_TARGET_LOCATION} NAME)
-       CREATE_SYMLINK(${BUILD_TARGET_FILE} libpng${CMAKE_IMPORT_LIBRARY_SUFFIX})
+       CREATE_SYMLINK(${BUILD_TARGET_LOCATION} libpng${CMAKE_IMPORT_LIBRARY_SUFFIX})
        install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_IMPORT_LIBRARY_SUFFIX}
          DESTINATION ${CMAKE_INSTALL_LIBDIR})
     endif(CYGWIN OR MINGW)
 
     if(NOT WIN32)
       get_target_property(BUILD_TARGET_LOCATION ${PNG_LIB_NAME} LOCATION_${CMAKE_BUILD_TYPE})
-      get_filename_component(BUILD_TARGET_FILE ${BUILD_TARGET_LOCATION} NAME)
-      CREATE_SYMLINK(${BUILD_TARGET_FILE} libpng${CMAKE_SHARED_LIBRARY_SUFFIX})
+      CREATE_SYMLINK(${BUILD_TARGET_LOCATION} libpng${CMAKE_SHARED_LIBRARY_SUFFIX})
       install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_SHARED_LIBRARY_SUFFIX}
          DESTINATION ${CMAKE_INSTALL_LIBDIR})
     endif(NOT WIN32)
   endif(PNG_SHARED)
 
   if(PNG_STATIC)
-    install(TARGETS ${PNG_LIB_NAME_STATIC}
-        ${PNG_EXPORT_RULE}
-        LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
-        ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR})
     if(NOT WIN32 OR CYGWIN OR MINGW)
       get_target_property(BUILD_TARGET_LOCATION ${PNG_LIB_NAME_STATIC} LOCATION_${CMAKE_BUILD_TYPE})
-      get_filename_component(BUILD_TARGET_FILE ${BUILD_TARGET_LOCATION} NAME)
-      CREATE_SYMLINK(${BUILD_TARGET_FILE} libpng${CMAKE_STATIC_LIBRARY_SUFFIX})
+      CREATE_SYMLINK(${BUILD_TARGET_LOCATION} libpng${CMAKE_STATIC_LIBRARY_SUFFIX})
       install(FILES ${CMAKE_CURRENT_BINARY_DIR}/libpng${CMAKE_STATIC_LIBRARY_SUFFIX}
          DESTINATION ${CMAKE_INSTALL_LIBDIR})
     endif(NOT WIN32 OR CYGWIN OR MINGW)
diff --git a/INSTALL b/INSTALL
index 2644c7a..f987378 100644
--- a/INSTALL
+++ b/INSTALL
@@ -41,19 +41,33 @@
 wherever you got libpng.  zlib can be placed in another directory,
 at the same level as libpng.
 
+If your system already has a preinstalled zlib you will still need
+to have access to the zlib.h and zconf.h include files that
+correspond to the version of zlib that's installed.
+
+If you wish to test with a particular zlib that is not first in the
+standard library search path, put ZLIBLIB, ZLIBINC, CPPFLAGS, LDFLAGS,
+and LD_LIBRARY_PATH in your environment before running "make test"
+or "make distcheck":
+
+ZLIBLIB=/path/to/lib export ZLIBLIB
+ZLIBINC=/path/to/include export ZLIBINC
+CPPFLAGS="-I$ZLIBINC" export CPPFLAGS
+LDFLAGS="-L$ZLIBLIB" export LDFLAGS
+LD_LIBRARY_PATH="$ZLIBLIB:$LD_LIBRARY_PATH" export LD_LIBRARY_PATH
+
+If you are using one of the makefile scripts, put ZLIBLIB and ZLIBINC
+in your environment and type "make ZLIBLIB=$ZLIBLIB ZLIBINC=$ZLIBINC test".
+
 If you want to use "cmake" (see www.cmake.org), type
 
    cmake . -DCMAKE_INSTALL_PREFIX=/path
    make
    make install
 
-If your system already has a preinstalled zlib you will still need
-to have access to the zlib.h and zconf.h include files that
-correspond to the version of zlib that's installed.
-
 You can rename the directories that you downloaded (they
-might be called "libpng-x.y.z" or "libpngNN" and "zlib-1.2.5"
-or "zlib125") so that you have directories called "zlib" and "libpng".
+might be called "libpng-x.y.z" or "libpngNN" and "zlib-1.2.7"
+or "zlib127") so that you have directories called "zlib" and "libpng".
 
 Your directory structure should look like this:
 
@@ -71,6 +85,7 @@
              depcomp, install-sh, mkinstalldirs, test-pngtest.sh
           contrib
              gregbook
+             libtests
              pngminim
              pngminus
              pngsuite
diff --git a/LICENSE b/LICENSE
index e95d359..d86a7da 100644
--- a/LICENSE
+++ b/LICENSE
@@ -10,7 +10,7 @@
 
 This code is released under the libpng license.
 
-libpng versions 1.2.6, August 15, 2004, through 1.6.0, February 14, 2013, are
+libpng versions 1.2.6, August 15, 2004, through 1.6.1, March 28, 2013, are
 Copyright (c) 2004, 2006-2012 Glenn Randers-Pehrson, and are
 distributed according to the same disclaimer and license as libpng-1.2.5
 with the following individual added to the list of Contributing Authors
@@ -108,4 +108,4 @@
 
 Glenn Randers-Pehrson
 glennrp at users.sourceforge.net
-February 14, 2013
+March 28, 2013
diff --git a/Makefile.am b/Makefile.am
index ca44c68..a2cd2c1 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -7,24 +7,44 @@
 ACLOCAL_AMFLAGS = -I scripts
 
 # test programs - run on make check, make distcheck
-TESTS_ENVIRONMENT= srcdir=$(srcdir)
-check_PROGRAMS= pngtest
+check_PROGRAMS= pngtest pngunknown pngstest pngvalid
+
 pngtest_SOURCES = pngtest.c
 pngtest_LDADD = libpng@PNGLIB_MAJOR@@PNGLIB_MINOR@.la
-TESTS = test-pngtest.sh
 
-# Only do the following if the contrib directory is present.
-check_PROGRAMS+= pngvalid pngstest pngunknown
 pngvalid_SOURCES = contrib/libtests/pngvalid.c
-pngstest_SOURCES = contrib/libtests/pngstest.c
-pngunknown_SOURCES = contrib/libtests/pngunknown.c
 pngvalid_LDADD = libpng@PNGLIB_MAJOR@@PNGLIB_MINOR@.la
+
+pngstest_SOURCES = contrib/libtests/pngstest.c
 pngstest_LDADD = libpng@PNGLIB_MAJOR@@PNGLIB_MINOR@.la
+
+pngunknown_SOURCES = contrib/libtests/pngunknown.c
 pngunknown_LDADD = libpng@PNGLIB_MAJOR@@PNGLIB_MINOR@.la
-TESTS += contrib/libtests/test-pngunknown.sh\
-	 contrib/libtests/test-pngvalid-simple.sh\
-	 contrib/libtests/test-pngvalid-full.sh\
-	 contrib/libtests/test-pngstest.sh
+
+# Generally these are single line shell scripts to run a test with a particular
+# set of parameters:
+TESTS =\
+   tests/pngtest\
+   tests/pngvalid-gamma-16-to-8 tests/pngvalid-gamma-alpha-mode\
+   tests/pngvalid-gamma-background tests/pngvalid-gamma-expand16-alpha-mode\
+   tests/pngvalid-gamma-expand16-background\
+   tests/pngvalid-gamma-expand16-transform tests/pngvalid-gamma-sbit\
+   tests/pngvalid-gamma-threshold tests/pngvalid-gamma-transform\
+   tests/pngvalid-progressive-interlace-size\
+   tests/pngvalid-progressive-interlace-standard\
+   tests/pngvalid-progressive-interlace-transform\
+   tests/pngvalid-progressive-standard tests/pngvalid-standard\
+   tests/pngstest-0g01 tests/pngstest-0g02 tests/pngstest-0g04\
+   tests/pngstest-0g08 tests/pngstest-0g16 tests/pngstest-2c08\
+   tests/pngstest-2c16 tests/pngstest-3p01 tests/pngstest-3p02\
+   tests/pngstest-3p04 tests/pngstest-3p08 tests/pngstest-4a08\
+   tests/pngstest-4a16 tests/pngstest-6a08 tests/pngstest-6a16\
+   tests/pngstest-error tests/pngunknown-IDAT\
+   tests/pngunknown-discard tests/pngunknown-if-safe tests/pngunknown-sAPI\
+   tests/pngunknown-sTER tests/pngunknown-save tests/pngunknown-vpAg
+
+# These tests are expected, and required, to fail:
+XFAIL_TESTS = tests/pngstest-error
 
 # man pages
 dist_man_MANS= libpng.3 libpngpf.3 png.5
@@ -77,12 +97,14 @@
 pkgconfigdir = @pkgconfigdir@
 pkgconfig_DATA = libpng@PNGLIB_MAJOR@@PNGLIB_MINOR@.pc
 
-#extra source distribution files.
+# Extra source distribution files, '${srcdir}' is used below to stop build files
+# from those directories being included.  This only works if the configure is
+# not done in the source directory!
 EXTRA_DIST= \
 	ANNOUNCE CHANGES INSTALL LICENSE README TODO \
 	pngtest.png pngbar.png pngnow.png pngbar.jpg autogen.sh \
 	${srcdir}/contrib ${srcdir}/projects ${srcdir}/scripts \
-	$(TESTS) \
+	$(TESTS) $(XFAIL_TESTS) tests/pngstest \
 	CMakeLists.txt example.c libpng-manual.txt
 
 SCRIPT_CLEANFILES=scripts/*.out scripts/*.chk scripts/pnglibconf.dfn
@@ -180,16 +202,14 @@
 endif
 
 .dfn.out:
-	rm -f $@ $*.c $*.tf[123]
-	test -d scripts || mkdir scripts
+	rm -f $@ $*.c $*.tf[12]
+	test -d scripts || mkdir scripts || test -d scripts
 	echo '#include "$<"' >$*.c
 	$(DFNCPP) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES)\
 	    $(CPPFLAGS) $(SYMBOL_CFLAGS) $*.c > $*.tf1
-	$(SED) -n -e 's|^.*PNG_DEFN_MAGIC *-\(.*\)- *PNG_DEFN_END.*$$|\1|p'\
-	    $*.tf1 >$*.tf2
-	$(SED) -e 's| *PNG_JOIN *||g' -e 's| *$$||' $*.tf2 >$*.tf3
-	rm -f $*.c $*.tf[12]
-	mv $*.tf3 $@
+	$(AWK) -f "${srcdir}/scripts/dfn.awk" out="$*.tf2" $*.tf1 1>&2
+	rm -f $*.c $*.tf1
+	mv $*.tf2 $@
 
 # The .dfn file for pnglibconf.h is machine generated
 pnglibconf.dfn: scripts/pnglibconf.dfa scripts/options.awk pngconf.h pngusr.dfa $(DFA_XTRA)
diff --git a/README b/README
index 295ee1f..fcf8f3f 100644
--- a/README
+++ b/README
@@ -1,4 +1,4 @@
-README for libpng version 1.6.0 - February 14, 2013 (shared library 16.0)
+README for libpng version 1.6.1 - March 28, 2013 (shared library 16.0)
 See the note about version numbers near the top of png.h
 
 See INSTALL for instructions on how to install libpng.
diff --git a/arm/arm_init.c b/arm/arm_init.c
index 8925f35..795dd49 100644
--- a/arm/arm_init.c
+++ b/arm/arm_init.c
@@ -1,54 +1,190 @@
 
 /* arm_init.c - NEON optimised filter functions
  *
- * Copyright (c) 2012 Glenn Randers-Pehrson
+ * Copyright (c) 2013 Glenn Randers-Pehrson
  * Written by Mans Rullgard, 2011.
- * Last changed in libpng 1.5.14 [(PENDING RELEASE)]
+ * Last changed in libpng 1.5.15 [March 28, 2013]
  *
  * This code is released under the libpng license.
  * For conditions of distribution and use, see the disclaimer
  * and license in png.h
  */
+/* Below, after checking __linux__, various non-C90 POSIX 1003.1 functions are
+ * called.
+ */
+#define _POSIX_SOURCE 1
+
 #include "../pngpriv.h"
 
-/* __arm__ is defined by GCC, MSVC defines _M_ARM to the ARM version number */
-#if defined __linux__ && defined __arm__
-#include <stdio.h>
+#ifdef PNG_ARM_NEON_SUPPORTED
+#ifdef PNG_ARM_NEON_CHECK_SUPPORTED /* Do run-time checks */
+#include <signal.h> /* for sig_atomic_t */
+
+#ifdef __ANDROID__
+/* Linux provides access to information about CPU capabilites via
+ * /proc/self/auxv, however Android blocks this while still claiming to be
+ * Linux.  The Andoid NDK, however, provides appropriate support.
+ *
+ * Documentation: http://www.kandroid.org/ndk/docs/CPU-ARM-NEON.html
+ */
+#include <cpu-features.h>
+
+static int
+png_have_neon(png_structp png_ptr)
+{
+   /* This is a whole lot easier than the mess below, however it is probably
+    * implemented as below, therefore it is better to cache the result (these
+    * function calls may be slow!)
+    */
+   PNG_UNUSED(png_ptr)
+   return android_getCpuFamily() == ANDROID_CPU_FAMILY_ARM &&
+      (android_getCpuFeatures() & ANDROID_CPU_ARM_FEATURE_NEON) != 0;
+}
+#elif defined(__linux__)
+/* The generic __linux__ implementation requires reading /proc/self/auxv and
+ * looking at each element for one that records NEON capabilities.
+ */
+#include <unistd.h> /* for POSIX 1003.1 */
+#include <errno.h>  /* for EINTR */
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
 #include <elf.h>
 #include <asm/hwcap.h>
 
-static int png_have_hwcap(unsigned cap)
+/* A read call may be interrupted, in which case it returns -1 and sets errno to
+ * EINTR if nothing was done, otherwise (if something was done) a partial read
+ * may result.
+ */
+static size_t
+safe_read(png_structp png_ptr, int fd, void *buffer_in, size_t nbytes)
 {
-   FILE *f = fopen("/proc/self/auxv", "r");
-   Elf32_auxv_t aux;
-   int have_cap = 0;
+   size_t ntotal = 0;
+   char *buffer = png_voidcast(char*, buffer_in);
 
-   if (!f)
-      return 0;
-
-   while (fread(&aux, sizeof(aux), 1, f) > 0)
+   while (nbytes > 0)
    {
-      if (aux.a_type == AT_HWCAP &&
-          aux.a_un.a_val & cap)
+      unsigned int nread;
+      int iread;
+
+      /* Passing nread > INT_MAX to read is implementation defined in POSIX
+       * 1003.1, therefore despite the unsigned argument portable code must
+       * limit the value to INT_MAX!
+       */
+      if (nbytes > INT_MAX)
+         nread = INT_MAX;
+
+      else
+         nread = (unsigned int)/*SAFE*/nbytes;
+
+      iread = read(fd, buffer, nread);
+
+      if (iread == -1)
       {
-         have_cap = 1;
-         break;
+         /* This is the devil in the details, a read can terminate early with 0
+          * bytes read because of EINTR, yet it still returns -1 otherwise end
+          * of file cannot be distinguished.
+          */
+         if (errno != EINTR)
+         {
+            png_warning(png_ptr, "/proc read failed");
+            return 0; /* I.e. a permanent failure */
+         }
+      }
+
+      else if (iread < 0)
+      {
+         /* Not a valid 'read' result: */
+         png_warning(png_ptr, "OS /proc read bug");
+         return 0;
+      }
+
+      else if (iread > 0)
+      {
+         /* Continue reading until a permanent failure, or EOF */
+         buffer += iread;
+         nbytes -= (unsigned int)/*SAFE*/iread;
+         ntotal += (unsigned int)/*SAFE*/iread;
+      }
+
+      else
+         return ntotal;
+   }
+
+   return ntotal; /* nbytes == 0 */
+}
+
+static int
+png_have_neon(png_structp png_ptr)
+{
+   int fd = open("/proc/self/auxv", O_RDONLY);
+   Elf32_auxv_t aux;
+
+   /* Failsafe: failure to open means no NEON */
+   if (fd == -1)
+   {
+      png_warning(png_ptr, "/proc/self/auxv open failed");
+      return 0;
+   }
+
+   while (safe_read(png_ptr, fd, &aux, sizeof aux) == sizeof aux)
+   {
+      if (aux.a_type == AT_HWCAP && (aux.a_un.a_val & HWCAP_NEON) != 0)
+      {
+         close(fd);
+         return 1;
       }
    }
 
-   fclose(f);
-
-   return have_cap;
+   close(fd);
+   return 0;
 }
-#endif /* __linux__ && __arm__ */
+#else
+   /* We don't know how to do a run-time check on this system */
+#  error "no support for run-time ARM NEON checks"
+#endif /* OS checks */
+#endif /* PNG_ARM_NEON_CHECK_SUPPORTED */
+
+#ifndef PNG_ALIGNED_MEMORY_SUPPORTED
+#  error "ALIGNED_MEMORY is required; set: -DPNG_ALIGNED_MEMORY_SUPPORTED"
+#endif
 
 void
 png_init_filter_functions_neon(png_structp pp, unsigned int bpp)
 {
-#ifdef __arm__
-#ifdef __linux__
-   if (!png_have_hwcap(HWCAP_NEON))
-      return;
+#ifdef PNG_ARM_NEON_API_SUPPORTED
+   switch ((pp->options >> PNG_ARM_NEON) & 3)
+   {
+      case PNG_OPTION_UNSET:
+         /* Allow the run-time check to execute if it has been enabled -
+          * thus both API and CHECK can be turned on.  If it isn't supported
+          * this case will fall through to the 'default' below, which just
+          * returns.
+          */
+#endif /* PNG_ARM_NEON_API_SUPPORTED */
+#ifdef PNG_ARM_NEON_CHECK_SUPPORTED
+         {
+            static volatile sig_atomic_t no_neon = -1; /* not checked */
+
+            if (no_neon < 0)
+               no_neon = !png_have_neon(pp);
+
+            if (no_neon)
+               return;
+         }
+#ifdef PNG_ARM_NEON_API_SUPPORTED
+         break;
+#endif
+#endif /* PNG_ARM_NEON_CHECK_SUPPORTED */
+#ifdef PNG_ARM_NEON_API_SUPPORTED
+      case PNG_OPTION_ON:
+         /* Option turned on */
+         break;
+
+      default: /* OFF or INVALID */
+         return;
+   }
 #endif
 
    /* IMPORTANT: any new external functions used here must be declared using
@@ -79,8 +215,5 @@
       pp->read_filter[PNG_FILTER_VALUE_PAETH-1] =
           png_read_filter_row_paeth4_neon;
    }
-#else
-   PNG_UNUSED(pp)
-   PNG_UNUSED(bpp)
-#endif
 }
+#endif /* FILTER_OPTIMIZATIONS && __arm__ && __ARM_NEON__ */
diff --git a/arm/filter_neon.S b/arm/filter_neon.S
index 4aa500c..72420a9 100644
--- a/arm/filter_neon.S
+++ b/arm/filter_neon.S
@@ -10,7 +10,14 @@
  * and license in png.h
  */
 
-#ifdef __arm__
+/* This is required to get the symbol renames, which are #defines, and also
+ * includes the value of PNG_FILTER_OPTIMIZATIONS.
+ */
+#define PNG_VERSION_INFO_ONLY
+#include "../pngpriv.h"
+
+#if defined(PNG_FILTER_OPTIMIZATIONS) && defined(__arm__) && \
+   defined(__ARM_NEON__)
 #if defined(__linux__) && defined(__ELF__)
 .section .note.GNU-stack,"",%progbits /* mark stack as non-executable */
 #endif
@@ -225,4 +232,4 @@
 
         pop             {r4,pc}
 endfunc
-#endif
+#endif /* FILTER_OPTIMIZATIONS && __arm__ && __ARM_NEON__ */
diff --git a/autogen.sh b/autogen.sh
index b6db4eb..decf0f5 100755
--- a/autogen.sh
+++ b/autogen.sh
@@ -74,7 +74,7 @@
 #
 # The autotools generated files:
 libpng_autotools_files="Makefile.in aclocal.m4 config.guess config.h.in\
-   config.sub configure depcomp install-sh ltmain.sh missing"
+   config.sub configure depcomp install-sh ltmain.sh missing test-driver"
 #
 # These are separate because 'maintainer-clean' does not remove them.
 libpng_libtool_files="scripts/libtool.m4 scripts/ltoptions.m4\
diff --git a/configure.ac b/configure.ac
index e6ff077..1745d31 100644
--- a/configure.ac
+++ b/configure.ac
@@ -18,7 +18,7 @@
 
 dnl Version number stuff here:
 
-AC_INIT([libpng],[1.6.0],[png-mng-implement@lists.sourceforge.net])
+AC_INIT([libpng],[1.6.1],[png-mng-implement@lists.sourceforge.net])
 AC_CONFIG_MACRO_DIR([scripts])
 
 # libpng does not follow GNU file name conventions (hence 'foreign')
@@ -26,9 +26,11 @@
 # silent-rules requires automake 1.11 or later
 # dist-xz requires automake 1.11 or later
 # 1.12.2 fixes a security issue in 1.11.2 and 1.12.1
-AM_INIT_AUTOMAKE([1.12.2 foreign dist-xz color-tests silent-rules serial-tests])
-# was:
-# AM_INIT_AUTOMAKE
+# 1.13 is required for parallel tests
+AM_INIT_AUTOMAKE([1.13 foreign dist-xz color-tests silent-rules])
+# The following line causes --disable-maintainer-mode to be the default to
+# configure, this is necessary because libpng distributions cannot rely on the
+# time stamps of the autotools generated files being correct
 AM_MAINTAINER_MODE
 
 dnl configure.ac and Makefile.am expect automake 1.11.2 or a compatible later
@@ -37,10 +39,10 @@
 dnl AM_PREREQ([1.11.2])
 dnl stop configure from automagically running automake
 
-PNGLIB_VERSION=1.6.0
+PNGLIB_VERSION=1.6.1
 PNGLIB_MAJOR=1
 PNGLIB_MINOR=6
-PNGLIB_RELEASE=0
+PNGLIB_RELEASE=1
 
 dnl End of version number stuff
 
@@ -53,8 +55,7 @@
 AM_PROG_AS
 LT_PATH_LD
 AC_PROG_CPP
-AC_CHECK_TOOL(SED, sed, :)
-AC_CHECK_TOOL(AWK, awk, :)
+AC_PROG_AWK
 AC_PROG_INSTALL
 AC_PROG_LN_S
 AC_PROG_MAKE_SET
@@ -64,30 +65,23 @@
 LT_INIT([win32-dll])
 LT_PREREQ([2.4.2])
 
-# On Solaris 10 and 12 CPP gets set to cc -E, however this still
-# does some input parsing.  We need strict ANSI-C style tokenization,
-# check this:
-AC_REQUIRE_CPP
-AC_MSG_CHECKING([for a C preprocessor that does not parse its input])
-AC_PREPROC_IFELSE([AC_LANG_SOURCE([[1.1.1 16BIT]])],
-   [DFNCPP="$CPP"],
-   [  DFNCPP=""
-      sav_CPP="$CPP"
-      for CPP in "${CC-cc} -E" "${CC-cc} -E -traditional-cpp" "/lib/cpp" "cpp"
-      do
-         AC_PREPROC_IFELSE([AC_LANG_SOURCE([[1.1.1 16BIT]])],
-            [DFNCPP="$CPP"]
-            [break],,)
-      done
-      CPP="$sav_CPP"
-   ])
-if test -n "$DFNCPP"; then
-   AC_MSG_RESULT([$DFNCPP])
-   AC_SUBST(DFNCPP)
+# Some awks crash when confronted with pnglibconf.dfa, do a test run now
+# to make sure this doesn't happen
+AC_MSG_CHECKING([that AWK works])
+if ${AWK} -f ${srcdir}/scripts/options.awk out="/dev/null" version=search\
+   ${srcdir}/pngconf.h ${srcdir}/scripts/pnglibconf.dfa\
+   ${srcdir}/pngusr.dfa 1>&2
+then
+   AC_MSG_RESULT([ok])
 else
-   AC_MSG_FAILURE([not found], 1)
+   AC_MSG_FAILURE([failed], 1)
 fi
 
+# This is a remnant of the old cc -E validation, where it may have been
+# necessary to use a different preprocessor for .dfn files
+DFNCPP="$CPP"
+AC_SUBST(DFNCPP)
+
 # -Werror cannot be passed to GCC in CFLAGS because configure will fail (it
 # checks the compiler with a program that generates a warning), add the
 # following option to deal with this
@@ -115,7 +109,6 @@
 
 # Checks for header files.
 AC_HEADER_STDC
-AC_CHECK_HEADERS([malloc.h stdlib.h string.h strings.h])
 
 # Checks for typedefs, structures, and compiler characteristics.
 AC_C_CONST
@@ -239,15 +232,29 @@
 AC_SUBST([AM_CCASFLAGS], [-Wa,--noexecstack])
 
 AC_ARG_ENABLE([arm-neon],
-   AS_HELP_STRING([[[--enable-arm-neon]]], [Enable ARM NEON optimizations]),
-   [if test "${enableval}" = "yes"; then
-      AC_DEFINE([PNG_FILTER_OPTIMIZATIONS],
-                [png_init_filter_functions_neon],
-                [ARM NEON filter initialization function])
-      AC_DEFINE([PNG_ALIGNED_MEMORY_SUPPORTED], [1],
-                [Align row buffers])
-    fi])
-AM_CONDITIONAL([PNG_ARM_NEON], [test "${enable_arm_neon:-no}" = yes])
+   AS_HELP_STRING([[[--enable-arm-neon]]],
+      [Enable ARM NEON optimizations: =off, check, api, on:]
+      [off: disable the optimizations; check: use internal checking code]
+      [(deprecated and poorly supported); api: disable by default, enable by]
+      [a call to png_set_option; on: turn on unconditionally.  The]
+      [default is 'on' if __ARM_NEON__ is defined, otherwise 'off'.]),
+   [case "$enableval" in
+      off)
+         AC_DEFINE([PNG_NO_ARM_NEON], [],
+                   [Disable ARM Neon optimizations]);;
+      check)
+         AC_DEFINE([PNG_ARM_NEON_CHECK_SUPPORTED], [],
+                   [Check for ARM Neon support at run-time]);;
+      api)
+         AC_DEFINE([PNG_ARM_NEON_API_SUPPORTED], [],
+                   [Turn on ARM Neon optimizations at run-time]);;
+      on)
+         AC_DEFINE([PNG_ARM_NEON_SUPPORTED], [],
+                   [Enable ARM Neon optimizations]);;
+      *)
+         AC_MSG_ERROR([arm-neon: option value required (off/check/api/on)]);;
+   esac])
+AM_CONDITIONAL([PNG_ARM_NEON], [test "${enable_arm_neon:-no}" != "no"])
 
 AC_MSG_NOTICE([[Extra options for compiler: $PNG_COPTS]])
 
diff --git a/contrib/examples/pngpixel.c b/contrib/examples/pngpixel.c
index d82ef83..4104760 100644
--- a/contrib/examples/pngpixel.c
+++ b/contrib/examples/pngpixel.c
@@ -37,7 +37,7 @@
     * bytes wide.  Since the row fitted into memory, however, the following must
     * work:
     */
-   png_uint_32 bit_offset_hi = bit_depth * ((x >> 6) * channels + c);
+   png_uint_32 bit_offset_hi = bit_depth * ((x >> 6) * channels);
    png_uint_32 bit_offset_lo = bit_depth * ((x & 0x3f) * channels + c);
 
    row = (png_const_bytep)(((PNG_CONST png_byte (*)[8])row) + bit_offset_hi);
diff --git a/contrib/gregbook/readpng.c b/contrib/gregbook/readpng.c
index 0b75fac..f5e1fb1 100644
--- a/contrib/gregbook/readpng.c
+++ b/contrib/gregbook/readpng.c
@@ -215,6 +215,10 @@
      * libpng function */
 
     if (setjmp(png_jmpbuf(png_ptr))) {
+        free(image_data);
+        image_data = NULL;
+        free(row_pointers);
+        row_pointers = NULL;
         png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
         return NULL;
     }
diff --git a/contrib/libtests/makepng.c b/contrib/libtests/makepng.c
index 7f85db9..9f11b29 100644
--- a/contrib/libtests/makepng.c
+++ b/contrib/libtests/makepng.c
@@ -2,7 +2,7 @@
  *
  * Copyright (c) 2013 John Cunningham Bowler
  *
- * Last changed in libpng 1.6.0 [February 14, 2013]
+ * Last changed in libpng 1.6.1 [March 28, 2013]
  *
  * This code is released under the libpng license.
  * For conditions of distribution and use, see the disclaimer
@@ -83,7 +83,7 @@
 #include <math.h>
 #include <errno.h>
 
-#if (defined HAVE_CONFIG_H) && !(defined PNG_NO_CONFIG_H)
+#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
 #  include <config.h>
 #endif
 
@@ -440,7 +440,7 @@
       /* Palette with fixed color: the image rows are all 0 and the image width
        * is 16.
        */
-      memset(row, rowbytes, 0);
+      memset(row, 0, rowbytes);
    }
 
    else if (colors[0] == channels_of_type(color_type))
@@ -624,8 +624,8 @@
 
             gamma_table[0] = 0;
 
-            for (i=0; i<255; ++i)
-               gamma_table[i] = (png_byte)floor(pow(i/255.,conv) * 255 + 127.5);
+            for (i=1; i<255; ++i)
+               gamma_table[i] = (png_byte)floor(pow(i/255.,conv) * 255 + .5);
 
             gamma_table[255] = 255;
          }
diff --git a/contrib/libtests/pngstest.c b/contrib/libtests/pngstest.c
index c2a2b2b..35e7c87 100644
--- a/contrib/libtests/pngstest.c
+++ b/contrib/libtests/pngstest.c
@@ -3,7 +3,7 @@
  *
  * Copyright (c) 2013 John Cunningham Bowler
  *
- * Last changed in libpng 1.6.0 [February 14, 2013]
+ * Last changed in libpng 1.6.1 [March 28, 2013]
  *
  * This code is released under the libpng license.
  * For conditions of distribution and use, see the disclaimer
@@ -22,7 +22,7 @@
 #include <ctype.h>
 #include <math.h>
 
-#if (defined HAVE_CONFIG_H) && !(defined PNG_NO_CONFIG_H)
+#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
 #  include <config.h>
 #endif
 
@@ -62,6 +62,12 @@
 #  define aligncastconst(type, value) ((const void*)(value))
 #endif /* __cplusplus */
 
+/* During parallel runs of pngstest each temporary file needs a unique name,
+ * this is used to permit uniqueness using a command line argument which can be
+ * up to 22 characters long.
+ */
+static char tmpf[23] = "TMP";
+
 /* Generate random bytes.  This uses a boring repeatable algorithm and it
  * is implemented here so that it gives the same set of numbers on every
  * architecture.  It's a linear congruential generator (Knuth or Sedgewick
@@ -309,6 +315,7 @@
 #define KEEP_GOING 32
 #define ACCUMULATE 64
 #define FAST_WRITE 128
+#define sRGB_16BIT 256
 
 static void
 print_opts(png_uint_32 opts)
@@ -329,6 +336,8 @@
       printf(" --accumulate");
    if (!(opts & FAST_WRITE)) /* --fast is currently the default */
       printf(" --slow");
+   if (opts & sRGB_16BIT)
+      printf(" --sRGB-16bit");
 }
 
 #define FORMAT_NO_CHANGE 0x80000000 /* additional flag */
@@ -3020,6 +3029,10 @@
          return logerror(image, "file init: ", image->file_name, "");
    }
 
+   /* This must be set after the begin_read call: */
+   if (image->opts & sRGB_16BIT)
+      image->image.flags |= PNG_IMAGE_FLAG_16BIT_sRGB;
+
    /* Have an initialized image with all the data we need plus, maybe, an
     * allocated file (myfile) or buffer (mybuffer) that need to be freed.
     */
@@ -3198,7 +3211,7 @@
       static int counter = 0;
       char name[32];
 
-      sprintf(name, "TMP%d.png", ++counter);
+      sprintf(name, "%s%d.png", tmpf, ++counter);
 
       if (png_image_write_to_file(&image->image, name, convert_to_8bit,
          image->buffer+16, (png_int_32)image->stride, image->colormap))
@@ -3482,6 +3495,34 @@
          opts &= ~KEEP_GOING;
       else if (strcmp(arg, "--strict") == 0)
          opts |= STRICT;
+      else if (strcmp(arg, "--sRGB-16bit") == 0)
+         opts |= sRGB_16BIT;
+      else if (strcmp(arg, "--linear-16bit") == 0)
+         opts &= ~sRGB_16BIT;
+      else if (strcmp(arg, "--tmpfile") == 0)
+      {
+         if (c+1 < argc)
+         {
+            if (strlen(argv[++c]) >= sizeof tmpf)
+            {
+               fflush(stdout);
+               fprintf(stderr, "%s: %s is too long for a temp file prefix\n",
+                  argv[0], argv[c]);
+               exit(99);
+            }
+
+            /* Safe: checked above */
+            strcpy(tmpf, argv[c]);
+         }
+
+         else
+         {
+            fflush(stdout);
+            fprintf(stderr, "%s: %s requires a temporary file prefix\n",
+               argv[0], arg);
+            exit(99);
+         }
+      }
       else if (strcmp(arg, "--touch") == 0)
       {
          if (c+1 < argc)
@@ -3492,7 +3533,7 @@
             fflush(stdout);
             fprintf(stderr, "%s: %s requires a file name argument\n",
                argv[0], arg);
-            exit(1);
+            exit(99);
          }
       }
       else if (arg[0] == '+')
@@ -3500,7 +3541,7 @@
          png_uint_32 format = formatof(arg+1);
 
          if (format > FORMAT_COUNT)
-            exit(1);
+            exit(99);
 
          format_set(&formats, format);
       }
@@ -3508,7 +3549,7 @@
       {
          fflush(stdout);
          fprintf(stderr, "%s: unknown option: %s\n", argv[0], arg);
-         exit(1);
+         exit(99);
       }
       else
       {
@@ -3557,7 +3598,7 @@
                buffer[4095] = 0;
                fprintf(stderr, "%s...%s: file name too long\n", buffer,
                   buffer+(4096-32));
-               exit(1);
+               exit(99);
             }
          }
 
@@ -3676,7 +3717,7 @@
          {
             fflush(stdout);
             fprintf(stderr, "%s: write failed\n", touch);
-            exit(1);
+            exit(99);
          }
       }
 
@@ -3684,7 +3725,7 @@
       {
          fflush(stdout);
          fprintf(stderr, "%s: open failed\n", touch);
-         exit(1);
+         exit(99);
       }
    }
 
@@ -3695,6 +3736,7 @@
 int main(void)
 {
    fprintf(stderr, "pngstest: no read support in libpng, test skipped\n");
-   return 0;
+   /* So the test is skipped: */
+   return 77;
 }
 #endif /* PNG_SIMPLIFIED_READ_SUPPORTED */
diff --git a/contrib/libtests/pngunknown.c b/contrib/libtests/pngunknown.c
index 9d9acc0..9b8de34 100644
--- a/contrib/libtests/pngunknown.c
+++ b/contrib/libtests/pngunknown.c
@@ -1,7 +1,7 @@
 
 /* pngunknown.c - test the read side unknown chunk handling
  *
- * Last changed in libpng 1.6.0 [(PENDING RELEASE)]
+ * Last changed in libpng 1.6.0 [February 14, 2013]
  * Copyright (c) 2013 Glenn Randers-Pehrson
  * Written by John Cunningham Bowler
  *
diff --git a/contrib/libtests/pngvalid.c b/contrib/libtests/pngvalid.c
index fae8e6f..f36b1f8 100644
--- a/contrib/libtests/pngvalid.c
+++ b/contrib/libtests/pngvalid.c
@@ -1,7 +1,7 @@
 
 /* pngvalid.c - validate libpng by constructing then reading png files.
  *
- * Last changed in libpng 1.6.0 [February 14, 2013]
+ * Last changed in libpng 1.6.1 [March 28, 2013]
  * Copyright (c) 2013 Glenn Randers-Pehrson
  * Written by John Cunningham Bowler
  *
@@ -26,7 +26,7 @@
 #include <signal.h>
 #include <stdio.h>
 
-#if (defined HAVE_CONFIG_H) && !(defined PNG_NO_CONFIG_H)
+#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
 #  include <config.h>
 #endif
 
@@ -1999,8 +1999,8 @@
 }
 #endif
 
-#if (defined PNG_READ_GAMMA_SUPPORTED) ||\
-   (defined PNG_READ_RGB_TO_GRAY_SUPPORTED)
+#if defined(PNG_READ_GAMMA_SUPPORTED) ||\
+   defined(PNG_READ_RGB_TO_GRAY_SUPPORTED)
 static double abserr(PNG_CONST png_modifier *pm, int in_depth, int out_depth)
 {
    /* Absolute error permitted in linear values - affected by the bit depth of
@@ -3357,7 +3357,7 @@
          PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
 
 #ifdef PNG_TEXT_SUPPORTED
-#  if (defined PNG_READ_zTXt_SUPPORTED) && (defined PNG_WRITE_zTXt_SUPPORTED)
+#  if defined(PNG_READ_zTXt_SUPPORTED) && defined(PNG_WRITE_zTXt_SUPPORTED)
 #     define TEXT_COMPRESSION PNG_TEXT_COMPRESSION_zTXt
 #  else
 #     define TEXT_COMPRESSION PNG_TEXT_COMPRESSION_NONE
@@ -8895,8 +8895,8 @@
 }
 #endif /* 16 to 8 bit conversion */
 
-#if defined PNG_READ_BACKGROUND_SUPPORTED ||\
-   defined PNG_READ_ALPHA_MODE_SUPPORTED
+#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
+   defined(PNG_READ_ALPHA_MODE_SUPPORTED)
 static void gamma_composition_test(png_modifier *pm,
    PNG_CONST png_byte colour_type, PNG_CONST png_byte bit_depth,
    PNG_CONST int palette_number,
diff --git a/contrib/libtests/readpng.c b/contrib/libtests/readpng.c
index 6313257..7ba46d0 100644
--- a/contrib/libtests/readpng.c
+++ b/contrib/libtests/readpng.c
@@ -2,7 +2,7 @@
  *
  * Copyright (c) 2013 John Cunningham Bowler
  *
- * Last changed in libpng 1.6.0 [(PENDING RELEASE)]
+ * Last changed in libpng 1.6.1 [March 28, 2013]
  *
  * This code is released under the libpng license.
  * For conditions of distribution and use, see the disclaimer
@@ -18,7 +18,7 @@
 #include <stdio.h>
 #include <string.h>
 
-#if (defined HAVE_CONFIG_H) && !(defined PNG_NO_CONFIG_H)
+#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
 #  include <config.h>
 #endif
 
diff --git a/contrib/libtests/test-pngstest.sh b/contrib/libtests/test-pngstest.sh
deleted file mode 100755
index e977a5b..0000000
--- a/contrib/libtests/test-pngstest.sh
+++ /dev/null
@@ -1,24 +0,0 @@
-#!/bin/sh
-#
-# Run the simplified API tests
-err=0
-
-echo >> pngtest-log.txt
-echo "============ pngstest.sh ==============" >> pngtest-log.txt
-
-echo "Running test-pngstest.sh"
-for image in ${srcdir}/contrib/pngsuite/*.png
-do
-   for opts in ""
-   do
-      if ./pngstest --strict --log "$@" $opts $image >>pngtest-log.txt 2>&1
-      then
-         echo "  PASS: pngstest $opts $image"
-      else
-         echo "  FAIL: pngstest $opts $image"
-         err=1
-      fi
-   done
-done
-
-exit $err
diff --git a/contrib/libtests/test-pngunknown.sh b/contrib/libtests/test-pngunknown.sh
deleted file mode 100755
index 1a31864..0000000
--- a/contrib/libtests/test-pngunknown.sh
+++ /dev/null
@@ -1,38 +0,0 @@
-#!/bin/sh
-#
-# Run the unknown API tests
-err=0
-image="${srcdir}/pngtest.png"
-#
-# stream 4 is used for the output of the shell, pngtest-log.txt gets all the
-# normal program output.
-exec 4>&1 1>>pngtest-log.txt 2>&1
-
-echo
-echo "============ test-pngunknown.sh =============="
-
-echo "Running test-pngunknown.sh" >&4
-
-for tests in \
- "discard default=discard"\
- "save default=save"\
- "if-safe default=if-safe"\
- "vpAg vpAg=if-safe"\
- "sTER sTER=if-safe"\
- "IDAT default=discard IDAT=save"\
- "sAPI bKGD=save cHRM=save gAMA=save all=discard iCCP=save sBIT=save sRGB=save"
-do
-   set $tests
-   test="$1"
-   shift
-
-   if ./pngunknown "$@" "$image" 4>&-
-   then
-      echo "  PASS: test-pngunknown $test" >&4
-   else
-      echo "  FAIL: test-pngunknown $test" >&4
-      err=1
-   fi
-done
-
-exit $err
diff --git a/contrib/libtests/test-pngvalid-full.sh b/contrib/libtests/test-pngvalid-full.sh
deleted file mode 100755
index f7d1b2b..0000000
--- a/contrib/libtests/test-pngvalid-full.sh
+++ /dev/null
@@ -1,21 +0,0 @@
-#!/bin/sh
-#
-# Run a sequence of gamma tests quietly
-err=0
-
-echo >> pngtest-log.txt
-echo "============ pngvalid-full.sh ==============" >> pngtest-log.txt
-
-echo "Running test-pngvalid-full.sh"
-for gamma in threshold transform sbit 16-to-8 background alpha-mode "transform --expand16" "background --expand16" "alpha-mode --expand16"
-do
-   if ./pngvalid "$@" --gamma-$gamma >> pngtest-log.txt 2>&1
-   then
-      echo "  PASS: pngvalid" "$@" "--gamma-$gamma"
-   else
-      echo "  FAIL: pngvalid" "$@" "--gamma-$gamma"
-      err=1
-   fi
-done
-
-exit $err
diff --git a/contrib/libtests/test-pngvalid-simple.sh b/contrib/libtests/test-pngvalid-simple.sh
deleted file mode 100755
index 7d4d9d8..0000000
--- a/contrib/libtests/test-pngvalid-simple.sh
+++ /dev/null
@@ -1,30 +0,0 @@
-#!/bin/sh
-#
-# Run a sequence of tests quietly, without the slow
-# gamma tests
-err=0
-
-echo >> pngtest-log.txt
-echo "============ pngvalid-simple.sh ==============" >> pngtest-log.txt
-echo "Running test-pngvalid-simple.sh"
-# The options to test are:
-#
-# standard tests with and without progressive reading and interlace
-# size images with and without progressive reading
-# transform tests (standard, non-interlaced only)
-#
-for opts in "--standard" "--standard --progressive-read" \
-   "--standard --interlace" "--standard --progressive-read --interlace" \
-   "--size" "--size --progressive-read" \
-   "--transform"
-do
-   if ./pngvalid "$@" $opts >> pngtest-log.txt 2>&1
-   then
-      echo "  PASS: pngvalid" "$@" $opts
-   else
-      echo "  FAIL: pngvalid" "$@" $opts
-      err=1
-   fi
-done
-
-exit $err
diff --git a/contrib/libtests/timepng.c b/contrib/libtests/timepng.c
index 1222708..523627f 100644
--- a/contrib/libtests/timepng.c
+++ b/contrib/libtests/timepng.c
@@ -2,7 +2,7 @@
  *
  * Copyright (c) 2013 John Cunningham Bowler
  *
- * Last changed in libpng 1.6.0 [February 14, 2013]
+ * Last changed in libpng 1.6.1 [March 28, 2013]
  *
  * This code is released under the libpng license.
  * For conditions of distribution and use, see the disclaimer
@@ -22,7 +22,7 @@
 
 #include <time.h>
 
-#if (defined HAVE_CONFIG_H) && !(defined PNG_NO_CONFIG_H)
+#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
 #  include <config.h>
 #endif
 
diff --git a/contrib/tools/cvtcolor.c b/contrib/tools/cvtcolor.c
index 2853254..e6793c7 100644
--- a/contrib/tools/cvtcolor.c
+++ b/contrib/tools/cvtcolor.c
@@ -3,7 +3,7 @@
  *
  * Last changed in libpng 1.6.0 [February 14, 2013]
  *
- * COPYRIGHT: Written by John Cunningham Bowler, 2012.
+ * COPYRIGHT: Written by John Cunningham Bowler, 2013.
  * To the extent possible under law, the author has waived all copyright and
  * related or neighboring rights to this work.  This work is published from:
  * United States.
diff --git a/contrib/tools/intgamma.sh b/contrib/tools/intgamma.sh
index e321947..41c5d6d 100644
--- a/contrib/tools/intgamma.sh
+++ b/contrib/tools/intgamma.sh
@@ -4,7 +4,7 @@
 #
 # Last changed in libpng 1.6.0 [February 14, 2013]
 #
-# COPYRIGHT: Written by John Cunningham Bowler, 2012.
+# COPYRIGHT: Written by John Cunningham Bowler, 2013.
 # To the extent possible under law, the author has waived all copyright and
 # related or neighboring rights to this work.  This work is published from:
 # United States.
diff --git a/contrib/tools/makesRGB.c b/contrib/tools/makesRGB.c
index de9191e..e66c9f1 100644
--- a/contrib/tools/makesRGB.c
+++ b/contrib/tools/makesRGB.c
@@ -2,7 +2,7 @@
  *
  * Last changed in libpng 1.6.0 [February 14, 2013]
  *
- * COPYRIGHT: Written by John Cunningham Bowler, 2012.
+ * COPYRIGHT: Written by John Cunningham Bowler, 2013.
  * To the extent possible under law, the author has waived all copyright and
  * related or neighboring rights to this work.  This work is published from:
  * United States.
diff --git a/contrib/tools/sRGB.h b/contrib/tools/sRGB.h
index 833f230..22c8f7c 100644
--- a/contrib/tools/sRGB.h
+++ b/contrib/tools/sRGB.h
@@ -3,7 +3,7 @@
  *
  * Last changed in libpng 1.6.0 [February 14, 2013]
  *
- * COPYRIGHT: Written by John Cunningham Bowler, 2012.
+ * COPYRIGHT: Written by John Cunningham Bowler, 2013.
  * To the extent possible under law, the author has waived all copyright and
  * related or neighboring rights to this work.  This work is published from:
  * United States.
diff --git a/libpng-manual.txt b/libpng-manual.txt
index 63fa15b..4b91d86 100644
--- a/libpng-manual.txt
+++ b/libpng-manual.txt
@@ -1,6 +1,6 @@
 libpng-manual.txt - A description on how to use and modify libpng
 
- libpng version 1.6.0 - February 14, 2013
+ libpng version 1.6.1 - March 28, 2013
  Updated and distributed by Glenn Randers-Pehrson
  <glennrp at users.sourceforge.net>
  Copyright (c) 1998-2013 Glenn Randers-Pehrson
@@ -11,7 +11,7 @@
 
  Based on:
 
- libpng versions 0.97, January 1998, through 1.6.0 - February 14, 2013
+ libpng versions 0.97, January 1998, through 1.6.1 - March 28, 2013
  Updated and distributed by Glenn Randers-Pehrson
  Copyright (c) 1998-2013 Glenn Randers-Pehrson
 
@@ -527,9 +527,14 @@
     png_get_user_chunk_ptr(png_ptr);
 
 If you call the png_set_read_user_chunk_fn() function, then all unknown
-chunks will be saved when read, in case your callback function will need
-one or more of them.  This behavior can be changed with the
-png_set_keep_unknown_chunks() function, described below.
+chunks which the callback does not handle will be saved when read.  You can
+cause them to be discarded by returning '1' ("handled") instead of '0'.  This
+behavior will change in libpng 1.7 and the default handling set by the
+png_set_keep_unknown_chunks() function, described below, will be used when the
+callback returns 0.  If you want the existing behavior you should set the global
+default to PNG_HANDLE_CHUNK_IF_SAFE now; this is compatible with all current
+versions of libpng and with 1.7.  Libpng 1.6 issues a warning if you keep the
+default, or PNG_HANDLE_CHUNK_NEVER, and the callback returns 0.
 
 At this point, you can set up a callback function that will be
 called after each row has been read, which you can use to control
@@ -628,15 +633,17 @@
     ...
 
     #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
-      /* ignore all unknown chunks: */
-      png_set_keep_unknown_chunks(read_ptr, 1, NULL, 0);
+      /* ignore all unknown chunks
+       * (use global setting "2" for libpng16 and earlier):
+       */
+      png_set_keep_unknown_chunks(read_ptr, 2, NULL, 0);
 
       /* except for vpAg: */
       png_set_keep_unknown_chunks(read_ptr, 2, vpAg, 1);
 
       /* also ignore unused known chunks: */
       png_set_keep_unknown_chunks(read_ptr, 1, unused_chunks,
-         (int)sizeof(unused_chunks)/5);
+         (int)(sizeof unused_chunks)/5);
     #endif
 
 User limits
@@ -988,7 +995,7 @@
 If you know your image size and pixel size ahead of time, you can allocate
 row_pointers prior to calling png_read_png() with
 
-   if (height > PNG_UINT_32_MAX/png_sizeof(png_byte))
+   if (height > PNG_UINT_32_MAX/(sizeof (png_byte)))
       png_error (png_ptr,
           "Image is too tall to process in memory");
 
@@ -997,7 +1004,7 @@
           "Image is too wide to process in memory");
 
    row_pointers = png_malloc(png_ptr,
-       height*png_sizeof(png_bytep));
+       height*(sizeof (png_bytep)));
 
    for (int i=0; i<height, i++)
       row_pointers[i]=NULL;  /* security precaution */
@@ -4953,7 +4960,7 @@
    png_free_default()
    png_reset_zstream()
 
-The following has been removed:
+The following have been removed:
    png_get_io_chunk_name(), which has been replaced
      with png_get_io_chunk_type().  The new
      function returns a 32-bit integer instead of
@@ -4973,6 +4980,9 @@
 profiles that were previously accepted are now rejected, in particular the
 very old broken Microsoft/HP sRGB profile.
 
+The library now issues an error if the application attempts to set a
+transform after it calls png_read_update_info().
+
 The library now issues a warning if both background processing and RGB to
 gray are used when gamma correction happens. As with previous versions of
 the library the results are numerically very incorrect in this case.
@@ -5134,7 +5144,8 @@
        y[i] = a(x) + (int)b;
 
 We prefer #ifdef and #ifndef to #if defined() and #if !defined()
-when there is only one macro being tested.
+when there is only one macro being tested.  We always use parentheses
+with "defined".
 
 We prefer to express integers that are used as bit masks in hex format,
 with an even number of lower-case hex digits (e.g., 0x00, 0xff, 0x0100).
@@ -5150,13 +5161,13 @@
 
 XVI. Y2K Compliance in libpng
 
-February 14, 2013
+March 28, 2013
 
 Since the PNG Development group is an ad-hoc body, we can't make
 an official declaration.
 
 This is your unofficial assurance that libpng from version 0.71 and
-upward through 1.6.0 are Y2K compliant.  It is my belief that earlier
+upward through 1.6.1 are Y2K compliant.  It is my belief that earlier
 versions were also Y2K compliant.
 
 Libpng only has two year fields.  One is a 2-byte unsigned integer
diff --git a/libpng.3 b/libpng.3
index 11da8ce..e4f4e94 100644
--- a/libpng.3
+++ b/libpng.3
@@ -1,6 +1,6 @@
-.TH LIBPNG 3 "February 14, 2013"
+.TH LIBPNG 3 "March 28, 2013"
 .SH NAME
-libpng \- Portable Network Graphics (PNG) Reference Library 1.6.0
+libpng \- Portable Network Graphics (PNG) Reference Library 1.6.1
 .SH SYNOPSIS
 \fB
 #include <png.h>\fP
@@ -504,7 +504,7 @@
 .SH LIBPNG.TXT
 libpng-manual.txt - A description on how to use and modify libpng
 
- libpng version 1.6.0 - February 14, 2013
+ libpng version 1.6.1 - March 28, 2013
  Updated and distributed by Glenn Randers-Pehrson
  <glennrp at users.sourceforge.net>
  Copyright (c) 1998-2013 Glenn Randers-Pehrson
@@ -515,7 +515,7 @@
 
  Based on:
 
- libpng versions 0.97, January 1998, through 1.6.0 - February 14, 2013
+ libpng versions 0.97, January 1998, through 1.6.1 - March 28, 2013
  Updated and distributed by Glenn Randers-Pehrson
  Copyright (c) 1998-2013 Glenn Randers-Pehrson
 
@@ -1031,9 +1031,14 @@
     png_get_user_chunk_ptr(png_ptr);
 
 If you call the png_set_read_user_chunk_fn() function, then all unknown
-chunks will be saved when read, in case your callback function will need
-one or more of them.  This behavior can be changed with the
-png_set_keep_unknown_chunks() function, described below.
+chunks which the callback does not handle will be saved when read.  You can
+cause them to be discarded by returning '1' ("handled") instead of '0'.  This
+behavior will change in libpng 1.7 and the default handling set by the
+png_set_keep_unknown_chunks() function, described below, will be used when the
+callback returns 0.  If you want the existing behavior you should set the global
+default to PNG_HANDLE_CHUNK_IF_SAFE now; this is compatible with all current
+versions of libpng and with 1.7.  Libpng 1.6 issues a warning if you keep the
+default, or PNG_HANDLE_CHUNK_NEVER, and the callback returns 0.
 
 At this point, you can set up a callback function that will be
 called after each row has been read, which you can use to control
@@ -1132,15 +1137,17 @@
     ...
 
     #if defined(PNG_UNKNOWN_CHUNKS_SUPPORTED)
-      /* ignore all unknown chunks: */
-      png_set_keep_unknown_chunks(read_ptr, 1, NULL, 0);
+      /* ignore all unknown chunks
+       * (use global setting "2" for libpng16 and earlier):
+       */
+      png_set_keep_unknown_chunks(read_ptr, 2, NULL, 0);
 
       /* except for vpAg: */
       png_set_keep_unknown_chunks(read_ptr, 2, vpAg, 1);
 
       /* also ignore unused known chunks: */
       png_set_keep_unknown_chunks(read_ptr, 1, unused_chunks,
-         (int)sizeof(unused_chunks)/5);
+         (int)(sizeof unused_chunks)/5);
     #endif
 
 .SS User limits
@@ -1492,7 +1499,7 @@
 If you know your image size and pixel size ahead of time, you can allocate
 row_pointers prior to calling png_read_png() with
 
-   if (height > PNG_UINT_32_MAX/png_sizeof(png_byte))
+   if (height > PNG_UINT_32_MAX/(sizeof (png_byte)))
       png_error (png_ptr,
           "Image is too tall to process in memory");
 
@@ -1501,7 +1508,7 @@
           "Image is too wide to process in memory");
 
    row_pointers = png_malloc(png_ptr,
-       height*png_sizeof(png_bytep));
+       height*(sizeof (png_bytep)));
 
    for (int i=0; i<height, i++)
       row_pointers[i]=NULL;  /* security precaution */
@@ -5458,7 +5465,7 @@
    png_free_default()
    png_reset_zstream()
 
-The following has been removed:
+The following have been removed:
    png_get_io_chunk_name(), which has been replaced
      with png_get_io_chunk_type().  The new
      function returns a 32-bit integer instead of
@@ -5478,6 +5485,9 @@
 profiles that were previously accepted are now rejected, in particular the
 very old broken Microsoft/HP sRGB profile.
 
+The library now issues an error if the application attempts to set a
+transform after it calls png_read_update_info().
+
 The library now issues a warning if both background processing and RGB to
 gray are used when gamma correction happens. As with previous versions of
 the library the results are numerically very incorrect in this case.
@@ -5639,7 +5649,8 @@
        y[i] = a(x) + (int)b;
 
 We prefer #ifdef and #ifndef to #if defined() and #if !defined()
-when there is only one macro being tested.
+when there is only one macro being tested.  We always use parentheses
+with "defined".
 
 We prefer to express integers that are used as bit masks in hex format,
 with an even number of lower-case hex digits (e.g., 0x00, 0xff, 0x0100).
@@ -5655,13 +5666,13 @@
 
 .SH XVI. Y2K Compliance in libpng
 
-February 14, 2013
+March 28, 2013
 
 Since the PNG Development group is an ad-hoc body, we can't make
 an official declaration.
 
 This is your unofficial assurance that libpng from version 0.71 and
-upward through 1.6.0 are Y2K compliant.  It is my belief that earlier
+upward through 1.6.1 are Y2K compliant.  It is my belief that earlier
 versions were also Y2K compliant.
 
 Libpng only has two year fields.  One is a 2-byte unsigned integer
@@ -5869,6 +5880,8 @@
  1.6.0beta01-40      16    10600  16.so.16.0[.0]
  1.6.0rc01-08        16    10600  16.so.16.0[.0]
  1.6.0               16    10600  16.so.16.0[.0]
+ 1.6.1rc01           16    10601  16.so.16.1[.0]
+ 1.6.1               16    10601  16.so.16.1[.0]
 
 Henceforth the source version will match the shared-library minor
 and patch numbers; the shared-library major version number will be
@@ -5925,7 +5938,7 @@
 
 Thanks to Frank J. T. Wojcik for helping with the documentation.
 
-Libpng version 1.6.0 - February 14, 2013:
+Libpng version 1.6.1 - March 28, 2013:
 Initially created in 1995 by Guy Eric Schalnat, then of Group 42, Inc.
 Currently maintained by Glenn Randers-Pehrson (glennrp at users.sourceforge.net).
 
@@ -5948,7 +5961,7 @@
 
 This code is released under the libpng license.
 
-libpng versions 1.2.6, August 15, 2004, through 1.6.0, February 14, 2013, are
+libpng versions 1.2.6, August 15, 2004, through 1.6.1, March 28, 2013, are
 Copyright (c) 2004,2006-2007 Glenn Randers-Pehrson, and are
 distributed according to the same disclaimer and license as libpng-1.2.5
 with the following individual added to the list of Contributing Authors
@@ -6047,7 +6060,7 @@
 
 Glenn Randers-Pehrson
 glennrp at users.sourceforge.net
-February 14, 2013
+March 28, 2013
 
 .\" end of man page
 
diff --git a/libpngpf.3 b/libpngpf.3
index f06520c..21b56cf 100644
--- a/libpngpf.3
+++ b/libpngpf.3
@@ -1,6 +1,6 @@
-.TH LIBPNGPF 3 "February 14, 2013"
+.TH LIBPNGPF 3 "March 28, 2013"
 .SH NAME
-libpng \- Portable Network Graphics (PNG) Reference Library 1.6.0
+libpng \- Portable Network Graphics (PNG) Reference Library 1.6.1
 (private functions)
 .SH SYNOPSIS
 \fB#include \fI"pngpriv.h"
diff --git a/png.5 b/png.5
index 8832f8c..1d25bf5 100644
--- a/png.5
+++ b/png.5
@@ -1,4 +1,4 @@
-.TH PNG 5 "February 14, 2013"
+.TH PNG 5 "March 28, 2013"
 .SH NAME
 png \- Portable Network Graphics (PNG) format
 .SH DESCRIPTION
diff --git a/png.c b/png.c
index 59d747d..67f760e 100644
--- a/png.c
+++ b/png.c
@@ -1,7 +1,7 @@
 
 /* png.c - location for general purpose libpng functions
  *
- * Last changed in libpng 1.6.0 [February 14, 2013]
+ * Last changed in libpng 1.6.1 [March 28, 2013]
  * Copyright (c) 1998-2013 Glenn Randers-Pehrson
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -14,7 +14,7 @@
 #include "pngpriv.h"
 
 /* Generate a compiler error if there is an old png.h in the search path. */
-typedef png_libpng_version_1_6_0 Your_png_h_is_not_version_1_6_0;
+typedef png_libpng_version_1_6_1 Your_png_h_is_not_version_1_6_1;
 
 /* Tells libpng that we have already handled the first "num_bytes" bytes
  * of the PNG file signature.  If the PNG data is embedded into another
@@ -768,13 +768,13 @@
 #else
 #  ifdef __STDC__
    return PNG_STRING_NEWLINE \
-     "libpng version 1.6.0 - February 14, 2013" PNG_STRING_NEWLINE \
+     "libpng version 1.6.1 - March 28, 2013" PNG_STRING_NEWLINE \
      "Copyright (c) 1998-2013 Glenn Randers-Pehrson" PNG_STRING_NEWLINE \
      "Copyright (c) 1996-1997 Andreas Dilger" PNG_STRING_NEWLINE \
      "Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc." \
      PNG_STRING_NEWLINE;
 #  else
-      return "libpng version 1.6.0 - February 14, 2013\
+      return "libpng version 1.6.1 - March 28, 2013\
       Copyright (c) 1998-2013 Glenn Randers-Pehrson\
       Copyright (c) 1996-1997 Andreas Dilger\
       Copyright (c) 1995-1996 Guy Eric Schalnat, Group 42, Inc.";
@@ -1407,8 +1407,10 @@
       red_inverse))
       return 1;
 
-   if (!png_muldiv(&XYZ->green_X, xy->greenx, PNG_FP_1, green_inverse)) return 1;
-   if (!png_muldiv(&XYZ->green_Y, xy->greeny, PNG_FP_1, green_inverse)) return 1;
+   if (!png_muldiv(&XYZ->green_X, xy->greenx, PNG_FP_1, green_inverse))
+      return 1;
+   if (!png_muldiv(&XYZ->green_Y, xy->greeny, PNG_FP_1, green_inverse))
+      return 1;
    if (!png_muldiv(&XYZ->green_Z, PNG_FP_1 - xy->greenx - xy->greeny, PNG_FP_1,
       green_inverse))
       return 1;
@@ -1647,7 +1649,7 @@
    return 0; /* failed */
 }
 
-#if defined PNG_sRGB_SUPPORTED || defined PNG_iCCP_SUPPORTED
+#if defined(PNG_sRGB_SUPPORTED) || defined(PNG_iCCP_SUPPORTED)
 /* Error message generation */
 static char
 png_icc_tag_char(png_uint_32 byte)
@@ -2127,7 +2129,7 @@
       PNG_MD5(0x00000000, 0x00000000, 0x00000000, 0x00000000), 0, 1/*broken*/,
       "1998/02/09 06:49:00", 3144, "HP-Microsoft sRGB v2 perceptual")
 
-   PNG_ICC_CHECKSUM(0x0398f3fcUL, 0xf29e526dUL,
+   PNG_ICC_CHECKSUM(0x0398f3fc, 0xf29e526d,
       PNG_MD5(0x00000000, 0x00000000, 0x00000000, 0x00000000), 1, 1/*broken*/,
       "1998/02/09 06:49:00", 3144, "HP-Microsoft sRGB v2 media-relative")
 };
@@ -2177,7 +2179,7 @@
          if (length == png_sRGB_checks[i].length &&
             intent == png_sRGB_checks[i].intent)
          {
-            /* Now calculate the alder32 if not done already. */
+            /* Now calculate the adler32 if not done already. */
             if (adler == 0)
             {
                adler = adler32(0, NULL, 0);
@@ -3090,7 +3092,7 @@
 #endif
 
 #if defined(PNG_READ_GAMMA_SUPPORTED) || \
-    defined(PNG_INCH_CONVERSIONS_SUPPORTED) || defined(PNG__READ_pHYs_SUPPORTED)
+    defined(PNG_INCH_CONVERSIONS_SUPPORTED) || defined(PNG_READ_pHYs_SUPPORTED)
 /* muldiv functions */
 /* This API takes signed arguments and rounds the result to the nearest
  * integer (or, for a fixed point number - the standard argument - to
@@ -4010,9 +4012,30 @@
 }
 #endif /* READ_GAMMA */
 
+/* HARDWARE OPTION SUPPORT */
+#ifdef PNG_SET_OPTION_SUPPORTED
+int PNGAPI
+png_set_option(png_structrp png_ptr, int option, int onoff)
+{
+   if (png_ptr != NULL && option >= 0 && option < PNG_OPTION_NEXT &&
+      (option & 1) == 0)
+   {
+      int mask = 3 << option;
+      int setting = (2 + (onoff != 0)) << option;
+      int current = png_ptr->options;
+
+      png_ptr->options = (png_byte)((current & ~mask) | setting);
+
+      return (current & mask) >> option;
+   }
+
+   return PNG_OPTION_INVALID;
+}
+#endif
+
 /* sRGB support */
-#if defined PNG_SIMPLIFIED_READ_SUPPORTED ||\
-   defined PNG_SIMPLIFIED_WRITE_SUPPORTED
+#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
+   defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
 /* sRGB conversion tables; these are machine generated with the code in
  * contrib/tools/makesRGB.c.  The actual sRGB transfer curve defined in the
  * specification (see the article at http://en.wikipedia.org/wiki/SRGB)
@@ -4180,8 +4203,8 @@
 #endif /* SIMPLIFIED READ/WRITE sRGB support */
 
 /* SIMPLIFIED READ/WRITE SUPPORT */
-#if defined PNG_SIMPLIFIED_READ_SUPPORTED ||\
-   defined PNG_SIMPLIFIED_WRITE_SUPPORTED
+#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
+   defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
 static int
 png_image_free_function(png_voidp argument)
 {
diff --git a/png.h b/png.h
index 448fe88..0ad09eb 100644
--- a/png.h
+++ b/png.h
@@ -1,7 +1,7 @@
 
 /* png.h - header file for PNG reference library
  *
- * libpng version 1.6.0 - February 14, 2013
+ * libpng version 1.6.1 - March 28, 2013
  * Copyright (c) 1998-2013 Glenn Randers-Pehrson
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -11,7 +11,7 @@
  * Authors and maintainers:
  *   libpng versions 0.71, May 1995, through 0.88, January 1996: Guy Schalnat
  *   libpng versions 0.89c, June 1996, through 0.96, May 1997: Andreas Dilger
- *   libpng versions 0.97, January 1998, through 1.6.0 - February 14, 2013: Glenn
+ *   libpng versions 0.97, January 1998, through 1.6.1 - March 28, 2013: Glenn
  *   See also "Contributing Authors", below.
  *
  * Note about libpng version numbers:
@@ -169,6 +169,9 @@
  *    1.6.0beta01-40          16    10600  16.so.16.0[.0]
  *    1.6.0rc01-08            16    10600  16.so.16.0[.0]
  *    1.6.0                   16    10600  16.so.16.0[.0]
+ *    1.6.1beta01-10          16    10601  16.so.16.1[.0]
+ *    1.6.1rc01               16    10601  16.so.16.1[.0]
+ *    1.6.1                   16    10601  16.so.16.1[.0]
  *
  *   Henceforth the source version will match the shared-library major
  *   and minor numbers; the shared-library major version number will be
@@ -200,7 +203,7 @@
  *
  * This code is released under the libpng license.
  *
- * libpng versions 1.2.6, August 15, 2004, through 1.6.0, February 14, 2013, are
+ * libpng versions 1.2.6, August 15, 2004, through 1.6.1, March 28, 2013, are
  * Copyright (c) 2004, 2006-2013 Glenn Randers-Pehrson, and are
  * distributed according to the same disclaimer and license as libpng-1.2.5
  * with the following individual added to the list of Contributing Authors:
@@ -312,13 +315,13 @@
  * Y2K compliance in libpng:
  * =========================
  *
- *    February 14, 2013
+ *    March 28, 2013
  *
  *    Since the PNG Development group is an ad-hoc body, we can't make
  *    an official declaration.
  *
  *    This is your unofficial assurance that libpng from version 0.71 and
- *    upward through 1.6.0 are Y2K compliant.  It is my belief that
+ *    upward through 1.6.1 are Y2K compliant.  It is my belief that
  *    earlier versions were also Y2K compliant.
  *
  *    Libpng only has two year fields.  One is a 2-byte unsigned integer
@@ -378,9 +381,9 @@
  */
 
 /* Version information for png.h - this should match the version in png.c */
-#define PNG_LIBPNG_VER_STRING "1.6.0"
+#define PNG_LIBPNG_VER_STRING "1.6.1"
 #define PNG_HEADER_VERSION_STRING \
-     " libpng version 1.6.0 - February 14, 2013\n"
+     " libpng version 1.6.1 - March 28, 2013\n"
 
 #define PNG_LIBPNG_VER_SONUM   16
 #define PNG_LIBPNG_VER_DLLNUM  16
@@ -388,7 +391,7 @@
 /* These should match the first 3 components of PNG_LIBPNG_VER_STRING: */
 #define PNG_LIBPNG_VER_MAJOR   1
 #define PNG_LIBPNG_VER_MINOR   6
-#define PNG_LIBPNG_VER_RELEASE 0
+#define PNG_LIBPNG_VER_RELEASE 1
 
 /* This should match the numeric part of the final component of
  * PNG_LIBPNG_VER_STRING, omitting any leading zero:
@@ -419,7 +422,7 @@
  * version 1.0.0 was mis-numbered 100 instead of 10000).  From
  * version 1.0.1 it's    xxyyzz, where x=major, y=minor, z=release
  */
-#define PNG_LIBPNG_VER 10600 /* 1.6.0 */
+#define PNG_LIBPNG_VER 10601 /* 1.6.1 */
 
 /* Library configuration: these options cannot be changed after
  * the library has been built.
@@ -524,7 +527,7 @@
 /* This triggers a compiler error in png.c, if png.c and png.h
  * do not agree upon the version number.
  */
-typedef char* png_libpng_version_1_6_0;
+typedef char* png_libpng_version_1_6_1;
 
 /* Basic control structions.  Read libpng-manual.txt or libpng.3 for more info.
  *
@@ -1837,7 +1840,7 @@
 #endif
 
 #ifdef PNG_READ_USER_CHUNKS_SUPPORTED
-/* This callback is called only for *unknown* chunks, if
+/* This callback is called only for *unknown* chunks.  If
  * PNG_HANDLE_AS_UNKNOWN_SUPPORTED is set then it is possible to set known
  * chunks to be treated as unknown, however in this case the callback must do
  * any processing required by the chunk (e.g. by calling the appropriate
@@ -1849,12 +1852,12 @@
  * The integer return from the callback function is interpreted thus:
  *
  * negative: An error occured, png_chunk_error will be called.
- *     zero: The chunk was not handled, the chunk will be discarded unless
- *           png_set_keep_unknown_chunks has been used to set a 'keep' behavior
- *           for this particular chunk, in which case that will be used.  A
- *           critical chunk will cause an error at this point unless it is to be
- *           saved.
+ *     zero: The chunk was not handled, the chunk will be saved. A critical
+ *           chunk will cause an error at this point unless it is to be saved.
  * positive: The chunk was handled, libpng will ignore/discard it.
+ *
+ * See "INTERACTION WTIH USER CHUNK CALLBACKS" below for important notes about
+ * how this behavior will change in libpng 1.7
  */
 PNG_EXPORT(88, void, png_set_read_user_chunk_fn, (png_structrp png_ptr,
     png_voidp user_chunk_ptr, png_user_chunk_ptr read_user_chunk_fn));
@@ -2331,8 +2334,8 @@
 #ifdef PNG_sCAL_SUPPORTED
 PNG_FP_EXPORT(168, png_uint_32, png_get_sCAL, (png_const_structrp png_ptr,
     png_const_inforp info_ptr, int *unit, double *width, double *height))
-#if (defined PNG_FLOATING_ARITHMETIC_SUPPORTED) || \
-   (defined PNG_FLOATING_POINT_SUPPORTED)
+#if defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) || \
+   defined(PNG_FLOATING_POINT_SUPPORTED)
 /* NOTE: this API is currently implemented using floating point arithmetic,
  * consequently it can only be used on systems with floating point support.
  * In any case the range of values supported by png_fixed_point is small and it
@@ -2372,7 +2375,7 @@
  * READ:
  *    PNG_HANDLE_CHUNK_AS_DEFAULT:
  *       Known chunks: do normal libpng processing, do not keep the chunk (but
- *          set the comments below about PNG_HANDLE_AS_UNKNOWN_SUPPORTED)
+ *          see the comments below about PNG_HANDLE_AS_UNKNOWN_SUPPORTED)
  *       Unknown chunks: for a specific chunk use the global default, when used
  *          as the default discard the chunk data.
  *    PNG_HANDLE_CHUNK_NEVER:
@@ -2388,12 +2391,21 @@
  * to specifying "NEVER", however when "AS_DEFAULT" is used for specific chunks
  * it simply resets the behavior to the libpng default.
  *
+ * INTERACTION WTIH USER CHUNK CALLBACKS:
  * The per-chunk handling is always used when there is a png_user_chunk_ptr
  * callback and the callback returns 0; the chunk is then always stored *unless*
  * it is critical and the per-chunk setting is other than ALWAYS.  Notice that
  * the global default is *not* used in this case.  (In effect the per-chunk
  * value is incremented to at least IF_SAFE.)
  *
+ * IMPORTANT NOTE: this behavior will change in libpng 1.7 - the global and
+ * per-chunk defaults will be honored.  If you want to preserve the current
+ * behavior when your callback returns 0 you must set PNG_HANDLE_CHUNK_IF_SAFE
+ * as the default - if you don't do this libpng 1.6 will issue a warning.
+ *
+ * If you want unhandled unknown chunks to be discarded in libpng 1.6 and
+ * earlier simply return '1' (handled).
+ *
  * PNG_HANDLE_AS_UNKNOWN_SUPPORTED:
  *    If this is *not* set known chunks will always be handled by libpng and
  *    will never be stored in the unknown chunk list.  Known chunks listed to
@@ -3088,6 +3100,24 @@
     * slight speed gain.
     */
 
+#define PNG_IMAGE_FLAG_16BIT_sRGB 0x04
+   /* On read if the image is a 16-bit per component image and there is no gAMA
+    * or sRGB chunk assume that the components are sRGB encoded.  Notice that
+    * images output by the simplified API always have gamma information; setting
+    * this flag only affects the interpretation of 16-bit images from an
+    * external source.  It is recommended that the application expose this flag
+    * to the user; the user can normally easily recognize the difference between
+    * linear and sRGB encoding.  This flag has no effect on write - the data
+    * passed to the write APIs must have the correct encoding (as defined
+    * above.)
+    *
+    * If the flag is not set (the default) input 16-bit per component data is
+    * assumed to be linear.
+    *
+    * NOTE: the flag can only be set after the png_image_begin_read_ call,
+    * because that call initializes the 'flags' field.
+    */
+
 #ifdef PNG_SIMPLIFIED_READ_SUPPORTED
 /* READ APIs
  * ---------
@@ -3138,7 +3168,7 @@
     *    PNG_FORMAT_FLAG_LINEAR *not* set.
     *
     * For linear output removing the alpha channel is always done by compositing
-    * on black and background is ignored.:
+    * on black and background is ignored.
     *
     * colormap must be supplied when PNG_FORMAT_FLAG_COLORMAP is set.  It must
     * be at least the size (in bytes) returned by PNG_IMAGE_COLORMAP_SIZE.
@@ -3209,6 +3239,47 @@
 #  endif
 #endif /* CHECK_FOR_INVALID_INDEX */
 
+/*******************************************************************************
+ *  IMPLEMENTATION OPTIONS
+ *******************************************************************************
+ *
+ * Support for arbitrary implementation-specific optimizations.  The API allows
+ * particular options to be turned on or off.  'Option' is the number of the
+ * option and 'onoff' is 0 (off) or non-0 (on).  The value returned is given
+ * by the PNG_OPTION_ defines below.
+ *
+ * HARDWARE: normally hardware capabilites, such as the Intel SSE instructions,
+ *           are detected at run time, however sometimes it may be impossible
+ *           to do this in user mode, in which case it is necessary to discover
+ *           the capabilities in an OS specific way.  Such capabilities are
+ *           listed here when libpng has support for them and must be turned
+ *           ON by the application if present.
+ *
+ * SOFTWARE: sometimes software optimizations actually result in performance
+ *           decrease on some architectures or systems, or with some sets of
+ *           PNG images.  'Software' options allow such optimizations to be
+ *           selected at run time.
+ */
+#ifdef PNG_SET_OPTION_SUPPORTED
+#ifdef PNG_ARM_NEON_API_SUPPORTED
+#  define PNG_ARM_NEON   0 /* HARDWARE: ARM Neon SIMD instructions supported */
+#endif
+#define PNG_OPTION_NEXT  2 /* Next option - numbers must be even */
+
+/* Return values: NOTE: there are four values and 'off' is *not* zero */
+#define PNG_OPTION_UNSET   0 /* Unset - defaults to off */
+#define PNG_OPTION_INVALID 1 /* Option number out of range */
+#define PNG_OPTION_OFF     2
+#define PNG_OPTION_ON      3
+
+PNG_EXPORT(244, int, png_set_option, (png_structrp png_ptr, int option,
+   int onoff));
+#endif
+
+/*******************************************************************************
+ *  END OF HARDWARE OPTIONS
+ ******************************************************************************/
+
 /* Maintainer: Put new public prototypes here ^, in libpng.3, and project
  * defs, scripts/pnglibconf.h, and scripts/pnglibconf.h.prebuilt
  */
@@ -3218,7 +3289,7 @@
  * scripts/symbols.def as well.
  */
 #ifdef PNG_EXPORT_LAST_ORDINAL
-  PNG_EXPORT_LAST_ORDINAL(243);
+  PNG_EXPORT_LAST_ORDINAL(244);
 #endif
 
 #ifdef __cplusplus
diff --git a/pngconf.h b/pngconf.h
index cbfd517..d9dbff0 100644
--- a/pngconf.h
+++ b/pngconf.h
@@ -1,7 +1,7 @@
 
 /* pngconf.h - machine configurable file for libpng
  *
- * libpng version 1.6.0 - February 14, 2013
+ * libpng version 1.6.1 - March 28, 2013
  *
  * Copyright (c) 1998-2013 Glenn Randers-Pehrson
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
@@ -218,7 +218,7 @@
 #    define PNGCAPI __watcall
 #  endif
 
-#  if defined(__GNUC__) || (defined (_MSC_VER) && (_MSC_VER >= 800))
+#  if defined(__GNUC__) || (defined(_MSC_VER) && (_MSC_VER >= 800))
 #    define PNGCAPI __cdecl
 #    if PNG_API_RULE == 1
        /* If this line results in an error __stdcall is not understood and
diff --git a/pngerror.c b/pngerror.c
index 18152d9..f469206 100644
--- a/pngerror.c
+++ b/pngerror.c
@@ -1,7 +1,7 @@
 
 /* pngerror.c - stub functions for i/o and memory allocation
  *
- * Last changed in libpng 1.6.0 [February 14, 2013]
+ * Last changed in libpng 1.6.1 [March 28, 2013]
  * Copyright (c) 1998-2013 Glenn Randers-Pehrson
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -516,7 +516,7 @@
    /* This is always supported, but for just read or just write it
     * unconditionally does the right thing.
     */
-#  if (defined PNG_READ_SUPPORTED) && (defined PNG_WRITE_SUPPORTED)
+#  if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
       if (png_ptr->mode & PNG_IS_READ_STRUCT)
 #  endif
 
@@ -530,7 +530,7 @@
       }
 #  endif
 
-#  if (defined PNG_READ_SUPPORTED) && (defined PNG_WRITE_SUPPORTED)
+#  if defined(PNG_READ_SUPPORTED) && defined(PNG_WRITE_SUPPORTED)
       else if (!(png_ptr->mode & PNG_IS_READ_STRUCT))
 #  endif
 
@@ -844,8 +844,8 @@
 }
 #endif
 
-#if defined PNG_SIMPLIFIED_READ_SUPPORTED ||\
-   defined PNG_SIMPLIFIED_WRITE_SUPPORTED
+#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
+   defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
    /* Currently the above both depend on SETJMP_SUPPORTED, however it would be
     * possible to implement without setjmp support just so long as there is some
     * way to handle the error return here:
diff --git a/pngget.c b/pngget.c
index 03c8c10..aca63a9 100644
--- a/pngget.c
+++ b/pngget.c
@@ -1,7 +1,7 @@
 
 /* pngget.c - retrieval of values from info struct
  *
- * Last changed in libpng 1.6.0 [February 14, 2013]
+ * Last changed in libpng 1.6.1 [March 28, 2013]
  * Copyright (c) 1998-2013 Glenn Randers-Pehrson
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -552,14 +552,14 @@
          *green_Z = png_float(png_ptr,
             info_ptr->colorspace.end_points_XYZ.green_Z, "cHRM green Z");
       if (blue_X != NULL)
-         *blue_X = png_float(png_ptr, info_ptr->colorspace.end_points_XYZ.blue_X,
-            "cHRM blue X");
+         *blue_X = png_float(png_ptr,
+            info_ptr->colorspace.end_points_XYZ.blue_X, "cHRM blue X");
       if (blue_Y != NULL)
-         *blue_Y = png_float(png_ptr, info_ptr->colorspace.end_points_XYZ.blue_Y,
-            "cHRM blue Y");
+         *blue_Y = png_float(png_ptr,
+            info_ptr->colorspace.end_points_XYZ.blue_Y, "cHRM blue Y");
       if (blue_Z != NULL)
-         *blue_Z = png_float(png_ptr, info_ptr->colorspace.end_points_XYZ.blue_Z,
-            "cHRM blue Z");
+         *blue_Z = png_float(png_ptr,
+            info_ptr->colorspace.end_points_XYZ.blue_Z, "cHRM blue Z");
       return (PNG_INFO_cHRM);
    }
 
@@ -844,8 +844,8 @@
 
 #ifdef PNG_sCAL_SUPPORTED
 #  ifdef PNG_FIXED_POINT_SUPPORTED
-#    if (defined PNG_FLOATING_ARITHMETIC_SUPPORTED) || \
-         (defined PNG_FLOATING_POINT_SUPPORTED)
+#    if defined(PNG_FLOATING_ARITHMETIC_SUPPORTED) || \
+         defined(PNG_FLOATING_POINT_SUPPORTED)
 png_uint_32 PNGAPI
 png_get_sCAL_fixed(png_const_structrp png_ptr, png_const_inforp info_ptr,
     int *unit, png_fixed_point *width, png_fixed_point *height)
diff --git a/pnginfo.h b/pnginfo.h
index 90f11da..26bf265 100644
--- a/pnginfo.h
+++ b/pnginfo.h
@@ -1,11 +1,11 @@
 
 /* pnginfo.h - header file for PNG reference library
  *
- * Copyright (c) 1998-2011 Glenn Randers-Pehrson
+ * Copyright (c) 1998-2013 Glenn Randers-Pehrson
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  *
- * Last changed in libpng 1.5.0 [January 6, 2011]
+ * Last changed in libpng 1.6.1 [March 28, 2013]
  *
  * This code is released under the libpng license.
  * For conditions of distribution and use, see the disclaimer
@@ -88,7 +88,7 @@
     * and initialize the appropriate fields below.
     */
 
-#if defined PNG_COLORSPACE_SUPPORTED || defined PNG_GAMMA_SUPPORTED
+#if defined(PNG_COLORSPACE_SUPPORTED) || defined(PNG_GAMMA_SUPPORTED)
    /* png_colorspace only contains 'flags' if neither GAMMA or COLORSPACE are
     * defined.  When COLORSPACE is switched on all the colorspace-defining
     * chunks should be enabled, when GAMMA is switched on all the gamma-defining
diff --git a/pngpriv.h b/pngpriv.h
index 22f1919..35e7edd 100644
--- a/pngpriv.h
+++ b/pngpriv.h
@@ -6,7 +6,7 @@
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  *
- * Last changed in libpng 1.6.0 [February 14, 2013]
+ * Last changed in libpng 1.6.1 [March 28, 2013]
  *
  * This code is released under the libpng license.
  * For conditions of distribution and use, see the disclaimer
@@ -39,9 +39,11 @@
  */
 #define _POSIX_SOURCE 1 /* Just the POSIX 1003.1 and C89 APIs */
 
+#ifndef PNG_VERSION_INFO_ONLY
 /* Standard library headers not required by png.h: */
-#include <stdlib.h>
-#include <string.h>
+#  include <stdlib.h>
+#  include <string.h>
+#endif
 
 #define PNGLIB_BUILD /*libpng is being built, not used*/
 
@@ -54,7 +56,7 @@
  * configure generated config.h.  Libpng is expected to compile without *any*
  * special build system support on a reasonably ANSI-C compliant system.
  */
-#if (defined HAVE_CONFIG_H) && !(defined PNG_NO_CONFIG_H)
+#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
 #  include <config.h>
 
    /* Pick up the definition of 'restrict' from config.h if it was read: */
@@ -71,7 +73,7 @@
 #endif
 
 /* Local renames may change non-exported API functions from png.h */
-#if defined PNG_PREFIX && !defined PNGPREFIX_H
+#if defined(PNG_PREFIX) && !defined(PNGPREFIX_H)
 #  include "pngprefix.h"
 #endif
 
@@ -177,10 +179,12 @@
 #  ifndef PNG_FLOATING_POINT_SUPPORTED
 #     define PNG_FP_EXPORT(ordinal, type, name, args)\
          PNG_INTERNAL_FUNCTION(type, name, args, PNG_EMPTY);
-      typedef struct png_incomplete png_double;
-      typedef png_double*           png_doublep;
-      typedef const png_double*     png_const_doublep;
-      typedef png_double**          png_doublepp;
+#     ifndef PNG_VERSION_INFO_ONLY
+         typedef struct png_incomplete png_double;
+         typedef png_double*           png_doublep;
+         typedef const png_double*     png_const_doublep;
+         typedef png_double**          png_doublepp;
+#     endif
 #  endif
 #endif
 #ifndef PNG_FIXED_EXPORT
@@ -191,8 +195,6 @@
 #endif
 
 #include "png.h"
-#include "pngstruct.h"
-#include "pnginfo.h"
 
 /* pngconf.h does not set PNG_DLL_EXPORT unless it is required, so: */
 #ifndef PNG_DLL_EXPORT
@@ -238,11 +240,6 @@
 #  endif
 #endif
 
-/* This is used for 16 bit gamma tables -- only the top level pointers are
- * const; this could be changed:
- */
-typedef const png_uint_16p * png_const_uint_16pp;
-
 /* Moved to pngpriv.h at libpng-1.5.0 */
 /* NOTE: some of these may have been used in external applications as
  * these definitions were exposed in pngconf.h prior to 1.5.
@@ -331,6 +328,7 @@
 #  define PNGFAPI /* PRIVATE */
 #endif
 
+#ifndef PNG_VERSION_INFO_ONLY
 /* Other defines specific to compilers can go here.  Try to keep
  * them inside an appropriate ifdef/endif pair for portability.
  */
@@ -375,6 +373,7 @@
     defined(_WIN32) || defined(__WIN32__)
 #  include <windows.h>  /* defines _WINDOWS_ macro */
 #endif
+#endif /* PNG_VERSION_INFO_ONLY */
 
 /* Moved here around 1.5.0beta36 from pngconf.h */
 /* Users may want to use these so they are not private.  Any library
@@ -553,26 +552,6 @@
    abs((int)((c1).green) - (int)((c2).green)) + \
    abs((int)((c1).blue) - (int)((c2).blue)))
 
-/* Added to libpng-1.5.7: sRGB conversion tables */
-#if defined PNG_SIMPLIFIED_READ_SUPPORTED ||\
-   defined PNG_SIMPLIFIED_WRITE_SUPPORTED
-#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
-PNG_INTERNAL_DATA(const png_uint_16, png_sRGB_table, [256]);
-   /* Convert from an sRGB encoded value 0..255 to a 16-bit linear value,
-    * 0..65535.  This table gives the closest 16-bit answers (no errors).
-    */
-#endif
-
-PNG_INTERNAL_DATA(const png_uint_16, png_sRGB_base, [512]);
-PNG_INTERNAL_DATA(const png_byte, png_sRGB_delta, [512]);
-
-#define PNG_sRGB_FROM_LINEAR(linear) ((png_byte)((png_sRGB_base[(linear)>>15] +\
-   ((((linear)&0x7fff)*png_sRGB_delta[(linear)>>15])>>12)) >> 8))
-   /* Given a value 'linear' in the range 0..255*65535 calculate the 8-bit sRGB
-    * encoded value with maximum error 0.646365.  Note that the input is not a
-    * 16-bit value; it has been multiplied by 255! */
-#endif /* PNG_SIMPLIFIED_READ/WRITE */
-
 /* Added to libpng-1.6.0: scale a 16-bit value in the range 0..65535 to 0..255
  * by dividing by 257 *with rounding*.  This macro is exact for the given range.
  * See the discourse in pngrtran.c png_do_scale_16_to_8.  The values in the
@@ -707,6 +686,39 @@
 #define PNG_GAMMA_MAC_INVERSE 65909
 #define PNG_GAMMA_sRGB_INVERSE 45455
 
+/* Almost everything below is C specific; the #defines above can be used in
+ * non-C code (so long as it is C-preprocessed) the rest of this stuff cannot.
+ */
+#ifndef PNG_VERSION_INFO_ONLY
+
+#include "pngstruct.h"
+#include "pnginfo.h"
+
+/* This is used for 16 bit gamma tables -- only the top level pointers are
+ * const; this could be changed:
+ */
+typedef const png_uint_16p * png_const_uint_16pp;
+
+/* Added to libpng-1.5.7: sRGB conversion tables */
+#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
+   defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
+#ifdef PNG_SIMPLIFIED_READ_SUPPORTED
+PNG_INTERNAL_DATA(const png_uint_16, png_sRGB_table, [256]);
+   /* Convert from an sRGB encoded value 0..255 to a 16-bit linear value,
+    * 0..65535.  This table gives the closest 16-bit answers (no errors).
+    */
+#endif
+
+PNG_INTERNAL_DATA(const png_uint_16, png_sRGB_base, [512]);
+PNG_INTERNAL_DATA(const png_byte, png_sRGB_delta, [512]);
+
+#define PNG_sRGB_FROM_LINEAR(linear) ((png_byte)((png_sRGB_base[(linear)>>15] +\
+   ((((linear)&0x7fff)*png_sRGB_delta[(linear)>>15])>>12)) >> 8))
+   /* Given a value 'linear' in the range 0..255*65535 calculate the 8-bit sRGB
+    * encoded value with maximum error 0.646365.  Note that the input is not a
+    * 16-bit value; it has been multiplied by 255! */
+#endif /* PNG_SIMPLIFIED_READ/WRITE */
+
 
 /* Inhibit C++ name-mangling for libpng functions but not for system calls. */
 #ifdef __cplusplus
@@ -757,8 +769,8 @@
 PNG_INTERNAL_FUNCTION(png_voidp,png_malloc_base,(png_const_structrp png_ptr,
    png_alloc_size_t size),PNG_ALLOCATED);
 
-#if defined PNG_TEXT_SUPPORTED || defined PNG_sPLT_SUPPORTED ||\
-   defined PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
+#if defined(PNG_TEXT_SUPPORTED) || defined(PNG_sPLT_SUPPORTED) ||\
+   defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED)
 /* Internal array allocator, outputs no error or warning messages on failure,
  * just returns NULL.  
  */
@@ -1824,8 +1836,8 @@
 #endif
 
 /* SIMPLIFIED READ/WRITE SUPPORT */
-#if defined PNG_SIMPLIFIED_READ_SUPPORTED ||\
-   defined PNG_SIMPLIFIED_WRITE_SUPPORTED
+#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) ||\
+   defined(PNG_SIMPLIFIED_WRITE_SUPPORTED)
 /* The internal structure that png_image::opaque points to. */
 typedef struct png_control
 {
@@ -1880,7 +1892,8 @@
 #endif /* SIMPLIFIED READ/WRITE */
 
 #ifdef PNG_FILTER_OPTIMIZATIONS
-PNG_INTERNAL_FUNCTION(void, PNG_FILTER_OPTIMIZATIONS, (png_structp png_ptr, unsigned int bpp), PNG_EMPTY);
+PNG_INTERNAL_FUNCTION(void, PNG_FILTER_OPTIMIZATIONS, (png_structp png_ptr,
+    unsigned int bpp), PNG_EMPTY);
    /* This is the initialization function for hardware specific optimizations,
     * one implementation (for ARM NEON machines) is contained in
     * arm/filter_neon.c.  It need not be defined - the generic code will be used
@@ -1896,4 +1909,5 @@
 }
 #endif
 
+#endif /* PNG_VERSION_INFO_ONLY */
 #endif /* PNGPRIV_H */
diff --git a/pngread.c b/pngread.c
index 5cd20e8..e32f31d 100644
--- a/pngread.c
+++ b/pngread.c
@@ -1,7 +1,7 @@
 
 /* pngread.c - read a PNG file
  *
- * Last changed in libpng 1.6.0 [February 14, 2013]
+ * Last changed in libpng 1.6.1 [March 28, 2013]
  * Copyright (c) 1998-2013 Glenn Randers-Pehrson
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -15,7 +15,7 @@
  */
 
 #include "pngpriv.h"
-#if defined PNG_SIMPLIFIED_READ_SUPPORTED && defined PNG_STDIO_SUPPORTED
+#if defined(PNG_SIMPLIFIED_READ_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
 #  include <errno.h>
 #endif
 
@@ -877,8 +877,8 @@
    png_free(png_ptr, png_ptr->save_buffer);
 #endif
 
-#if (defined PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) &&\
-   (defined PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
+#if defined(PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED) &&\
+   defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED)
    png_free(png_ptr, png_ptr->unknown_chunk.data);
 #endif
 
@@ -2004,6 +2004,28 @@
    else
       back_b = back_r = back_g = 255;
 
+   /* Default the input file gamma if required - this is necessary because
+    * libpng assumes that if no gamma information is present the data is in the
+    * output format, but the simplified API deduces the gamma from the input
+    * format.
+    */
+   if ((png_ptr->colorspace.flags & PNG_COLORSPACE_HAVE_GAMMA) == 0)
+   {
+      /* Do this directly, not using the png_colorspace functions, to ensure
+       * that it happens even if the colorspace is invalid (though probably if
+       * it is the setting will be ignored)  Note that the same thing can be
+       * achieved at the application interface with png_set_gAMA.
+       */
+      if (png_ptr->bit_depth == 16 &&
+         (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
+         png_ptr->colorspace.gamma = PNG_GAMMA_LINEAR;
+
+      else
+         png_ptr->colorspace.gamma = PNG_GAMMA_sRGB_INVERSE;
+
+      png_ptr->colorspace.flags |= PNG_COLORSPACE_HAVE_GAMMA;
+   }
+
    /* Decide what to do based on the PNG color type of the input data.  The
     * utility function png_create_colormap_entry deals with most aspects of the
     * output transformations; this code works out how to produce bytes of
@@ -3547,7 +3569,8 @@
       {
          png_fixed_point input_gamma_default;
 
-         if (base_format & PNG_FORMAT_FLAG_LINEAR)
+         if ((base_format & PNG_FORMAT_FLAG_LINEAR) &&
+            (image->flags & PNG_IMAGE_FLAG_16BIT_sRGB) == 0)
             input_gamma_default = PNG_GAMMA_LINEAR;
          else
             input_gamma_default = PNG_DEFAULT_sRGB;
diff --git a/pngrtran.c b/pngrtran.c
index a485cac..626f3b0 100644
--- a/pngrtran.c
+++ b/pngrtran.c
@@ -1,7 +1,7 @@
 
 /* pngrtran.c - transforms the data in a row for PNG readers
  *
- * Last changed in libpng 1.6.0 [February 14, 2013]
+ * Last changed in libpng 1.6.1 [March 28, 2013]
  * Copyright (c) 1998-2013 Glenn Randers-Pehrson
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -1412,7 +1412,7 @@
 #endif
 
 #ifdef PNG_READ_GRAY_TO_RGB_SUPPORTED
-#if defined PNG_READ_EXPAND_SUPPORTED && defined PNG_READ_BACKGROUND_SUPPORTED
+#if defined(PNG_READ_EXPAND_SUPPORTED) && defined(PNG_READ_BACKGROUND_SUPPORTED)
    /* Detect gray background and attempt to enable optimization for
     * gray --> RGB case.
     *
@@ -2239,8 +2239,8 @@
       png_do_gray_to_rgb(row_info, png_ptr->row_buf + 1);
 #endif
 
-#if (defined PNG_READ_BACKGROUND_SUPPORTED) ||\
-   (defined PNG_READ_ALPHA_MODE_SUPPORTED)
+#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
+   defined(PNG_READ_ALPHA_MODE_SUPPORTED)
    if (png_ptr->transformations & PNG_COMPOSE)
       png_do_compose(row_info, png_ptr->row_buf + 1, png_ptr);
 #endif
@@ -2251,8 +2251,8 @@
       /* Because RGB_TO_GRAY does the gamma transform. */
       !(png_ptr->transformations & PNG_RGB_TO_GRAY) &&
 #endif
-#if (defined PNG_READ_BACKGROUND_SUPPORTED) ||\
-   (defined PNG_READ_ALPHA_MODE_SUPPORTED)
+#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
+   defined(PNG_READ_ALPHA_MODE_SUPPORTED)
       /* Because PNG_COMPOSE does the gamma transform if there is something to
        * do (if there is an alpha channel or transparency.)
        */
@@ -3518,8 +3518,8 @@
 
 
 #ifdef PNG_READ_TRANSFORMS_SUPPORTED
-#if (defined PNG_READ_BACKGROUND_SUPPORTED) ||\
-   (defined PNG_READ_ALPHA_MODE_SUPPORTED)
+#if defined(PNG_READ_BACKGROUND_SUPPORTED) ||\
+   defined(PNG_READ_ALPHA_MODE_SUPPORTED)
 /* Replace any alpha or transparency with the supplied background color.
  * "background" is already in the screen gamma, while "background_1" is
  * at a gamma of 1.0.  Paletted files have already been taken care of.
diff --git a/pngrutil.c b/pngrutil.c
index e660757..3f4474c 100644
--- a/pngrutil.c
+++ b/pngrutil.c
@@ -1,8 +1,8 @@
 
 /* pngrutil.c - utilities to read a PNG file
  *
- * Last changed in libpng 1.6.0 [(PENDING RELEASE)]
- * Copyright (c) 1998-2012 Glenn Randers-Pehrson
+ * Last changed in libpng 1.6.1 [March 28, 2013]
+ * Copyright (c) 1998-2013 Glenn Randers-Pehrson
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  *
@@ -2804,21 +2804,26 @@
             {
                /* If the keep value is 'default' or 'never' override it, but
                 * still error out on critical chunks unless the keep value is
-                * 'always'  While this is weird it is the behavior in 1.4.12.  A
-                * possible improvement would be to obey the value set for the
+                * 'always'  While this is weird it is the behavior in 1.4.12.
+                * A possible improvement would be to obey the value set for the
                 * chunk, but this would be an API change that would probably
                 * damage some applications.
                 *
                 * The png_app_warning below catches the case that matters, where
-                * the application has neither set specific save for this chunk
-                * or global save.
+                * the application has not set specific save or ignore for this
+                * chunk or global save or ignore.
                 */
                if (keep < PNG_HANDLE_CHUNK_IF_SAFE)
                {
 #                 ifdef PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
                      if (png_ptr->unknown_default < PNG_HANDLE_CHUNK_IF_SAFE)
+                     {
+                        png_chunk_warning(png_ptr, "Saving unknown chunk:");
                         png_app_warning(png_ptr,
- "forcing save of an unhandled chunk; please call png_set_keep_unknown_chunks");
+                           "forcing save of an unhandled chunk;"
+                           " please call png_set_keep_unknown_chunks");
+                           /* with keep = PNG_HANDLE_CHUNK_IF_SAFE */
+                     }
 #                 endif
                   keep = PNG_HANDLE_CHUNK_IF_SAFE;
                }
@@ -3350,7 +3355,7 @@
                      png_uint_32p dp32 = png_aligncast(png_uint_32p,dp);
                      png_const_uint_32p sp32 = png_aligncastconst(
                         png_const_uint_32p, sp);
-                     unsigned int skip = (bytes_to_jump-bytes_to_copy) /
+                     size_t skip = (bytes_to_jump-bytes_to_copy) /
                         (sizeof (png_uint_32));
 
                      do
@@ -3392,7 +3397,7 @@
                      png_uint_16p dp16 = png_aligncast(png_uint_16p, dp);
                      png_const_uint_16p sp16 = png_aligncastconst(
                         png_const_uint_16p, sp);
-                     unsigned int skip = (bytes_to_jump-bytes_to_copy) /
+                     size_t skip = (bytes_to_jump-bytes_to_copy) /
                         (sizeof (png_uint_16));
 
                      do
diff --git a/pngset.c b/pngset.c
index 84973f5..c88d621 100644
--- a/pngset.c
+++ b/pngset.c
@@ -1,7 +1,7 @@
 
 /* pngset.c - storage of image information into info struct
  *
- * Last changed in libpng 1.6.0 [February 14, 2013]
+ * Last changed in libpng 1.6.1 [March 28, 2013]
  * Copyright (c) 1998-2013 Glenn Randers-Pehrson
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -514,7 +514,7 @@
 
    png_debug1(1, "in %s storage function", "PLTE");
 
-   if (png_ptr == NULL || info_ptr == NULL || palette == NULL)
+   if (png_ptr == NULL || info_ptr == NULL)
       return;
 
    if (num_palette < 0 || num_palette > PNG_MAX_PALETTE_LENGTH)
@@ -529,6 +529,17 @@
       }
    }
 
+   if ((num_palette > 0 && palette == NULL) ||
+      (num_palette == 0
+#        ifdef PNG_MNG_FEATURES_SUPPORTED
+            && (png_ptr->mng_features_permitted & PNG_FLAG_MNG_EMPTY_PLTE) == 0
+#        endif
+      ))
+   {
+      png_chunk_report(png_ptr, "Invalid palette", PNG_CHUNK_ERROR);
+      return;
+   }
+
    /* It may not actually be necessary to set png_ptr->palette here;
     * we do it for backward compatibility with the way the png_handle_tRNS
     * function used to do the allocation.
@@ -545,7 +556,8 @@
    png_ptr->palette = png_voidcast(png_colorp, png_calloc(png_ptr,
        PNG_MAX_PALETTE_LENGTH * (sizeof (png_color))));
 
-   memcpy(png_ptr->palette, palette, num_palette * (sizeof (png_color)));
+   if (num_palette > 0)
+      memcpy(png_ptr->palette, palette, num_palette * (sizeof (png_color)));
    info_ptr->palette = png_ptr->palette;
    info_ptr->num_palette = png_ptr->num_palette = (png_uint_16)num_palette;
 
@@ -1116,16 +1128,16 @@
     * code) but may be meaningless if the read or write handling of unknown
     * chunks is not compiled in.
     */
-#  if !(defined PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) && \
-      (defined PNG_READ_SUPPORTED)
+#  if !defined(PNG_READ_UNKNOWN_CHUNKS_SUPPORTED) && \
+      defined(PNG_READ_SUPPORTED)
       if (png_ptr->mode & PNG_IS_READ_STRUCT)
       {
          png_app_error(png_ptr, "no unknown chunk support on read");
          return;
       }
 #  endif
-#  if !(defined PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) && \
-      (defined PNG_WRITE_SUPPORTED)
+#  if !defined(PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED) && \
+      defined(PNG_WRITE_SUPPORTED)
       if (!(png_ptr->mode & PNG_IS_READ_STRUCT))
       {
          png_app_error(png_ptr, "no unknown chunk support on write");
diff --git a/pngstruct.h b/pngstruct.h
index b8202d1..d58c028 100644
--- a/pngstruct.h
+++ b/pngstruct.h
@@ -5,7 +5,7 @@
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
  *
- * Last changed in libpng 1.6.0 [February 14, 2013]
+ * Last changed in libpng 1.6.1 [March 28, 2013]
  *
  * This code is released under the libpng license.
  * For conditions of distribution and use, see the disclaimer
@@ -100,7 +100,7 @@
 } png_XYZ;
 #endif /* COLORSPACE */
 
-#if defined PNG_COLORSPACE_SUPPORTED || defined PNG_GAMMA_SUPPORTED
+#if defined(PNG_COLORSPACE_SUPPORTED) || defined(PNG_GAMMA_SUPPORTED)
 /* A colorspace is all the above plus, potentially, profile information,
  * however at present libpng does not use the profile internally so it is only
  * stored in the png_info struct (if iCCP is supported.)  The rendering intent
@@ -357,6 +357,11 @@
    png_uint_16p inv_filter_costs;    /* 1/relative filter calculation cost */
 #endif
 
+   /* Options */
+#ifdef PNG_SET_OPTION_SUPPORTED
+   png_byte options;           /* On/off state (up to 4 options) */
+#endif
+
 #if PNG_LIBPNG_VER < 10700
 /* To do: remove this from libpng-1.7 */
 #ifdef PNG_TIME_RFC1123_SUPPORTED
@@ -471,11 +476,12 @@
 /* New member added in libpng-1.5.6 */
    png_bytep big_prev_row;
 
+/* New member added in libpng-1.5.7 */
    void (*read_filter[PNG_FILTER_VALUE_LAST-1])(png_row_infop row_info,
       png_bytep row, png_const_bytep prev_row);
 
 #ifdef PNG_READ_SUPPORTED
-#if defined PNG_COLORSPACE_SUPPORTED || defined PNG_GAMMA_SUPPORTED
+#if defined(PNG_COLORSPACE_SUPPORTED) || defined(PNG_GAMMA_SUPPORTED)
    png_colorspace   colorspace;
 #endif
 #endif
diff --git a/pngtest.c b/pngtest.c
index 4787a24..fd0e817 100644
--- a/pngtest.c
+++ b/pngtest.c
@@ -1,7 +1,7 @@
 
 /* pngtest.c - a simple test program to test libpng
  *
- * Last changed in libpng 1.6.0 [February 14, 2013]
+ * Last changed in libpng 1.6.1 [March 28, 2013]
  * Copyright (c) 1998-2013 Glenn Randers-Pehrson
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -1470,7 +1470,8 @@
 
    /* When the unknown vpAg/sTER chunks are written by pngtest the only way to
     * do it is to write them *before* calling png_write_end.  When unknown
-    * chunks are written by libpng, however, they are written just before IEND.     * There seems to be no way round this, however vpAg/sTER are not expected
+    * chunks are written by libpng, however, they are written just before IEND.
+    * There seems to be no way round this, however vpAg/sTER are not expected
     * after IDAT.
     */
    write_chunks(write_ptr, after_IDAT);
@@ -1949,4 +1950,4 @@
 #endif
 
 /* Generate a compiler error if there is an old png.h in the search path. */
-typedef png_libpng_version_1_6_0 Your_png_h_is_not_version_1_6_0;
+typedef png_libpng_version_1_6_1 Your_png_h_is_not_version_1_6_1;
diff --git a/pngwrite.c b/pngwrite.c
index aa18712..b0887bd 100644
--- a/pngwrite.c
+++ b/pngwrite.c
@@ -1,7 +1,7 @@
 
 /* pngwrite.c - general routines to write a PNG file
  *
- * Last changed in libpng 1.6.0 [February 14, 2013]
+ * Last changed in libpng 1.6.1 [March 28, 2013]
  * Copyright (c) 1998-2013 Glenn Randers-Pehrson
  * (Version 0.96 Copyright (c) 1996, 1997 Andreas Dilger)
  * (Version 0.88 Copyright (c) 1995, 1996 Guy Eric Schalnat, Group 42, Inc.)
@@ -12,7 +12,7 @@
  */
 
 #include "pngpriv.h"
-#if defined PNG_SIMPLIFIED_WRITE_SUPPORTED && defined PNG_STDIO_SUPPORTED
+#if defined(PNG_SIMPLIFIED_WRITE_SUPPORTED) && defined(PNG_STDIO_SUPPORTED)
 #  include <errno.h>
 #endif
 
diff --git a/projects/vstudio/readme.txt b/projects/vstudio/readme.txt
index 556a85a..bc16eb1 100644
--- a/projects/vstudio/readme.txt
+++ b/projects/vstudio/readme.txt
@@ -1,7 +1,7 @@
 
 VisualStudio instructions
 
-libpng version 1.6.0 - February 14, 2013
+libpng version 1.6.1 - March 28, 2013
 
 Copyright (c) 1998-2010 Glenn Randers-Pehrson
 
diff --git a/projects/vstudio/zlib.props b/projects/vstudio/zlib.props
index c88e7ba..8e92fdc 100644
--- a/projects/vstudio/zlib.props
+++ b/projects/vstudio/zlib.props
@@ -2,7 +2,7 @@
 <!--
  * zlib.props - location of zlib source
  *
- * libpng version 1.6.0 - February 14, 2013
+ * libpng version 1.6.1 - March 28, 2013
  *
  * Copyright (c) 1998-2011 Glenn Randers-Pehrson
  *
diff --git a/scripts/README.txt b/scripts/README.txt
index 907292f..27b5e52 100644
--- a/scripts/README.txt
+++ b/scripts/README.txt
@@ -1,9 +1,9 @@
 
-Makefiles for  libpng version 1.6.0 - February 14, 2013
+Makefiles for  libpng version 1.6.1 - March 28, 2013
 
 pnglibconf.h.prebuilt       =>  Stores configuration settings
  makefile.linux    =>  Linux/ELF makefile
-                       (gcc, creates libpng16.so.16.1.6.0)
+                       (gcc, creates libpng16.so.16.1.6.1)
  makefile.gcc      =>  Generic makefile (gcc, creates static libpng.a)
  makefile.knr      =>  Archaic UNIX Makefile that converts files with
                        ansi2knr (Requires ansi2knr.c from
@@ -20,7 +20,7 @@
  makefile.dec      =>  DEC Alpha UNIX makefile
  makefile.dj2      =>  DJGPP 2 makefile
  makefile.elf      =>  Linux/ELF makefile symbol versioning,
-                       (gcc, creates libpng16.so.16.1.6.0)
+                       (gcc, creates libpng16.so.16.1.6.1)
  makefile.freebsd  =>  FreeBSD makefile
  makefile.gcc      =>  Generic gcc makefile
  makefile.hpgcc    =>  HPUX makefile using gcc
@@ -35,12 +35,12 @@
  makefile.os2      =>  OS/2 Makefile (gcc and emx, requires libpng.def)
  makefile.sco      =>  For SCO OSr5  ELF and Unixware 7 with Native cc
  makefile.sggcc    =>  Silicon Graphics (gcc,
-                       creates libpng16.so.16.1.6.0)
+                       creates libpng16.so.16.1.6.1)
  makefile.sgi      =>  Silicon Graphics IRIX makefile (cc, creates static lib)
  makefile.solaris  =>  Solaris 2.X makefile (gcc,
-                       creates libpng16.so.16.1.6.0)
+                       creates libpng16.so.16.1.6.1)
  makefile.so9      =>  Solaris 9 makefile (gcc,
-                       creates libpng16.so.16.1.6.0)
+                       creates libpng16.so.16.1.6.1)
  makefile.std      =>  Generic UNIX makefile (cc, creates static libpng.a)
  makefile.sunos    =>  Sun makefile
  makefile.32sunu   =>  Sun Ultra 32-bit makefile
diff --git a/scripts/checksym.awk b/scripts/checksym.awk
index 6857dff..fe3af55 100755
--- a/scripts/checksym.awk
+++ b/scripts/checksym.awk
@@ -113,6 +113,16 @@
       err = 1
    }
    unexported=0
+   # Add a standard header to symbols.new:
+   print ";Version INSERT-VERSION-HERE" >of
+   print ";--------------------------------------------------------------" >of
+   print "; LIBPNG symbol list as a Win32 DEF file" >of
+   print "; Contains all the symbols that can be exported from libpng" >of
+   print ";--------------------------------------------------------------" >of
+   print "LIBRARY" >of
+   print "" >of
+   print "EXPORTS" >of
+
    for (o=1; o<=lasto; ++o) {
       if (symbol[o] == "" && removed[o] == "") {
          if (unexported == 0) unexported = o
diff --git a/scripts/def.dfn b/scripts/def.dfn
index cac3cd8..5e1dc68 100644
--- a/scripts/def.dfn
+++ b/scripts/def.dfn
@@ -8,31 +8,22 @@
  * and license in png.h
  */
 
-/* These macros exist to make the header and trailer shorter below: */
-#define S PNG_DEFN_MAGIC
-#define E PNG_DEFN_END
-
 /* Write the export file header: */
-S-;---------------------------------------------------------------E
-S-; LIBPNG module definition file for OS/2-E
-S-;---------------------------------------------------------------E
-S--E
-S-; If you give the library an explicit name one or other files-E
-S-; may need modifying to support the new name on one or more-E
-S-; systems.-E
-S-LIBRARY-E
-S-OS2 DESCRIPTION "PNG image compression library"-E
-S-OS2 CODE PRELOAD MOVEABLE DISCARDABLE-E
-S--E
-S-EXPORTS-E
-S-;Version 1.6.0-E
+PNG_DFN ";--------------------------------------------------------------"
+PNG_DFN "; LIBPNG module definition file for OS/2"
+PNG_DFN ";--------------------------------------------------------------"
+PNG_DFN ""
+PNG_DFN "; If you give the library an explicit name one or other files"
+PNG_DFN "; may need modifying to support the new name on one or more"
+PNG_DFN "; systems."
+PNG_DFN "LIBRARY"
+PNG_DFN "OS2 DESCRIPTION "PNG image compression library""
+PNG_DFN "OS2 CODE PRELOAD MOVEABLE DISCARDABLE"
+PNG_DFN ""
+PNG_DFN "EXPORTS"
+PNG_DFN ";Version 1.6.1"
 
-/* NOTE: PNG_JOIN is interpreted by the calling script as a signal to
- * join the two things on either side, so we can do symbol
- * substitution within the name, regular C ## joins the pp-tokens,
- * not their final values.
- */
 #define PNG_EXPORTA(ordinal, type, name, args, attributes)\
-	PNG_DEFN_MAGIC- SYMBOL_PREFIX PNG_JOIN name-PNG_DEFN_END
+        PNG_DFN "@" SYMBOL_PREFIX "@@" name "@"
 
 #include "../png.h"
diff --git a/scripts/dfn.awk b/scripts/dfn.awk
new file mode 100644
index 0000000..89b92d5
--- /dev/null
+++ b/scripts/dfn.awk
Binary files differ
diff --git a/scripts/intprefix.dfn b/scripts/intprefix.dfn
index d48d540..790bdfe 100644
--- a/scripts/intprefix.dfn
+++ b/scripts/intprefix.dfn
@@ -10,10 +10,10 @@
  */
 
 #define PNG_INTERNAL_DATA(type, name, array)\
-	PNG_DEFN_MAGIC-name-PNG_DEFN_END
+        PNG_DFN "@" name "@"
 
 #define PNG_INTERNAL_FUNCTION(type, name, args, attributes)\
-	PNG_DEFN_MAGIC-name-PNG_DEFN_END
+        PNG_DFN "@" name "@"
 
 #define PNGPREFIX_H /* self generation */
 #include "../pngpriv.h"
diff --git a/scripts/libpng-config-head.in b/scripts/libpng-config-head.in
index 02d7400..9803d2e 100755
--- a/scripts/libpng-config-head.in
+++ b/scripts/libpng-config-head.in
@@ -11,7 +11,7 @@
 
 # Modeled after libxml-config.
 
-version=1.6.0
+version=1.6.1
 prefix=""
 libdir=""
 libs=""
diff --git a/scripts/libpng.pc.in b/scripts/libpng.pc.in
index 367b38c..9bdfd73 100644
--- a/scripts/libpng.pc.in
+++ b/scripts/libpng.pc.in
@@ -5,6 +5,6 @@
 
 Name: libpng
 Description: Loads and saves PNG files
-Version: 1.6.0
+Version: 1.6.1
 Libs: -L${libdir} -lpng16
 Cflags: -I${includedir}
diff --git a/scripts/makefile.cegcc b/scripts/makefile.cegcc
index 820518a..3912afe 100644
--- a/scripts/makefile.cegcc
+++ b/scripts/makefile.cegcc
@@ -23,7 +23,7 @@
 
 VERMAJ = 1
 VERMIN = 6
-VERMIC = 0
+VERMIC = 1
 VER = $(VERMAJ).$(VERMIN).$(VERMIC)
 NAME = libpng
 PACKAGE = $(NAME)-$(VER)
diff --git a/scripts/makefile.freebsd b/scripts/makefile.freebsd
index bc03393..0ddf4ae 100644
--- a/scripts/makefile.freebsd
+++ b/scripts/makefile.freebsd
@@ -26,19 +26,24 @@
 		libpng/pngconf.h ${INCSDIR}/../pngconf.h \
 		libpng/pnglibconf.h ${INCSDIR}/../pnglibconf.h
 
-LDADD+=         -lm -lz
-#LDADD+=         -lm -lz -lssp_nonshared   # for OSVERSION < 800000 ?
+# where make install finds libz.a and zlib.h
+ZLIBLIB=	/usr/lib
+ZLIBINC=	/usr/include
+
+LDADD+=        -lm -lz
+#LDADD+=       -lm -lz -lssp_nonshared   # for OSVERSION < 800000 ?
 
 DPADD+=         ${LIBM} ${LIBZ}
 
-CFLAGS+= -I.
+CFLAGS+= -I. -I${ZLIBINC}
 
 SRCS=	png.c pngset.c pngget.c pngrutil.c pngtrans.c pngwutil.c \
 	pngread.c pngrio.c pngwio.c pngwrite.c pngrtran.c \
 	pngwtran.c pngmem.c pngerror.c pngpread.c
 
 pngtest: pngtest.o libpng.a
-	${CC} ${CFLAGS} -L. -static -o pngtest pngtest.o -lpng -lz -lm
+	${CC} ${CFLAGS} -L. -static -o pngtest pngtest.o -L${ZLIBLIB} \
+	-lpng ${LDADD}
 
 CLEANFILES= pngtest pngtest.o pngout.png
 
diff --git a/scripts/makefile.linux b/scripts/makefile.linux
index 6e95c87..4e94ce9 100644
--- a/scripts/makefile.linux
+++ b/scripts/makefile.linux
@@ -10,7 +10,7 @@
 # Library name:
 LIBNAME = libpng16
 PNGMAJ = 16
-RELEASE = 0
+RELEASE = 1
 
 # Shared library names:
 LIBSO=$(LIBNAME).so
diff --git a/scripts/makefile.msys b/scripts/makefile.msys
index 074fb58..342c912 100644
--- a/scripts/makefile.msys
+++ b/scripts/makefile.msys
@@ -18,7 +18,7 @@
 # Library name:
 LIBNAME = libpng16
 PNGMAJ = 16
-RELEASE = 0
+RELEASE = 1
 
 # Shared library names:
 LIBSO=$(LIBNAME).dll
@@ -43,7 +43,7 @@
 
 #ARCH = -march=pentium3
 #ARCH = -march=i686
-ARCH = 
+ARCH =
 CDEBUG = -g -DPNG_DEBUG=5
 LDDEBUG =
 CRELEASE = -O2
diff --git a/scripts/makefile.ne12bsd b/scripts/makefile.ne12bsd
index a20bc4e..2313dd3 100644
--- a/scripts/makefile.ne12bsd
+++ b/scripts/makefile.ne12bsd
@@ -17,7 +17,7 @@
 
 LIB=	png16
 SHLIB_MAJOR=	0
-SHLIB_MINOR=	1.6.0
+SHLIB_MINOR=	1.6.1
 SRCS=	png.c pngset.c pngget.c pngrutil.c pngtrans.c pngwutil.c \
 	pngread.c pngrio.c pngwio.c pngwrite.c pngrtran.c \
 	pngwtran.c pngmem.c pngerror.c pngpread.c
diff --git a/scripts/makefile.netbsd b/scripts/makefile.netbsd
index 719e8c3..41a2e9d 100644
--- a/scripts/makefile.netbsd
+++ b/scripts/makefile.netbsd
@@ -17,7 +17,7 @@
 
 LIB=	png
 SHLIB_MAJOR=	16
-SHLIB_MINOR=	1.6.0
+SHLIB_MINOR=	1.6.1
 SRCS=	png.c pngset.c pngget.c pngrutil.c pngtrans.c pngwutil.c \
 	pngread.c pngrio.c pngwio.c pngwrite.c pngrtran.c \
 	pngwtran.c pngmem.c pngerror.c pngpread.c
diff --git a/scripts/makefile.openbsd b/scripts/makefile.openbsd
index 447b38b..2d63374 100644
--- a/scripts/makefile.openbsd
+++ b/scripts/makefile.openbsd
@@ -11,7 +11,7 @@
 MANDIR= ${PREFIX}/man/cat
 
 SHLIB_MAJOR=	16
-SHLIB_MINOR=	1.6.0
+SHLIB_MINOR=	1.6.1
 
 LIB=	png
 SRCS=	png.c pngerror.c pngget.c pngmem.c pngpread.c \
diff --git a/scripts/makefile.std b/scripts/makefile.std
index af70328..419eeed 100644
--- a/scripts/makefile.std
+++ b/scripts/makefile.std
@@ -39,6 +39,7 @@
 ECHO = echo
 
 DFNFLAGS = # DFNFLAGS contains -D options to use in the libpng build
+DFA_EXTRA = # extra files that can be used to control configuration
 CFLAGS=-I$(ZLIBINC) -O # -g -DPNG_DEBUG=5
 LDFLAGS=-L. -L$(ZLIBLIB) -lpng -lz -lm
 
@@ -50,23 +51,23 @@
 
 # The standard pnglibconf.h exists as scripts/pnglibconf.h.prebuilt,
 # copy this if the following doesn't work.
-pnglibconf.dfn: scripts/pnglibconf.dfa scripts/options.awk pngconf.h
-	$(RM_F) $@ dfn?.out
-	$(AWK) -f scripts/options.awk out=dfn1.out version=search pngconf.h\
-	    scripts/pnglibconf.dfa $(DFA_XTRA) 1>&2
-	$(AWK) -f scripts/options.awk out=dfn2.out dfn1.out 1>&2
-	cp dfn2.out $@
-	$(RM_F) dfn?.out
-
 pnglibconf.h: pnglibconf.dfn
-	$(RM_F) $@ dfn.c dfn?.out
-	$(ECHO) '#include "pnglibconf.dfn"' >dfn.c
-	$(CPP) $(DFNFLAGS) dfn.c >dfn1.out
-	$(SED) -n -e 's|^.*PNG_DEFN_MAGIC *-\(.*\)- *PNG_DEFN_END.*$$|\1|p'\
-	    dfn1.out >dfn2.out
-	$(SED) -e 's| *PNG_JOIN *||g' -e 's| *$$||' dfn2.out >dfn3.out
-	cp dfn3.out $@
-	$(RM_F) dfn.c dfn?.out
+	$(RM_F) $@ pnglibconf.c pnglibconf.out pnglibconf.tmp
+	$(ECHO) '#include "pnglibconf.dfn"' >pnglibconf.c
+	$(ECHO) "If '$(CC) -E' crashes try /lib/cpp (e.g. CPP='/lib/cpp')" >&2
+	$(CPP) $(DFNFLAGS) pnglibconf.c >pnglibconf.out
+	$(AWK) -f "scripts/dfn.awk" out="pnglibconf.tmp" pnglibconf.out 1>&2
+	mv pnglibconf.tmp $@
+
+pnglibconf.dfn: scripts/pnglibconf.dfa scripts/options.awk pngconf.h pngusr.dfa $(DFA_XTRA)
+	$(RM_F) $@ pnglibconf.pre pnglibconf.tmp
+	$(ECHO) "Calling $(AWK) from scripts/pnglibconf.mak" >&2
+	$(ECHO) "If 'awk' crashes try a better awk (e.g. AWK='nawk')" >&2
+	$(AWK) -f scripts/options.awk out="pnglibconf.pre"\
+	    version=search pngconf.h scripts/pnglibconf.dfa\
+	    pngusr.dfa $(DFA_XTRA) 1>&2
+	$(AWK) -f scripts/options.awk out="pnglibconf.tmp" pnglibconf.pre 1>&2
+	mv pnglibconf.tmp $@
 
 libpng.a: $(OBJS)
 	$(AR_RC) $@  $(OBJS)
@@ -96,7 +97,7 @@
 	chmod 644 $(DESTDIR)$(LIBPATH)/libpng.a
 
 clean:
-	$(RM_F) *.o libpng.a pngtest pngout.png pnglibconf.* dfn.c dfn?.out
+	$(RM_F) *.o libpng.a pngtest pngout.png pnglibconf.*
 
 DOCS = ANNOUNCE CHANGES INSTALL KNOWNBUG LICENSE README TODO Y2KINFO
 writelock:
diff --git a/scripts/options.awk b/scripts/options.awk
index cb8cd18..c3a8501 100755
--- a/scripts/options.awk
+++ b/scripts/options.awk
@@ -30,22 +30,33 @@
 # are copied to the preprocessed file).
 
 BEGIN{
-   out="/dev/null"              # intermediate, preprocessed, file
+   out=""                       # intermediate, preprocessed, file
    pre=-1                       # preprocess (first line)
    version="libpng version unknown" # version information
    version_file=""              # where to find the version
    err=0                        # in-line exit sets this
-   start="PNG_DEFN_MAGIC-"      # Arbitrary start
-   end="-PNG_DEFN_END"          # Arbitrary end
-   ct="PNG_JOIN"                # Join two tokens
-   cx= "/" ct "*"               # Open C comment for output file
-   comment=start cx             # Comment start
+   # The following definitions prevent the C preprocessor noticing the lines
+   # that will be in the final output file.  Some C preprocessors tokenise
+   # the lines, for example by inserting spaces around operators, and all
+   # C preprocessors notice lines that start with '#', most remove comments.
+   # The technique adopted here is to make the final output lines into
+   # C strings (enclosed in double quotes), preceeded by PNG_DFN.  As a
+   # consequence the output cannot contain a 'raw' double quote - instead put
+   # @' in, this will be replaced by a single " afterward.  See the parser
+   # script dfn.awk for more capabilities (not required here).  Note that if
+   # you need a " in a 'setting' in pnglibconf.dfa it must also be @'!
+   dq="@'"                      # For a single double quote
+   start=" PNG_DFN \""          # Start stuff to output (can't contain a "!)
+   end="\" "                    # End stuff to output
+   subs="@\" "                  # Substitute start (substitute a C macro)
+   sube=" \"@"                  # Substitute end
+   comment=start "/*"           # Comment start
    cend="*/" end                # Comment end
-   def=start "#define PNG_" ct  # Arbitrary define
-   sup=ct "_SUPPORTED" end      # end supported option
-   und=comment "#undef PNG_" ct # Unsupported option
-   une=ct "_SUPPORTED" cend     # end unsupported option
-   error=start "ERROR:"         # error message
+   def=start "#define PNG_"     # Arbitrary define
+   sup="_SUPPORTED" end         # end supported option
+   und=comment "#undef PNG_"    # Unsupported option
+   une="_SUPPORTED" cend        # end unsupported option
+   error=start "ERROR:"         # error message, terminate with 'end'
 
    # Variables
    deb=0                        # debug - set on command line
@@ -53,11 +64,15 @@
    logunsupported=0             # write unsupported options too
 
    # Precreate arrays
+   # for each option:
    option[""] = ""    # list of all options: default enabled/disabled
    done[""] = 1       # marks option as having been output
    requires[""] = ""  # requires by option
    iffs[""] = ""      # if by option
    enabledby[""] = "" # options that enable it by option
+   sets[""] = ""      # settings set by each option
+   setval[""] = ""    # value to set (indexed: 'option sets[option]')
+   # for each setting:
    setting[""] = ""   # requires by setting
    defaults[""] = ""  # used for a defaulted value
    doneset[""] = 1    # marks setting as having been output
@@ -68,7 +83,7 @@
 }
 
 # The output file must be specified before any input:
-out == "/dev/null" {
+out == "" {
    print "out=output.file must be given on the command line"
    err = 1
    exit 1
@@ -102,7 +117,6 @@
 
 pre && version == "search" && $0 ~ /^ \* libpng version/{
    version = substr($0, 4)
-   gsub(/\./, " PNG_JOIN . PNG_JOIN", version)
    print "version =", version >out
    next
 }
@@ -190,7 +204,7 @@
    if (NF > 1) {
       # sub(/^[ 	]*com[ 	]*/, "")
       $1 = ""
-      print comment, $0, cend >out
+      print comment $0, cend >out
    } else
       print start end >out
    next
@@ -227,7 +241,9 @@
    next
 }
 
-# option NAME ( (requires|enables|if) NAME* | on | off | disabled )*
+# option NAME ( (requires|enables|if) NAME* | on | off | disabled |
+#                sets SETTING VALUE+ )*
+#     
 #   Declares an option 'NAME' and describes its default setting (disabled)
 #   and its relationship to other options.  The option is disabled
 #   unless *all* the options listed after 'requires' are set and at
@@ -242,46 +258,77 @@
 #   be later) entry may turn an option on or off explicitly.
 
 $1 == "option" && NF >= 2{
-   onoff = option[$2]  # records current (and the default is "", enabled)
+   opt = $2
+   sub(/,$/,"",opt)
+   onoff = option[opt]  # records current (and the default is "", enabled)
    key = ""
-   for (i=3; i<=NF; ++i) {
-      if ($(i) == "on" || $(i) == "off" || $(i) == "disabled") {
-         key = ""
-         if (onoff != $(i)) {
-            # on or off can zap disabled or enabled:
-            if (onoff == "" || (onoff == "disabled" || onoff == "enabled") && ($(i) == "on" || $(i) == "off")) {
-               # It's easy to mis-spell the option when turning it
-               # on or off, so warn about it here:
-               if (onoff == "" && ($(i) == "on" || $(i) == "off")) {
-                  print $2 ": ERROR: turning unrecognized option", $(i)
-                  # For the moment error out - it is safer
-                  err = 1 # prevent END{} running
-                  exit 1
-               }
-               onoff = $(i)
-            } else {
-               # Print a message, otherwise the error
-               # below is incomprehensible
-               print $2 ": currently", onoff ": attempt to turn", $(i)
-               break
-            }
+   istart = 3
+   do {
+      if (istart == 1) {     # continuation line
+         val = getline
+
+         if (val != 1) { # error reading it
+            if (val == 0)
+               print "option", opt ": ERROR: missing contination line"
+            else
+               print "option", opt ": ERROR: error reading continuation line"
+
+            # This is a hard error
+            err = 1 # prevent END{} running
+            exit 1
          }
-      } else if ($(i) == "requires" || $(i) == "if" || $(i) == "enables") {
-         key = $(i)
-      } else if (key == "requires") {
-         requires[$2] = requires[$2] " " $(i)
-      } else if (key == "if") {
-         iffs[$2] = iffs[$2] " " $(i)
-      } else if (key == "enables") {
-         enabledby[$(i)] = enabledby[$(i)] " " $2
-      } else
-         break # bad line format
-   }
+      }
+
+      for (i=istart; i<=NF; ++i) {
+         val=$(i)
+         sub(/,$/,"",val)
+         if (val == "on" || val == "off" || val == "disabled") {
+            key = ""
+            if (onoff != val) {
+               # on or off can zap disabled or enabled:
+               if (onoff == "" || (onoff == "disabled" || onoff == "enabled") &&
+                   (val == "on" || val == "off")) {
+                  # It's easy to mis-spell the option when turning it
+                  # on or off, so warn about it here:
+                  if (onoff == "" && (val == "on" || val == "off")) {
+                     print "option", opt ": ERROR: turning unrecognized option", val
+                     # For the moment error out - it is safer
+                     err = 1 # prevent END{} running
+                     exit 1
+                  }
+                  onoff = val
+               } else {
+                  # Print a message, otherwise the error
+                  # below is incomprehensible
+                  print "option", opt ": currently", onoff ": attempt to turn", val
+                  break
+               }
+            }
+         } else if (val == "requires" || val == "if" || val == "enables" || val =="sets") {
+            key = val
+         } else if (key == "requires") {
+            requires[opt] = requires[opt] " " val
+         } else if (key == "if") {
+            iffs[opt] = iffs[opt] " " val
+         } else if (key == "enables") {
+            enabledby[val] = enabledby[val] " " opt
+         } else if (key == "sets") {
+            sets[opt] = sets[opt] " " val
+            key = "setval"
+            set = val
+         } else if (key == "setval") {
+            setval[opt " " set] = setval[opt " " set] " " val
+         } else
+            break # bad line format
+      }
+
+      istart = 1
+   } while (i > NF && $0 ~ /,$/)
 
    if (i > NF) {
       # Set the option, defaulting to 'enabled'
       if (onoff == "") onoff = "enabled"
-      option[$2] = onoff
+      option[opt] = onoff
       next
    }
    # Else fall through to the error handler
@@ -301,41 +348,62 @@
 pre != 0 && $1 == "chunk" && NF >= 2{
    # 'chunk' is handled on the first pass by writing appropriate
    # 'option' lines into the intermediate file.
+   opt = $2
+   sub(/,$/,"",opt)
    onoff = ""
    reqread = ""
    reqwrite = ""
    enables = ""
-   i = 3 # indicates format error
-   if (NF > 2) {
+   req = 0
+   istart = 3
+   do {
+      if (istart == 1) {     # continuation line
+         val = getline
+
+         if (val != 1) { # error reading it
+            if (val == 0)
+               print "chunk", opt ": ERROR: missing contination line"
+            else
+               print "chunk", opt ": ERROR: error reading continuation line"
+
+            # This is a hard error
+            err = 1 # prevent END{} running
+            exit 1
+         }
+      }
+
       # read the keywords/additional OPTS
-      req = 0
-      for (i=3; i<=NF; ++i) {
-         if ($(i) == "on" || $(i) == "off" || $(i) == "disabled") {
-            if (onoff != $(i)) {
+      for (i=istart; i<=NF; ++i) {
+         val = $(i)
+         sub(/,$/,"",val)
+         if (val == "on" || val == "off" || val == "disabled") {
+            if (onoff != val) {
                if (onoff == "")
-                  onoff = $(i)
+                  onoff = val
                else
                   break # on/off conflict
             }
             req = 0
-         } else if ($(i) == "requires")
+         } else if (val == "requires")
             req = 1
-         else if ($(i) == "enables")
+         else if (val == "enables")
             req = 2
          else if (req == 1){
-            reqread = reqread " READ_" $(i)
-            reqwrite = reqwrite " WRITE_" $(i)
+            reqread = reqread " READ_" val
+            reqwrite = reqwrite " WRITE_" val
          } else if (req == 2)
-            enables = enables " " $(i)
+            enables = enables " " val
          else
             break # bad line: handled below
       }
-   }
+
+      istart = 1
+   } while (i > NF && $0 ~ /,$/)
 
    if (i > NF) {
       # Output new 'option' lines to the intermediate file (out)
-      print "option READ_" $2, "requires READ_ANCILLARY_CHUNKS" reqread, "enables", $2 enables , onoff >out
-      print "option WRITE_" $2, "requires WRITE_ANCILLARY_CHUNKS" reqwrite, "enables", $2 enables, onoff >out
+      print "option READ_" opt, "requires READ_ANCILLARY_CHUNKS" reqread, "enables", opt enables , onoff >out
+      print "option WRITE_" opt, "requires WRITE_ANCILLARY_CHUNKS" reqwrite, "enables", opt enables, onoff >out
       next
    }
    # Else hit the error handler below - bad line format!
@@ -466,72 +534,7 @@
       exit 0
    }
 
-   # Do the 'setting' values first, the algorithm the standard
-   # tree walk (O(1)) done in an O(2) while/for loop; interations
-   # settings x depth, outputing the deepest required macros
-   # first.
-   print "" >out
-   print "/* SETTINGS */" >out
-   print comment, "settings", cend >out
-   finished = 0
-   while (!finished) {
-      finished = 1
-      movement = 0 # done nothing
-      for (i in setting) if (!doneset[i]) {
-         nreqs = split(setting[i], r)
-         if (nreqs > 0) {
-            for (j=1; j<=nreqs; ++j) if (!doneset[r[j]]) {
-               break
-            }
-            if (j<=nreqs) {
-               finished = 0
-               continue # try a different setting
-            }
-         }
-
-         # All the requirements have been processed, output
-         # this setting.
-         if (deb) print "setting", i
-         print "" >out
-         print "/* setting: ", i >out
-         print " *   requires:" setting[i] >out
-         print " *   default: ", defaults[i], "*/" >out
-         if (defaults[i] == "") { # no default, only check if defined
-            print "#ifdef PNG_" i >out
-         }
-         for (j=1; j<=nreqs; ++j) {
-            print "# ifndef PNG_" r[j] >out
-            print error, i, "requires", r[j] end >out
-            print "# endif" >out
-         }
-         if (defaults[i] != "") { # default handling
-            print "#ifdef PNG_" i >out
-         }
-         print def i, "PNG_" i end >out
-         if (defaults[i] != "") {
-            print "#else /*default*/" >out
-            # And add the default definition for the benefit
-            # of later settings an options test:
-            print "# define PNG_" i defaults[i] >out
-            print def i defaults[i] end >out
-         }
-         print "#endif" >out
-
-         doneset[i] = 1
-         ++movement
-      }
-
-      if (!finished && !movement) {
-         print "setting: loop or missing setting in 'requires', cannot process:"
-         for (i in setting) if (!doneset[i]) {
-            print "  setting", i, "requires" setting[i]
-         }
-         exit 1
-      }
-   }
-   print comment, "end of settings", cend >out
-
-   # Now do the options - somewhat more complex.  The dependency
+   # Do the options first (allowing options to set settings).  The dependency
    # tree is thus:
    #
    #   name     >     name
@@ -623,6 +626,9 @@
    }
    if (err) exit 1
 
+   # Sort options:
+   print "PNG_DFN_START_SORT 2" >out
+
    # option[i] is now the complete list of all the tokens we may
    # need to output, go through it as above, depth first.
    finished = 0
@@ -660,8 +666,9 @@
          print "" >out
          print "/* option:", i, option[i] >out
          print " *   requires:  " requires[i] >out
-         print " *   if:      " iffs[i] >out
-         print " *   enabled-by:" enabledby[i], "*/" >out
+         print " *   if:        " iffs[i] >out
+         print " *   enabled-by:" enabledby[i] >out
+         print " *   sets:      " sets[i], "*/" >out
          print "#undef PNG_on" >out
          print "#define PNG_on 1" >out
 
@@ -744,6 +751,21 @@
             print error, i, "is on: enabled by:" iffs[i] enabledby[i] ", requires" requires[i] end >out
          } else if (i !~ /^ok_/) {
             print def i sup >out
+            # Supported option, set required settings
+            nreqs = split(sets[i], r)
+            for (j=1; j<=nreqs; ++j) {
+               print "#    ifdef PNG_set_" r[j] >out
+               # Some other option has already set a value:
+               print error, i, "sets", r[j] ": duplicate setting" end >out
+               print error, "   previous value: " end "PNG_set_" r[j] >out
+               print "#    else" >out
+               # Else set the default: note that this won't accept arbitrary
+               # values, the setval string must be acceptable to all the C
+               # compilers we use.  That means it must be VERY simple; a number,
+               # a name or a string.
+               print "#     define PNG_set_" r[j], setval[i " " r[j]] >out
+               print "#    endif" >out
+            }
          }
          print "#   endif /* definition */" >out
          print "#endif /*requires, if*/" >out
@@ -774,10 +796,98 @@
          exit 1
       }
    }
+   print "PNG_DFN_END_SORT" >out
    print comment, "end of options", cend >out
 
+   # Do the 'setting' values second, the algorithm the standard
+   # tree walk (O(1)) done in an O(2) while/for loop; interations
+   # settings x depth, outputing the deepest required macros
+   # first.
+   print "" >out
+   print "/* SETTINGS */" >out
+   print comment, "settings", cend >out
+   # Sort (in dfn.awk) on field 2, the setting name
+   print "PNG_DFN_START_SORT 2" >out
+   finished = 0
+   while (!finished) {
+      finished = 1
+      movement = 0 # done nothing
+      for (i in setting) if (!doneset[i]) {
+         nreqs = split(setting[i], r)
+         if (nreqs > 0) {
+            # By default assume the requires values are options, but if there
+            # is no option with that name check for a setting
+            for (j=1; j<=nreqs; ++j) if (option[r[j]] == "" && !doneset[r[j]]) {
+               break
+            }
+            if (j<=nreqs) {
+               finished = 0
+               continue # try a different setting
+            }
+         }
+
+         # All the requirements have been processed, output
+         # this setting.
+         if (deb) print "setting", i
+         deflt = defaults[i]
+         # Remove any spurious trailing spaces
+         sub(/ *$/,"",deflt)
+         # A leading @ means leave it unquoted so the preprocessor
+         # can substitute the build time value
+         if (deflt ~ /^ @/)
+            deflt = " " subs substr(deflt, 3) sube
+         print "" >out
+         print "/* setting: ", i >out
+         print " *   requires:" setting[i] >out
+         print " *   default: ", defaults[i] deflt, "*/" >out
+         for (j=1; j<=nreqs; ++j) {
+            if (option[r[j]] != "")
+               print "#ifndef PNG_" r[j] "_SUPPORTED" >out
+            else
+               print "#ifndef PNG_" r[j] >out
+            print error, i, "requires", r[j] end >out
+            print "# endif" >out
+         }
+         # The precedence is:
+         #
+         #  1) External definition; trumps:
+         #  2) Option 'sets' value; trumps:
+         #  3) Setting 'default'
+         #
+         print "#ifdef PNG_" i >out
+         # PNG_<i> is defined, so substitute the value:
+         print def i, subs "PNG_" i sube end >out
+         print "#else /* use default */" >out
+         print "# ifdef PNG_set_" i >out
+         # Value from an option 'sets' argument
+         print def i, subs "PNG_set_" i sube end >out
+         # This is so that subsequent tests on the setting work:
+         print "#  define PNG_" i, "1" >out
+         if (defaults[i] != "") {
+            print "# else /*default*/" >out
+            print def i deflt end >out
+            print "#  define PNG_" i, "1" >out
+         }
+         print "# endif /* defaults */" >out
+         print "#endif /* setting", i, "*/" >out
+
+         doneset[i] = 1
+         ++movement
+      }
+
+      if (!finished && !movement) {
+         print "setting: loop or missing setting in 'requires', cannot process:"
+         for (i in setting) if (!doneset[i]) {
+            print "  setting", i, "requires" setting[i]
+         }
+         exit 1
+      }
+   }
+   print "PNG_DFN_END_SORT" >out
+   print comment, "end of settings", cend >out
+
    # Regular end - everything looks ok
    if (protect != "") {
-      print start "#endif", cx, protect, "*/" end >out
+      print start "#endif", "/*", protect, "*/" end >out
    }
 }
diff --git a/scripts/pnglibconf.dfa b/scripts/pnglibconf.dfa
old mode 100644
new mode 100755
index 60f6e2b..c95b102
--- a/scripts/pnglibconf.dfa
+++ b/scripts/pnglibconf.dfa
@@ -27,7 +27,7 @@
 # The syntax is detailed in scripts/options.awk; this is a summary
 # only:
 #
-# setting <name> [default]
+# setting <name> [requires ...] [default]
 #    #define PNG_<name> <value>  /* value comes from current setting */
 # option <name> [requires ...] [if ...] [enables ...] [disabled]
 #    #define PNG_<name>_SUPPORTED if the requirements are met and
@@ -39,6 +39,11 @@
 #    used as given (e.g. enables GAMMA just expands to that on the
 #    correspond READ_name and WRITE_name lines.)
 #
+# "," may be used to separate options on an 'option' line and is ignored; it
+# doesn't change the meaning of the line.  (NOT setting, where "," becomes
+# part of the setting!)  A comma at the end of an option line causes a
+# continuation (the next line is included in the option too.)
+#
 # Note that the 'on' and 'off' keywords, while valid on both option
 # and chunk, should not be used in this file because they force the
 # relevant options on or off.
@@ -131,6 +136,12 @@
 # - PNG_USER_VERSIONINFO_COMPANYNAME
 # - PNG_USER_VERSIONINFO_LEGALTRADEMARKS
 
+# It is necessary to include configures definitions here so that AC_DEFINE
+# in configure.ac works in a comprehensible way
+@#if defined(HAVE_CONFIG_H) && !defined(PNG_NO_CONFIG_H)
+@#  include "config.h"
+@#endif
+
 @#ifdef PNG_USER_CONFIG
 @#  include "pngusr.h"
 @#endif
@@ -147,6 +158,12 @@
 @#  endif
 @#endif
 
+# This changes the default for the ARM NEON optimizations according to
+# __ARM_NEON__
+@#ifdef __ARM_NEON__
+@#  define PNG_ARM_NEON_SUPPORTED
+@#endif
+
 # IN DEVELOPMENT
 # These are currently experimental features; define them if you want (NOTE:
 # experimental options must be disabled before they are defined in this file!)
@@ -182,6 +199,29 @@
 
 setting DEFAULT_READ_MACROS default 1
 
+# This setting allows a hardware or configuration specific filter optimization
+# function to be specified, the argument is the name of the filter initializaion
+# function to use.
+
+setting FILTER_OPTIMIZATIONS
+
+# Implementation specific control of the optimizations, enabled by those
+# hardware or software options that need it (typically when run-time choices
+# must be made by the user)
+option SET_OPTION disabled
+
+# These options are specific to the ARM NEON hardware optimizations:
+#
+# ARM_NEON: the optimization itself
+# ARM_NEON_API: allow the optimization to be switched on with png_set_hardware
+# ARM_NEON_CHECK: compile a run-time check to see if Neon extensions are
+#                 supported, this is poorly supported and deprectated - use the
+#                 png_set_hardware API.
+option ARM_NEON disabled,
+   sets FILTER_OPTIMIZATIONS png_init_filter_functions_neon
+option ARM_NEON_API disabled enables SET_OPTION ARM_NEON
+option ARM_NEON_CHECK disabled enables ARM_NEON
+
 # These settings configure the default compression level (0-9) and 'strategy';
 # strategy is as defined by the implementors of zlib, it describes the input
 # data and modifies the zlib parameters in an attempt to optimize the balance
@@ -190,13 +230,17 @@
 # because the name of the setting is prefixed by PNG_
 #
 # The TEXT values are the defaults when writing compressed text (all forms)
+#
+# Include the zlib header too, so that the defaults below are known
+@#  include <zlib.h>
 
-setting Z_DEFAULT_COMPRESSION default Z_DEFAULT_COMPRESSION
-setting Z_DEFAULT_STRATEGY default Z_FILTERED
-setting Z_DEFAULT_NOFILTER_STRATEGY default Z_DEFAULT_STRATEGY
+# The '@' here means to substitute the value when pnglibconf.h is built
+setting Z_DEFAULT_COMPRESSION default @Z_DEFAULT_COMPRESSION
+setting Z_DEFAULT_STRATEGY default @Z_FILTERED
+setting Z_DEFAULT_NOFILTER_STRATEGY default @Z_DEFAULT_STRATEGY
 
-setting TEXT_Z_DEFAULT_COMPRESSION default Z_DEFAULT_COMPRESSION
-setting TEXT_Z_DEFAULT_STRATEGY default Z_DEFAULT_STRATEGY
+setting TEXT_Z_DEFAULT_COMPRESSION default @Z_DEFAULT_COMPRESSION
+setting TEXT_Z_DEFAULT_STRATEGY default @Z_DEFAULT_STRATEGY
 
 # The alternative is to call functions to read PNG values, if
 # the functions are turned *off* the read macros must always
@@ -386,12 +430,12 @@
 # If you handle gamma issues outside libpng then you do not need the libpng
 # gamma processing; and it is an enormous waste of space.  You just need to
 # remove the use of libpng APIs that depend on it.
-option READ_GAMMA requires READ_TRANSFORMS READ_gAMA
+option READ_GAMMA requires READ_TRANSFORMS, READ_gAMA
 
-option READ_ALPHA_MODE requires READ_TRANSFORMS READ_GAMMA
-option READ_BACKGROUND requires READ_TRANSFORMS READ_STRIP_ALPHA READ_GAMMA
+option READ_ALPHA_MODE requires READ_TRANSFORMS, READ_GAMMA
+option READ_BACKGROUND requires READ_TRANSFORMS, READ_STRIP_ALPHA, READ_GAMMA
 option READ_BGR requires READ_TRANSFORMS
-option READ_EXPAND_16 requires READ_TRANSFORMS READ_16BIT READ_EXPAND
+option READ_EXPAND_16 requires READ_TRANSFORMS, READ_16BIT, READ_EXPAND
 option READ_EXPAND requires READ_TRANSFORMS
 option READ_FILLER requires READ_TRANSFORMS
 option READ_GRAY_TO_RGB requires READ_TRANSFORMS
@@ -399,13 +443,13 @@
 option READ_INVERT requires READ_TRANSFORMS
 option READ_PACK requires READ_TRANSFORMS
 option READ_PACKSWAP requires READ_TRANSFORMS
-option READ_RGB_TO_GRAY requires READ_TRANSFORMS READ_GAMMA
+option READ_RGB_TO_GRAY requires READ_TRANSFORMS, READ_GAMMA
 option READ_SCALE_16_TO_8 requires READ_TRANSFORMS
 option READ_SHIFT requires READ_TRANSFORMS
 option READ_STRIP_16_TO_8 requires READ_TRANSFORMS
 option READ_STRIP_ALPHA requires READ_TRANSFORMS
 option READ_SWAP_ALPHA requires READ_TRANSFORMS
-option READ_SWAP requires READ_TRANSFORMS READ_16BIT
+option READ_SWAP requires READ_TRANSFORMS, READ_16BIT
 option READ_USER_TRANSFORM requires READ_TRANSFORMS
 
 option PROGRESSIVE_READ requires READ
@@ -444,7 +488,7 @@
 option WRITE_SHIFT requires WRITE_TRANSFORMS
 option WRITE_PACK requires WRITE_TRANSFORMS
 option WRITE_BGR requires WRITE_TRANSFORMS
-option WRITE_SWAP requires WRITE_TRANSFORMS WRITE_16BIT
+option WRITE_SWAP requires WRITE_TRANSFORMS, WRITE_16BIT
 option WRITE_PACKSWAP requires WRITE_TRANSFORMS
 option WRITE_INVERT requires WRITE_TRANSFORMS
 option WRITE_FILLER requires WRITE_TRANSFORMS
@@ -466,8 +510,8 @@
 
 # Note: these can be turned off explicitly if not required by the
 # apps implementing the user transforms
-option USER_TRANSFORM_PTR if READ_USER_TRANSFORM WRITE_USER_TRANSFORM
-option USER_TRANSFORM_INFO if READ_USER_TRANSFORM WRITE_USER_TRANSFORM
+option USER_TRANSFORM_PTR if READ_USER_TRANSFORM, WRITE_USER_TRANSFORM
+option USER_TRANSFORM_INFO if READ_USER_TRANSFORM, WRITE_USER_TRANSFORM
 
 # This enables API to set compression parameters for compressing
 # non-IDAT chunks (zTXt, iTXt, iCCP, and unknown chunks).  This feature
@@ -568,7 +612,7 @@
 # but can help (in theory) on some architectures.  Only affects
 # internal structures.  Added at libpng 1.4.0
 
-option ALIGN_MEMORY
+option ALIGNED_MEMORY
 
 # Buggy compilers (e.g., gcc 2.7.2.2) need PNG_NO_POINTER_INDEXING
 # See png[wr]util.c, normally this should always be *on*
@@ -660,7 +704,7 @@
 chunk cHRM enables COLORSPACE
 chunk gAMA enables GAMMA
 chunk hIST
-chunk iCCP enables COLORSPACE GAMMA
+chunk iCCP enables COLORSPACE, GAMMA
 chunk iTXt
 chunk oFFs
 chunk pCAL
@@ -668,7 +712,7 @@
 chunk sBIT
 chunk sCAL
 chunk sPLT
-chunk sRGB enables COLORSPACE GAMMA
+chunk sRGB enables COLORSPACE, GAMMA
 chunk tEXt requires TEXT
 chunk tIME
 chunk tRNS
@@ -698,7 +742,7 @@
 # to png_get_unknown_chunks, the application must call
 # png_set_keep_unknown_chunks to cause this to actually happen (see png.h)
 option SAVE_UNKNOWN_CHUNKS requires READ requires SET_UNKNOWN_CHUNKS
-option SAVE_UNKNOWN_CHUNKS enables READ_UNKNOWN_CHUNKS STORE_UNKNOWN_CHUNKS
+option SAVE_UNKNOWN_CHUNKS enables READ_UNKNOWN_CHUNKS, STORE_UNKNOWN_CHUNKS
 
 # The second approach is to use an application provided callback to process the
 # chunks, the callback can either handle the chunk entirely itself or request
@@ -706,8 +750,8 @@
 #
 # Note that there is no 'WRITE_USER_CHUNKS' so the USER_CHUNKS option is always
 # the same as READ_USER_CHUNKS at present
-option READ_USER_CHUNKS requires READ requires UNKNOWN_CHUNKS
-option READ_USER_CHUNKS enables READ_UNKNOWN_CHUNKS USER_CHUNKS
+option READ_USER_CHUNKS requires READ, UNKNOWN_CHUNKS
+option READ_USER_CHUNKS enables READ_UNKNOWN_CHUNKS, USER_CHUNKS
 
 # Two further options are provided to allow detailed control of the handling.
 # The first enables png_set_keep_unknown_chunks; this allows the default to be
@@ -774,8 +818,8 @@
 
 option CHECK_FOR_INVALID_INDEX enables READ_CHECK_FOR_INVALID_INDEX
 option CHECK_FOR_INVALID_INDEX enables WRITE_CHECK_FOR_INVALID_INDEX
-option READ_CHECK_FOR_INVALID_INDEX requires READ CHECK_FOR_INVALID_INDEX
-option WRITE_CHECK_FOR_INVALID_INDEX requires WRITE CHECK_FOR_INVALID_INDEX
+option READ_CHECK_FOR_INVALID_INDEX requires READ, CHECK_FOR_INVALID_INDEX
+option WRITE_CHECK_FOR_INVALID_INDEX requires WRITE, CHECK_FOR_INVALID_INDEX
 
 # added at libpng-1.5.15
 option GET_PALETTE_MAX enables READ_GET_PALETTE_MAX WRITE_GET_PALETTE_MAX
@@ -784,12 +828,11 @@
 
 # Simplified API options (added at libpng-1.6.0)
 # Read:
-option SIMPLIFIED_READ requires SEQUENTIAL_READ READ_TRANSFORMS SETJMP
-option SIMPLIFIED_READ requires BENIGN_ERRORS READ_GAMMA
-option SIMPLIFIED_READ enables READ_EXPAND READ_16BIT READ_EXPAND_16
-option SIMPLIFIED_READ enables READ_SCALE_16_TO_8 READ_RGB_TO_GRAY
-option SIMPLIFIED_READ enables READ_ALPHA_MODE READ_BACKGROUND READ_STRIP_ALPHA
-option SIMPLIFIED_READ enables READ_FILLER READ_SWAP
+option SIMPLIFIED_READ,
+   requires SEQUENTIAL_READ READ_TRANSFORMS, SETJMP, BENIGN_ERRORS READ_GAMMA,
+   enables READ_EXPAND, READ_16BIT READ_EXPAND_16, READ_SCALE_16_TO_8,
+      READ_RGB_TO_GRAY, READ_ALPHA_MODE READ_BACKGROUND READ_STRIP_ALPHA,
+      READ_FILLER, READ_SWAP
 
 option SIMPLIFIED_READ_AFIRST requires SIMPLIFIED_READ disabled
 option READ_SWAP_ALPHA enables SIMPLIFIED_READ_AFIRST
@@ -798,8 +841,9 @@
 option READ_BGR enables SIMPLIFIED_READ_BGR
 
 # Write:
-option SIMPLIFIED_WRITE requires WRITE STDIO SETJMP
-option SIMPLIFIED_WRITE enables WRITE_SWAP WRITE_gAMA WRITE_sRGB WRITE_cHRM
+option SIMPLIFIED_WRITE,
+   requires WRITE STDIO, SETJMP,
+   enables WRITE_SWAP WRITE_gAMA, WRITE_sRGB WRITE_cHRM
 
 option SIMPLIFIED_WRITE_AFIRST requires SIMPLIFIED_WRITE disabled
 option WRITE_SWAP_ALPHA enables SIMPLIFIED_WRITE_AFIRST
@@ -808,5 +852,5 @@
 option WRITE_BGR enables SIMPLIFIED_WRITE_BGR
 
 # Formats:
-option FORMAT_AFIRST if SIMPLIFIED_READ_AFIRST SIMPLIFIED_WRITE_AFIRST
-option FORMAT_BGR if SIMPLIFIED_READ_BGR SIMPLIFIED_WRITE_BGR
+option FORMAT_AFIRST if SIMPLIFIED_READ_AFIRST, SIMPLIFIED_WRITE_AFIRST
+option FORMAT_BGR if SIMPLIFIED_READ_BGR, SIMPLIFIED_WRITE_BGR
diff --git a/scripts/pnglibconf.h.prebuilt b/scripts/pnglibconf.h.prebuilt
index b318279..1894e2d 100644
--- a/scripts/pnglibconf.h.prebuilt
+++ b/scripts/pnglibconf.h.prebuilt
@@ -1,9 +1,8 @@
-
-/* libpng STANDARD API DEFINITION */
+/* libpng 1.6.1 STANDARD API DEFINITION */
 
 /* pnglibconf.h - library build configuration */
 
-/* Libpng 1.6.0 - February 14, 2013 */
+/* Libpng version 1.6.1 - March 28, 2013 */
 
 /* Copyright (c) 1998-2013 Glenn Randers-Pehrson */
 
@@ -12,14 +11,181 @@
 /* and license in png.h */
 
 /* pnglibconf.h */
+/* Machine generated file: DO NOT EDIT */
 /* Derived from: scripts/pnglibconf.dfa */
-/* If you edit this file by hand you must obey the rules expressed in */
-/* pnglibconf.dfa with respect to the dependencies between the following */
-/* symbols.  It is much better to generate a new file using */
-/* scripts/libpngconf.mak */
-
 #ifndef PNGLCONF_H
 #define PNGLCONF_H
+/* options */
+#define PNG_16BIT_SUPPORTED
+#define PNG_ALIGNED_MEMORY_SUPPORTED
+/*#undef PNG_ARM_NEON_API_SUPPORTED*/
+/*#undef PNG_ARM_NEON_CHECK_SUPPORTED*/
+/*#undef PNG_ARM_NEON_SUPPORTED*/
+#define PNG_BENIGN_ERRORS_SUPPORTED
+#define PNG_BENIGN_READ_ERRORS_SUPPORTED
+/*#undef PNG_BENIGN_WRITE_ERRORS_SUPPORTED*/
+#define PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED
+#define PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
+#define PNG_COLORSPACE_SUPPORTED
+#define PNG_CONSOLE_IO_SUPPORTED
+#define PNG_CONVERT_tIME_SUPPORTED
+#define PNG_EASY_ACCESS_SUPPORTED
+/*#undef PNG_ERROR_NUMBERS_SUPPORTED*/
+#define PNG_ERROR_TEXT_SUPPORTED
+#define PNG_FIXED_POINT_SUPPORTED
+#define PNG_FLOATING_ARITHMETIC_SUPPORTED
+#define PNG_FLOATING_POINT_SUPPORTED
+#define PNG_FORMAT_AFIRST_SUPPORTED
+#define PNG_FORMAT_BGR_SUPPORTED
+#define PNG_GAMMA_SUPPORTED
+#define PNG_GET_PALETTE_MAX_SUPPORTED
+#define PNG_HANDLE_AS_UNKNOWN_SUPPORTED
+#define PNG_INCH_CONVERSIONS_SUPPORTED
+#define PNG_INFO_IMAGE_SUPPORTED
+#define PNG_IO_STATE_SUPPORTED
+#define PNG_MNG_FEATURES_SUPPORTED
+#define PNG_POINTER_INDEXING_SUPPORTED
+#define PNG_PROGRESSIVE_READ_SUPPORTED
+#define PNG_READ_16BIT_SUPPORTED
+#define PNG_READ_ALPHA_MODE_SUPPORTED
+#define PNG_READ_ANCILLARY_CHUNKS_SUPPORTED
+#define PNG_READ_BACKGROUND_SUPPORTED
+#define PNG_READ_BGR_SUPPORTED
+#define PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
+#define PNG_READ_COMPOSITE_NODIV_SUPPORTED
+#define PNG_READ_COMPRESSED_TEXT_SUPPORTED
+#define PNG_READ_EXPAND_16_SUPPORTED
+#define PNG_READ_EXPAND_SUPPORTED
+#define PNG_READ_FILLER_SUPPORTED
+#define PNG_READ_GAMMA_SUPPORTED
+#define PNG_READ_GET_PALETTE_MAX_SUPPORTED
+#define PNG_READ_GRAY_TO_RGB_SUPPORTED
+#define PNG_READ_INTERLACING_SUPPORTED
+#define PNG_READ_INT_FUNCTIONS_SUPPORTED
+#define PNG_READ_INVERT_ALPHA_SUPPORTED
+#define PNG_READ_INVERT_SUPPORTED
+#define PNG_READ_OPT_PLTE_SUPPORTED
+#define PNG_READ_PACKSWAP_SUPPORTED
+#define PNG_READ_PACK_SUPPORTED
+#define PNG_READ_QUANTIZE_SUPPORTED
+#define PNG_READ_RGB_TO_GRAY_SUPPORTED
+#define PNG_READ_SCALE_16_TO_8_SUPPORTED
+#define PNG_READ_SHIFT_SUPPORTED
+#define PNG_READ_STRIP_16_TO_8_SUPPORTED
+#define PNG_READ_STRIP_ALPHA_SUPPORTED
+#define PNG_READ_SUPPORTED
+#define PNG_READ_SWAP_ALPHA_SUPPORTED
+#define PNG_READ_SWAP_SUPPORTED
+#define PNG_READ_TEXT_SUPPORTED
+#define PNG_READ_TRANSFORMS_SUPPORTED
+#define PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
+#define PNG_READ_USER_CHUNKS_SUPPORTED
+#define PNG_READ_USER_TRANSFORM_SUPPORTED
+#define PNG_READ_bKGD_SUPPORTED
+#define PNG_READ_cHRM_SUPPORTED
+#define PNG_READ_gAMA_SUPPORTED
+#define PNG_READ_hIST_SUPPORTED
+#define PNG_READ_iCCP_SUPPORTED
+#define PNG_READ_iTXt_SUPPORTED
+#define PNG_READ_oFFs_SUPPORTED
+#define PNG_READ_pCAL_SUPPORTED
+#define PNG_READ_pHYs_SUPPORTED
+#define PNG_READ_sBIT_SUPPORTED
+#define PNG_READ_sCAL_SUPPORTED
+#define PNG_READ_sPLT_SUPPORTED
+#define PNG_READ_sRGB_SUPPORTED
+#define PNG_READ_tEXt_SUPPORTED
+#define PNG_READ_tIME_SUPPORTED
+#define PNG_READ_tRNS_SUPPORTED
+#define PNG_READ_zTXt_SUPPORTED
+/*#undef PNG_SAFE_LIMITS_SUPPORTED*/
+#define PNG_SAVE_INT_32_SUPPORTED
+#define PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
+#define PNG_SEQUENTIAL_READ_SUPPORTED
+#define PNG_SETJMP_SUPPORTED
+#define PNG_SET_CHUNK_CACHE_LIMIT_SUPPORTED
+#define PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
+/*#undef PNG_SET_OPTION_SUPPORTED*/
+#define PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
+#define PNG_SET_USER_LIMITS_SUPPORTED
+#define PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
+#define PNG_SIMPLIFIED_READ_BGR_SUPPORTED
+#define PNG_SIMPLIFIED_READ_SUPPORTED
+#define PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
+#define PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
+#define PNG_SIMPLIFIED_WRITE_SUPPORTED
+#define PNG_STDIO_SUPPORTED
+#define PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
+#define PNG_TEXT_SUPPORTED
+#define PNG_TIME_RFC1123_SUPPORTED
+#define PNG_UNKNOWN_CHUNKS_SUPPORTED
+#define PNG_USER_CHUNKS_SUPPORTED
+#define PNG_USER_LIMITS_SUPPORTED
+#define PNG_USER_MEM_SUPPORTED
+#define PNG_USER_TRANSFORM_INFO_SUPPORTED
+#define PNG_USER_TRANSFORM_PTR_SUPPORTED
+#define PNG_WARNINGS_SUPPORTED
+#define PNG_WRITE_16BIT_SUPPORTED
+#define PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED
+#define PNG_WRITE_BGR_SUPPORTED
+#define PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
+#define PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
+#define PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
+#define PNG_WRITE_FILLER_SUPPORTED
+#define PNG_WRITE_FILTER_SUPPORTED
+#define PNG_WRITE_FLUSH_SUPPORTED
+#define PNG_WRITE_GET_PALETTE_MAX_SUPPORTED
+#define PNG_WRITE_INTERLACING_SUPPORTED
+#define PNG_WRITE_INT_FUNCTIONS_SUPPORTED
+#define PNG_WRITE_INVERT_ALPHA_SUPPORTED
+#define PNG_WRITE_INVERT_SUPPORTED
+#define PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
+#define PNG_WRITE_PACKSWAP_SUPPORTED
+#define PNG_WRITE_PACK_SUPPORTED
+#define PNG_WRITE_SHIFT_SUPPORTED
+#define PNG_WRITE_SUPPORTED
+#define PNG_WRITE_SWAP_ALPHA_SUPPORTED
+#define PNG_WRITE_SWAP_SUPPORTED
+#define PNG_WRITE_TEXT_SUPPORTED
+#define PNG_WRITE_TRANSFORMS_SUPPORTED
+#define PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
+#define PNG_WRITE_USER_TRANSFORM_SUPPORTED
+#define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
+#define PNG_WRITE_bKGD_SUPPORTED
+#define PNG_WRITE_cHRM_SUPPORTED
+#define PNG_WRITE_gAMA_SUPPORTED
+#define PNG_WRITE_hIST_SUPPORTED
+#define PNG_WRITE_iCCP_SUPPORTED
+#define PNG_WRITE_iTXt_SUPPORTED
+#define PNG_WRITE_oFFs_SUPPORTED
+#define PNG_WRITE_pCAL_SUPPORTED
+#define PNG_WRITE_pHYs_SUPPORTED
+#define PNG_WRITE_sBIT_SUPPORTED
+#define PNG_WRITE_sCAL_SUPPORTED
+#define PNG_WRITE_sPLT_SUPPORTED
+#define PNG_WRITE_sRGB_SUPPORTED
+#define PNG_WRITE_tEXt_SUPPORTED
+#define PNG_WRITE_tIME_SUPPORTED
+#define PNG_WRITE_tRNS_SUPPORTED
+#define PNG_WRITE_zTXt_SUPPORTED
+#define PNG_bKGD_SUPPORTED
+#define PNG_cHRM_SUPPORTED
+#define PNG_gAMA_SUPPORTED
+#define PNG_hIST_SUPPORTED
+#define PNG_iCCP_SUPPORTED
+#define PNG_iTXt_SUPPORTED
+#define PNG_oFFs_SUPPORTED
+#define PNG_pCAL_SUPPORTED
+#define PNG_pHYs_SUPPORTED
+#define PNG_sBIT_SUPPORTED
+#define PNG_sCAL_SUPPORTED
+#define PNG_sPLT_SUPPORTED
+#define PNG_sRGB_SUPPORTED
+#define PNG_tEXt_SUPPORTED
+#define PNG_tIME_SUPPORTED
+#define PNG_tRNS_SUPPORTED
+#define PNG_zTXt_SUPPORTED
+/* end of options */
 /* settings */
 #define PNG_API_RULE 0
 #define PNG_CALLOC_SUPPORTED
@@ -32,181 +198,14 @@
 #define PNG_QUANTIZE_BLUE_BITS 5
 #define PNG_QUANTIZE_GREEN_BITS 5
 #define PNG_QUANTIZE_RED_BITS 5
-#define PNG_sCAL_PRECISION 5
-#define PNG_sRGB_PROFILE_CHECKS 2
-#define PNG_TEXT_Z_DEFAULT_COMPRESSION Z_DEFAULT_COMPRESSION
-#define PNG_TEXT_Z_DEFAULT_STRATEGY Z_DEFAULT_STRATEGY
+#define PNG_TEXT_Z_DEFAULT_COMPRESSION (-1)
+#define PNG_TEXT_Z_DEFAULT_STRATEGY 0
 #define PNG_WEIGHT_SHIFT 8
 #define PNG_ZBUF_SIZE 8192
-#define PNG_Z_DEFAULT_COMPRESSION Z_DEFAULT_COMPRESSION
-#define PNG_Z_DEFAULT_NOFILTER_STRATEGY Z_DEFAULT_STRATEGY
-#define PNG_Z_DEFAULT_STRATEGY Z_FILTERED
+#define PNG_Z_DEFAULT_COMPRESSION (-1)
+#define PNG_Z_DEFAULT_NOFILTER_STRATEGY 0
+#define PNG_Z_DEFAULT_STRATEGY 1
+#define PNG_sCAL_PRECISION 5
+#define PNG_sRGB_PROFILE_CHECKS 2
 /* end of settings */
-/* options */
-#define PNG_16BIT_SUPPORTED
-#define PNG_ALIGN_MEMORY_SUPPORTED
-#define PNG_BENIGN_ERRORS_SUPPORTED
-#define PNG_BENIGN_READ_ERRORS_SUPPORTED
-/*#undef PNG_BENIGN_WRITE_ERRORS_SUPPORTED*/
-#define PNG_bKGD_SUPPORTED
-#define PNG_BUILD_GRAYSCALE_PALETTE_SUPPORTED
-#define PNG_CHECK_FOR_INVALID_INDEX_SUPPORTED
-#define PNG_cHRM_SUPPORTED
-#define PNG_COLORSPACE_SUPPORTED
-#define PNG_CONSOLE_IO_SUPPORTED
-#define PNG_CONVERT_tIME_SUPPORTED
-#define PNG_EASY_ACCESS_SUPPORTED
-/*#undef PNG_ERROR_NUMBERS_SUPPORTED*/
-#define PNG_ERROR_TEXT_SUPPORTED
-#define PNG_FIXED_POINT_SUPPORTED
-#define PNG_FLOATING_ARITHMETIC_SUPPORTED
-#define PNG_FLOATING_POINT_SUPPORTED
-#define PNG_FORMAT_AFIRST_SUPPORTED
-#define PNG_FORMAT_BGR_SUPPORTED
-#define PNG_gAMA_SUPPORTED
-#define PNG_GAMMA_SUPPORTED
-#define PNG_GET_PALETTE_MAX_SUPPORTED
-#define PNG_HANDLE_AS_UNKNOWN_SUPPORTED
-#define PNG_hIST_SUPPORTED
-#define PNG_iCCP_SUPPORTED
-#define PNG_INCH_CONVERSIONS_SUPPORTED
-#define PNG_INFO_IMAGE_SUPPORTED
-#define PNG_IO_STATE_SUPPORTED
-#define PNG_iTXt_SUPPORTED
-#define PNG_MNG_FEATURES_SUPPORTED
-#define PNG_oFFs_SUPPORTED
-#define PNG_pCAL_SUPPORTED
-#define PNG_pHYs_SUPPORTED
-#define PNG_POINTER_INDEXING_SUPPORTED
-#define PNG_PROGRESSIVE_READ_SUPPORTED
-#define PNG_READ_16BIT_SUPPORTED
-#define PNG_READ_ALPHA_MODE_SUPPORTED
-#define PNG_READ_ANCILLARY_CHUNKS_SUPPORTED
-#define PNG_READ_BACKGROUND_SUPPORTED
-#define PNG_READ_BGR_SUPPORTED
-#define PNG_READ_bKGD_SUPPORTED
-#define PNG_READ_CHECK_FOR_INVALID_INDEX_SUPPORTED
-#define PNG_READ_cHRM_SUPPORTED
-#define PNG_READ_COMPOSITE_NODIV_SUPPORTED
-#define PNG_READ_COMPRESSED_TEXT_SUPPORTED
-#define PNG_READ_EXPAND_16_SUPPORTED
-#define PNG_READ_EXPAND_SUPPORTED
-#define PNG_READ_FILLER_SUPPORTED
-#define PNG_READ_gAMA_SUPPORTED
-#define PNG_READ_GAMMA_SUPPORTED
-#define PNG_READ_GET_PALETTE_MAX_SUPPORTED
-#define PNG_READ_GRAY_TO_RGB_SUPPORTED
-#define PNG_READ_hIST_SUPPORTED
-#define PNG_READ_iCCP_SUPPORTED
-#define PNG_READ_INTERLACING_SUPPORTED
-#define PNG_READ_INT_FUNCTIONS_SUPPORTED
-#define PNG_READ_INVERT_ALPHA_SUPPORTED
-#define PNG_READ_INVERT_SUPPORTED
-#define PNG_READ_iTXt_SUPPORTED
-#define PNG_READ_oFFs_SUPPORTED
-#define PNG_READ_OPT_PLTE_SUPPORTED
-#define PNG_READ_PACK_SUPPORTED
-#define PNG_READ_PACKSWAP_SUPPORTED
-#define PNG_READ_pCAL_SUPPORTED
-#define PNG_READ_pHYs_SUPPORTED
-#define PNG_READ_QUANTIZE_SUPPORTED
-#define PNG_READ_RGB_TO_GRAY_SUPPORTED
-#define PNG_READ_sBIT_SUPPORTED
-#define PNG_READ_SCALE_16_TO_8_SUPPORTED
-#define PNG_READ_sCAL_SUPPORTED
-#define PNG_READ_SHIFT_SUPPORTED
-#define PNG_READ_sPLT_SUPPORTED
-#define PNG_READ_sRGB_SUPPORTED
-#define PNG_READ_STRIP_16_TO_8_SUPPORTED
-#define PNG_READ_STRIP_ALPHA_SUPPORTED
-#define PNG_READ_SUPPORTED
-#define PNG_READ_SWAP_ALPHA_SUPPORTED
-#define PNG_READ_SWAP_SUPPORTED
-#define PNG_READ_tEXt_SUPPORTED
-#define PNG_READ_TEXT_SUPPORTED
-#define PNG_READ_tIME_SUPPORTED
-#define PNG_READ_TRANSFORMS_SUPPORTED
-#define PNG_READ_tRNS_SUPPORTED
-#define PNG_READ_UNKNOWN_CHUNKS_SUPPORTED
-#define PNG_READ_USER_CHUNKS_SUPPORTED
-#define PNG_READ_USER_TRANSFORM_SUPPORTED
-#define PNG_READ_zTXt_SUPPORTED
-/*#undef PNG_SAFE_LIMITS_SUPPORTED*/
-#define PNG_SAVE_INT_32_SUPPORTED
-#define PNG_SAVE_UNKNOWN_CHUNKS_SUPPORTED
-#define PNG_sBIT_SUPPORTED
-#define PNG_sCAL_SUPPORTED
-#define PNG_SEQUENTIAL_READ_SUPPORTED
-#define PNG_SET_CHUNK_CACHE_LIMIT_SUPPORTED
-#define PNG_SET_CHUNK_MALLOC_LIMIT_SUPPORTED
-#define PNG_SETJMP_SUPPORTED
-#define PNG_SET_UNKNOWN_CHUNKS_SUPPORTED
-#define PNG_SET_USER_LIMITS_SUPPORTED
-#define PNG_SIMPLIFIED_READ_AFIRST_SUPPORTED
-#define PNG_SIMPLIFIED_READ_BGR_SUPPORTED
-#define PNG_SIMPLIFIED_READ_SUPPORTED
-#define PNG_SIMPLIFIED_WRITE_AFIRST_SUPPORTED
-#define PNG_SIMPLIFIED_WRITE_BGR_SUPPORTED
-#define PNG_SIMPLIFIED_WRITE_SUPPORTED
-#define PNG_sPLT_SUPPORTED
-#define PNG_sRGB_SUPPORTED
-#define PNG_STDIO_SUPPORTED
-#define PNG_STORE_UNKNOWN_CHUNKS_SUPPORTED
-#define PNG_tEXt_SUPPORTED
-#define PNG_TEXT_SUPPORTED
-#define PNG_TIME_RFC1123_SUPPORTED
-#define PNG_tIME_SUPPORTED
-#define PNG_tRNS_SUPPORTED
-#define PNG_UNKNOWN_CHUNKS_SUPPORTED
-#define PNG_USER_CHUNKS_SUPPORTED
-#define PNG_USER_LIMITS_SUPPORTED
-#define PNG_USER_MEM_SUPPORTED
-#define PNG_USER_TRANSFORM_INFO_SUPPORTED
-#define PNG_USER_TRANSFORM_PTR_SUPPORTED
-#define PNG_WARNINGS_SUPPORTED
-#define PNG_WRITE_16BIT_SUPPORTED
-#define PNG_WRITE_ANCILLARY_CHUNKS_SUPPORTED
-#define PNG_WRITE_BGR_SUPPORTED
-#define PNG_WRITE_bKGD_SUPPORTED
-#define PNG_WRITE_CHECK_FOR_INVALID_INDEX_SUPPORTED
-#define PNG_WRITE_cHRM_SUPPORTED
-#define PNG_WRITE_COMPRESSED_TEXT_SUPPORTED
-#define PNG_WRITE_CUSTOMIZE_ZTXT_COMPRESSION_SUPPORTED
-#define PNG_WRITE_FILLER_SUPPORTED
-#define PNG_WRITE_FILTER_SUPPORTED
-#define PNG_WRITE_FLUSH_SUPPORTED
-#define PNG_WRITE_gAMA_SUPPORTED
-#define PNG_WRITE_GET_PALETTE_MAX_SUPPORTED
-#define PNG_WRITE_hIST_SUPPORTED
-#define PNG_WRITE_iCCP_SUPPORTED
-#define PNG_WRITE_INTERLACING_SUPPORTED
-#define PNG_WRITE_INT_FUNCTIONS_SUPPORTED
-#define PNG_WRITE_INVERT_ALPHA_SUPPORTED
-#define PNG_WRITE_INVERT_SUPPORTED
-#define PNG_WRITE_iTXt_SUPPORTED
-#define PNG_WRITE_oFFs_SUPPORTED
-#define PNG_WRITE_OPTIMIZE_CMF_SUPPORTED
-#define PNG_WRITE_PACK_SUPPORTED
-#define PNG_WRITE_PACKSWAP_SUPPORTED
-#define PNG_WRITE_pCAL_SUPPORTED
-#define PNG_WRITE_pHYs_SUPPORTED
-#define PNG_WRITE_sBIT_SUPPORTED
-#define PNG_WRITE_sCAL_SUPPORTED
-#define PNG_WRITE_SHIFT_SUPPORTED
-#define PNG_WRITE_sPLT_SUPPORTED
-#define PNG_WRITE_sRGB_SUPPORTED
-#define PNG_WRITE_SUPPORTED
-#define PNG_WRITE_SWAP_ALPHA_SUPPORTED
-#define PNG_WRITE_SWAP_SUPPORTED
-#define PNG_WRITE_tEXt_SUPPORTED
-#define PNG_WRITE_TEXT_SUPPORTED
-#define PNG_WRITE_tIME_SUPPORTED
-#define PNG_WRITE_TRANSFORMS_SUPPORTED
-#define PNG_WRITE_tRNS_SUPPORTED
-#define PNG_WRITE_UNKNOWN_CHUNKS_SUPPORTED
-#define PNG_WRITE_USER_TRANSFORM_SUPPORTED
-#define PNG_WRITE_WEIGHTED_FILTER_SUPPORTED
-#define PNG_WRITE_zTXt_SUPPORTED
-#define PNG_zTXt_SUPPORTED
-/* end of options */
 #endif /* PNGLCONF_H */
diff --git a/scripts/pnglibconf.mak b/scripts/pnglibconf.mak
index c799fb7..c7117dd 100755
--- a/scripts/pnglibconf.mak
+++ b/scripts/pnglibconf.mak
@@ -3,7 +3,7 @@
 #
 # These lines are copied from Makefile.am, they illustrate
 # how to automate the build of pnglibconf.h from scripts/pnglibconf.dfa
-# given 'awk' and 'sed'
+# given just 'awk', a C preprocessor and standard command line utilities
 
 # Override as appropriate, these definitions can be overridden on
 # the make command line (AWK='nawk' for example).
@@ -12,10 +12,9 @@
 AWK = nawk
 AWK = one-true-awk
 AWK = awk  # Crashes on SunOS 5.10 - use 'nawk'
-CPP = $(CC) -E # Does not work on SUN OS 5.10 - use /lib/cpp
-SED = sed
+CPP = $(CC) -E # On SUN OS 5.10 if this causes problems use /lib/cpp
 
-COPY = cp
+MOVE = mv
 DELETE = rm -f
 ECHO = echo
 DFA_XTRA = # Put your configuration file here, see scripts/pnglibconf.dfa.  Eg:
@@ -32,28 +31,24 @@
 # The standard pnglibconf.h exists as scripts/pnglibconf.h.prebuilt,
 # copy this if the following doesn't work.
 pnglibconf.h: pnglibconf.dfn
-	$(DELETE) $@ dfn.c dfn1.out dfn2.out dfn3.out
-	$(ECHO) '#include "pnglibconf.dfn"' >dfn.c
-	$(CPP) $(DFNFLAGS) dfn.c >dfn1.out
-	$(ECHO) "If 'cpp -e' crashes try /lib/cpp (e.g. CPP='/lib/cpp')" >&2
-	$(SED) -n -e 's|^.*PNG_DEFN_MAGIC *-\(.*\)- *PNG_DEFN_END.*$$|\1|p'\
-	    dfn1.out >dfn2.out
-	$(SED) -e 's| *PNG_JOIN *||g' -e 's| *$$||' dfn2.out >dfn3.out
-	$(COPY) dfn3.out $@
-	$(DELETE) dfn.c dfn1.out dfn2.out dfn3.out
+	$(DELETE) $@ pnglibconf.c pnglibconf.out pnglibconf.tmp
+	$(ECHO) '#include "pnglibconf.dfn"' >pnglibconf.c
+	$(ECHO) "If '$(CC) -E' crashes try /lib/cpp (e.g. CPP='/lib/cpp')" >&2
+	$(CPP) $(DFNFLAGS) pnglibconf.c >pnglibconf.out
+	$(AWK) -f "$(srcdir)/scripts/dfn.awk" out="pnglibconf.tmp" pnglibconf.out 1>&2
+	$(MOVE) pnglibconf.tmp $@
 
-pnglibconf.dfn: $(srcdir)/scripts/pnglibconf.dfa $(srcdir)/scripts/options.awk $(srcdir)/pngconf.h $(srcdir)/pngusr.dfa
-	$(DELETE) $@ dfn1.out dfn2.out
+pnglibconf.dfn: $(srcdir)/scripts/pnglibconf.dfa $(srcdir)/scripts/options.awk $(srcdir)/pngconf.h $(srcdir)/pngusr.dfa $(DFA_XTRA)
+	$(DELETE) $@ pnglibconf.pre pnglibconf.tmp
 	$(ECHO) "Calling $(AWK) from scripts/pnglibconf.mak" >&2
 	$(ECHO) "If 'awk' crashes try a better awk (e.g. AWK='nawk')" >&2
-	$(AWK) -f $(srcdir)/scripts/options.awk out=dfn1.out version=search\
-		$(srcdir)/pngconf.h $(srcdir)/scripts/pnglibconf.dfa\
-		$(srcdir)/pngusr.dfa $(DFA_XTRA) 1>&2
-	$(AWK) -f $(srcdir)/scripts/options.awk out=dfn2.out dfn1.out 1>&2
-	$(COPY) dfn2.out $@
-	$(DELETE) dfn1.out dfn2.out
+	$(AWK) -f $(srcdir)/scripts/options.awk out="pnglibconf.pre"\
+	    version=search $(srcdir)/pngconf.h $(srcdir)/scripts/pnglibconf.dfa\
+	    $(srcdir)/pngusr.dfa $(DFA_XTRA) 1>&2
+	$(AWK) -f $(srcdir)/scripts/options.awk out="pnglibconf.tmp" pnglibconf.pre 1>&2
+	$(MOVE) pnglibconf.tmp $@
 
 clean-pnglibconf:
-	$(DELETE) pnglibconf.h pnglibconf.dfn dfn.c dfn1.out dfn2.out dfn3.out
+	$(DELETE) pnglibconf.*
 
 clean: clean-pnglibconf
diff --git a/scripts/prefix.dfn b/scripts/prefix.dfn
index 1e8561f..0997310 100644
--- a/scripts/prefix.dfn
+++ b/scripts/prefix.dfn
@@ -10,11 +10,15 @@
  */
 
 #define PNG_EXPORTA(ordinal, type, name, args, attributes)\
-	PNG_DEFN_MAGIC-name-PNG_DEFN_END
+        PNG_DFN "@" name "@"
 
 /* The configuration information *before* the additional of symbol renames,
  * the list is the C name list; no symbol prefix.
  */
 #include "pnglibconf.out"
 
+PNG_DFN_START_SORT 1
+
 #include "../png.h"
+
+PNG_DFN_END_SORT
diff --git a/scripts/sym.dfn b/scripts/sym.dfn
index f4f13df..9ccd810 100644
--- a/scripts/sym.dfn
+++ b/scripts/sym.dfn
@@ -10,6 +10,6 @@
  */
 
 #define PNG_EXPORTA(ordinal, type, name, args, attributes)\
-	PNG_DEFN_MAGIC-SYMBOL_PREFIX PNG_JOIN name-PNG_DEFN_END
+        PNG_DFN "@" SYMBOL_PREFIX "@@" name "@"
 
 #include "../png.h"
diff --git a/scripts/symbols.def b/scripts/symbols.def
index 1e150df..d19022c 100644
--- a/scripts/symbols.def
+++ b/scripts/symbols.def
@@ -1,3 +1,4 @@
+;Version 1.6.1
 ;--------------------------------------------------------------
 ; LIBPNG symbol list as a Win32 DEF file
 ; Contains all the symbols that can be exported from libpng
@@ -5,7 +6,6 @@
 LIBRARY
 
 EXPORTS
-;Version 1.6.0
  png_access_version_number @1
  png_set_sig_bytes @2
  png_sig_cmp @3
@@ -248,3 +248,4 @@
  png_convert_to_rfc1123_buffer @241
  png_set_check_for_invalid_index @242
  png_get_palette_max @243
+ png_set_option @244
diff --git a/scripts/symbols.dfn b/scripts/symbols.dfn
index d790929..6f52918 100644
--- a/scripts/symbols.dfn
+++ b/scripts/symbols.dfn
@@ -17,11 +17,11 @@
  */
 
 #define PNG_EXPORTA(ordinal, type, name, args, attributes)\
-	PNG_DEFN_MAGIC-name @ordinal-PNG_DEFN_END
+        PNG_DFN "@" name "@ @@" ordinal "@"
 #define PNG_REMOVED(ordinal, type, name, args, attributes)\
-	PNG_DEFN_MAGIC-; name @ordinal-PNG_DEFN_END
+        PNG_DFN "; @" name "@ @@" ordinal "@"
 #define PNG_EXPORT_LAST_ORDINAL(ordinal)\
-	PNG_DEFN_MAGIC-; @ordinal-PNG_DEFN_END
+        PNG_DFN "; @@" ordinal "@"
 
 /* Read the defaults, but use scripts/pnglibconf.h.prebuilt; the 'standard'
  * header file.
@@ -35,13 +35,14 @@
  * defaulted to 'off' in scripts/pnglibconf.dfa
  *
  * Maintenance: if scripts/pnglibconf.dfa options are changed
- * from, or to, 'off' this needs updating!
+ * from, or to, 'disabled' this needs updating!
  */
 #define PNG_BENIGN_ERRORS_SUPPORTED
 #define PNG_ERROR_NUMBERS_SUPPORTED
 #define PNG_READ_BIG_ENDIAN_SUPPORTED  /* should do nothing! */
 #define PNG_INCH_CONVERSIONS_SUPPORTED
 #define PNG_READ_16_TO_8_ACCURATE_SCALE_SUPPORTED
+#define PNG_SET_OPTION_SUPPORTED
 
 #undef PNG_H
 #include "../png.h"
diff --git a/scripts/vers.dfn b/scripts/vers.dfn
index 89d5f38..61ba7d8 100644
--- a/scripts/vers.dfn
+++ b/scripts/vers.dfn
@@ -9,18 +9,11 @@
  * and license in png.h
  */
 
-#define HEADER PNG_DEFN_MAGIC-PNGLIB_LIBNAME {global:-PNG_DEFN_END
-
-/* NOTE: PNG_JOIN is interpreted by the calling script as a signal to
- * join the two things on either side, so we can do symbol
- * substitution within the name, regular C ## joins the pp-tokens,
- * not their final values.
- */
 #define PNG_EXPORTA(ordinal, type, name, args, attributes)\
-	PNG_DEFN_MAGIC- SYMBOL_PREFIX PNG_JOIN name;-PNG_DEFN_END
+        PNG_DFN " @" SYMBOL_PREFIX "@@" name "@;"
 
-#define TRAILER PNG_DEFN_MAGIC-local: *; };-PNG_DEFN_END
+PNG_DFN "@" PNGLIB_LIBNAME "@ {global:"
 
-HEADER
 #include "../png.h"
-TRAILER
+
+PNG_DFN "local: *; };"
diff --git a/test-pngtest.sh b/test-pngtest.sh
deleted file mode 100755
index bba128b..0000000
--- a/test-pngtest.sh
+++ /dev/null
@@ -1,16 +0,0 @@
-#!/bin/sh
-
-echo "Running tests.  For details see pngtest-log.txt"
-
-echo "============ pngtest pngtest.png ==============" > pngtest-log.txt
-
-echo "Running test-pngtest.sh"
-if ./pngtest --strict ${srcdir}/pngtest.png >> pngtest-log.txt 2>&1
-then
-  echo "  PASS: pngtest --strict pngtest.png"
-  err=0
-else
-  echo "  FAIL: pngtest --strict pngtest.png"
-  err=1
-fi
-exit $err
diff --git a/tests/pngstest b/tests/pngstest
new file mode 100755
index 0000000..f5cbdbc
--- /dev/null
+++ b/tests/pngstest
@@ -0,0 +1,13 @@
+#!/bin/sh
+#
+# Usage:
+#
+#  tests/pngstest pattern
+#
+# Runs pngstest on all the contrib/pngsuite/[^x]*${pattern}.png files
+# NOTE: pattern is used to name the temporary files pngstest generates
+#
+pattern="$1"
+shift
+exec ./pngstest --strict --tmpfile "${pattern}" --log ${1+"$@"}\
+   "${srcdir}/contrib/pngsuite/"[a-wyz]*${pattern}".png"
diff --git a/tests/pngstest-0g01 b/tests/pngstest-0g01
new file mode 100755
index 0000000..4a495cc
--- /dev/null
+++ b/tests/pngstest-0g01
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec "${srcdir}/tests/pngstest" 0g01
diff --git a/tests/pngstest-0g02 b/tests/pngstest-0g02
new file mode 100755
index 0000000..1a607c8
--- /dev/null
+++ b/tests/pngstest-0g02
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec "${srcdir}/tests/pngstest" 0g02
diff --git a/tests/pngstest-0g04 b/tests/pngstest-0g04
new file mode 100755
index 0000000..595ba01
--- /dev/null
+++ b/tests/pngstest-0g04
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec "${srcdir}/tests/pngstest" 0g04
diff --git a/tests/pngstest-0g08 b/tests/pngstest-0g08
new file mode 100755
index 0000000..21a8b4b
--- /dev/null
+++ b/tests/pngstest-0g08
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec "${srcdir}/tests/pngstest" 0g08
diff --git a/tests/pngstest-0g16 b/tests/pngstest-0g16
new file mode 100755
index 0000000..0104c37
--- /dev/null
+++ b/tests/pngstest-0g16
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec "${srcdir}/tests/pngstest" 0g16
diff --git a/tests/pngstest-2c08 b/tests/pngstest-2c08
new file mode 100755
index 0000000..9e0de6f
--- /dev/null
+++ b/tests/pngstest-2c08
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec "${srcdir}/tests/pngstest" 2c08
diff --git a/tests/pngstest-2c16 b/tests/pngstest-2c16
new file mode 100755
index 0000000..c1b88b0
--- /dev/null
+++ b/tests/pngstest-2c16
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec "${srcdir}/tests/pngstest" 2c16
diff --git a/tests/pngstest-3p01 b/tests/pngstest-3p01
new file mode 100755
index 0000000..6aeb8d0
--- /dev/null
+++ b/tests/pngstest-3p01
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec "${srcdir}/tests/pngstest" 3p01
diff --git a/tests/pngstest-3p02 b/tests/pngstest-3p02
new file mode 100755
index 0000000..770a75c
--- /dev/null
+++ b/tests/pngstest-3p02
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec "${srcdir}/tests/pngstest" 3p02
diff --git a/tests/pngstest-3p04 b/tests/pngstest-3p04
new file mode 100755
index 0000000..957d6ae
--- /dev/null
+++ b/tests/pngstest-3p04
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec "${srcdir}/tests/pngstest" 3p04
diff --git a/tests/pngstest-3p08 b/tests/pngstest-3p08
new file mode 100755
index 0000000..d0947af
--- /dev/null
+++ b/tests/pngstest-3p08
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec "${srcdir}/tests/pngstest" 3p08
diff --git a/tests/pngstest-4a08 b/tests/pngstest-4a08
new file mode 100755
index 0000000..2fab57f
--- /dev/null
+++ b/tests/pngstest-4a08
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec "${srcdir}/tests/pngstest" 4a08
diff --git a/tests/pngstest-4a16 b/tests/pngstest-4a16
new file mode 100755
index 0000000..e4ad5aa
--- /dev/null
+++ b/tests/pngstest-4a16
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec "${srcdir}/tests/pngstest" 4a16
diff --git a/tests/pngstest-6a08 b/tests/pngstest-6a08
new file mode 100755
index 0000000..aa6c03c
--- /dev/null
+++ b/tests/pngstest-6a08
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec "${srcdir}/tests/pngstest" 6a08
diff --git a/tests/pngstest-6a16 b/tests/pngstest-6a16
new file mode 100755
index 0000000..545b1b3
--- /dev/null
+++ b/tests/pngstest-6a16
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec "${srcdir}/tests/pngstest" 6a16
diff --git a/tests/pngstest-error b/tests/pngstest-error
new file mode 100755
index 0000000..ebf3a7b
--- /dev/null
+++ b/tests/pngstest-error
@@ -0,0 +1,14 @@
+#!/bin/sh
+code=77 # skipped
+for t in "${srcdir}/contrib/pngsuite/"x*".png"
+do
+   if test "$t" != "${srcdir}/contrib/pngsuite/x*.png"
+   then
+      # not skipped, test it
+      if ./pngstest --strict --tmpfile "error" --log "$@" "$t"
+      then
+         code=0 # oops, success: should not happen!
+      fi
+   fi
+done
+exit $code
diff --git a/tests/pngtest b/tests/pngtest
new file mode 100755
index 0000000..813973b
--- /dev/null
+++ b/tests/pngtest
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngtest --strict ${srcdir}/pngtest.png
diff --git a/tests/pngunknown-IDAT b/tests/pngunknown-IDAT
new file mode 100755
index 0000000..71d915a
--- /dev/null
+++ b/tests/pngunknown-IDAT
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngunknown default=discard IDAT=save "${srcdir}/pngtest.png"
diff --git a/tests/pngunknown-discard b/tests/pngunknown-discard
new file mode 100755
index 0000000..f1f02d4
--- /dev/null
+++ b/tests/pngunknown-discard
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngunknown default=discard "${srcdir}/pngtest.png"
diff --git a/tests/pngunknown-if-safe b/tests/pngunknown-if-safe
new file mode 100755
index 0000000..7c52824
--- /dev/null
+++ b/tests/pngunknown-if-safe
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngunknown default=if-safe "${srcdir}/pngtest.png"
diff --git a/tests/pngunknown-sAPI b/tests/pngunknown-sAPI
new file mode 100755
index 0000000..e087ef2
--- /dev/null
+++ b/tests/pngunknown-sAPI
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngunknown bKGD=save cHRM=save gAMA=save all=discard iCCP=save sBIT=save sRGB=save "${srcdir}/pngtest.png"
diff --git a/tests/pngunknown-sTER b/tests/pngunknown-sTER
new file mode 100755
index 0000000..1b945dc
--- /dev/null
+++ b/tests/pngunknown-sTER
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngunknown sTER=if-safe "${srcdir}/pngtest.png"
diff --git a/tests/pngunknown-save b/tests/pngunknown-save
new file mode 100755
index 0000000..ae84930
--- /dev/null
+++ b/tests/pngunknown-save
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngunknown default=save "${srcdir}/pngtest.png"
diff --git a/tests/pngunknown-vpAg b/tests/pngunknown-vpAg
new file mode 100755
index 0000000..396c6e4
--- /dev/null
+++ b/tests/pngunknown-vpAg
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngunknown vpAg=if-safe "${srcdir}/pngtest.png"
diff --git a/tests/pngvalid-gamma-16-to-8 b/tests/pngvalid-gamma-16-to-8
new file mode 100755
index 0000000..b322338
--- /dev/null
+++ b/tests/pngvalid-gamma-16-to-8
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngvalid --gamma-16-to-8
diff --git a/tests/pngvalid-gamma-alpha-mode b/tests/pngvalid-gamma-alpha-mode
new file mode 100755
index 0000000..ed04fa5
--- /dev/null
+++ b/tests/pngvalid-gamma-alpha-mode
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngvalid --gamma-alpha-mode
diff --git a/tests/pngvalid-gamma-background b/tests/pngvalid-gamma-background
new file mode 100755
index 0000000..658e5fe
--- /dev/null
+++ b/tests/pngvalid-gamma-background
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngvalid --gamma-background
diff --git a/tests/pngvalid-gamma-expand16-alpha-mode b/tests/pngvalid-gamma-expand16-alpha-mode
new file mode 100755
index 0000000..bdda566
--- /dev/null
+++ b/tests/pngvalid-gamma-expand16-alpha-mode
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngvalid --gamma-alpha-mode --expand16
diff --git a/tests/pngvalid-gamma-expand16-background b/tests/pngvalid-gamma-expand16-background
new file mode 100755
index 0000000..f993c21
--- /dev/null
+++ b/tests/pngvalid-gamma-expand16-background
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngvalid --gamma-background --expand16
diff --git a/tests/pngvalid-gamma-expand16-transform b/tests/pngvalid-gamma-expand16-transform
new file mode 100755
index 0000000..f462534
--- /dev/null
+++ b/tests/pngvalid-gamma-expand16-transform
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngvalid --gamma-transform --expand16
diff --git a/tests/pngvalid-gamma-sbit b/tests/pngvalid-gamma-sbit
new file mode 100755
index 0000000..aa82fe0
--- /dev/null
+++ b/tests/pngvalid-gamma-sbit
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngvalid --gamma-sbit
diff --git a/tests/pngvalid-gamma-threshold b/tests/pngvalid-gamma-threshold
new file mode 100755
index 0000000..2cfca91
--- /dev/null
+++ b/tests/pngvalid-gamma-threshold
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngvalid --gamma-threshold
diff --git a/tests/pngvalid-gamma-transform b/tests/pngvalid-gamma-transform
new file mode 100755
index 0000000..a71ab4d
--- /dev/null
+++ b/tests/pngvalid-gamma-transform
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngvalid --gamma-transform
diff --git a/tests/pngvalid-progressive-interlace-size b/tests/pngvalid-progressive-interlace-size
new file mode 100755
index 0000000..a31b283
--- /dev/null
+++ b/tests/pngvalid-progressive-interlace-size
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngvalid --size --progressive-read
diff --git a/tests/pngvalid-progressive-interlace-standard b/tests/pngvalid-progressive-interlace-standard
new file mode 100755
index 0000000..59042eb
--- /dev/null
+++ b/tests/pngvalid-progressive-interlace-standard
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngvalid --standard --progressive-read --interlace
diff --git a/tests/pngvalid-progressive-interlace-transform b/tests/pngvalid-progressive-interlace-transform
new file mode 100755
index 0000000..edf9a8a
--- /dev/null
+++ b/tests/pngvalid-progressive-interlace-transform
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngvalid --transform
diff --git a/tests/pngvalid-progressive-standard b/tests/pngvalid-progressive-standard
new file mode 100755
index 0000000..20ecd15
--- /dev/null
+++ b/tests/pngvalid-progressive-standard
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngvalid --standard --interlace
diff --git a/tests/pngvalid-standard b/tests/pngvalid-standard
new file mode 100755
index 0000000..aa1b4b4
--- /dev/null
+++ b/tests/pngvalid-standard
@@ -0,0 +1,2 @@
+#!/bin/sh
+exec ./pngvalid --standard --progressive-read