OpenCVFindLibsGrfmt.cmake 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. # ----------------------------------------------------------------------------
  2. # Detect 3rd-party image IO libraries
  3. # ----------------------------------------------------------------------------
  4. # --- zlib (required) ---
  5. if(BUILD_ZLIB)
  6. ocv_clear_vars(ZLIB_FOUND)
  7. else()
  8. ocv_clear_internal_cache_vars(ZLIB_LIBRARY ZLIB_INCLUDE_DIR)
  9. if(ANDROID)
  10. set(_zlib_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES ${CMAKE_FIND_LIBRARY_SUFFIXES})
  11. set(CMAKE_FIND_LIBRARY_SUFFIXES .so)
  12. endif()
  13. find_package(ZLIB "${MIN_VER_ZLIB}")
  14. if(ANDROID)
  15. set(CMAKE_FIND_LIBRARY_SUFFIXES ${_zlib_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES})
  16. unset(_zlib_ORIG_CMAKE_FIND_LIBRARY_SUFFIXES)
  17. endif()
  18. if(ZLIB_FOUND AND ANDROID)
  19. if(ZLIB_LIBRARY MATCHES "/usr/lib.*/libz.so$")
  20. set(ZLIB_LIBRARY z)
  21. set(ZLIB_LIBRARIES z)
  22. set(ZLIB_LIBRARY_RELEASE z)
  23. endif()
  24. endif()
  25. endif()
  26. if(NOT ZLIB_FOUND)
  27. ocv_clear_vars(ZLIB_LIBRARY ZLIB_LIBRARIES ZLIB_INCLUDE_DIR)
  28. set(ZLIB_LIBRARY zlib CACHE INTERNAL "")
  29. add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/zlib")
  30. set(ZLIB_INCLUDE_DIR "${${ZLIB_LIBRARY}_SOURCE_DIR}" "${${ZLIB_LIBRARY}_BINARY_DIR}" CACHE INTERNAL "")
  31. set(ZLIB_INCLUDE_DIRS ${ZLIB_INCLUDE_DIR})
  32. set(ZLIB_LIBRARIES ${ZLIB_LIBRARY})
  33. ocv_parse_header2(ZLIB "${${ZLIB_LIBRARY}_SOURCE_DIR}/zlib.h" ZLIB_VERSION)
  34. endif()
  35. # --- libjpeg (optional) ---
  36. if(WITH_JPEG)
  37. if(BUILD_JPEG)
  38. ocv_clear_vars(JPEG_FOUND)
  39. else()
  40. ocv_clear_internal_cache_vars(JPEG_LIBRARY JPEG_INCLUDE_DIR)
  41. include(FindJPEG)
  42. endif()
  43. if(NOT JPEG_FOUND)
  44. ocv_clear_vars(JPEG_LIBRARY JPEG_INCLUDE_DIR)
  45. if(NOT BUILD_JPEG_TURBO_DISABLE)
  46. set(JPEG_LIBRARY libjpeg-turbo CACHE INTERNAL "")
  47. set(JPEG_LIBRARIES ${JPEG_LIBRARY})
  48. add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/libjpeg-turbo")
  49. set(JPEG_INCLUDE_DIR "${${JPEG_LIBRARY}_SOURCE_DIR}/src" CACHE INTERNAL "")
  50. else()
  51. set(JPEG_LIBRARY libjpeg CACHE INTERNAL "")
  52. set(JPEG_LIBRARIES ${JPEG_LIBRARY})
  53. add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/libjpeg")
  54. set(JPEG_INCLUDE_DIR "${${JPEG_LIBRARY}_SOURCE_DIR}" CACHE INTERNAL "")
  55. endif()
  56. set(JPEG_INCLUDE_DIRS "${JPEG_INCLUDE_DIR}")
  57. endif()
  58. macro(ocv_detect_jpeg_version header_file)
  59. if(NOT DEFINED JPEG_LIB_VERSION AND EXISTS "${header_file}")
  60. ocv_parse_header("${header_file}" JPEG_VERSION_LINES JPEG_LIB_VERSION)
  61. endif()
  62. endmacro()
  63. ocv_detect_jpeg_version("${JPEG_INCLUDE_DIR}/jpeglib.h")
  64. if(DEFINED CMAKE_CXX_LIBRARY_ARCHITECTURE)
  65. ocv_detect_jpeg_version("${JPEG_INCLUDE_DIR}/${CMAKE_CXX_LIBRARY_ARCHITECTURE}/jconfig.h")
  66. endif()
  67. # no needed for strict platform check here, both files 64/32 should contain the same version
  68. ocv_detect_jpeg_version("${JPEG_INCLUDE_DIR}/jconfig-64.h")
  69. ocv_detect_jpeg_version("${JPEG_INCLUDE_DIR}/jconfig-32.h")
  70. ocv_detect_jpeg_version("${JPEG_INCLUDE_DIR}/jconfig.h")
  71. ocv_detect_jpeg_version("${${JPEG_LIBRARY}_BINARY_DIR}/jconfig.h")
  72. if(NOT DEFINED JPEG_LIB_VERSION)
  73. set(JPEG_LIB_VERSION "unknown")
  74. endif()
  75. set(HAVE_JPEG YES)
  76. endif()
  77. # --- libtiff (optional, should be searched after zlib and libjpeg) ---
  78. if(WITH_TIFF)
  79. if(BUILD_TIFF)
  80. ocv_clear_vars(TIFF_FOUND)
  81. else()
  82. ocv_clear_internal_cache_vars(TIFF_LIBRARY TIFF_INCLUDE_DIR)
  83. include(FindTIFF)
  84. if(TIFF_FOUND)
  85. ocv_parse_header("${TIFF_INCLUDE_DIR}/tiff.h" TIFF_VERSION_LINES TIFF_VERSION_CLASSIC TIFF_VERSION_BIG TIFF_VERSION TIFF_BIGTIFF_VERSION)
  86. endif()
  87. endif()
  88. if(NOT TIFF_FOUND)
  89. ocv_clear_vars(TIFF_LIBRARY TIFF_LIBRARIES TIFF_INCLUDE_DIR)
  90. set(TIFF_LIBRARY libtiff CACHE INTERNAL "")
  91. set(TIFF_LIBRARIES ${TIFF_LIBRARY})
  92. add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/libtiff")
  93. set(TIFF_INCLUDE_DIR "${${TIFF_LIBRARY}_SOURCE_DIR}" "${${TIFF_LIBRARY}_BINARY_DIR}" CACHE INTERNAL "")
  94. ocv_parse_header("${${TIFF_LIBRARY}_SOURCE_DIR}/tiff.h" TIFF_VERSION_LINES TIFF_VERSION_CLASSIC TIFF_VERSION_BIG TIFF_VERSION TIFF_BIGTIFF_VERSION)
  95. endif()
  96. if(TIFF_VERSION_CLASSIC AND NOT TIFF_VERSION)
  97. set(TIFF_VERSION ${TIFF_VERSION_CLASSIC})
  98. endif()
  99. if(TIFF_BIGTIFF_VERSION AND NOT TIFF_VERSION_BIG)
  100. set(TIFF_VERSION_BIG ${TIFF_BIGTIFF_VERSION})
  101. endif()
  102. if(NOT TIFF_VERSION_STRING AND TIFF_INCLUDE_DIR)
  103. list(GET TIFF_INCLUDE_DIR 0 _TIFF_INCLUDE_DIR)
  104. if(EXISTS "${_TIFF_INCLUDE_DIR}/tiffvers.h")
  105. file(STRINGS "${_TIFF_INCLUDE_DIR}/tiffvers.h" tiff_version_str REGEX "^#define[\t ]+TIFFLIB_VERSION_STR[\t ]+\"LIBTIFF, Version .*")
  106. string(REGEX REPLACE "^#define[\t ]+TIFFLIB_VERSION_STR[\t ]+\"LIBTIFF, Version +([^ \\n]*).*" "\\1" TIFF_VERSION_STRING "${tiff_version_str}")
  107. unset(tiff_version_str)
  108. endif()
  109. unset(_TIFF_INCLUDE_DIR)
  110. endif()
  111. set(HAVE_TIFF YES)
  112. endif()
  113. # --- libwebp (optional) ---
  114. if(WITH_WEBP)
  115. if(BUILD_WEBP)
  116. ocv_clear_vars(WEBP_FOUND WEBP_LIBRARY WEBP_LIBRARIES WEBP_INCLUDE_DIR)
  117. else()
  118. ocv_clear_internal_cache_vars(WEBP_LIBRARY WEBP_INCLUDE_DIR)
  119. include(cmake/OpenCVFindWebP.cmake)
  120. if(WEBP_FOUND)
  121. set(HAVE_WEBP 1)
  122. endif()
  123. endif()
  124. endif()
  125. # --- Add libwebp to 3rdparty/libwebp and compile it if not available ---
  126. if(WITH_WEBP AND NOT WEBP_FOUND
  127. AND (NOT ANDROID OR HAVE_CPUFEATURES)
  128. )
  129. ocv_clear_vars(WEBP_LIBRARY WEBP_INCLUDE_DIR)
  130. set(WEBP_LIBRARY libwebp CACHE INTERNAL "")
  131. set(WEBP_LIBRARIES ${WEBP_LIBRARY})
  132. add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/libwebp")
  133. set(WEBP_INCLUDE_DIR "${${WEBP_LIBRARY}_SOURCE_DIR}/src" CACHE INTERNAL "")
  134. set(HAVE_WEBP 1)
  135. endif()
  136. if(NOT WEBP_VERSION AND WEBP_INCLUDE_DIR)
  137. ocv_clear_vars(ENC_MAJ_VERSION ENC_MIN_VERSION ENC_REV_VERSION)
  138. if(EXISTS "${WEBP_INCLUDE_DIR}/enc/vp8enci.h")
  139. ocv_parse_header("${WEBP_INCLUDE_DIR}/enc/vp8enci.h" WEBP_VERSION_LINES ENC_MAJ_VERSION ENC_MIN_VERSION ENC_REV_VERSION)
  140. set(WEBP_VERSION "${ENC_MAJ_VERSION}.${ENC_MIN_VERSION}.${ENC_REV_VERSION}")
  141. elseif(EXISTS "${WEBP_INCLUDE_DIR}/webp/encode.h")
  142. file(STRINGS "${WEBP_INCLUDE_DIR}/webp/encode.h" WEBP_ENCODER_ABI_VERSION REGEX "#define[ \t]+WEBP_ENCODER_ABI_VERSION[ \t]+([x0-9a-f]+)" )
  143. if(WEBP_ENCODER_ABI_VERSION MATCHES "#define[ \t]+WEBP_ENCODER_ABI_VERSION[ \t]+([x0-9a-f]+)")
  144. set(WEBP_ENCODER_ABI_VERSION "${CMAKE_MATCH_1}")
  145. set(WEBP_VERSION "encoder: ${WEBP_ENCODER_ABI_VERSION}")
  146. else()
  147. unset(WEBP_ENCODER_ABI_VERSION)
  148. endif()
  149. endif()
  150. endif()
  151. # --- libopenjp2 (optional, check before libjasper) ---
  152. if(WITH_OPENJPEG)
  153. if(BUILD_OPENJPEG)
  154. ocv_clear_vars(OpenJPEG_FOUND)
  155. else()
  156. find_package(OpenJPEG QUIET)
  157. endif()
  158. if(NOT OpenJPEG_FOUND OR OPENJPEG_MAJOR_VERSION LESS 2)
  159. ocv_clear_vars(OPENJPEG_MAJOR_VERSION OPENJPEG_MINOR_VERSION OPENJPEG_BUILD_VERSION OPENJPEG_LIBRARIES OPENJPEG_INCLUDE_DIRS)
  160. message(STATUS "Could NOT find OpenJPEG (minimal suitable version: 2.0, "
  161. "recommended version >= 2.3.1). OpenJPEG will be built from sources")
  162. add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/openjpeg")
  163. if(OCV_CAN_BUILD_OPENJPEG)
  164. set(HAVE_OPENJPEG YES)
  165. message(STATUS "OpenJPEG libraries will be built from sources: ${OPENJPEG_LIBRARIES} "
  166. "(version \"${OPENJPEG_VERSION}\")")
  167. else()
  168. set(HAVE_OPENJPEG NO)
  169. message(STATUS "OpenJPEG libraries can't be built from sources. System requirements are not fulfilled.")
  170. endif()
  171. else()
  172. set(HAVE_OPENJPEG YES)
  173. message(STATUS "Found system OpenJPEG: ${OPENJPEG_LIBRARIES} "
  174. "(found version \"${OPENJPEG_VERSION}\")")
  175. endif()
  176. endif()
  177. # --- libjasper (optional, should be searched after libjpeg) ---
  178. if(WITH_JASPER AND NOT HAVE_OPENJPEG)
  179. if(BUILD_JASPER)
  180. ocv_clear_vars(JASPER_FOUND)
  181. else()
  182. include(FindJasper)
  183. endif()
  184. if(NOT JASPER_FOUND)
  185. ocv_clear_vars(JASPER_LIBRARY JASPER_LIBRARIES JASPER_INCLUDE_DIR)
  186. set(JASPER_LIBRARY libjasper CACHE INTERNAL "")
  187. set(JASPER_LIBRARIES ${JASPER_LIBRARY})
  188. add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/libjasper")
  189. set(JASPER_INCLUDE_DIR "${${JASPER_LIBRARY}_SOURCE_DIR}" CACHE INTERNAL "")
  190. endif()
  191. set(HAVE_JASPER YES)
  192. if(NOT JASPER_VERSION_STRING)
  193. ocv_parse_header2(JASPER "${JASPER_INCLUDE_DIR}/jasper/jas_config.h" JAS_VERSION "")
  194. endif()
  195. endif()
  196. # --- libpng (optional, should be searched after zlib) ---
  197. if(WITH_PNG)
  198. if(BUILD_PNG)
  199. ocv_clear_vars(PNG_FOUND)
  200. else()
  201. ocv_clear_internal_cache_vars(PNG_LIBRARY PNG_INCLUDE_DIR)
  202. include(FindPNG)
  203. if(PNG_FOUND)
  204. include(CheckIncludeFile)
  205. check_include_file("${PNG_PNG_INCLUDE_DIR}/libpng/png.h" HAVE_LIBPNG_PNG_H)
  206. if(HAVE_LIBPNG_PNG_H)
  207. ocv_parse_header("${PNG_PNG_INCLUDE_DIR}/libpng/png.h" PNG_VERSION_LINES PNG_LIBPNG_VER_MAJOR PNG_LIBPNG_VER_MINOR PNG_LIBPNG_VER_RELEASE)
  208. else()
  209. ocv_parse_header("${PNG_PNG_INCLUDE_DIR}/png.h" PNG_VERSION_LINES PNG_LIBPNG_VER_MAJOR PNG_LIBPNG_VER_MINOR PNG_LIBPNG_VER_RELEASE)
  210. endif()
  211. endif()
  212. endif()
  213. if(NOT PNG_FOUND)
  214. ocv_clear_vars(PNG_LIBRARY PNG_LIBRARIES PNG_INCLUDE_DIR PNG_PNG_INCLUDE_DIR HAVE_LIBPNG_PNG_H PNG_DEFINITIONS)
  215. set(PNG_LIBRARY libpng CACHE INTERNAL "")
  216. set(PNG_LIBRARIES ${PNG_LIBRARY})
  217. add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/libpng")
  218. set(PNG_INCLUDE_DIR "${${PNG_LIBRARY}_SOURCE_DIR}" CACHE INTERNAL "")
  219. set(PNG_DEFINITIONS "")
  220. ocv_parse_header("${PNG_INCLUDE_DIR}/png.h" PNG_VERSION_LINES PNG_LIBPNG_VER_MAJOR PNG_LIBPNG_VER_MINOR PNG_LIBPNG_VER_RELEASE)
  221. endif()
  222. set(HAVE_PNG YES)
  223. set(PNG_VERSION "${PNG_LIBPNG_VER_MAJOR}.${PNG_LIBPNG_VER_MINOR}.${PNG_LIBPNG_VER_RELEASE}")
  224. endif()
  225. # --- OpenEXR (optional) ---
  226. if(WITH_OPENEXR)
  227. ocv_clear_vars(HAVE_OPENEXR)
  228. if(NOT BUILD_OPENEXR)
  229. ocv_clear_internal_cache_vars(OPENEXR_INCLUDE_PATHS OPENEXR_LIBRARIES OPENEXR_ILMIMF_LIBRARY OPENEXR_VERSION)
  230. include("${OpenCV_SOURCE_DIR}/cmake/OpenCVFindOpenEXR.cmake")
  231. endif()
  232. if(OPENEXR_FOUND)
  233. set(HAVE_OPENEXR YES)
  234. else()
  235. ocv_clear_vars(OPENEXR_INCLUDE_PATHS OPENEXR_LIBRARIES OPENEXR_ILMIMF_LIBRARY OPENEXR_VERSION)
  236. set(OPENEXR_LIBRARIES IlmImf)
  237. add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/openexr")
  238. if(OPENEXR_VERSION) # check via TARGET doesn't work
  239. set(BUILD_OPENEXR ON)
  240. set(HAVE_OPENEXR YES)
  241. set(BUILD_OPENEXR ON)
  242. endif()
  243. endif()
  244. endif()
  245. # --- GDAL (optional) ---
  246. if(WITH_GDAL)
  247. find_package(GDAL QUIET)
  248. if(NOT GDAL_FOUND)
  249. set(HAVE_GDAL NO)
  250. ocv_clear_vars(GDAL_VERSION GDAL_LIBRARIES)
  251. else()
  252. set(HAVE_GDAL YES)
  253. ocv_include_directories(${GDAL_INCLUDE_DIR})
  254. endif()
  255. endif()
  256. if(WITH_GDCM)
  257. find_package(GDCM QUIET)
  258. if(NOT GDCM_FOUND)
  259. set(HAVE_GDCM NO)
  260. ocv_clear_vars(GDCM_VERSION GDCM_LIBRARIES)
  261. else()
  262. set(HAVE_GDCM YES)
  263. # include(${GDCM_USE_FILE})
  264. set(GDCM_LIBRARIES gdcmMSFF) # GDCM does not set this variable for some reason
  265. endif()
  266. endif()
  267. if(WITH_IMGCODEC_HDR)
  268. set(HAVE_IMGCODEC_HDR ON)
  269. elseif(DEFINED WITH_IMGCODEC_HDR)
  270. set(HAVE_IMGCODEC_HDR OFF)
  271. endif()
  272. if(WITH_IMGCODEC_SUNRASTER)
  273. set(HAVE_IMGCODEC_SUNRASTER ON)
  274. elseif(DEFINED WITH_IMGCODEC_SUNRASTER)
  275. set(HAVE_IMGCODEC_SUNRASTER OFF)
  276. endif()
  277. if(WITH_IMGCODEC_PXM)
  278. set(HAVE_IMGCODEC_PXM ON)
  279. elseif(DEFINED WITH_IMGCODEC_PXM)
  280. set(HAVE_IMGCODEC_PXM OFF)
  281. endif()
  282. if(WITH_IMGCODEC_PFM)
  283. set(HAVE_IMGCODEC_PFM ON)
  284. elseif(DEFINED WITH_IMGCODEC_PFM)
  285. set(HAVE_IMGCODEC_PFM OFF)
  286. endif()