Merge pull request #7656 from alalek:python_warning
[platform/upstream/opencv.git] / cmake / OpenCVFindLibsGrfmt.cmake
1 # ----------------------------------------------------------------------------
2 #  Detect 3rd-party image IO libraries
3 # ----------------------------------------------------------------------------
4
5 # --- zlib (required) ---
6 if(BUILD_ZLIB)
7   ocv_clear_vars(ZLIB_FOUND)
8 else()
9   find_package(ZLIB "${MIN_VER_ZLIB}")
10   if(ZLIB_FOUND AND ANDROID)
11     if(ZLIB_LIBRARIES STREQUAL "${ANDROID_SYSROOT}/usr/lib/libz.so" OR
12         ZLIB_LIBRARIES STREQUAL "${ANDROID_SYSROOT}/usr/lib64/libz.so")
13       set(ZLIB_LIBRARIES z)
14     endif()
15   endif()
16 endif()
17
18 if(NOT ZLIB_FOUND)
19   ocv_clear_vars(ZLIB_LIBRARY ZLIB_LIBRARIES ZLIB_INCLUDE_DIRS)
20
21   set(ZLIB_LIBRARY zlib)
22   add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/zlib")
23   set(ZLIB_INCLUDE_DIRS "${${ZLIB_LIBRARY}_SOURCE_DIR}" "${${ZLIB_LIBRARY}_BINARY_DIR}")
24   set(ZLIB_LIBRARIES ${ZLIB_LIBRARY})
25
26   ocv_parse_header2(ZLIB "${${ZLIB_LIBRARY}_SOURCE_DIR}/zlib.h" ZLIB_VERSION)
27 endif()
28
29 # --- libtiff (optional, should be searched after zlib) ---
30 if(WITH_TIFF)
31   if(BUILD_TIFF)
32     ocv_clear_vars(TIFF_FOUND)
33   else()
34     include(FindTIFF)
35     if(TIFF_FOUND)
36       ocv_parse_header("${TIFF_INCLUDE_DIR}/tiff.h" TIFF_VERSION_LINES TIFF_VERSION_CLASSIC TIFF_VERSION_BIG TIFF_VERSION TIFF_BIGTIFF_VERSION)
37     endif()
38   endif()
39
40   if(NOT TIFF_FOUND)
41     ocv_clear_vars(TIFF_LIBRARY TIFF_LIBRARIES TIFF_INCLUDE_DIR)
42
43     set(TIFF_LIBRARY libtiff)
44     set(TIFF_LIBRARIES ${TIFF_LIBRARY})
45     add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/libtiff")
46     set(TIFF_INCLUDE_DIR "${${TIFF_LIBRARY}_SOURCE_DIR}" "${${TIFF_LIBRARY}_BINARY_DIR}")
47     ocv_parse_header("${${TIFF_LIBRARY}_SOURCE_DIR}/tiff.h" TIFF_VERSION_LINES TIFF_VERSION_CLASSIC TIFF_VERSION_BIG TIFF_VERSION TIFF_BIGTIFF_VERSION)
48   endif()
49
50   if(TIFF_VERSION_CLASSIC AND NOT TIFF_VERSION)
51     set(TIFF_VERSION ${TIFF_VERSION_CLASSIC})
52   endif()
53
54   if(TIFF_BIGTIFF_VERSION AND NOT TIFF_VERSION_BIG)
55     set(TIFF_VERSION_BIG ${TIFF_BIGTIFF_VERSION})
56   endif()
57
58   if(NOT TIFF_VERSION_STRING AND TIFF_INCLUDE_DIR)
59     list(GET TIFF_INCLUDE_DIR 0 _TIFF_INCLUDE_DIR)
60     if(EXISTS "${_TIFF_INCLUDE_DIR}/tiffvers.h")
61       file(STRINGS "${_TIFF_INCLUDE_DIR}/tiffvers.h" tiff_version_str REGEX "^#define[\t ]+TIFFLIB_VERSION_STR[\t ]+\"LIBTIFF, Version .*")
62       string(REGEX REPLACE "^#define[\t ]+TIFFLIB_VERSION_STR[\t ]+\"LIBTIFF, Version +([^ \\n]*).*" "\\1" TIFF_VERSION_STRING "${tiff_version_str}")
63       unset(tiff_version_str)
64     endif()
65     unset(_TIFF_INCLUDE_DIR)
66   endif()
67
68   set(HAVE_TIFF YES)
69 endif()
70
71 # --- libjpeg (optional) ---
72 if(WITH_JPEG)
73   if(BUILD_JPEG)
74     ocv_clear_vars(JPEG_FOUND)
75   else()
76     include(FindJPEG)
77   endif()
78
79   if(NOT JPEG_FOUND)
80     ocv_clear_vars(JPEG_LIBRARY JPEG_LIBRARIES JPEG_INCLUDE_DIR)
81
82     set(JPEG_LIBRARY libjpeg)
83     set(JPEG_LIBRARIES ${JPEG_LIBRARY})
84     add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/libjpeg")
85     set(JPEG_INCLUDE_DIR "${${JPEG_LIBRARY}_SOURCE_DIR}")
86   endif()
87
88   ocv_parse_header("${JPEG_INCLUDE_DIR}/jpeglib.h" JPEG_VERSION_LINES JPEG_LIB_VERSION)
89   set(HAVE_JPEG YES)
90 endif()
91
92 # --- libwebp (optional) ---
93
94 if(WITH_WEBP)
95   if(BUILD_WEBP)
96     ocv_clear_vars(WEBP_FOUND WEBP_LIBRARY WEBP_LIBRARIES WEBP_INCLUDE_DIR)
97   else()
98     include(cmake/OpenCVFindWebP.cmake)
99   endif()
100 endif()
101
102 # --- Add libwebp to 3rdparty/libwebp and compile it if not available ---
103 if(WITH_WEBP AND NOT WEBP_FOUND)
104
105   set(WEBP_LIBRARY libwebp)
106   set(WEBP_LIBRARIES ${WEBP_LIBRARY})
107
108   add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/libwebp")
109   set(WEBP_INCLUDE_DIR "${${WEBP_LIBRARY}_SOURCE_DIR}")
110 endif()
111
112 if(NOT WEBP_VERSION AND WEBP_INCLUDE_DIR)
113   ocv_clear_vars(ENC_MAJ_VERSION ENC_MIN_VERSION ENC_REV_VERSION)
114   if(EXISTS "${WEBP_INCLUDE_DIR}/enc/vp8enci.h")
115     ocv_parse_header("${WEBP_INCLUDE_DIR}/enc/vp8enci.h" WEBP_VERSION_LINES ENC_MAJ_VERSION ENC_MIN_VERSION ENC_REV_VERSION)
116     set(WEBP_VERSION "${ENC_MAJ_VERSION}.${ENC_MIN_VERSION}.${ENC_REV_VERSION}")
117   elseif(EXISTS "${WEBP_INCLUDE_DIR}/webp/encode.h")
118     file(STRINGS "${WEBP_INCLUDE_DIR}/webp/encode.h" WEBP_ENCODER_ABI_VERSION REGEX "#define[ \t]+WEBP_ENCODER_ABI_VERSION[ \t]+([x0-9a-f]+)" )
119     if(WEBP_ENCODER_ABI_VERSION MATCHES "#define[ \t]+WEBP_ENCODER_ABI_VERSION[ \t]+([x0-9a-f]+)")
120         set(WEBP_ENCODER_ABI_VERSION "${CMAKE_MATCH_1}")
121         set(WEBP_VERSION "encoder: ${WEBP_ENCODER_ABI_VERSION}")
122     else()
123       unset(WEBP_ENCODER_ABI_VERSION)
124     endif()
125   endif()
126 endif()
127
128 # --- libjasper (optional, should be searched after libjpeg) ---
129 if(WITH_JASPER)
130   if(BUILD_JASPER)
131     ocv_clear_vars(JASPER_FOUND)
132   else()
133     include(FindJasper)
134   endif()
135
136   if(NOT JASPER_FOUND)
137     ocv_clear_vars(JASPER_LIBRARY JASPER_LIBRARIES JASPER_INCLUDE_DIR)
138
139     set(JASPER_LIBRARY libjasper)
140     set(JASPER_LIBRARIES ${JASPER_LIBRARY})
141     add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/libjasper")
142     set(JASPER_INCLUDE_DIR "${${JASPER_LIBRARY}_SOURCE_DIR}")
143   endif()
144
145   set(HAVE_JASPER YES)
146
147   if(NOT JASPER_VERSION_STRING)
148     ocv_parse_header2(JASPER "${JASPER_INCLUDE_DIR}/jasper/jas_config.h" JAS_VERSION "")
149   endif()
150 endif()
151
152 # --- libpng (optional, should be searched after zlib) ---
153 if(WITH_PNG)
154   if(BUILD_PNG)
155     ocv_clear_vars(PNG_FOUND)
156   else()
157     include(FindPNG)
158     if(PNG_FOUND)
159       include(CheckIncludeFile)
160       check_include_file("${PNG_PNG_INCLUDE_DIR}/libpng/png.h" HAVE_LIBPNG_PNG_H)
161       if(HAVE_LIBPNG_PNG_H)
162         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)
163       else()
164         ocv_parse_header("${PNG_PNG_INCLUDE_DIR}/png.h" PNG_VERSION_LINES PNG_LIBPNG_VER_MAJOR PNG_LIBPNG_VER_MINOR PNG_LIBPNG_VER_RELEASE)
165       endif()
166     endif()
167   endif()
168
169   if(NOT PNG_FOUND)
170     ocv_clear_vars(PNG_LIBRARY PNG_LIBRARIES PNG_INCLUDE_DIR PNG_PNG_INCLUDE_DIR HAVE_LIBPNG_PNG_H PNG_DEFINITIONS)
171
172     set(PNG_LIBRARY libpng)
173     set(PNG_LIBRARIES ${PNG_LIBRARY})
174     add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/libpng")
175     set(PNG_INCLUDE_DIR "${${PNG_LIBRARY}_SOURCE_DIR}")
176     set(PNG_DEFINITIONS "")
177     ocv_parse_header("${PNG_INCLUDE_DIR}/png.h" PNG_VERSION_LINES PNG_LIBPNG_VER_MAJOR PNG_LIBPNG_VER_MINOR PNG_LIBPNG_VER_RELEASE)
178   endif()
179
180   set(HAVE_PNG YES)
181   set(PNG_VERSION "${PNG_LIBPNG_VER_MAJOR}.${PNG_LIBPNG_VER_MINOR}.${PNG_LIBPNG_VER_RELEASE}")
182 endif()
183
184 # --- OpenEXR (optional) ---
185 if(WITH_OPENEXR)
186   if(BUILD_OPENEXR)
187     ocv_clear_vars(OPENEXR_FOUND)
188   else()
189     include("${OpenCV_SOURCE_DIR}/cmake/OpenCVFindOpenEXR.cmake")
190   endif()
191
192   if(NOT OPENEXR_FOUND)
193     ocv_clear_vars(OPENEXR_INCLUDE_PATHS OPENEXR_LIBRARIES OPENEXR_ILMIMF_LIBRARY OPENEXR_VERSION)
194
195     set(OPENEXR_LIBRARIES IlmImf)
196     set(OPENEXR_ILMIMF_LIBRARY IlmImf)
197     add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/openexr")
198   endif()
199
200   set(HAVE_OPENEXR YES)
201 endif()
202
203 # --- GDAL (optional) ---
204 if(WITH_GDAL)
205     find_package(GDAL QUIET)
206
207     if(NOT GDAL_FOUND)
208         ocv_clear_vars(GDAL_LIBRARY GDAL_INCLUDE_DIR)
209         set(HAVE_GDAL NO)
210     else()
211         set(HAVE_GDAL YES)
212         ocv_include_directories(${GDAL_INCLUDE_DIR})
213     endif()
214 endif()
215
216 if (WITH_GDCM)
217   find_package(GDCM QUIET)
218   if(NOT GDCM_FOUND)
219     set(HAVE_GDCM NO)
220     ocv_clear_vars(GDCM_VERSION GDCM_LIBRARIES)
221   else()
222     set(HAVE_GDCM YES)
223     # include(${GDCM_USE_FILE})
224     set(GDCM_LIBRARIES gdcmMSFF) # GDCM does not set this variable for some reason
225   endif()
226 endif()