Skip to content

Commit

Permalink
[image io]
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
demoulinv committed Jun 17, 2022
1 parent cdb989b commit 90fe1f4
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 13 deletions.
4 changes: 0 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
39 changes: 32 additions & 7 deletions src/aliceVision/image/io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/aliceVision/image/io_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> extensions = {"jpg", "png", "pgm", "ppm", "tiff", "exr"};
static std::vector<std::string> extensions = { "jpg", "png", "pgm", "ppm", "tiff", "exr" };

BOOST_AUTO_TEST_CASE(read_unexisting) {
Image<unsigned char> image;
Expand Down
2 changes: 1 addition & 1 deletion src/aliceVision/image/share/aliceVision/config.ocio
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ file_rules:
#
# The next rule uses the OCIO v1 method of searching the path for all colorspaces in the config.
#
- !<Rule> {name: ColorSpaceNamePathSearch}
# - !<Rule> {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.
Expand Down

0 comments on commit 90fe1f4

Please sign in to comment.