Cmake support for openjpeg2

With some tweaks from Albert
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 7fdb49a..8845d00 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -30,7 +30,7 @@
 option(ENABLE_SPLASH "Build the Splash graphics backend." ON)
 option(ENABLE_UTILS "Compile poppler command line utils." ON)
 option(ENABLE_CPP "Compile poppler cpp wrapper." ON)
-option(ENABLE_LIBOPENJPEG "Use libopenjpeg for JPX streams." ON)
+set(ENABLE_LIBOPENJPEG "auto" CACHE STRING "Use libopenjpeg for JPX streams. Possible values: auto, openjpeg1, openjpeg2. 'auto' prefers openjpeg1 over openjpeg2 if both are available. Unset to not use openjpeg.")
 set(ENABLE_CMS "auto" CACHE STRING "Use color management system. Possible values: auto, lcms1, lcms2. 'auto' prefers lcms2 over lcms1 if both are available. Unset to disable color management system.")
 option(ENABLE_LIBCURL "Build libcurl based HTTP support." OFF)
 option(ENABLE_ZLIB "Build with zlib (not totally safe)." OFF)
@@ -151,11 +151,26 @@
   endif(ZLIB_FOUND)
   set(ENABLE_ZLIB ${ZLIB_FOUND})
 endif(ENABLE_ZLIB)
-if(ENABLE_LIBOPENJPEG)
+set(USE_OPENJPEG1 FALSE)
+set(USE_OPENJPEG2 FALSE)
+if(ENABLE_LIBOPENJPEG STREQUAL "auto")
   find_package(LIBOPENJPEG)
-  set(ENABLE_LIBOPENJPEG ${LIBOPENJPEG_FOUND})
-  set(HAVE_OPENJPEG_H ON)
-endif(ENABLE_LIBOPENJPEG)
+  set(USE_OPENJPEG1 ${LIBOPENJPEG_FOUND})
+  set(WITH_OPENJPEG ${LIBOPENJPEG_FOUND})
+  if(NOT LIBOPENJPEG_FOUND)
+    find_package(LIBOPENJPEG2)
+    set(USE_OPENJPEG2 ${LIBOPENJPEG2_FOUND})
+    set(WITH_OPENJPEG ${LIBOPENJPEG2_FOUND})
+  endif()
+elseif(ENABLE_LIBOPENJPEG STREQUAL "openjpeg1")
+  find_package(LIBOPENJPEG)
+  set(USE_OPENJPEG1 ${LIBOPENJPEG_FOUND})
+  set(WITH_OPENJPEG ${LIBOPENJPEG_FOUND})
+elseif(ENABLE_LIBOPENJPEG STREQUAL "openjpeg2")
+  find_package(LIBOPENJPEG2)
+  set(USE_OPENJPEG2 ${LIBOPENJPEG2_FOUND})
+  set(WITH_OPENJPEG ${LIBOPENJPEG2_FOUND})
+endif()
 if(ENABLE_CMS STREQUAL "auto")
   find_package(LCMS2)
   set(USE_CMS ${LCMS2_FOUND})
@@ -225,6 +240,9 @@
 if(LIBOPENJPEG_FOUND)
   include_directories(${LIBOPENJPEG_INCLUDE_DIR})
 endif(LIBOPENJPEG_FOUND)
+if(LIBOPENJPEG2_FOUND)
+  include_directories(${LIBOPENJPEG2_INCLUDE_DIRS})
+endif()
 if(LCMS_FOUND)
   include_directories(${LCMS_INCLUDE_DIR})
 endif(LCMS_FOUND)
@@ -409,11 +427,19 @@
     poppler/JPEG2000Stream.cc
   )
   set(poppler_LIBS ${poppler_LIBS} ${LIBOPENJPEG_LIBRARIES})
-else (LIBOPENJPEG_FOUND)
+  add_definitions(-DUSE_OPENJPEG1)
+elseif (LIBOPENJPEG2_FOUND)
+  set(poppler_SRCS ${poppler_SRCS}
+    poppler/JPEG2000Stream.cc
+  )
+  add_definitions(-DUSE_OPENJPEG2)
+MESSAGE(${LIBOPENJPEG2_LIBRARIES})
+  set(poppler_LIBS ${poppler_LIBS} ${LIBOPENJPEG2_LIBRARIES})
+else ()
   set(poppler_SRCS ${poppler_SRCS}
     poppler/JPXStream.cc
   )
-endif(LIBOPENJPEG_FOUND)
+endif()
 if(USE_CMS)
   if(LCMS_FOUND)
     set(poppler_LIBS ${poppler_LIBS} ${LCMS_LIBRARIES})
@@ -574,11 +600,15 @@
     install(FILES
       poppler/JPEG2000Stream.h
       DESTINATION include/poppler)
-  else(LIBOPENJPEG_FOUND)
+  elseif(LIBOPENJPEG2_FOUND)
+    install(FILES
+      poppler/JPEG2000Stream.h
+      DESTINATION include/poppler)
+  else()
     install(FILES
       poppler/JPXStream.h
       DESTINATION include/poppler)
-  endif(LIBOPENJPEG_FOUND)
+  endif()
   if(ENABLE_SPLASH)
     install(FILES
       poppler/SplashOutputDev.h
@@ -678,7 +708,13 @@
 show_end_message_yesno("use libtiff" ENABLE_LIBTIFF)
 show_end_message_yesno("use zlib" ENABLE_ZLIB)
 show_end_message_yesno("use curl" ENABLE_LIBCURL)
-show_end_message_yesno("use libopenjpeg" LIBOPENJPEG_FOUND)
+show_end_message_yesno("use libopenjpeg" WITH_OPENJPEG)
+if(USE_OPENJPEG1)
+  message("      with openjpeg1")
+endif()
+if(USE_OPENJPEG2)
+  message("      with openjpeg2")
+endif()
 show_end_message_yesno("use cms" USE_CMS)
 if(LCMS_FOUND)
   message("      with lcms1")
diff --git a/cmake/modules/FindLIBOPENJPEG2.cmake b/cmake/modules/FindLIBOPENJPEG2.cmake
new file mode 100644
index 0000000..f18bd3c
--- /dev/null
+++ b/cmake/modules/FindLIBOPENJPEG2.cmake
@@ -0,0 +1,30 @@
+# - Try to find the libopenjpeg2 library
+# Once done this will define
+#
+#  LIBOPENJPEG2_FOUND - system has libopenjpeg
+#  LIBOPENJPEG2_INCLUDE_DIRS - the libopenjpeg include directories
+#  LIBOPENJPEG2_LIBRARIES - Link these to use libopenjpeg
+
+# Copyright (c) 2008, Albert Astals Cid, <aacid@kde.org>
+#
+# Redistribution and use is allowed according to the terms of the BSD license.
+# For details see the accompanying COPYING-CMAKE-SCRIPTS file.
+
+
+if (LIBOPENJPEG2_LIBRARIES AND LIBOPENJPEG2_INCLUDE_DIR)
+
+  # in cache already
+  set(LIBOPENJPEG2_FOUND TRUE)
+
+else ()
+
+  set(LIBOPENJPEG2_FOUND FALSE)
+  set(LIBOPENJPEG2_INCLUDE_DIRS)
+  set(LIBOPENJPEG2_LIBRARIES)
+
+  find_package(PkgConfig REQUIRED)
+  pkg_check_modules(LIBOPENJPEG2 libopenjp2)
+  if (LIBOPENJPEG2_FOUND)
+    add_definitions(-DUSE_OPENJPEG2)
+  endif ()
+endif ()