OpenCVFindLibsGrfmt.cmake 12 KB

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