From 90fe1f403cf5ba579ade9ded3c3e625ae302c4db Mon Sep 17 00:00:00 2001 From: demoulinv Date: Sat, 7 May 2022 21:16:12 +0200 Subject: [PATCH] [image io] Add log trace when OCIO config file found. Update OCIO config file by removing the possibility to call the OCIO V1 method to identify the color space from the file name. Default color space is still sRGB. When reading image, avoid searching input image color space if no conversion is needed. If conversion is needed, check color space validity after getting it from image metadate or image file name. --- src/CMakeLists.txt | 4 -- src/aliceVision/image/io.cpp | 39 +++++++++++++++---- src/aliceVision/image/io_test.cpp | 2 +- .../image/share/aliceVision/config.ocio | 2 +- 4 files changed, 34 insertions(+), 13 deletions(-) diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 31d28ddc51..f8c99f64d8 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -307,10 +307,6 @@ endif() find_package(OpenImageIO 2.0.9 REQUIRED) if(OPENIMAGEIO_FOUND OR OpenImageIO_FOUND) message(STATUS "OpenImageIO found.") - if(UNIX) - # Add DL dependency on linux - set(OPENIMAGEIO_LIBRARIES "${OPENIMAGEIO_LIBRARIES};dl") - endif() else() message(SEND_ERROR "Failed to find OpenImageIO.") endif() diff --git a/src/aliceVision/image/io.cpp b/src/aliceVision/image/io.cpp index 05fefdd8a4..a58730906c 100644 --- a/src/aliceVision/image/io.cpp +++ b/src/aliceVision/image/io.cpp @@ -79,6 +79,13 @@ std::string EImageColorSpace_enumToString(const EImageColorSpace dataType) throw std::out_of_range("Invalid EImageColorSpace enum"); } +bool isValidColorSpace(std::string colorSpace) +{ + const std::string CSlc = boost::to_lower_copy(colorSpace); + + return (CSlc == "auto") || (CSlc == "srgb_linear") || (CSlc == "linear") || (CSlc == "srgb") || (CSlc == "aces") || (CSlc == "acescg") || (CSlc == "no_conversion"); +} + std::ostream& operator<<(std::ostream& os, EImageColorSpace dataType) { return os << EImageColorSpace_enumToString(dataType); @@ -103,7 +110,13 @@ std::string getColorConfigFilePath() configOCIOFilePath.append("/share/aliceVision/config.ocio"); if (!fs::exists(configOCIOFilePath)) + { ALICEVISION_THROW_ERROR("OCIO configuration file: '" << configOCIOFilePath << "' does not exist."); + } + else + { + ALICEVISION_LOG_TRACE("OCIO configuration file: '" << configOCIOFilePath << "' found."); + } return configOCIOFilePath; } @@ -143,6 +156,16 @@ EImageColorSpace getImageColorSpace(const std::string imagePath) } } + if (!isValidColorSpace(colorSpace)) + { + size_t npos = imagePath.find_last_of("."); + std::string ext = imagePath.substr(npos + 1); + std::string forcedColorSpace = (ext == "exr" || ext == "EXR") ? "sRGB_linear" : "sRGB"; + + ALICEVISION_LOG_WARNING("The color space " << colorSpace << " detected for " << imagePath << " is not supported. Force Color space to " << forcedColorSpace << "."); + colorSpace = forcedColorSpace; + } + return EImageColorSpace_stringToEnum(colorSpace); } @@ -406,14 +429,16 @@ void readImage(const std::string& path, if(imageReadOptions.workingColorSpace == EImageColorSpace::AUTO) throw std::runtime_error("You must specify a requested color space for image file '" + path + "'."); - std::string inputColorSpace = EImageColorSpace_enumToString(getImageColorSpace(path)); - - if ((imageReadOptions.workingColorSpace != EImageColorSpace::NO_CONVERSION) && (EImageColorSpace_stringToEnum(boost::to_lower_copy(inputColorSpace)) != imageReadOptions.workingColorSpace)) + if (imageReadOptions.workingColorSpace != EImageColorSpace::NO_CONVERSION) { - oiio::ColorConfig colorConfig(getColorConfigFilePath()); - std::string outputColorSpace = (imageReadOptions.workingColorSpace == EImageColorSpace::SRGB_LINEAR) ? "linear" : EImageColorSpace_enumToString(imageReadOptions.workingColorSpace); - oiio::ImageBufAlgo::colorconvert(inBuf, inBuf, inputColorSpace, outputColorSpace, true, "", "", &colorConfig); - ALICEVISION_LOG_TRACE("Convert image " << path << " from " << inputColorSpace << " to " << outputColorSpace << " colorspace"); + std::string inputColorSpace = EImageColorSpace_enumToString(getImageColorSpace(path)); + if (EImageColorSpace_stringToEnum(boost::to_lower_copy(inputColorSpace)) != imageReadOptions.workingColorSpace) + { + oiio::ColorConfig colorConfig(getColorConfigFilePath()); + std::string outputColorSpace = (imageReadOptions.workingColorSpace == EImageColorSpace::SRGB_LINEAR) ? "linear" : EImageColorSpace_enumToString(imageReadOptions.workingColorSpace); + oiio::ImageBufAlgo::colorconvert(inBuf, inBuf, inputColorSpace, outputColorSpace, true, "", "", &colorConfig); + ALICEVISION_LOG_TRACE("Convert image " << path << " from " << inputColorSpace << " to " << outputColorSpace << " colorspace"); + } } // convert to grayscale if needed diff --git a/src/aliceVision/image/io_test.cpp b/src/aliceVision/image/io_test.cpp index d60a21afcb..e4c848e243 100644 --- a/src/aliceVision/image/io_test.cpp +++ b/src/aliceVision/image/io_test.cpp @@ -38,7 +38,7 @@ int setenv(const char* name, const char* value, int overwrite) int err = setenv("ALICEVISION_ROOT", std::string(THIS_SOURCE_DIR).c_str(), 1); // tested extensions -static std::vector extensions = {"jpg", "png", "pgm", "ppm", "tiff", "exr"}; +static std::vector extensions = { "jpg", "png", "pgm", "ppm", "tiff", "exr" }; BOOST_AUTO_TEST_CASE(read_unexisting) { Image image; diff --git a/src/aliceVision/image/share/aliceVision/config.ocio b/src/aliceVision/image/share/aliceVision/config.ocio index 3c90c26ee8..a6969d5b04 100644 --- a/src/aliceVision/image/share/aliceVision/config.ocio +++ b/src/aliceVision/image/share/aliceVision/config.ocio @@ -64,7 +64,7 @@ file_rules: # # The next rule uses the OCIO v1 method of searching the path for all colorspaces in the config. # - - ! {name: ColorSpaceNamePathSearch} +# - ! {name: ColorSpaceNamePathSearch} # # The rules are ordered, highest priority first. OCIO takes the path to a file and applies # the rules one-by-one until there is a match. The last rule, "Default", always matches.