diff --git a/CMakeLists.txt b/CMakeLists.txt index 8411604d32..c8e4ef09c3 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -536,6 +536,7 @@ OCV_OPTION(ENABLE_CONFIG_VERIFICATION "Fail build if actual configuration doesn' OCV_OPTION(OPENCV_ENABLE_MEMALIGN "Enable posix_memalign or memalign usage" ON) OCV_OPTION(OPENCV_DISABLE_FILESYSTEM_SUPPORT "Disable filesystem support" OFF) OCV_OPTION(OPENCV_DISABLE_THREAD_SUPPORT "Build the library without multi-threaded code." OFF) +OCV_OPTION(OPENCV_DISABLE_ENV_SUPPORT "Disable environment variables access (getenv)" (CMAKE_SYSTEM_NAME MATCHES "Windows(CE|Phone|Store)")) OCV_OPTION(OPENCV_SEMIHOSTING "Build the library for semihosting target (Arm). See https://developer.arm.com/documentation/100863/latest." OFF) OCV_OPTION(ENABLE_CUDA_FIRST_CLASS_LANGUAGE "Enable CUDA as a first class language, if enabled dependant projects will need to use CMake >= 3.18" OFF VISIBLE_IF (WITH_CUDA AND NOT CMAKE_VERSION VERSION_LESS 3.18) diff --git a/cmake/platforms/OpenCV-WinRT.cmake b/cmake/platforms/OpenCV-WinRT.cmake index 0a5e9f3c7d..350c280fd2 100644 --- a/cmake/platforms/OpenCV-WinRT.cmake +++ b/cmake/platforms/OpenCV-WinRT.cmake @@ -1,6 +1,6 @@ set(WINRT TRUE) -add_definitions(-DWINRT -DNO_GETENV) +add_definitions(-DWINRT) # Making definitions available to other configurations and # to filter dependency restrictions at compile time. diff --git a/cmake/platforms/OpenCV-WindowsCE.cmake b/cmake/platforms/OpenCV-WindowsCE.cmake index f5ac590754..1bb8bf6d7f 100644 --- a/cmake/platforms/OpenCV-WindowsCE.cmake +++ b/cmake/platforms/OpenCV-WindowsCE.cmake @@ -1 +1 @@ -add_definitions(-DNO_GETENV) +# empty diff --git a/doc/tutorials/introduction/env_reference/env_reference.markdown b/doc/tutorials/introduction/env_reference/env_reference.markdown index afbde7b715..88b4008c8f 100644 --- a/doc/tutorials/introduction/env_reference/env_reference.markdown +++ b/doc/tutorials/introduction/env_reference/env_reference.markdown @@ -59,7 +59,6 @@ import cv2 # variables set after this may not have effect ## Types -- _non-null_ - set to anything to enable feature, in some cases can be interpreted as other types (e.g. path) - _bool_ - `1`, `True`, `true`, `TRUE` / `0`, `False`, `false`, `FALSE` - _number_/_size_ - unsigned number, suffixes `MB`, `Mb`, `mb`, `KB`, `Kb`, `kb` - _string_ - plain string or can have a structure @@ -70,7 +69,7 @@ import cv2 # variables set after this may not have effect ## General, core | name | type | default | description | |------|------|---------|-------------| -| OPENCV_SKIP_CPU_BASELINE_CHECK | non-null | | do not check that current CPU supports all features used by the build (baseline) | +| OPENCV_SKIP_CPU_BASELINE_CHECK | bool | false | do not check that current CPU supports all features used by the build (baseline) | | OPENCV_CPU_DISABLE | `,` or `;`-separated | | disable code branches which use CPU features (dispatched code) | | OPENCV_SETUP_TERMINATE_HANDLER | bool | true (Windows) | use std::set_terminate to install own termination handler | | OPENCV_LIBVA_RUNTIME | file path | | libva for VA interoperability utils | @@ -78,9 +77,9 @@ import cv2 # variables set after this may not have effect | OPENCV_BUFFER_AREA_ALWAYS_SAFE | bool | false | enable safe mode for multi-buffer allocations (each buffer separately) | | OPENCV_KMEANS_PARALLEL_GRANULARITY | num | 1000 | tune algorithm parallel work distribution parameter `parallel_for_(..., ..., ..., granularity)` | | OPENCV_DUMP_ERRORS | bool | true (Debug or Android), false (others) | print extra information on exception (log to Android) | -| OPENCV_DUMP_CONFIG | non-null | | print build configuration to stderr (`getBuildInformation`) | +| OPENCV_DUMP_CONFIG | bool | false | print build configuration to stderr (`getBuildInformation`) | | OPENCV_PYTHON_DEBUG | bool | false | enable extra warnings in Python bindings | -| OPENCV_TEMP_PATH | non-null / path | `/tmp/` (Linux), `/data/local/tmp/` (Android), `GetTempPathA` (Windows) | directory for temporary files | +| OPENCV_TEMP_PATH | path | `/tmp/` (Linux), `/data/local/tmp/` (Android), `GetTempPathA` (Windows) | directory for temporary files | | OPENCV_DATA_PATH_HINT | paths | | paths for findDataFile | | OPENCV_DATA_PATH | paths | | paths for findDataFile | | OPENCV_SAMPLES_DATA_PATH_HINT | paths | | paths for findDataFile | @@ -272,7 +271,7 @@ Some external dependencies can be detached into a dynamic library, which will be | ⭐ OPENCV_FFMPEG_CAPTURE_OPTIONS | string (see note) | | extra options for VideoCapture FFmpeg backend | | ⭐ OPENCV_FFMPEG_WRITER_OPTIONS | string (see note) | | extra options for VideoWriter FFmpeg backend | | OPENCV_FFMPEG_THREADS | num | | set FFmpeg thread count | -| OPENCV_FFMPEG_DEBUG | non-null | | enable logging messages from FFmpeg | +| OPENCV_FFMPEG_DEBUG | bool | false | enable logging messages from FFmpeg | | OPENCV_FFMPEG_LOGLEVEL | num | | set FFmpeg logging level | | OPENCV_FFMPEG_DLL_DIR | dir path | | directory with FFmpeg plugin (legacy) | | OPENCV_FFMPEG_IS_THREAD_SAFE | bool | false | enabling this option will turn off thread safety locks in the FFmpeg backend (use only if you are sure FFmpeg is built with threading support, tested on Linux) | @@ -286,7 +285,7 @@ Some external dependencies can be detached into a dynamic library, which will be | OPENCV_VIDEOIO_MFX_BITRATE_DIVISOR | num | 300 | this option allows to tune encoding bitrate (video quality/size) | | OPENCV_VIDEOIO_MFX_WRITER_TIMEOUT | num | 1 | timeout for encoding operation (in seconds) | | OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS | bool | true | allow HW-accelerated transformations (DXVA) in MediaFoundation processing graph (may slow down camera probing process) | -| OPENCV_DSHOW_DEBUG | non-null | | enable verbose logging in the DShow backend | +| OPENCV_DSHOW_DEBUG | bool | false | enable verbose logging in the DShow backend | | OPENCV_DSHOW_SAVEGRAPH_FILENAME | file path | | enable processing graph tump in the DShow backend | | OPENCV_VIDEOIO_V4L_RANGE_NORMALIZED | bool | false | use (0, 1) range for properties (V4L) | | OPENCV_VIDEOIO_V4L_SELECT_TIMEOUT | num | 10 | timeout for select call (in seconds) (V4L) | @@ -297,7 +296,7 @@ Some external dependencies can be detached into a dynamic library, which will be ### videoio tests | name | type | default | description | |------|------|---------|-------------| -| OPENCV_TEST_VIDEOIO_BACKEND_REQUIRE_FFMPEG | | | test app will exit if no FFmpeg backend is available | +| OPENCV_TEST_VIDEOIO_BACKEND_REQUIRE_FFMPEG | bool | false | test app will exit if no FFmpeg backend is available | | OPENCV_TEST_V4L2_VIVID_DEVICE | file path | | path to VIVID virtual camera device for V4L2 test (e.g. `/dev/video5`) | | OPENCV_TEST_PERF_CAMERA_LIST | paths | | cameras to use in performance test (waitAny_V4L test) | | OPENCV_TEST_CAMERA_%d_FPS | num | | fps to set for N-th camera (0-based index) (waitAny_V4L test) | @@ -327,7 +326,7 @@ Some external dependencies can be detached into a dynamic library, which will be | name | type | default | description | |------|------|---------|-------------| -| OPENCV_LEGACY_WAITKEY | non-null | | switch `waitKey` return result (default behavior: `return code & 0xff` (or -1), legacy behavior: `return code`) | +| OPENCV_LEGACY_WAITKEY | bool | false | switch `waitKey` return result (default behavior: `return code & 0xff` (or -1), legacy behavior: `return code`) | | $XDG_RUNTIME_DIR | | | Wayland backend specific - create shared memory-mapped file for interprocess communication (named `opencv-shared-??????`) | | OPENCV_HIGHGUI_FB_MODE | string | `FB` | Selects output mode for the framebuffer backend (`FB` - regular frambuffer, `EMU` - emulation, perform internal checks but does nothing, `XVFB` - compatible with _xvfb_ virtual frambuffer) | | OPENCV_HIGHGUI_FB_DEVICE | file path | | Path to frambuffer device to use (will be checked first) | diff --git a/modules/core/CMakeLists.txt b/modules/core/CMakeLists.txt index ea1100c954..95fe8d6a4c 100644 --- a/modules/core/CMakeLists.txt +++ b/modules/core/CMakeLists.txt @@ -182,6 +182,10 @@ if(OPENCV_DISABLE_THREAD_SUPPORT) ocv_target_compile_definitions(${the_module} PUBLIC "OPENCV_DISABLE_THREAD_SUPPORT=1") endif() +if(OPENCV_DISABLE_ENV_SUPPORT) + ocv_append_source_file_compile_definitions(${CMAKE_CURRENT_SOURCE_DIR}/src/system.cpp "NO_GETENV") +endif() + if(OPENCV_SEMIHOSTING) ocv_target_compile_definitions(${the_module} PRIVATE "-DOPENCV_SEMIHOSTING") endif(OPENCV_SEMIHOSTING) diff --git a/modules/core/include/opencv2/core/utils/configuration.private.hpp b/modules/core/include/opencv2/core/utils/configuration.private.hpp index b35f35e9d4..756b477165 100644 --- a/modules/core/include/opencv2/core/utils/configuration.private.hpp +++ b/modules/core/include/opencv2/core/utils/configuration.private.hpp @@ -12,9 +12,9 @@ namespace cv { namespace utils { typedef std::vector Paths; -CV_EXPORTS bool getConfigurationParameterBool(const char* name, bool defaultValue); -CV_EXPORTS size_t getConfigurationParameterSizeT(const char* name, size_t defaultValue); -CV_EXPORTS cv::String getConfigurationParameterString(const char* name, const char* defaultValue); +CV_EXPORTS bool getConfigurationParameterBool(const char* name, bool defaultValue = false); +CV_EXPORTS size_t getConfigurationParameterSizeT(const char* name, size_t defaultValue = 0); +CV_EXPORTS std::string getConfigurationParameterString(const char* name, const std::string & defaultValue = std::string()); CV_EXPORTS Paths getConfigurationParameterPaths(const char* name, const Paths &defaultValue = Paths()); }} // namespace diff --git a/modules/core/src/ocl.cpp b/modules/core/src/ocl.cpp index 8d7d7faf44..8479667fd7 100644 --- a/modules/core/src/ocl.cpp +++ b/modules/core/src/ocl.cpp @@ -1170,10 +1170,10 @@ bool haveOpenCL() if (!g_isOpenCLInitialized) { CV_TRACE_REGION("Init_OpenCL_Runtime"); - const char* envPath = getenv("OPENCV_OPENCL_RUNTIME"); - if (envPath) + std::string envPath = utils::getConfigurationParameterString("OPENCV_OPENCL_RUNTIME"); + if (!envPath.empty()) { - if (cv::String(envPath) == "disabled") + if (envPath == "disabled") { g_isOpenCLAvailable = false; g_isOpenCLInitialized = true; @@ -2119,24 +2119,18 @@ static bool parseOpenCLDeviceConfiguration(const std::string& configurationStr, return true; } -#if defined WINRT || defined _WIN32_WCE -static cl_device_id selectOpenCLDevice(const char* configuration = NULL) -{ - CV_UNUSED(configuration) - return NULL; -} -#else -static cl_device_id selectOpenCLDevice(const char* configuration = NULL) +static cl_device_id selectOpenCLDevice(const std::string & configuration_ = std::string()) { std::string platform, deviceName; std::vector deviceTypes; - if (!configuration) - configuration = getenv("OPENCV_OPENCL_DEVICE"); + std::string configuration(configuration_); + if (configuration.empty()) + configuration = utils::getConfigurationParameterString("OPENCV_OPENCL_DEVICE"); - if (configuration && - (strcmp(configuration, "disabled") == 0 || - !parseOpenCLDeviceConfiguration(std::string(configuration), platform, deviceTypes, deviceName) + if (!configuration.empty() && + (configuration == "disabled" || + !parseOpenCLDeviceConfiguration(configuration, platform, deviceTypes, deviceName) )) return NULL; @@ -2204,7 +2198,7 @@ static cl_device_id selectOpenCLDevice(const char* configuration = NULL) if (!isID) { deviceTypes.push_back("GPU"); - if (configuration) + if (!configuration.empty()) deviceTypes.push_back("CPU"); } else @@ -2272,7 +2266,7 @@ static cl_device_id selectOpenCLDevice(const char* configuration = NULL) } not_found: - if (!configuration) + if (configuration.empty()) return NULL; // suppress messages on stderr std::ostringstream msg; @@ -2287,7 +2281,6 @@ not_found: CV_LOG_ERROR(NULL, msg.str()); return NULL; } -#endif #ifdef HAVE_OPENCL_SVM namespace svm { @@ -2340,12 +2333,12 @@ static unsigned int getSVMCapabilitiesMask() static unsigned int mask = 0; if (!initialized) { - const char* envValue = getenv("OPENCV_OPENCL_SVM_CAPABILITIES_MASK"); - if (envValue == NULL) + const std::string envValue = utils::getConfigurationParameterString("OPENCV_OPENCL_SVM_CAPABILITIES_MASK"); + if (envValue.empty()) { return ~0U; // all bits 1 } - mask = atoi(envValue); + mask = atoi(envValue.c_str()); initialized = true; } return mask; @@ -2482,8 +2475,8 @@ public: std::string configuration = configuration_; if (configuration_.empty()) { - const char* c = getenv("OPENCV_OPENCL_DEVICE"); - if (c) + const std::string c = utils::getConfigurationParameterString("OPENCV_OPENCL_DEVICE"); + if (!c.empty()) configuration = c; } Impl* impl = findContext(configuration); @@ -2494,7 +2487,7 @@ public: return impl; } - cl_device_id d = selectOpenCLDevice(configuration.empty() ? NULL : configuration.c_str()); + cl_device_id d = selectOpenCLDevice(configuration); if (d == NULL) return NULL; diff --git a/modules/core/src/opencl/runtime/opencl_core.cpp b/modules/core/src/opencl/runtime/opencl_core.cpp index f264d623c5..3a454faa29 100644 --- a/modules/core/src/opencl/runtime/opencl_core.cpp +++ b/modules/core/src/opencl/runtime/opencl_core.cpp @@ -44,6 +44,7 @@ #if defined(HAVE_OPENCL) #include "opencv2/core.hpp" // CV_Error +#include "opencv2/core/utils/configuration.private.hpp" #if defined(HAVE_OPENCL_STATIC) #if defined __APPLE__ @@ -64,18 +65,14 @@ CV_SUPPRESS_DEPRECATED_END #define ERROR_MSG_CANT_LOAD "Failed to load OpenCL runtime\n" #define ERROR_MSG_INVALID_VERSION "Failed to load OpenCL runtime (expected version 1.1+)\n" -static const char* getRuntimePath(const char* defaultPath) +static std::string getRuntimePath(const std::string & defaultPath) { - const char* envPath = getenv("OPENCV_OPENCL_RUNTIME"); - if (envPath) - { - static const char disabled_str[] = "disabled"; - if ((strlen(envPath) == sizeof(disabled_str) - 1) && - (memcmp(envPath, disabled_str, sizeof(disabled_str) - 1) == 0)) - return NULL; - return envPath; - } - return defaultPath; + const std::string res = cv::utils::getConfigurationParameterString( + "OPENCV_OPENCL_RUNTIME", defaultPath); + if (res == "disabled") + return std::string(); + else + return res; } #if defined(__APPLE__) @@ -91,9 +88,9 @@ static void* AppleCLGetProcAddress(const char* name) if (!initialized) { const char* defaultPath = "/System/Library/Frameworks/OpenCL.framework/Versions/Current/OpenCL"; - const char* path = getRuntimePath(defaultPath); - if (path) - handle = dlopen(path, RTLD_LAZY | RTLD_GLOBAL); + std::string path = getRuntimePath(defaultPath); + if (!path.empty()) + handle = dlopen(path.c_str(), RTLD_LAZY | RTLD_GLOBAL); if (handle == NULL) { if (path != NULL && path != defaultPath) @@ -129,13 +126,13 @@ static void* WinGetProcAddress(const char* name) handle = GetModuleHandleA("OpenCL.dll"); if (!handle) { - const char* defaultPath = "OpenCL.dll"; - const char* path = getRuntimePath(defaultPath); - if (path) - handle = LoadLibraryA(path); + const std::string defaultPath = "OpenCL.dll"; + const std::string path = getRuntimePath(defaultPath); + if (!path.empty()) + handle = LoadLibraryA(path.c_str()); if (!handle) { - if (path != NULL && path != defaultPath) + if (!path.empty() && path != defaultPath) fprintf(stderr, ERROR_MSG_CANT_LOAD); } else if (GetProcAddress(handle, OPENCL_FUNC_TO_CHECK_1_1) == NULL) @@ -205,8 +202,8 @@ static void* GetProcAddress(const char* name) bool foundOpenCL = false; for (unsigned int i = 0; i < (sizeof(defaultAndroidPaths)/sizeof(char*)); i++) { - const char* path = (i==0) ? getRuntimePath(defaultAndroidPaths[i]) : defaultAndroidPaths[i]; - if (path) { + const std::string path = (i==0) ? getRuntimePath(defaultAndroidPaths[i]) : defaultAndroidPaths[i]; + if (!path.empty()) { handle = GetHandle(path); if (handle) { foundOpenCL = true; @@ -236,10 +233,10 @@ static void* GetProcAddress(const char* name) if (!initialized) { const char* defaultPath = "libOpenCL.so"; - const char* path = getRuntimePath(defaultPath); - if (path) + const std::string path = getRuntimePath(defaultPath); + if (!path.empty()) { - handle = GetHandle(path); + handle = GetHandle(path.c_str()); if (!handle) { if (path == defaultPath) diff --git a/modules/core/src/parallel/registry_parallel.impl.hpp b/modules/core/src/parallel/registry_parallel.impl.hpp index 2208748bb1..471cd681e4 100644 --- a/modules/core/src/parallel/registry_parallel.impl.hpp +++ b/modules/core/src/parallel/registry_parallel.impl.hpp @@ -111,7 +111,7 @@ protected: bool readPrioritySettings() { bool hasChanges = false; - cv::String prioritized_backends = utils::getConfigurationParameterString("OPENCV_PARALLEL_PRIORITY_LIST", NULL); + cv::String prioritized_backends = utils::getConfigurationParameterString("OPENCV_PARALLEL_PRIORITY_LIST"); if (prioritized_backends.empty()) return hasChanges; CV_LOG_INFO(NULL, "core(parallel): Configured priority list (OPENCV_PARALLEL_PRIORITY_LIST): " << prioritized_backends); diff --git a/modules/core/src/system.cpp b/modules/core/src/system.cpp index 7e819acac7..810f62297a 100644 --- a/modules/core/src/system.cpp +++ b/modules/core/src/system.cpp @@ -450,13 +450,11 @@ struct HWFeatures void initialize(void) { -#ifndef NO_GETENV - if (getenv("OPENCV_DUMP_CONFIG")) + if (utils::getConfigurationParameterBool("OPENCV_DUMP_CONFIG")) { fprintf(stderr, "\nOpenCV build configuration is:\n%s\n", cv::getBuildInformation().c_str()); } -#endif initializeNames(); @@ -731,12 +729,10 @@ struct HWFeatures #endif bool skip_baseline_check = false; -#ifndef NO_GETENV - if (getenv("OPENCV_SKIP_CPU_BASELINE_CHECK")) + if (utils::getConfigurationParameterBool("OPENCV_SKIP_CPU_BASELINE_CHECK")) { skip_baseline_check = true; } -#endif int baseline_features[] = { CV_CPU_BASELINE_FEATURES }; if (!checkFeatures(baseline_features, sizeof(baseline_features) / sizeof(baseline_features[0])) && !skip_baseline_check) @@ -786,15 +782,10 @@ struct HWFeatures void readSettings(const int* baseline_features, int baseline_count) { bool dump = true; - const char* disabled_features = -#ifdef NO_GETENV - NULL; -#else - getenv("OPENCV_CPU_DISABLE"); -#endif - if (disabled_features && disabled_features[0] != 0) + std::string disabled_features = utils::getConfigurationParameterString("OPENCV_CPU_DISABLE"); + if (!disabled_features.empty()) { - const char* start = disabled_features; + const char* start = disabled_features.c_str(); for (;;) { while (start[0] != 0 && isSymbolSeparator(start[0])) @@ -1080,20 +1071,19 @@ String tempfile( const char* suffix ) { #if OPENCV_HAVE_FILESYSTEM_SUPPORT String fname; -#ifndef NO_GETENV - const char *temp_dir = getenv("OPENCV_TEMP_PATH"); -#endif + + std::string temp_dir = utils::getConfigurationParameterString("OPENCV_TEMP_PATH"); #if defined _WIN32 #ifdef WINRT RoInitialize(RO_INIT_MULTITHREADED); - std::wstring temp_dir = GetTempPathWinRT(); + std::wstring temp_dir_rt = GetTempPathWinRT(); std::wstring temp_file = GetTempFileNameWinRT(L"ocv"); if (temp_file.empty()) return String(); - temp_file = temp_dir.append(std::wstring(L"\\")).append(temp_file); + temp_file = temp_dir_rt.append(std::wstring(L"\\")).append(temp_file); DeleteFileW(temp_file.c_str()); char aname[MAX_PATH]; @@ -1103,12 +1093,12 @@ String tempfile( const char* suffix ) RoUninitialize(); #elif defined(_WIN32_WCE) const auto kMaxPathSize = MAX_PATH+1; - wchar_t temp_dir[kMaxPathSize] = {0}; + wchar_t temp_dir_ce[kMaxPathSize] = {0}; wchar_t temp_file[kMaxPathSize] = {0}; - ::GetTempPathW(kMaxPathSize, temp_dir); + ::GetTempPathW(kMaxPathSize, temp_dir_ce); - if(0 != ::GetTempFileNameW(temp_dir, L"ocv", 0, temp_file)) { + if(0 != ::GetTempFileNameW(temp_dir_ce, L"ocv", 0, temp_file)) { DeleteFileW(temp_file); char aname[MAX_PATH]; size_t copied = wcstombs(aname, temp_file, MAX_PATH); @@ -1119,12 +1109,12 @@ String tempfile( const char* suffix ) char temp_dir2[MAX_PATH] = { 0 }; char temp_file[MAX_PATH] = { 0 }; - if (temp_dir == 0 || temp_dir[0] == 0) + if (temp_dir.empty()) { ::GetTempPathA(sizeof(temp_dir2), temp_dir2); - temp_dir = temp_dir2; + temp_dir = std::string(temp_dir2); } - if(0 == ::GetTempFileNameA(temp_dir, "ocv", 0, temp_file)) + if(0 == ::GetTempFileNameA(temp_dir.c_str(), "ocv", 0, temp_file)) return String(); DeleteFileA(temp_file); @@ -1139,7 +1129,7 @@ String tempfile( const char* suffix ) char defaultTemplate[] = "/tmp/__opencv_temp.XXXXXX"; # endif - if (temp_dir == 0 || temp_dir[0] == 0) + if (temp_dir.empty()) fname = defaultTemplate; else { @@ -2289,9 +2279,9 @@ size_t utils::getConfigurationParameterSizeT(const char* name, size_t defaultVal return read(name, defaultValue); } -cv::String utils::getConfigurationParameterString(const char* name, const char* defaultValue) +std::string utils::getConfigurationParameterString(const char* name, const std::string & defaultValue) { - return read(name, defaultValue ? cv::String(defaultValue) : cv::String()); + return read(name, defaultValue); } utils::Paths utils::getConfigurationParameterPaths(const char* name, const utils::Paths &defaultValue) @@ -2588,11 +2578,8 @@ public: } ippFeatures = cpuFeatures; - const char* pIppEnv = getenv("OPENCV_IPP"); - cv::String env; - if(pIppEnv != NULL) - env = pIppEnv; - if(env.size()) + std::string env = utils::getConfigurationParameterString("OPENCV_IPP"); + if(!env.empty()) { #if IPP_VERSION_X100 >= 201900 const Ipp64u minorFeatures = ippCPUID_MOVBE|ippCPUID_AES|ippCPUID_CLMUL|ippCPUID_ABR|ippCPUID_RDRAND|ippCPUID_F16C| diff --git a/modules/core/src/utils/datafile.cpp b/modules/core/src/utils/datafile.cpp index 67cc2571ac..380ba59258 100644 --- a/modules/core/src/utils/datafile.cpp +++ b/modules/core/src/utils/datafile.cpp @@ -60,7 +60,7 @@ static std::vector& _getDataSearchSubDirectory() CV_EXPORTS void addDataSearchPath(const cv::String& path) { - if (utils::fs::isDirectory(path)) + if (!path.empty() && utils::fs::isDirectory(path)) _getDataSearchPath().push_back(path); } CV_EXPORTS void addDataSearchSubDirectory(const cv::String& subdir) diff --git a/modules/core/src/utils/filesystem.cpp b/modules/core/src/utils/filesystem.cpp index d416284062..0a44d48d40 100644 --- a/modules/core/src/utils/filesystem.cpp +++ b/modules/core/src/utils/filesystem.cpp @@ -447,8 +447,8 @@ cv::String getCacheDirectory(const char* sub_directory_name, const char* configu #elif defined __ANDROID__ // no defaults #elif defined __APPLE__ - const char* tmpdir_env = getenv("TMPDIR"); - if (tmpdir_env && utils::fs::isDirectory(tmpdir_env)) + const std::string tmpdir_env = utils::getConfigurationParameterString("TMPDIR"); + if (!tmpdir_env.empty() && utils::fs::isDirectory(tmpdir_env)) { default_cache_path = tmpdir_env; } @@ -461,16 +461,16 @@ cv::String getCacheDirectory(const char* sub_directory_name, const char* configu // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html if (default_cache_path.empty()) { - const char* xdg_cache_env = getenv("XDG_CACHE_HOME"); - if (xdg_cache_env && xdg_cache_env[0] && utils::fs::isDirectory(xdg_cache_env)) + const std::string xdg_cache_env = utils::getConfigurationParameterString("XDG_CACHE_HOME"); + if (!xdg_cache_env.empty() && utils::fs::isDirectory(xdg_cache_env)) { default_cache_path = xdg_cache_env; } } if (default_cache_path.empty()) { - const char* home_env = getenv("HOME"); - if (home_env && home_env[0] && utils::fs::isDirectory(home_env)) + const std::string home_env = utils::getConfigurationParameterString("HOME"); + if (!home_env.empty() && utils::fs::isDirectory(home_env)) { cv::String home_path = home_env; cv::String home_cache_path = utils::fs::join(home_path, ".cache/"); diff --git a/modules/core/src/va_wrapper.impl.hpp b/modules/core/src/va_wrapper.impl.hpp index 77faa984d0..23b1b69dcb 100644 --- a/modules/core/src/va_wrapper.impl.hpp +++ b/modules/core/src/va_wrapper.impl.hpp @@ -7,6 +7,7 @@ // #include "opencv2/core/utils/plugin_loader.private.hpp" // DynamicLib +#include "opencv2/core/utils/configuration.private.hpp" namespace cv { namespace detail { @@ -47,8 +48,8 @@ static FN_vaGetImage fn_vaGetImage = NULL; static std::shared_ptr loadLibVA() { std::shared_ptr lib; - const char* envPath = getenv("OPENCV_LIBVA_RUNTIME"); - if (envPath) + const std::string envPath = utils::getConfigurationParameterString("OPENCV_LIBVA_RUNTIME"); + if (!envPath.empty()) { lib = std::make_shared(envPath); return lib; diff --git a/modules/dnn/perf/perf_main.cpp b/modules/dnn/perf/perf_main.cpp index 073921efaf..bdb360fcb2 100644 --- a/modules/dnn/perf/perf_main.cpp +++ b/modules/dnn/perf/perf_main.cpp @@ -1,16 +1,7 @@ #include "perf_precomp.hpp" -static const char* extraTestDataPath = -#ifdef WINRT - NULL; -#else - getenv("OPENCV_DNN_TEST_DATA_PATH"); -#endif - #if defined(HAVE_HPX) #include #endif -CV_PERF_TEST_MAIN(dnn, - extraTestDataPath ? (void)cvtest::addDataSearchPath(extraTestDataPath) : (void)0 -) +CV_PERF_TEST_MAIN(dnn, cvtest::addDataSearchEnv("OPENCV_DNN_TEST_DATA_PATH")) diff --git a/modules/dnn/src/precomp.hpp b/modules/dnn/src/precomp.hpp index 178a8f42cf..cf17e0109d 100644 --- a/modules/dnn/src/precomp.hpp +++ b/modules/dnn/src/precomp.hpp @@ -47,6 +47,7 @@ #endif #include +#include "opencv2/core/utils/configuration.private.hpp" #ifndef CV_OCL4DNN #define CV_OCL4DNN 0 @@ -89,4 +90,4 @@ #include #include -#include "dnn_common.hpp" \ No newline at end of file +#include "dnn_common.hpp" diff --git a/modules/dnn/src/vkcom/vulkan/vk_loader.cpp b/modules/dnn/src/vkcom/vulkan/vk_loader.cpp index 4640d0a0cb..64cb7741b9 100644 --- a/modules/dnn/src/vkcom/vulkan/vk_loader.cpp +++ b/modules/dnn/src/vkcom/vulkan/vk_loader.cpp @@ -115,21 +115,12 @@ bool loadVulkanLibrary() if (handle != nullptr) return true; - const char* path; - const char* envPath = getenv("OPENCV_VULKAN_RUNTIME"); - if (envPath) - { - path = envPath; - } - else - { - path = DEFAULT_VK_LIBRARY_PATH; - } + const std::string path = cv::utils::getConfigurationParameterString("OPENCV_VULKAN_RUNTIME", DEFAULT_VK_LIBRARY_PATH); - handle = LOAD_VK_LIBRARY(path); + handle = LOAD_VK_LIBRARY(path.c_str()); if( handle == nullptr ) { - fprintf(stderr, "Could not load Vulkan library: %s!\n", path); + fprintf(stderr, "Could not load Vulkan library: %s!\n", path.c_str()); fprintf(stderr, "Please download the Vulkan SDK and set the environment variable of OPENCV_VULKAN_RUNTIME according " "to your system environment.\n"); fprintf(stderr, "For M1 Mac and IOS, we use MoltenVK to map the Vulkan code to native apple Metal code.\n"); diff --git a/modules/dnn/test/test_common.impl.hpp b/modules/dnn/test/test_common.impl.hpp index e78dc6c1e4..a12f2fe5b4 100644 --- a/modules/dnn/test/test_common.impl.hpp +++ b/modules/dnn/test/test_common.impl.hpp @@ -437,14 +437,7 @@ bool validateVPUType() void initDNNTests() { - const char* extraTestDataPath = -#ifdef WINRT - NULL; -#else - getenv("OPENCV_DNN_TEST_DATA_PATH"); -#endif - if (extraTestDataPath) - cvtest::addDataSearchPath(extraTestDataPath); + cvtest::addDataSearchEnv("OPENCV_DNN_TEST_DATA_PATH"); registerGlobalSkipTag( CV_TEST_TAG_DNN_SKIP_OPENCV_BACKEND, diff --git a/modules/dnn/test/test_ie_models.cpp b/modules/dnn/test/test_ie_models.cpp index eff389035d..647ee907bc 100644 --- a/modules/dnn/test/test_ie_models.cpp +++ b/modules/dnn/test/test_ie_models.cpp @@ -40,15 +40,11 @@ static void initDLDTDataPath() if (!initialized) { #if INF_ENGINE_RELEASE <= 2018050000 - const char* dldtTestDataPath = getenv("INTEL_CVSDK_DIR"); - if (dldtTestDataPath) - cvtest::addDataSearchPath(dldtTestDataPath); + cvtest::addDataSearchEnv("INTEL_CVSDK_DIR"); #else - const char* omzDataPath = getenv("OPENCV_OPEN_MODEL_ZOO_DATA_PATH"); - if (omzDataPath) - cvtest::addDataSearchPath(omzDataPath); - const char* dnnDataPath = getenv("OPENCV_DNN_TEST_DATA_PATH"); - if (dnnDataPath) + cvtest::addDataSearchEnv("OPENCV_OPEN_MODEL_ZOO_DATA_PATH"); + const std::string dnnDataPath = cv::utils::getConfigurationParameterString("OPENCV_DNN_TEST_DATA_PATH"); + if (!dnnDataPath.empty()) cvtest::addDataSearchPath(std::string(dnnDataPath) + "/omz_intel_models"); #endif initialized = true; diff --git a/modules/dnn/test/test_precomp.hpp b/modules/dnn/test/test_precomp.hpp index cc1ea639f7..931ae7698d 100644 --- a/modules/dnn/test/test_precomp.hpp +++ b/modules/dnn/test/test_precomp.hpp @@ -45,6 +45,7 @@ #include "opencv2/ts/ts_perf.hpp" #include "opencv2/core/utility.hpp" #include "opencv2/core/ocl.hpp" +#include "opencv2/core/utils/configuration.private.hpp" #include "opencv2/dnn.hpp" #include "test_common.hpp" diff --git a/modules/gapi/src/compiler/gcompiler.cpp b/modules/gapi/src/compiler/gcompiler.cpp index 568251f19e..666271d7ba 100644 --- a/modules/gapi/src/compiler/gcompiler.cpp +++ b/modules/gapi/src/compiler/gcompiler.cpp @@ -99,9 +99,9 @@ namespace auto dump_info = cv::gapi::getCompileArg(args); if (!dump_info.has_value()) { - const char* path = std::getenv("GRAPH_DUMP_PATH"); - return path - ? cv::util::make_optional(std::string(path)) + const std::string path = cv::utils::getConfigurationParameterString("GRAPH_DUMP_PATH"); + return !path.empty() + ? cv::util::make_optional(path) : cv::util::optional(); } else diff --git a/modules/gapi/src/precomp.hpp b/modules/gapi/src/precomp.hpp index f72d0a1ecc..6aeaa0e58b 100644 --- a/modules/gapi/src/precomp.hpp +++ b/modules/gapi/src/precomp.hpp @@ -10,6 +10,7 @@ #if !defined(GAPI_STANDALONE) # include +# include # include # include # include diff --git a/modules/gapi/test/infer/gapi_infer_ie_test.cpp b/modules/gapi/test/infer/gapi_infer_ie_test.cpp index a7c9637a4f..ff27489434 100644 --- a/modules/gapi/test/infer/gapi_infer_ie_test.cpp +++ b/modules/gapi/test/infer/gapi_infer_ie_test.cpp @@ -101,22 +101,18 @@ public: // FIXME: taken from DNN module static void initDLDTDataPath() { -#ifndef WINRT static bool initialized = false; if (!initialized) { - const char* omzDataPath = getenv("OPENCV_OPEN_MODEL_ZOO_DATA_PATH"); - if (omzDataPath) - cvtest::addDataSearchPath(omzDataPath); - const char* dnnDataPath = getenv("OPENCV_DNN_TEST_DATA_PATH"); - if (dnnDataPath) { + cvtest::addDataSearchEnv("OPENCV_OPEN_MODEL_ZOO_DATA_PATH"); + const std::string dnnDataPath = cv::utils::getConfigurationParameterString("OPENCV_DNN_TEST_DATA_PATH"); + if (!dnnDataPath.empty()) { // Add the dnnDataPath itself - G-API is using some images there directly cvtest::addDataSearchPath(dnnDataPath); cvtest::addDataSearchPath(dnnDataPath + std::string("/omz_intel_models")); } initialized = true; } -#endif // WINRT } #if INF_ENGINE_RELEASE >= 2020010000 diff --git a/modules/gapi/test/infer/gapi_infer_onnx_test.cpp b/modules/gapi/test/infer/gapi_infer_onnx_test.cpp index dff91c597e..6ca394aef2 100644 --- a/modules/gapi/test/infer/gapi_infer_onnx_test.cpp +++ b/modules/gapi/test/infer/gapi_infer_onnx_test.cpp @@ -58,10 +58,7 @@ public: }; struct ONNXInitPath { ONNXInitPath() { - const char* env_path = getenv("OPENCV_GAPI_ONNX_MODEL_PATH"); - if (env_path) { - cvtest::addDataSearchPath(env_path); - } + cvtest::addDataSearchEnv("OPENCV_GAPI_ONNX_MODEL_PATH"); } }; static ONNXInitPath g_init_path; diff --git a/modules/gapi/test/infer/gapi_infer_ov_tests.cpp b/modules/gapi/test/infer/gapi_infer_ov_tests.cpp index 49652db387..85df0b24da 100644 --- a/modules/gapi/test/infer/gapi_infer_ov_tests.cpp +++ b/modules/gapi/test/infer/gapi_infer_ov_tests.cpp @@ -25,11 +25,9 @@ void initDLDTDataPath() static bool initialized = false; if (!initialized) { - const char* omzDataPath = getenv("OPENCV_OPEN_MODEL_ZOO_DATA_PATH"); - if (omzDataPath) - cvtest::addDataSearchPath(omzDataPath); - const char* dnnDataPath = getenv("OPENCV_DNN_TEST_DATA_PATH"); - if (dnnDataPath) { + cvtest::addDataSearchEnv("OPENCV_OPEN_MODEL_ZOO_DATA_PATH"); + const std::string dnnDataPath = cv::utils::getConfigurationParameterString("OPENCV_DNN_TEST_DATA_PATH"); + if (!dnnDataPath.empty()) { // Add the dnnDataPath itself - G-API is using some images there directly cvtest::addDataSearchPath(dnnDataPath); cvtest::addDataSearchPath(dnnDataPath + std::string("/omz_intel_models")); diff --git a/modules/gapi/test/test_precomp.hpp b/modules/gapi/test/test_precomp.hpp index e92b1d03bf..708964dfe6 100644 --- a/modules/gapi/test/test_precomp.hpp +++ b/modules/gapi/test/test_precomp.hpp @@ -16,6 +16,8 @@ #include +#include + #include #include #include diff --git a/modules/highgui/src/precomp.hpp b/modules/highgui/src/precomp.hpp index 2bbfd6c14a..1301a4456a 100644 --- a/modules/highgui/src/precomp.hpp +++ b/modules/highgui/src/precomp.hpp @@ -54,6 +54,7 @@ #include "opencv2/core/utility.hpp" #if defined(__OPENCV_BUILD) #include "opencv2/core/private.hpp" +#include "opencv2/core/utils/configuration.private.hpp" #endif #include "opencv2/imgproc.hpp" diff --git a/modules/highgui/src/registry.impl.hpp b/modules/highgui/src/registry.impl.hpp index 782738a820..9cdf6b17eb 100644 --- a/modules/highgui/src/registry.impl.hpp +++ b/modules/highgui/src/registry.impl.hpp @@ -133,7 +133,7 @@ protected: bool readPrioritySettings() { bool hasChanges = false; - cv::String prioritized_backends = utils::getConfigurationParameterString("OPENCV_UI_PRIORITY_LIST", NULL); + cv::String prioritized_backends = utils::getConfigurationParameterString("OPENCV_UI_PRIORITY_LIST"); if (prioritized_backends.empty()) return hasChanges; CV_LOG_INFO(NULL, "UI: Configured priority list (OPENCV_UI_PRIORITY_LIST): " << prioritized_backends); diff --git a/modules/highgui/src/window.cpp b/modules/highgui/src/window.cpp index e6972b300b..4fb2169da9 100644 --- a/modules/highgui/src/window.cpp +++ b/modules/highgui/src/window.cpp @@ -662,7 +662,7 @@ int cv::waitKey(int delay) static int use_legacy = -1; if (use_legacy < 0) { - use_legacy = getenv("OPENCV_LEGACY_WAITKEY") != NULL ? 1 : 0; + use_legacy = utils::getConfigurationParameterBool("OPENCV_LEGACY_WAITKEY"); } if (use_legacy > 0) return code; diff --git a/modules/highgui/src/window_wayland.cpp b/modules/highgui/src/window_wayland.cpp index 3bf87c06ec..c04873ba35 100644 --- a/modules/highgui/src/window_wayland.cpp +++ b/modules/highgui/src/window_wayland.cpp @@ -1378,7 +1378,7 @@ int cv_wl_buffer::create_tmpfile(std::string const &tmpname) { } int cv_wl_buffer::create_anonymous_file(off_t size) { - auto path = getenv("XDG_RUNTIME_DIR") + std::string("/opencv-shared-XXXXXX"); + auto path = cv::utils::getConfigurationParameterString("XDG_RUNTIME_DIR") + std::string("/opencv-shared-XXXXXX"); int fd = create_tmpfile(path); int ret = posix_fallocate(fd, 0, size); diff --git a/modules/objdetect/test/test_main.cpp b/modules/objdetect/test/test_main.cpp index 4031f0522b..a7e31f2dd8 100644 --- a/modules/objdetect/test/test_main.cpp +++ b/modules/objdetect/test/test_main.cpp @@ -11,14 +11,7 @@ static void initTests() { #ifdef HAVE_OPENCV_DNN - const char* extraTestDataPath = -#ifdef WINRT - NULL; -#else - getenv("OPENCV_DNN_TEST_DATA_PATH"); -#endif - if (extraTestDataPath) - cvtest::addDataSearchPath(extraTestDataPath); + cvtest::addDataSearchEnv("OPENCV_DNN_TEST_DATA_PATH"); #endif // HAVE_OPENCV_DNN } diff --git a/modules/ts/include/opencv2/ts.hpp b/modules/ts/include/opencv2/ts.hpp index 943da1d067..14b9b2084f 100644 --- a/modules/ts/include/opencv2/ts.hpp +++ b/modules/ts/include/opencv2/ts.hpp @@ -754,6 +754,7 @@ void smoothBorder(Mat& img, const Scalar& color, int delta = 3); // Utility functions void addDataSearchPath(const std::string& path); +void addDataSearchEnv(const std::string& env_name); void addDataSearchSubDirectory(const std::string& subdir); /*! @brief Try to find requested data file diff --git a/modules/ts/src/precomp.hpp b/modules/ts/src/precomp.hpp index 2725f63429..8508765079 100644 --- a/modules/ts/src/precomp.hpp +++ b/modules/ts/src/precomp.hpp @@ -1,5 +1,6 @@ #include "opencv2/ts.hpp" #include +#include "opencv2/core/utils/configuration.private.hpp" #include "opencv2/core/utility.hpp" #if !defined(__EMSCRIPTEN__) #include "opencv2/core/private.hpp" diff --git a/modules/ts/src/ts.cpp b/modules/ts/src/ts.cpp index 163b573b3e..2cc3cc9a4a 100644 --- a/modules/ts/src/ts.cpp +++ b/modules/ts/src/ts.cpp @@ -553,13 +553,9 @@ static int tsErrorCallback( int status, const char* func_name, const char* err_m void TS::init( const string& modulename ) { data_search_subdir.push_back(modulename); -#ifndef WINRT - char* datapath_dir = getenv("OPENCV_TEST_DATA_PATH"); -#else - char* datapath_dir = OPENCV_TEST_DATA_PATH; -#endif + std::string datapath_dir = cv::utils::getConfigurationParameterString("OPENCV_TEST_DATA_PATH"); - if( datapath_dir ) + if( !datapath_dir.empty() ) { data_path = path_join(path_join(datapath_dir, modulename), ""); } @@ -903,11 +899,7 @@ void parseCustomOptions(int argc, char **argv) test_ipp_check = parser.get("test_ipp_check"); if (!test_ipp_check) -#ifndef WINRT - test_ipp_check = getenv("OPENCV_IPP_CHECK") != NULL; -#else - test_ipp_check = false; -#endif + test_ipp_check = cv::utils::getConfigurationParameterBool("OPENCV_IPP_CHECK"); param_seed = parser.get("test_seed"); @@ -953,9 +945,14 @@ static bool isDirectory(const std::string& path) void addDataSearchPath(const std::string& path) { - if (isDirectory(path)) + if (!path.empty() && isDirectory(path)) TS::ptr()->data_search_path.push_back(path); } +void addDataSearchEnv(const std::string& env_name) +{ + const std::string val = cv::utils::getConfigurationParameterString(env_name.c_str()); + cvtest::addDataSearchPath(val); +} void addDataSearchSubDirectory(const std::string& subdir) { TS::ptr()->data_search_subdir.push_back(subdir); @@ -1001,14 +998,10 @@ static std::string findData(const std::string& relative_path, bool required, boo const std::vector& search_subdir = TS::ptr()->data_search_subdir; -#ifndef WINRT - char* datapath_dir = getenv("OPENCV_TEST_DATA_PATH"); -#else - char* datapath_dir = OPENCV_TEST_DATA_PATH; -#endif + std::string datapath_dir = cv::utils::getConfigurationParameterString("OPENCV_TEST_DATA_PATH"); std::string datapath; - if (datapath_dir) + if (!datapath_dir.empty()) { datapath = datapath_dir; //CV_Assert(isDirectory(datapath) && "OPENCV_TEST_DATA_PATH is specified but it doesn't exist"); diff --git a/modules/ts/src/ts_perf.cpp b/modules/ts/src/ts_perf.cpp index 39147228b8..db57a2f0a4 100644 --- a/modules/ts/src/ts_perf.cpp +++ b/modules/ts/src/ts_perf.cpp @@ -192,22 +192,18 @@ void Regression::init(const std::string& testSuitName, const std::string& ext) return; } -#ifndef WINRT - const char *data_path_dir = getenv("OPENCV_TEST_DATA_PATH"); -#else - const char *data_path_dir = OPENCV_TEST_DATA_PATH; -#endif + const std::string data_path_dir = utils::getConfigurationParameterString("OPENCV_TEST_DATA_PATH"); cvtest::addDataSearchSubDirectory(""); cvtest::addDataSearchSubDirectory(testSuitName); const char *path_separator = "/"; - if (data_path_dir) + if (!data_path_dir.empty()) { - int len = (int)strlen(data_path_dir)-1; + int len = (int)data_path_dir.size()-1; if (len < 0) len = 0; - std::string path_base = (data_path_dir[0] == 0 ? std::string(".") : std::string(data_path_dir)) + std::string path_base = (data_path_dir[0] == 0 ? std::string(".") : data_path_dir) + (data_path_dir[len] == '/' || data_path_dir[len] == '\\' ? "" : path_separator) + "perf" + path_separator; @@ -1042,7 +1038,7 @@ void TestBase::Init(const std::vector & availableImpls, param_verify_sanity = args.get("perf_verify_sanity"); #ifdef HAVE_IPP - test_ipp_check = !args.get("perf_ipp_check") ? getenv("OPENCV_IPP_CHECK") != NULL : true; + test_ipp_check = !args.get("perf_ipp_check") ? utils::getConfigurationParameterBool("OPENCV_IPP_CHECK") : true; #endif testThreads = args.get("perf_threads"); #ifdef CV_COLLECT_IMPL_DATA @@ -1125,12 +1121,11 @@ void TestBase::Init(const std::vector & availableImpls, #endif { -#ifndef WINRT - const char* path = getenv("OPENCV_PERF_VALIDATION_DIR"); -#else - const char* path = OPENCV_PERF_VALIDATION_DIR; + std::string path = utils::getConfigurationParameterString("OPENCV_PERF_VALIDATION_DIR"); +#ifdef WINRT + path = OPENCV_PERF_VALIDATION_DIR; #endif - if (path) + if (!path.empty()) perf_validation_results_directory = path; } @@ -1888,17 +1883,16 @@ std::string TestBase::getDataPath(const std::string& relativePath) throw PerfEarlyExitException(); } -#ifndef WINRT - const char *data_path_dir = getenv("OPENCV_TEST_DATA_PATH"); -#else - const char *data_path_dir = OPENCV_TEST_DATA_PATH; + std::string data_path_dir = utils::getConfigurationParameterString("OPENCV_TEST_DATA_PATH"); +#ifdef WINRT + data_path_dir = OPENCV_TEST_DATA_PATH; #endif const char *path_separator = "/"; std::string path; - if (data_path_dir) + if (!data_path_dir.empty()) { - int len = (int)strlen(data_path_dir) - 1; + int len = (int)data_path_dir.size() - 1; if (len < 0) len = 0; path = (data_path_dir[0] == 0 ? std::string(".") : std::string(data_path_dir)) + (data_path_dir[len] == '/' || data_path_dir[len] == '\\' ? "" : path_separator); diff --git a/modules/video/perf/perf_main.cpp b/modules/video/perf/perf_main.cpp index 1879aeff90..2bf9123d96 100644 --- a/modules/video/perf/perf_main.cpp +++ b/modules/video/perf/perf_main.cpp @@ -7,15 +7,7 @@ static void initTests() { - const char* extraTestDataPath = -#ifdef WINRT - NULL; -#else - getenv("OPENCV_DNN_TEST_DATA_PATH"); -#endif - if (extraTestDataPath) - cvtest::addDataSearchPath(extraTestDataPath); - + cvtest::addDataSearchEnv("OPENCV_DNN_TEST_DATA_PATH"); cvtest::addDataSearchSubDirectory(""); // override "cv" prefix below to access without "../dnn" hacks } diff --git a/modules/video/perf/perf_precomp.hpp b/modules/video/perf/perf_precomp.hpp index ccc1865ff7..529a1436f9 100644 --- a/modules/video/perf/perf_precomp.hpp +++ b/modules/video/perf/perf_precomp.hpp @@ -7,6 +7,7 @@ #include "opencv2/ts.hpp" #include #include "opencv2/ts/ts_perf.hpp" +#include "opencv2/core/utils/configuration.private.hpp" namespace cvtest { diff --git a/modules/video/test/test_main.cpp b/modules/video/test/test_main.cpp index 9968380a17..68c90f775e 100644 --- a/modules/video/test/test_main.cpp +++ b/modules/video/test/test_main.cpp @@ -10,15 +10,7 @@ static void initTests() { - const char* extraTestDataPath = -#ifdef WINRT - NULL; -#else - getenv("OPENCV_DNN_TEST_DATA_PATH"); -#endif - if (extraTestDataPath) - cvtest::addDataSearchPath(extraTestDataPath); - + cvtest::addDataSearchEnv("OPENCV_DNN_TEST_DATA_PATH"); cvtest::addDataSearchSubDirectory(""); // override "cv" prefix below to access without "../dnn" hacks } diff --git a/modules/video/test/test_precomp.hpp b/modules/video/test/test_precomp.hpp index ba1f10998f..a6810ccc1f 100644 --- a/modules/video/test/test_precomp.hpp +++ b/modules/video/test/test_precomp.hpp @@ -7,6 +7,7 @@ #include "opencv2/ts.hpp" #include "opencv2/video.hpp" #include +#include "opencv2/core/utils/configuration.private.hpp" namespace opencv_test { using namespace perf; diff --git a/modules/videoio/src/backend_plugin.cpp b/modules/videoio/src/backend_plugin.cpp index 039083c6ad..cc9b33b80c 100644 --- a/modules/videoio/src/backend_plugin.cpp +++ b/modules/videoio/src/backend_plugin.cpp @@ -332,16 +332,14 @@ std::vector getPluginCandidates(const std::string& baseName) std::vector results; #ifdef _WIN32 FileSystemPath_t moduleName = toFileSystemPath(libraryPrefix() + "opencv_videoio_" + baseName_l + librarySuffix()); -#ifndef WINRT if (baseName_u == "FFMPEG") // backward compatibility { - const wchar_t* ffmpeg_env_path = _wgetenv(L"OPENCV_FFMPEG_DLL_DIR"); - if (ffmpeg_env_path) + const std::string ffmpeg_env_path = cv::utils::getConfigurationParameterString("OPENCV_FFMPEG_DLL_DIR"); + if (!ffmpeg_env_path.empty()) { - results.push_back(FileSystemPath_t(ffmpeg_env_path) + L"\\" + moduleName); + results.push_back(toFileSystemPath(ffmpeg_env_path + "\\" + toPrintablePath(moduleName))); } } -#endif if (plugin_expr != default_expr) { moduleName = toFileSystemPath(plugin_expr); diff --git a/modules/videoio/src/cap_dshow.cpp b/modules/videoio/src/cap_dshow.cpp index 7cc0469200..d46bb08a0d 100644 --- a/modules/videoio/src/cap_dshow.cpp +++ b/modules/videoio/src/cap_dshow.cpp @@ -303,15 +303,7 @@ interface ISampleGrabber : public IUnknown static void DebugPrintOut(const char *format, ...) { - static int gs_verbose = -1; - if (gs_verbose < 0) - { - // Fetch initial debug state from environment - defaults to disabled - const char* s = getenv("OPENCV_DSHOW_DEBUG"); - gs_verbose = s != NULL && atoi(s) != 0; - } - - + static const bool gs_verbose = utils::getConfigurationParameterBool("OPENCV_DSHOW_DEBUG"); if (gs_verbose) { va_list args; @@ -2982,18 +2974,18 @@ int videoInput::start(int deviceID, videoDevice *VD){ VD->readyToCapture = true; // check for optional saving the direct show graph to a file - const char* graph_filename = getenv("OPENCV_DSHOW_SAVEGRAPH_FILENAME"); - if (graph_filename) { - size_t filename_len = strlen(graph_filename); + std::string graph_filename = utils::getConfigurationParameterString("OPENCV_DSHOW_SAVEGRAPH_FILENAME"); + if (!graph_filename.empty()) { + size_t filename_len = graph_filename.size(); std::vector wfilename(filename_len + 1); - size_t len = mbstowcs(&wfilename[0], graph_filename, filename_len + 1); + size_t len = mbstowcs(&wfilename[0], &graph_filename[0], filename_len + 1); CV_Assert(len == filename_len); HRESULT res = SaveGraphFile(VD->pGraph, &wfilename[0]); if (SUCCEEDED(res)) { - DebugPrintOut("Saved DSHOW graph to %s\n", graph_filename); + DebugPrintOut("Saved DSHOW graph to %s\n", graph_filename.c_str()); } else { - DebugPrintOut("Failed to save DSHOW graph to %s\n", graph_filename); + DebugPrintOut("Failed to save DSHOW graph to %s\n", graph_filename.c_str()); } } diff --git a/modules/videoio/src/cap_ffmpeg_impl.hpp b/modules/videoio/src/cap_ffmpeg_impl.hpp index 17e9f4903f..f13cb8462b 100644 --- a/modules/videoio/src/cap_ffmpeg_impl.hpp +++ b/modules/videoio/src/cap_ffmpeg_impl.hpp @@ -928,21 +928,19 @@ public: } static void initLogger_() { -#ifndef NO_GETENV - char* debug_option = getenv("OPENCV_FFMPEG_DEBUG"); - char* level_option = getenv("OPENCV_FFMPEG_LOGLEVEL"); + const bool debug_option = utils::getConfigurationParameterBool("OPENCV_FFMPEG_DEBUG"); + std::string level_option = utils::getConfigurationParameterString("OPENCV_FFMPEG_LOGLEVEL"); int level = AV_LOG_VERBOSE; - if (level_option != NULL) + if (!level_option.empty()) { - level = atoi(level_option); + level = atoi(level_option.c_str()); } - if ( (debug_option != NULL) || (level_option != NULL) ) + if ( debug_option || (!level_option.empty()) ) { av_log_set_level(level); av_log_set_callback(ffmpeg_log_callback); } else -#endif { av_log_set_level(AV_LOG_ERROR); } @@ -979,10 +977,10 @@ inline void fill_codec_context(AVCodecContext * enc, AVDictionary * dict) { int nCpus = cv::getNumberOfCPUs(); int requestedThreads = std::min(nCpus, 16); // [OPENCV:FFMPEG:24] Application has requested XX threads. Using a thread count greater than 16 is not recommended. - char* threads_option = getenv("OPENCV_FFMPEG_THREADS"); - if (threads_option != NULL) + std::string threads_option = utils::getConfigurationParameterString("OPENCV_FFMPEG_THREADS"); + if (!threads_option.empty()) { - requestedThreads = atoi(threads_option); + requestedThreads = atoi(threads_option.c_str()); } enc->thread_count = requestedThreads; } @@ -1122,9 +1120,8 @@ bool CvCapture_FFMPEG::open(const char* _filename, const VideoCaptureParameters& ic->interrupt_callback.opaque = &interrupt_metadata; #endif -#ifndef NO_GETENV - char* options = getenv("OPENCV_FFMPEG_CAPTURE_OPTIONS"); - if(options == NULL) + std::string options = utils::getConfigurationParameterString("OPENCV_FFMPEG_CAPTURE_OPTIONS"); + if(!options.empty()) { #if LIBAVFORMAT_VERSION_MICRO >= 100 && LIBAVFORMAT_BUILD >= CALC_FFMPEG_VERSION(55, 48, 100) av_dict_set(&dict, "rtsp_flags", "prefer_tcp", 0); @@ -1136,14 +1133,11 @@ bool CvCapture_FFMPEG::open(const char* _filename, const VideoCaptureParameters& { CV_LOG_DEBUG(NULL, "VIDEOIO/FFMPEG: using capture options from environment: " << options); #if LIBAVUTIL_BUILD >= (LIBAVUTIL_VERSION_MICRO >= 100 ? CALC_FFMPEG_VERSION(52, 17, 100) : CALC_FFMPEG_VERSION(52, 7, 0)) - av_dict_parse_string(&dict, options, ";", "|", 0); + av_dict_parse_string(&dict, options.c_str(), ";", "|", 0); #else av_dict_set(&dict, "rtsp_transport", "tcp", 0); #endif } -#else - av_dict_set(&dict, "rtsp_transport", "tcp", 0); -#endif CV_FFMPEG_FMT_CONST AVInputFormat* input_format = NULL; AVDictionaryEntry* entry = av_dict_get(dict, "input_format", NULL, 0); if (entry != 0) @@ -3095,12 +3089,12 @@ bool CvVideoWriter_FFMPEG::open( const char * filename, int fourcc, } AVDictionary *dict = NULL; -#if !defined(NO_GETENV) && (LIBAVUTIL_VERSION_MAJOR >= 53) - char* options = getenv("OPENCV_FFMPEG_WRITER_OPTIONS"); - if (options) +#if (LIBAVUTIL_VERSION_MAJOR >= 53) + std::string options = utils::getConfigurationParameterString("OPENCV_FFMPEG_WRITER_OPTIONS"); + if (!options.empty()) { CV_LOG_DEBUG(NULL, "VIDEOIO/FFMPEG: using writer options from environment: " << options); - av_dict_parse_string(&dict, options, ";", "|", 0); + av_dict_parse_string(&dict, options.c_str(), ";", "|", 0); } #endif diff --git a/modules/videoio/src/videoio_registry.cpp b/modules/videoio/src/videoio_registry.cpp index b333797fe0..a3aee5abfe 100644 --- a/modules/videoio/src/videoio_registry.cpp +++ b/modules/videoio/src/videoio_registry.cpp @@ -259,7 +259,7 @@ protected: bool readPrioritySettings() { bool hasChanges = false; - cv::String prioritized_backends = utils::getConfigurationParameterString("OPENCV_VIDEOIO_PRIORITY_LIST", NULL); + cv::String prioritized_backends = utils::getConfigurationParameterString("OPENCV_VIDEOIO_PRIORITY_LIST"); if (prioritized_backends.empty()) return hasChanges; CV_LOG_INFO(NULL, "VIDEOIO: Configured priority list (OPENCV_VIDEOIO_PRIORITY_LIST): " << prioritized_backends); diff --git a/modules/videoio/test/test_main.cpp b/modules/videoio/test/test_main.cpp index d57a611c4b..db82968d76 100644 --- a/modules/videoio/test/test_main.cpp +++ b/modules/videoio/test/test_main.cpp @@ -11,15 +11,13 @@ static void initTests() { -#ifndef WINRT // missing getenv const std::vector backends = cv::videoio_registry::getStreamBackends(); - const char* requireFFmpeg = getenv("OPENCV_TEST_VIDEOIO_BACKEND_REQUIRE_FFMPEG"); + bool requireFFmpeg = cv::utils::getConfigurationParameterBool("OPENCV_TEST_VIDEOIO_BACKEND_REQUIRE_FFMPEG"); if (requireFFmpeg && !isBackendAvailable(cv::CAP_FFMPEG, backends)) { CV_LOG_FATAL(NULL, "OpenCV-Test: required FFmpeg backend is not available (broken plugin?). STOP."); exit(1); } -#endif } CV_TEST_MAIN("highgui", initTests()) diff --git a/modules/videoio/test/test_precomp.hpp b/modules/videoio/test/test_precomp.hpp index 835177729b..b17a2f0ab7 100644 --- a/modules/videoio/test/test_precomp.hpp +++ b/modules/videoio/test/test_precomp.hpp @@ -13,6 +13,7 @@ #include "opencv2/videoio.hpp" #include "opencv2/videoio/registry.hpp" #include "opencv2/core/private.hpp" +#include "opencv2/core/utils/configuration.private.hpp" namespace cv {