Add use_system_libopenjpeg2 build argument

This cl adds the possibility to build pdfium on Linux using a system
provided copy of libopenjpeg2, as it is preferred by many distros.

Change-Id: Id2805ffbf7b75678c2c1a73469ae9dfb9278f97b
Reviewed-on: https://pdfium-review.googlesource.com/c/50750
Reviewed-by: Lei Zhang <thestig@chromium.org>
Commit-Queue: Lei Zhang <thestig@chromium.org>
diff --git a/core/fxcodec/codec/ccodec_jpxmodule.cpp b/core/fxcodec/codec/ccodec_jpxmodule.cpp
index 3514d77..fbfc077 100644
--- a/core/fxcodec/codec/ccodec_jpxmodule.cpp
+++ b/core/fxcodec/codec/ccodec_jpxmodule.cpp
@@ -17,8 +17,11 @@
 #include "core/fxcrt/fx_safe_types.h"
 #include "third_party/base/ptr_util.h"
 #include "third_party/base/stl_util.h"
+
+#ifndef USE_SYSTEM_LIBOPENJPEG2
 #include "third_party/libopenjpeg20/openjpeg.h"
 #include "third_party/libopenjpeg20/opj_malloc.h"
+#endif
 
 namespace {
 
@@ -512,7 +515,9 @@
     return false;
 
   m_Image = pTempImage;
+#ifndef USE_SYSTEM_LIBOPENJPEG2
   m_Image->pdfium_use_colorspace = !!m_ColorSpace;
+#endif
   return true;
 }
 
@@ -549,7 +554,11 @@
     // TODO(palmer): Using |opj_free| here resolves the crash described in
     // https://crbug.com/737033, but ultimately we need to harmonize the
     // memory allocation strategy across OpenJPEG and its PDFium callers.
+#ifndef USE_SYSTEM_LIBOPENJPEG2
     opj_free(m_Image->icc_profile_buf);
+#else
+    free(m_Image->icc_profile_buf);
+#endif
     m_Image->icc_profile_buf = nullptr;
     m_Image->icc_profile_len = 0;
   }
diff --git a/core/fxcodec/codec/cjpx_decoder.h b/core/fxcodec/codec/cjpx_decoder.h
index 940efd8..3b6f547 100644
--- a/core/fxcodec/codec/cjpx_decoder.h
+++ b/core/fxcodec/codec/cjpx_decoder.h
@@ -13,7 +13,12 @@
 #include "core/fxcodec/codec/codec_int.h"
 #include "core/fxcrt/unowned_ptr.h"
 #include "third_party/base/span.h"
+
+#ifdef USE_SYSTEM_LIBOPENJPEG2
+#include <openjpeg.h>
+#else
 #include "third_party/libopenjpeg20/openjpeg.h"
+#endif
 
 class CPDF_ColorSpace;
 
diff --git a/core/fxcodec/codec/codec_int.h b/core/fxcodec/codec/codec_int.h
index 4fa819b..484d92e 100644
--- a/core/fxcodec/codec/codec_int.h
+++ b/core/fxcodec/codec/codec_int.h
@@ -10,7 +10,12 @@
 #include <limits.h>
 
 #include "core/fxcodec/jbig2/JBig2_Context.h"
+
+#ifdef USE_SYSTEM_LIBOPENJPEG2
+#include <openjpeg.h>
+#else
 #include "third_party/libopenjpeg20/openjpeg.h"
+#endif
 
 class CPDF_ColorSpace;