From 381d9bafdf10158a417a2dab9154b6890e6a6a1d Mon Sep 17 00:00:00 2001 From: Alexander Alekhin Date: Thu, 23 Dec 2021 09:58:56 +0000 Subject: [PATCH] imgcodecs: disable OpenEXR in runtime for 3rdparty source code - builtin OpenEXR source code is outdated - external OpenEXR distributions are allowed --- cmake/OpenCVFindLibsGrfmt.cmake | 1 + modules/imgcodecs/CMakeLists.txt | 12 ++++++++++++ modules/imgcodecs/src/grfmt_exr.cpp | 26 ++++++++++++++++++++++++++ modules/imgcodecs/test/test_grfmt.cpp | 2 +- 4 files changed, 40 insertions(+), 1 deletion(-) diff --git a/cmake/OpenCVFindLibsGrfmt.cmake b/cmake/OpenCVFindLibsGrfmt.cmake index 2d28dff875..d3244cf7a5 100644 --- a/cmake/OpenCVFindLibsGrfmt.cmake +++ b/cmake/OpenCVFindLibsGrfmt.cmake @@ -240,6 +240,7 @@ if(WITH_OPENEXR) set(OPENEXR_LIBRARIES IlmImf) add_subdirectory("${OpenCV_SOURCE_DIR}/3rdparty/openexr") if(OPENEXR_VERSION) # check via TARGET doesn't work + set(BUILD_OPENEXR ON) set(HAVE_OPENEXR YES) endif() endif() diff --git a/modules/imgcodecs/CMakeLists.txt b/modules/imgcodecs/CMakeLists.txt index 10fb3278fb..9b59a3821b 100644 --- a/modules/imgcodecs/CMakeLists.txt +++ b/modules/imgcodecs/CMakeLists.txt @@ -45,12 +45,21 @@ if(HAVE_JASPER) list(APPEND GRFMT_LIBS ${JASPER_LIBRARIES}) if(OPENCV_IO_FORCE_JASPER) add_definitions(-DOPENCV_IMGCODECS_FORCE_JASPER=1) + else() + message(STATUS "imgcodecs: Jasper codec is disabled in runtime. Details: https://github.com/opencv/opencv/issues/14058") endif() endif() if(HAVE_OPENEXR) include_directories(SYSTEM ${OPENEXR_INCLUDE_PATHS}) list(APPEND GRFMT_LIBS ${OPENEXR_LIBRARIES}) + if(OPENCV_IO_FORCE_OPENEXR + OR NOT BUILD_OPENEXR # external OpenEXR versions are not disabled + ) + add_definitions(-DOPENCV_IMGCODECS_USE_OPENEXR=1) + else() + message(STATUS "imgcodecs: OpenEXR codec is disabled in runtime. Details: https://github.com/opencv/opencv/issues/21326") + endif() endif() if(HAVE_PNG OR HAVE_TIFF OR HAVE_OPENEXR) @@ -149,6 +158,9 @@ ocv_add_accuracy_tests() if(TARGET opencv_test_imgcodecs AND HAVE_JASPER AND "$ENV{OPENCV_IO_ENABLE_JASPER}") ocv_target_compile_definitions(opencv_test_imgcodecs PRIVATE OPENCV_IMGCODECS_ENABLE_JASPER_TESTS=1) endif() +if(TARGET opencv_test_imgcodecs AND HAVE_OPENEXR AND "$ENV{OPENCV_IO_ENABLE_OPENEXR}") + ocv_target_compile_definitions(opencv_test_imgcodecs PRIVATE OPENCV_IMGCODECS_ENABLE_OPENEXR_TESTS=1) +endif() if(TARGET opencv_test_imgcodecs AND HAVE_PNG AND NOT (PNG_VERSION VERSION_LESS "1.6.31")) # details: https://github.com/glennrp/libpng/commit/68cb0aaee3de6371b81a4613476d9b33e43e95b1 ocv_target_compile_definitions(opencv_test_imgcodecs PRIVATE OPENCV_IMGCODECS_PNG_WITH_EXIF=1) diff --git a/modules/imgcodecs/src/grfmt_exr.cpp b/modules/imgcodecs/src/grfmt_exr.cpp index 8cf4db99c5..92b132bdba 100644 --- a/modules/imgcodecs/src/grfmt_exr.cpp +++ b/modules/imgcodecs/src/grfmt_exr.cpp @@ -44,6 +44,9 @@ #ifdef HAVE_OPENEXR +#include +#include + #if defined _MSC_VER && _MSC_VER >= 1200 # pragma warning( disable: 4100 4244 4267 ) #endif @@ -78,6 +81,27 @@ namespace cv { +static bool isOpenEXREnabled() +{ + static const bool PARAM_ENABLE_OPENEXR = utils::getConfigurationParameterBool("OPENCV_IO_ENABLE_OPENEXR", +#ifdef OPENCV_IMGCODECS_USE_OPENEXR + true +#else + false +#endif + ); + return PARAM_ENABLE_OPENEXR; +} +static void initOpenEXR() +{ + if (!isOpenEXREnabled()) + { + const char* message = "imgcodecs: OpenEXR codec is disabled. You can enable it via 'OPENCV_IO_ENABLE_OPENEXR' option. Refer for details and cautions here: https://github.com/opencv/opencv/issues/21326"; + CV_LOG_WARNING(NULL, message); + CV_Error(Error::StsNotImplemented, message); + } +} + /////////////////////// ExrDecoder /////////////////// ExrDecoder::ExrDecoder() @@ -575,6 +599,7 @@ void ExrDecoder::RGBToGray( float *in, float *out ) ImageDecoder ExrDecoder::newDecoder() const { + initOpenEXR(); return makePtr(); } @@ -698,6 +723,7 @@ bool ExrEncoder::write( const Mat& img, const std::vector& params ) ImageEncoder ExrEncoder::newEncoder() const { + initOpenEXR(); return makePtr(); } diff --git a/modules/imgcodecs/test/test_grfmt.cpp b/modules/imgcodecs/test/test_grfmt.cpp index 6866c8d092..cbf6289d23 100644 --- a/modules/imgcodecs/test/test_grfmt.cpp +++ b/modules/imgcodecs/test/test_grfmt.cpp @@ -363,6 +363,6 @@ TEST(Imgcodecs, write_parameter_type) }} // namespace -#ifdef HAVE_OPENEXR +#if defined(HAVE_OPENEXR) && defined(OPENCV_IMGCODECS_ENABLE_OPENEXR_TESTS) #include "test_exr.impl.hpp" #endif