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