+dnl Copyright (C) 2009 Nick Bowler
+dnl Copying and distribution of this file, with or without modification,
+dnl are permitted in any medium without royalty provided the copyright
+dnl notice and this notice are preserved. This file is offered as-is,
+dnl without any warranty.
+
+dnl _LIBPNG_TRY_VERSION(min-version)
+AC_DEFUN([_LIBPNG_TRY_VERSION], [dnl
+sedhead='s/@<:@0-9@:>@@<:@0-9@:>@*/<<<&>>>/'
+sedtail='s/.*<<<\(.*\)>>>.*/\1/;t;s/.*/0/'
+png_min_major=`printf '%s' '$1' | sed -e "$sedhead"1 -e "$sedtail"`
+png_min_minor=`printf '%s' '$1' | sed -e "$sedhead"2 -e "$sedtail"`
+png_min_release=`printf '%s' '$1' | sed -e "$sedhead"3 -e "$sedtail"`
+png_min_build=`printf '%s' '$1' | sed -e "$sedhead"4 -e "$sedtail"`
+png_min_status=`printf '%s' '$1' | sed '
+ t; s/.*alpha.*/PNG_LIBPNG_BUILD_ALPHA/
+ t; s/.*beta.*/PNG_LIBPNG_BUILD_BETA/
+ t; s/.*rc.*/PNG_LIBPNG_BUILD_RC/
+ t; s/.*/PNG_LIBPNG_BUILD_STABLE/
+'`
+
+AC_CACHE_CHECK([for libpng version at least $1],
+ [libpng_cv_sufficient_ver],
+ [AC_LINK_IFELSE([AC_LANG_PROGRAM([dnl
+#include <png.h>
+#if PNG_LIBPNG_VER_MAJOR < $png_min_major
+# error Invalid libpng major version
+#elif PNG_LIBPNG_VER_MAJOR == $png_min_major
+# if PNG_LIBPNG_VER_MINOR < $png_min_minor
+# error Invalid libpng minor version
+# elif PNG_LIBPNG_VER_MINOR == $png_min_minor
+# if PNG_LIBPNG_VER_RELEASE < $png_min_release
+# error Invalid libpng release
+# elif PNG_LIBPNG_VER_RELEASE == $png_min_release
+# if PNG_LIBPNG_BUILD_BASE_TYPE < $png_min_status
+# error Invalid libpng release status
+# elif PNG_LIBPNG_BUILD_BASE_TYPE == $png_min_status
+# if PNG_LIBPNG_VER_BUILD < $png_min_build
+# error Invalid libpng build
+# endif
+# endif
+# endif
+# endif
+#endif
+], [dnl
+png_structp png = png_create_read_struct("", 0, 0, 0);
+png_infop info = png_create_info_struct(png);
+])], [libpng_cv_sufficient_ver=yes], [libpng_cv_sufficient_ver=no])])
+])
+
+dnl CHECK_LIBPNG([min-version], [action-if-ok], [action-if-fail])
+AC_DEFUN([CHECK_LIBPNG],
+[AC_REQUIRE([PKG_PROG_PKG_CONFIG])dnl
+AC_ARG_VAR([LIBPNG_CFLAGS], [C compiler flags for libpng])dnl
+AC_ARG_VAR([LIBPNG_LIBS], [linker flags for libpng])dnl
+
+if test x"$PKG_CONFIG" != x; then
+ AC_MSG_CHECKING([pkg-config database for libpng])
+ _PKG_CONFIG([libpng_cflags], [cflags], [libpng])
+ _PKG_CONFIG([libpng_libs], [libs], [libpng])
+ if test x$pkg_failed = xyes; then
+ errors=`$PKG_CONFIG --errors-to-stdout --print-errors libpng`
+ echo "$errors" >&AS_MESSAGE_LOG_FD
+ AC_MSG_RESULT([no])
+ else
+ if test x"$LIBPNG_CFLAGS" = x; then
+ LIBPNG_CFLAGS=$pkg_cv_libpng_cflags
+ fi
+ if test x"$LIBPNG_LIBS" = x; then
+ LIBPNG_LIBS=$pkg_cv_libpng_libs
+ fi
+
+ AC_MSG_RESULT([yes])
+ fi
+fi
+
+if test x"$LIBPNG_LIBS" = x; then
+ LIBPNG_LIBS=-lpng
+fi
+
+AC_LANG_PUSH([C])
+old_cflags=$CFLAGS
+old_libs=$LIBS
+CFLAGS="$LIBPNG_CFLAGS $CFLAGS"
+LIBS="$LIBPNG_LIBS $LIBS"
+
+_LIBPNG_TRY_VERSION(ifelse([$1], [], [1.0], [$1]))
+
+CFLAGS=$old_cflags
+LIBS=$old_libs
+AC_LANG_POP([C])
+
+if test x"$libpng_cv_sufficient_ver" = x"yes"; then
+ AC_SUBST([LIBPNG_CFLAGS])
+ AC_SUBST([LIBPNG_LIBS])
+ ifelse([$2], [], [true], [$2])
+else
+ ifelse([$3], [], [AC_MSG_FAILURE([dnl
+libpng version >= ifelse([$1], [], [1.0], [$1]) is required. The latest
+version can be obtained from http://libpng.org/pub/png/libpng.html.
+
+If libpng is installed but was not found by this configure script,
+consider adjusting LIBPNG_CFLAGS and/or LIBPNG_LIBS as necessary.
+
+If pkg-config is installed, it may help to adjust PKG_CONFIG_PATH
+if libpng is installed in a non-standard prefix.
+])], [$3])
+fi
+])