Allow building pdfium against the system version of libpng.

Bug:
Change-Id: I0c930ca30637f58af3b60ed8f1383bd9234a1723
Reviewed-on: https://pdfium-review.googlesource.com/19850
Commit-Queue: Lei Zhang <thestig@chromium.org>
Reviewed-by: Lei Zhang <thestig@chromium.org>
diff --git a/BUILD.gn b/BUILD.gn
index 228271d..4a9daca 100644
--- a/BUILD.gn
+++ b/BUILD.gn
@@ -273,7 +273,7 @@
   configs += [ ":pdfium_core_config" ]
   deps = []
   if (!pdf_enable_xfa) {
-    deps += [ "third_party:fx_lpng" ]
+    deps += [ "third_party:png" ]
   }
 }
 
@@ -781,8 +781,8 @@
       "core/fxcodec/lbmp/fx_bmp.h",
     ]
     deps += [
-      "third_party:fx_lpng",
       "third_party:fx_tiff",
+      "third_party:png",
     ]
   }
   if (is_posix) {
diff --git a/core/fxcodec/codec/ccodec_pngmodule.cpp b/core/fxcodec/codec/ccodec_pngmodule.cpp
index 40bdfba..37e83f7 100644
--- a/core/fxcodec/codec/ccodec_pngmodule.cpp
+++ b/core/fxcodec/codec/ccodec_pngmodule.cpp
@@ -14,10 +14,11 @@
 #include "core/fxge/fx_dib.h"
 #include "third_party/base/ptr_util.h"
 
-extern "C" {
-#undef FAR
+#ifdef USE_SYSTEM_LIBPNG
+#include <png.h>
+#else
 #include "third_party/libpng16/png.h"
-}  // extern "C"
+#endif
 
 #define PNG_ERROR_SIZE 256
 
diff --git a/pdfium.gni b/pdfium.gni
index 530301a..f05623a 100644
--- a/pdfium.gni
+++ b/pdfium.gni
@@ -61,6 +61,9 @@
 
   # Don't build against bundled lcms2.
   use_system_lcms2 = false
+
+  # Don't build against bundled libpng.
+  use_system_libpng = false
 }
 
 if (pdf_use_skia && pdf_use_skia_paths) {
diff --git a/testing/image_diff/image_diff_png.cpp b/testing/image_diff/image_diff_png.cpp
index 56be539..48b5ae1 100644
--- a/testing/image_diff/image_diff_png.cpp
+++ b/testing/image_diff/image_diff_png.cpp
@@ -17,9 +17,14 @@
 #include <string>
 
 #include "third_party/base/logging.h"
-#include "third_party/libpng16/png.h"
 #include "third_party/zlib/zlib.h"
 
+#ifdef USE_SYSTEM_LIBPNG
+#include <png.h>
+#else
+#include "third_party/libpng16/png.h"
+#endif
+
 namespace image_diff_png {
 
 namespace {
diff --git a/third_party/BUILD.gn b/third_party/BUILD.gn
index a188f56..3861f6a 100644
--- a/third_party/BUILD.gn
+++ b/third_party/BUILD.gn
@@ -398,6 +398,21 @@
   ]
 }
 
+config("system_libpng_config") {
+  libs = [ "png" ]
+  defines = [ "USE_SYSTEM_LIBPNG" ]
+}
+
+group("png") {
+  if (use_system_libpng) {
+    public_configs = [ ":system_libpng_config" ]
+  } else {
+    public_deps = [
+      ":fx_lpng",
+    ]
+  }
+}
+
 static_library("fx_lpng") {
   configs -= [ "//build/config/compiler:chromium_code" ]
   configs += [