From 6677ef5d70cac204ecc159d5212eeab677ea5005 Mon Sep 17 00:00:00 2001 From: Zach Lewis Date: Mon, 28 Sep 2026 19:49:39 -0400 Subject: [PATCH 1/6] feat(color): add ColorSpaceInfo properties query to ColorConfig Add ColorSpaceInfo, an immutable description of a color space's RGB primaries and white point, and of its transfer function's exponent when that is a pure power (1.0 for linear), and two ColorConfig queries that return one: get_color_space_info() and derive_color_space_info(). Today both answer only from what the config declares. A color space declaring a scene-linear or display-linear encoding reports gamma 1.0. A published Color Interop ID the config declares for it, as its interop_id (OpenColorIO 2.5 or newer) or, when it declares none, as its name or an alias, gives that ID's primaries and transfer function from a small built-in table. Piecewise curves such as sRGB, PQ and HLG report gamma 0. A data color space is valid with no properties, and a name that is not a color space, alias or role of the config gives an invalid ColorSpaceInfo. derive_color_space_info() is where analysis of a color space's transforms will go, so that properties a config does not declare can be derived. That arrives later; for now it returns what get_color_space_info() does. ColorSpaceInfo keeps its data behind a shared pointer, so copies are cheap and outlive the ColorConfig that made them. The change is ABI-additive: a new class and two new const ColorConfig members. The Python bindings, stubs, docs and tests are included. Assisted-by: Claude Code / Claude Opus 5.5 Signed-off-by: Zach Lewis --- src/doc/imageioapi.md | 32 ++++ src/doc/pythonbindings.md | 47 +++++ src/include/OpenImageIO/color.h | 64 +++++++ src/libOpenImageIO/color_ocio.cpp | 164 ++++++++++++++++++ src/libOpenImageIO/color_test.cpp | 84 +++++++++ src/python/py_colorconfig.cpp | 23 +++ src/python/stubs/OpenImageIO/__init__.pyi | 8 + .../python-colorconfig/ref/out-ocio232.txt | 6 + .../python-colorconfig/ref/out-ocio24.txt | 6 + .../python-colorconfig/ref/out-ocio25.txt | 6 + .../python-colorconfig/ref/out-ocio26.txt | 6 + testsuite/python-colorconfig/ref/out.txt | 6 + .../src/test_colorconfig.py | 6 + 13 files changed, 458 insertions(+) diff --git a/src/doc/imageioapi.md b/src/doc/imageioapi.md index 7afbab7fce..ba59a66e55 100644 --- a/src/doc/imageioapi.md +++ b/src/doc/imageioapi.md @@ -257,6 +257,38 @@ just exist in the OIIO namespace as general utilities. (See :::{doxygenfunction} OIIO::equivalent_colorspace ::: +(sec-colorspaceinfo)= + +## Color space properties: `ColorSpaceInfo` + +`ColorConfig::get_color_space_info()` and +`ColorConfig::derive_color_space_info()` describe a color space, named by a +color space name, alias or role of the config, with a `ColorSpaceInfo`: its RGB +primaries and white point, and the exponent of its transfer function when that +is a pure power (1.0 for a linear one). Either property may be unavailable. +They are taken from what the config declares: a `scene-linear` or +`display-linear` encoding gives gamma 1.0, and a published Color Interop ID the +config declares for the color space, as its `interop_id` (OpenColorIO 2.5 or +newer) or as its name or one of its aliases, gives that ID's primaries and +transfer function. For example, a color space declared as `lin_ap1_scene` +reports gamma 1.0 and the ACES AP1 primaries, and one declared as +`g22_rec709_scene` gamma 2.2 and the Rec.709 primaries. The sRGB curve is +piecewise, so `srgb_rec709_scene` reports gamma 0. `derive_color_space_info()` +may also derive properties the config does not declare by analyzing the color +space's transforms; currently it derives nothing more. Neither call changes the +config, an image, or its metadata. See {ref}`sec-pythoncolorconfig` for the +Python bindings. + +:::{doxygenclass} OIIO::ColorSpaceInfo +:members: +::: + +:::{doxygenfunction} OIIO::ColorConfig::get_color_space_info +::: + +:::{doxygenfunction} OIIO::ColorConfig::derive_color_space_info +::: + > (sec-startupshutdown)= ## Startup and Shutdown diff --git a/src/doc/pythonbindings.md b/src/doc/pythonbindings.md index afd8947c48..4952e2209f 100644 --- a/src/doc/pythonbindings.md +++ b/src/doc/pythonbindings.md @@ -4040,6 +4040,31 @@ is provided for minimal color support. % TODO: The documentation for this class is incomplete. +:::{py:method} get_color_space_info (colorspace) +Return a `ColorSpaceInfo` with the properties of the named color space that +the config declares, without analyzing its transforms. A name that does not +resolve returns an invalid `ColorSpaceInfo`. See {ref}`sec-colorspaceinfo`. + +This function was added in OpenImageIO 3.3. +::: + +:::{py:method} derive_color_space_info (colorspace) +Return a `ColorSpaceInfo` with the properties of the named color space, as +`get_color_space_info()` does, plus any that can be derived by analyzing its +transforms. Currently it derives nothing more. + +Example: + +```python +info = colorconfig.derive_color_space_info("ACEScg") +if info.valid(): + gamma = info.transfer_function_gamma() # 1.0 for a linear encoding + xy = info.chromaticities() # [] if the primaries are unknown +``` + +This function was added in OpenImageIO 3.3. +::: + :::{py:method} get_cicp (colorspace) Find CICP code corresponding to the colorspace. Return a sequence of 4 ints, or None if not found. @@ -4084,6 +4109,28 @@ interop_id = colorconfig.get_color_interop_id([9, 16, 9, 1]) This function was added in OpenImageIO 3.1. ::: +:::{py:class} ColorSpaceInfo +Immutable properties of a color space. It remains usable after the +`ColorConfig` that returned it is destroyed. This class was added in +OpenImageIO 3.3. +::: + +:::{py:method} ColorSpaceInfo.valid () +Whether the color space name resolved, even if individual properties are +unavailable. +::: + +:::{py:method} ColorSpaceInfo.chromaticities () +The RGB primaries and white point as a list of eight floats (Rx, Ry, Gx, Gy, +Bx, By, Wx, Wy, CIE 1931 xy), or an empty list if they are unavailable. +::: + +:::{py:method} ColorSpaceInfo.transfer_function_gamma () +The exponent of a transfer function that decodes as a pure power, 1.0 for a +linear one, or 0 if the transfer function is not a pure power or is unknown. A +color space whose config declares a linear encoding reports 1.0. +::: + (sec-pythonmiscapi)= ## Miscellaneous Utilities diff --git a/src/include/OpenImageIO/color.h b/src/include/OpenImageIO/color.h index 865b32ea68..95b43c2b40 100644 --- a/src/include/OpenImageIO/color.h +++ b/src/include/OpenImageIO/color.h @@ -63,6 +63,44 @@ using ColorProcessorHandle = std::shared_ptr; +/// Immutable, owning properties of a color space, returned by +/// `ColorConfig::get_color_space_info()` and +/// `ColorConfig::derive_color_space_info()`. Copies share their storage and +/// remain usable after the originating ColorConfig resets or is destroyed. +/// +/// @version 3.3 +class OIIO_API ColorSpaceInfo { +public: + ColorSpaceInfo() noexcept; + ColorSpaceInfo(const ColorSpaceInfo&) noexcept; + ColorSpaceInfo(ColorSpaceInfo&&) noexcept; + ColorSpaceInfo& operator=(const ColorSpaceInfo&) noexcept; + ColorSpaceInfo& operator=(ColorSpaceInfo&&) noexcept; + ~ColorSpaceInfo(); + + /// Whether the color space name resolved, even if individual properties + /// are unavailable. + bool valid() const noexcept; + /// The RGB primaries and white point as eight floats (Rx, Ry, Gx, Gy, Bx, + /// By, Wx, Wy, CIE 1931 xy), or an empty span if they are unavailable. + /// The span borrows from this object and expires when its last owning + /// copy is destroyed or reassigned. + cspan chromaticities() const noexcept; + /// The exponent of a transfer function that decodes as a pure power, + /// 1.0 for a linear one, or 0 if the transfer function is not a pure + /// power or is unknown. Piecewise curves such as sRGB's are never + /// approximated by a gamma, so they report 0. A color space whose config + /// declares a linear encoding reports 1.0. + float transfer_function_gamma() const noexcept; + +private: + friend class ColorConfig; + friend struct ColorSpaceInfoAccess; + struct Impl; + std::shared_ptr m_impl; +}; + + /// Represents the set of all color transformations that are allowed. /// If OpenColorIO is enabled at build time, this configuration is loaded /// at runtime, allowing the user to have complete control of all color @@ -434,6 +472,31 @@ class OIIO_API ColorConfig { OIIO_NODISCARD string_view get_color_interop_id(string_view colorspace) const; + /// Return the properties of a color space that are already known, + /// without analyzing its transforms: gamma 1.0 for a color space whose + /// config declares a scene-linear or display-linear encoding, and the + /// primaries and transfer function of a published Color Interop ID + /// (https://github.com/AcademySoftwareFoundation/ColorInterop) the config + /// declares for it, as its interop_id (OpenColorIO 2.5 or newer) or, + /// when it declares none, as its name or one of its aliases. An ID whose + /// transfer function contradicts a declared linear encoding is ignored. + /// A data color space is valid but has no properties. `colorspace` may be + /// a color space name, alias or role of this config; a name that does + /// not resolve returns an invalid ColorSpaceInfo. + /// + /// @version 3.3 + OIIO_NODISCARD ColorSpaceInfo + get_color_space_info(string_view colorspace) const; + + /// Return the properties of a color space, as `get_color_space_info()` + /// does, plus any the config does not declare that can be derived by + /// analyzing the color space's transforms. Currently it derives nothing + /// beyond what `get_color_space_info()` returns. + /// + /// @version 3.3 + OIIO_NODISCARD ColorSpaceInfo + derive_color_space_info(string_view colorspace) const; + /// Find color interop ID corresponding to the CICP code. /// Returns empty string if not found. /// @@ -490,6 +553,7 @@ OIIO_NAMESPACE_3_1_END #ifndef OIIO_DOXYGEN OIIO_NAMESPACE_BEGIN using v3_1::ColorProcessorHandle; +using v3_1::ColorSpaceInfo; OIIO_NAMESPACE_END #endif diff --git a/src/libOpenImageIO/color_ocio.cpp b/src/libOpenImageIO/color_ocio.cpp index 30b37d2fa8..2b9f9d2652 100644 --- a/src/libOpenImageIO/color_ocio.cpp +++ b/src/libOpenImageIO/color_ocio.cpp @@ -2504,6 +2504,170 @@ ColorConfig::get_cicp(string_view colorspace) const } + +struct ColorSpaceInfo::Impl { + std::array chromaticities {}; + bool has_chromaticities = false; + float gamma = 0.0f; +}; + +ColorSpaceInfo::ColorSpaceInfo() noexcept = default; +ColorSpaceInfo::ColorSpaceInfo(const ColorSpaceInfo&) noexcept = default; +ColorSpaceInfo::ColorSpaceInfo(ColorSpaceInfo&&) noexcept = default; +ColorSpaceInfo& ColorSpaceInfo::operator=(const ColorSpaceInfo&) noexcept + = default; +ColorSpaceInfo& ColorSpaceInfo::operator=(ColorSpaceInfo&&) noexcept = default; +ColorSpaceInfo::~ColorSpaceInfo() = default; + +bool +ColorSpaceInfo::valid() const noexcept +{ + return bool(m_impl); +} + +cspan +ColorSpaceInfo::chromaticities() const noexcept +{ + return m_impl && m_impl->has_chromaticities + ? cspan(m_impl->chromaticities) + : cspan(); +} + +float +ColorSpaceInfo::transfer_function_gamma() const noexcept +{ + return m_impl ? m_impl->gamma : 0.0f; +} + + + +namespace { + +// Chromaticities (Rx, Ry, Gx, Gy, Bx, By, Wx, Wy) of the published gamuts. +constexpr float xy_rec709[] = { .64f, .33f, .30f, .60f, + .15f, .06f, .3127f, .329f }; +constexpr float xy_p3d65[] = { .68f, .32f, .265f, .69f, + .15f, .06f, .3127f, .329f }; +constexpr float xy_rec2020[] = { .708f, .292f, .17f, .797f, + .131f, .046f, .3127f, .329f }; +constexpr float xy_adobergb[] = { .64f, .33f, .21f, .71f, + .15f, .06f, .3127f, .329f }; +constexpr float xy_ap0[] = { .7347f, .2653f, 0.0f, 1.0f, + .0001f, -.077f, .32168f, .33767f }; +constexpr float xy_ap1[] = { .713f, .293f, .165f, .83f, + .128f, .044f, .32168f, .33767f }; +// CIE XYZ taken as RGB, so R = G = B is illuminant E. +constexpr float xy_xyz[] = { 1.0f, 0.0f, 0.0f, 1.0f, + 0.0f, 0.0f, 1.0f / 3, 1.0f / 3 }; + +// Properties of the Color Interop Forum's published (unnamespaced) IDs: +// gamma is 1 for linear, the exponent of a pure power, or 0 for any other +// curve (sRGB, extended sRGB, PQ, HLG). The DCDM XYZ encodings +// g26_xyzd65_display and pq_xyzd65_display have no entry: neither curve is a +// pure power (DCI headroom scaling precedes the 2.6 power), and no RGB +// primaries are reported for these display XYZ encodings. +struct PublishedColorSpace { + const char* interop_id; + const float* chromaticities; + float gamma; +}; +constexpr PublishedColorSpace published_color_spaces[] = { + { "lin_ap1_scene", xy_ap1, 1.0f }, + { "lin_ap0_scene", xy_ap0, 1.0f }, + { "lin_rec709_scene", xy_rec709, 1.0f }, + { "lin_p3d65_scene", xy_p3d65, 1.0f }, + { "lin_rec2020_scene", xy_rec2020, 1.0f }, + { "lin_adobergb_scene", xy_adobergb, 1.0f }, + { "lin_ciexyzd65_scene", xy_xyz, 1.0f }, + { "srgb_rec709_scene", xy_rec709, 0.0f }, + { "g24_rec709_scene", xy_rec709, 2.4f }, + { "g22_rec709_scene", xy_rec709, 2.2f }, + { "g18_rec709_scene", xy_rec709, 1.8f }, + { "srgb_ap1_scene", xy_ap1, 0.0f }, + { "g22_ap1_scene", xy_ap1, 2.2f }, + { "srgb_p3d65_scene", xy_p3d65, 0.0f }, + { "g22_adobergb_scene", xy_adobergb, 563.0f / 256.0f }, + { "srgb_rec709_display", xy_rec709, 0.0f }, + { "g24_rec709_display", xy_rec709, 2.4f }, + { "srgb_p3d65_display", xy_p3d65, 0.0f }, + { "srgbe_p3d65_display", xy_p3d65, 0.0f }, + { "pq_p3d65_display", xy_p3d65, 0.0f }, + { "pq_rec2020_display", xy_rec2020, 0.0f }, + { "hlg_rec2020_display", xy_rec2020, 0.0f }, + { "g22_rec709_display", xy_rec709, 2.2f }, + { "g22_adobergb_display", xy_adobergb, 563.0f / 256.0f }, + { "g26_p3d65_display", xy_p3d65, 2.6f }, + { "lin_rec709_display", xy_rec709, 1.0f }, + { "lin_p3d65_display", xy_p3d65, 1.0f }, + { "lin_rec2020_display", xy_rec2020, 1.0f }, +}; + +const PublishedColorSpace* +find_published_color_space(string_view interop_id) +{ + for (const auto& published : published_color_spaces) + if (Strutil::iequals(interop_id, published.interop_id)) + return &published; + return nullptr; +} + +} // namespace + + + +ColorSpaceInfo +ColorConfig::get_color_space_info(string_view colorspace) const +{ + OCIO::ConstColorSpaceRcPtr cs; + if (getImpl()->config_ && !disable_ocio && !colorspace.empty()) { + try { + cs = getImpl()->config_->getColorSpace(c_str(colorspace)); + } catch (const std::exception& e) { + DBG("OCIO exception in get_color_space_info: {}\n", e.what()); + } + } + if (!cs) + return {}; + ColorSpaceInfo result; + auto impl = std::make_shared(); + result.m_impl = impl; + if (cs->isData()) + return result; + const string_view encoding(cs->getEncoding()); + const bool linear = encoding == "scene-linear" + || encoding == "display-linear"; + // The ID the config declares for this space, else its name or an alias. + const PublishedColorSpace* published = nullptr; + string_view declared; +#if OCIO_VERSION_HEX >= MAKE_OCIO_VERSION_HEX(2, 5, 0) + declared = cs->getInteropID(); + published = find_published_color_space(declared); +#endif + if (declared.empty()) { + published = find_published_color_space(cs->getName()); + for (int i = 0, n = cs->getNumAliases(); !published && i < n; ++i) + published = find_published_color_space(cs->getAlias(i)); + } + if (published && !(linear && published->gamma != 1.0f)) { + std::copy_n(published->chromaticities, 8, impl->chromaticities.data()); + impl->has_chromaticities = true; + impl->gamma = published->gamma; + } + if (linear) + impl->gamma = 1.0f; + return result; +} + + + +ColorSpaceInfo +ColorConfig::derive_color_space_info(string_view colorspace) const +{ + // No transform analysis yet, so there is nothing more to derive. + return get_color_space_info(colorspace); +} + + ////////////////////////////////////////////////////////////////////////// // // Image Processing Implementations diff --git a/src/libOpenImageIO/color_test.cpp b/src/libOpenImageIO/color_test.cpp index ba50f7bddc..ea8d25db22 100644 --- a/src/libOpenImageIO/color_test.cpp +++ b/src/libOpenImageIO/color_test.cpp @@ -196,6 +196,89 @@ test_gamma_pair_conversion() +static void +test_color_space_info() +{ + ColorSpaceInfo invalid; + OIIO_CHECK_FALSE(invalid.valid()); + OIIO_CHECK_ASSERT(invalid.chromaticities().empty()); + OIIO_CHECK_EQUAL(invalid.transfer_function_gamma(), 0.0f); + if (!ColorConfig::supportsOpenColorIO()) + return; + + // Published IDs declared by name or alias, a linear encoding with no ID, + // one contradicting its ID, and a data space. + const std::string filename = Filesystem::temp_directory_path() + "/" + + Filesystem::unique_path() + ".ocio"; + OIIO_CHECK_ASSERT(Filesystem::write_text_file( + filename, + "ocio_profile_version: 2.3\n" + "roles: {default: ACEScg, scene_linear: ACEScg}\n" + "file_rules:\n - ! {name: Default, colorspace: default}\n" + "colorspaces:\n" + " - !\n name: ACEScg\n encoding: scene-linear\n" + " aliases: [lin_ap1_scene]\n" + " - ! {name: srgb_rec709_scene, encoding: sdr-video}\n" + " - ! {name: Adobe, aliases: [g22_adobergb_scene]}\n" + " - ! {name: Plain, encoding: scene-linear}\n" + " - !\n name: Mislabeled\n encoding: scene-linear\n" + " aliases: [g22_rec709_scene]\n" + " - ! {name: Data, isdata: true}\n")); + const float ap1[] = { .713f, .293f, .165f, .83f, + .128f, .044f, .32168f, .33767f }; + ColorSpaceInfo saved; + { + ColorConfig config(filename); + OIIO_CHECK_FALSE(config.has_error()); + saved = config.get_color_space_info("scene_linear"); + OIIO_CHECK_ASSERT(saved.valid()); + OIIO_CHECK_EQUAL(saved.transfer_function_gamma(), 1.0f); + OIIO_CHECK_ASSERT(saved.chromaticities() == cspan(ap1)); + auto srgb = config.get_color_space_info("srgb_rec709_scene"); + OIIO_CHECK_EQUAL(srgb.transfer_function_gamma(), 0.0f); + OIIO_CHECK_EQUAL(srgb.chromaticities().size(), 8); + OIIO_CHECK_EQUAL( + config.derive_color_space_info("Adobe").transfer_function_gamma(), + 563.0f / 256.0f); + for (auto name : { "Plain", "Mislabeled" }) { + auto info = config.get_color_space_info(name); + OIIO_CHECK_EQUAL(info.transfer_function_gamma(), 1.0f); + OIIO_CHECK_ASSERT(info.chromaticities().empty()); + } + auto data = config.get_color_space_info("Data"); + OIIO_CHECK_ASSERT(data.valid()); + OIIO_CHECK_ASSERT(data.chromaticities().empty()); + OIIO_CHECK_EQUAL(data.transfer_function_gamma(), 0.0f); + OIIO_CHECK_FALSE(config.get_color_space_info("missing").valid()); + } + // The properties outlive the config; a move leaves its source invalid. + auto copy = saved; + auto moved = std::move(saved); + OIIO_CHECK_FALSE(saved.valid()); + OIIO_CHECK_ASSERT(copy.chromaticities() == cspan(ap1)); + OIIO_CHECK_EQUAL(moved.transfer_function_gamma(), 1.0f); + Filesystem::remove(filename); + + // OpenColorIO 2.5 reads a declared interop_id, which outranks the name. + if (ColorConfig::OpenColorIO_version_hex() >= 0x02050000) { + OIIO_CHECK_ASSERT(Filesystem::write_text_file( + filename, + "ocio_profile_version: 2.5\n" + "roles: {default: g18_rec709_scene}\n" + "file_rules:\n - ! {name: Default, colorspace: default}\n" + "colorspaces:\n - !\n name: g18_rec709_scene\n" + " interop_id: g22_ap1_scene\n")); + ColorConfig config(filename); + OIIO_CHECK_FALSE(config.has_error()); + auto info = config.get_color_space_info("g18_rec709_scene"); + OIIO_CHECK_EQUAL(info.transfer_function_gamma(), 2.2f); + OIIO_CHECK_ASSERT(info.chromaticities() == cspan(ap1)); + Filesystem::remove(filename); + } +} + + + int main(int argc, char* argv[]) { @@ -214,6 +297,7 @@ main(int argc, char* argv[]) test_Rec709_conversion(); test_linear_display_cicp(); test_gamma_pair_conversion(); + test_color_space_info(); return unit_test_failures != 0; } diff --git a/src/python/py_colorconfig.cpp b/src/python/py_colorconfig.cpp index ea267a8f9b..a206c1d240 100644 --- a/src/python/py_colorconfig.cpp +++ b/src/python/py_colorconfig.cpp @@ -14,6 +14,17 @@ namespace PyOpenImageIO { void declare_colorconfig(py_module& m) { + py::class_(m, "ColorSpaceInfo") + .def(py::init<>()) + .def("valid", &ColorSpaceInfo::valid) + .def("transfer_function_gamma", + &ColorSpaceInfo::transfer_function_gamma) + .def("chromaticities", [](const ColorSpaceInfo& self) { + auto xy = self.chromaticities(); + return std::vector(xy.begin(), xy.end()); + }); + + py::class_(m, "ColorConfig") .def(py::init<>()) @@ -195,6 +206,18 @@ declare_colorconfig(py_module& m) } return std::nullopt; }) + .def( + "get_color_space_info", + [](const ColorConfig& self, const std::string& colorspace) { + return self.get_color_space_info(colorspace); + }, + "colorspace"_a) + .def( + "derive_color_space_info", + [](const ColorConfig& self, const std::string& colorspace) { + return self.derive_color_space_info(colorspace); + }, + "colorspace"_a) .def("configname", &ColorConfig::configname) .def_static( "default_colorconfig", diff --git a/src/python/stubs/OpenImageIO/__init__.pyi b/src/python/stubs/OpenImageIO/__init__.pyi index 0e7de1cd65..a740c43a0b 100644 --- a/src/python/stubs/OpenImageIO/__init__.pyi +++ b/src/python/stubs/OpenImageIO/__init__.pyi @@ -171,6 +171,7 @@ class ColorConfig: def configname(self) -> str: ... @staticmethod def default_colorconfig() -> ColorConfig: ... + def derive_color_space_info(self, colorspace: str) -> ColorSpaceInfo: ... def equivalent(self, color_space: str, other_color_space: str) -> bool: ... def filepathOnlyMatchesDefaultRule(self, filepath: str) -> bool: ... def getAliases(self, arg0: str, /) -> list[str]: ... @@ -213,11 +214,18 @@ class ColorConfig: def get_color_interop_id(self, arg0: str, /) -> str: ... @overload def get_color_interop_id(self, arg0, /) -> str: ... + def get_color_space_info(self, colorspace: str) -> ColorSpaceInfo: ... def geterror(self) -> str: ... def isColorSpaceActive(self, name: str) -> bool: ... def parseColorSpaceFromString(self, arg0: str, /) -> str: ... def resolve(self, name: str) -> str: ... +class ColorSpaceInfo: + def __init__(self) -> None: ... + def chromaticities(self) -> list[float]: ... + def transfer_function_gamma(self) -> float: ... + def valid(self) -> bool: ... + class CompareResults: def __init__(self) -> None: ... @property diff --git a/testsuite/python-colorconfig/ref/out-ocio232.txt b/testsuite/python-colorconfig/ref/out-ocio232.txt index 7bdc8137a1..a9d1d949a4 100644 --- a/testsuite/python-colorconfig/ref/out-ocio232.txt +++ b/testsuite/python-colorconfig/ref/out-ocio232.txt @@ -65,5 +65,11 @@ ociodisplay #2 (apply default display/view again): [[[-2.123 0.671 0.8037]] ociodisplay #3 (inverse look): [[[-2.252 0.6714 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #4 (forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #5 (inverse look + forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) +get_color_space_info('ACEScg'): valid=True gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +get_color_space_info('g24_rec709_display'): valid=True gamma=2.4 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +get_color_space_info('srgb_tx'): valid=True gamma=0 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +get_color_space_info('Raw'): valid=True gamma=0 chromaticities=[] +get_color_space_info('no_such_space'): valid=False gamma=0 chromaticities=[] +derive_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] Done. diff --git a/testsuite/python-colorconfig/ref/out-ocio24.txt b/testsuite/python-colorconfig/ref/out-ocio24.txt index 9d35024fcd..35697cf10d 100644 --- a/testsuite/python-colorconfig/ref/out-ocio24.txt +++ b/testsuite/python-colorconfig/ref/out-ocio24.txt @@ -65,5 +65,11 @@ ociodisplay #2 (apply default display/view again): [[[-2.123 0.671 0.8037]] ociodisplay #3 (inverse look): [[[-2.252 0.6714 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #4 (forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #5 (inverse look + forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) +get_color_space_info('ACEScg'): valid=True gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +get_color_space_info('g24_rec709_display'): valid=True gamma=2.4 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +get_color_space_info('srgb_tx'): valid=True gamma=0 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +get_color_space_info('Raw'): valid=True gamma=0 chromaticities=[] +get_color_space_info('no_such_space'): valid=False gamma=0 chromaticities=[] +derive_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] Done. diff --git a/testsuite/python-colorconfig/ref/out-ocio25.txt b/testsuite/python-colorconfig/ref/out-ocio25.txt index 443c7c9e54..35ba307128 100644 --- a/testsuite/python-colorconfig/ref/out-ocio25.txt +++ b/testsuite/python-colorconfig/ref/out-ocio25.txt @@ -65,5 +65,11 @@ ociodisplay #2 (apply default display/view again): [[[-2.123 0.671 0.8037]] ociodisplay #3 (inverse look): [[[-2.252 0.6714 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #4 (forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #5 (inverse look + forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) +get_color_space_info('ACEScg'): valid=True gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +get_color_space_info('g24_rec709_display'): valid=True gamma=2.4 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +get_color_space_info('srgb_tx'): valid=True gamma=0 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +get_color_space_info('Raw'): valid=True gamma=0 chromaticities=[] +get_color_space_info('no_such_space'): valid=False gamma=0 chromaticities=[] +derive_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] Done. diff --git a/testsuite/python-colorconfig/ref/out-ocio26.txt b/testsuite/python-colorconfig/ref/out-ocio26.txt index 1f94528d76..8478a6e3ad 100644 --- a/testsuite/python-colorconfig/ref/out-ocio26.txt +++ b/testsuite/python-colorconfig/ref/out-ocio26.txt @@ -65,5 +65,11 @@ ociodisplay #2 (apply default display/view again): [[[-2.123 0.671 0.8037]] ociodisplay #3 (inverse look): [[[-2.252 0.6714 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #4 (forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #5 (inverse look + forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) +get_color_space_info('ACEScg'): valid=True gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +get_color_space_info('g24_rec709_display'): valid=True gamma=2.4 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +get_color_space_info('srgb_tx'): valid=True gamma=0 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +get_color_space_info('Raw'): valid=True gamma=0 chromaticities=[] +get_color_space_info('no_such_space'): valid=False gamma=0 chromaticities=[] +derive_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] Done. diff --git a/testsuite/python-colorconfig/ref/out.txt b/testsuite/python-colorconfig/ref/out.txt index e223c92c2f..a0aa70aa0f 100644 --- a/testsuite/python-colorconfig/ref/out.txt +++ b/testsuite/python-colorconfig/ref/out.txt @@ -65,5 +65,11 @@ ociodisplay #2 (apply default display/view again): [[[-2.123 0.671 0.8037]] ociodisplay #3 (inverse look): [[[-2.252 0.6714 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #4 (forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #5 (inverse look + forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) +get_color_space_info('ACEScg'): valid=True gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +get_color_space_info('g24_rec709_display'): valid=True gamma=2.4 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +get_color_space_info('srgb_tx'): valid=True gamma=0 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +get_color_space_info('Raw'): valid=True gamma=0 chromaticities=[] +get_color_space_info('no_such_space'): valid=False gamma=0 chromaticities=[] +derive_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] Done. diff --git a/testsuite/python-colorconfig/src/test_colorconfig.py b/testsuite/python-colorconfig/src/test_colorconfig.py index d950447891..82e15889c6 100755 --- a/testsuite/python-colorconfig/src/test_colorconfig.py +++ b/testsuite/python-colorconfig/src/test_colorconfig.py @@ -107,6 +107,12 @@ print (f"ociodisplay #4 (forwards look): {buf.get_pixels(oiio.HALF)} ({buf.spec()['oiio:ColorSpace']})") buf = oiio.ImageBufAlgo.ociodisplay(buf, "", "", colorconfig=str(TEST_CONFIG_PATH), looks="-ACES 1.3 Reference Gamut Compression, +ACES 1.3 Reference Gamut Compression") print (f"ociodisplay #5 (inverse look + forwards look): {buf.get_pixels(oiio.HALF)} ({buf.spec()['oiio:ColorSpace']})") + for name in ("ACEScg", "g24_rec709_display", "srgb_tx", "Raw", "no_such_space"): + info = config.get_color_space_info(name) + print (f"get_color_space_info('{name}'): valid={info.valid()} gamma={info.transfer_function_gamma():g} chromaticities={[round(v, 5) for v in info.chromaticities()]}") + info = config.derive_color_space_info("lin_ap1_scene") + del config + print (f"derive_color_space_info('lin_ap1_scene') after del config: gamma={info.transfer_function_gamma():g} chromaticities={[round(v, 5) for v in info.chromaticities()]}") print ("") print ("Done.") From 8608869f7cec435700ace8c687d9140498f5b311 Mon Sep 17 00:00:00 2001 From: Zach Lewis Date: Thu, 1 Oct 2026 00:28:30 -0400 Subject: [PATCH 2/6] fix(color): resolve names in get_color_space_info() get_color_space_info() and derive_color_space_info() now resolve the requested name as the other ColorConfig queries do, so OIIO's built-in names such as "sRGB" and "linear" select the same color space they do elsewhere. Assisted-by: Claude Code / Claude Opus 5.5 Signed-off-by: Zach Lewis --- src/doc/imageioapi.md | 4 ++-- src/include/OpenImageIO/color.h | 4 ++-- src/libOpenImageIO/color_ocio.cpp | 3 ++- src/libOpenImageIO/color_test.cpp | 3 +++ 4 files changed, 9 insertions(+), 5 deletions(-) diff --git a/src/doc/imageioapi.md b/src/doc/imageioapi.md index ba59a66e55..5530bbe1da 100644 --- a/src/doc/imageioapi.md +++ b/src/doc/imageioapi.md @@ -262,8 +262,8 @@ just exist in the OIIO namespace as general utilities. (See ## Color space properties: `ColorSpaceInfo` `ColorConfig::get_color_space_info()` and -`ColorConfig::derive_color_space_info()` describe a color space, named by a -color space name, alias or role of the config, with a `ColorSpaceInfo`: its RGB +`ColorConfig::derive_color_space_info()` describe a color space, named by any +name `ColorConfig::resolve()` accepts, with a `ColorSpaceInfo`: its RGB primaries and white point, and the exponent of its transfer function when that is a pure power (1.0 for a linear one). Either property may be unavailable. They are taken from what the config declares: a `scene-linear` or diff --git a/src/include/OpenImageIO/color.h b/src/include/OpenImageIO/color.h index 95b43c2b40..587967b762 100644 --- a/src/include/OpenImageIO/color.h +++ b/src/include/OpenImageIO/color.h @@ -481,8 +481,8 @@ class OIIO_API ColorConfig { /// when it declares none, as its name or one of its aliases. An ID whose /// transfer function contradicts a declared linear encoding is ignored. /// A data color space is valid but has no properties. `colorspace` may be - /// a color space name, alias or role of this config; a name that does - /// not resolve returns an invalid ColorSpaceInfo. + /// any name `resolve()` accepts; a name that does not resolve to a color + /// space of this config returns an invalid ColorSpaceInfo. /// /// @version 3.3 OIIO_NODISCARD ColorSpaceInfo diff --git a/src/libOpenImageIO/color_ocio.cpp b/src/libOpenImageIO/color_ocio.cpp index 2b9f9d2652..28dfc6b834 100644 --- a/src/libOpenImageIO/color_ocio.cpp +++ b/src/libOpenImageIO/color_ocio.cpp @@ -2605,6 +2605,7 @@ constexpr PublishedColorSpace published_color_spaces[] = { const PublishedColorSpace* find_published_color_space(string_view interop_id) { + // Any case, as OCIO name lookups and get_color_interop_id() match names. for (const auto& published : published_color_spaces) if (Strutil::iequals(interop_id, published.interop_id)) return &published; @@ -2621,7 +2622,7 @@ ColorConfig::get_color_space_info(string_view colorspace) const OCIO::ConstColorSpaceRcPtr cs; if (getImpl()->config_ && !disable_ocio && !colorspace.empty()) { try { - cs = getImpl()->config_->getColorSpace(c_str(colorspace)); + cs = getImpl()->config_->getColorSpace(c_str(resolve(colorspace))); } catch (const std::exception& e) { DBG("OCIO exception in get_color_space_info: {}\n", e.what()); } diff --git a/src/libOpenImageIO/color_test.cpp b/src/libOpenImageIO/color_test.cpp index ea8d25db22..869d3c2a92 100644 --- a/src/libOpenImageIO/color_test.cpp +++ b/src/libOpenImageIO/color_test.cpp @@ -237,6 +237,9 @@ test_color_space_info() auto srgb = config.get_color_space_info("srgb_rec709_scene"); OIIO_CHECK_EQUAL(srgb.transfer_function_gamma(), 0.0f); OIIO_CHECK_EQUAL(srgb.chromaticities().size(), 8); + // OIIO's built-in names resolve as they do in the other queries. + OIIO_CHECK_EQUAL( + config.get_color_space_info("sRGB").chromaticities().size(), 8); OIIO_CHECK_EQUAL( config.derive_color_space_info("Adobe").transfer_function_gamma(), 563.0f / 256.0f); From e33403b8b2a3490789745ee8f4b7799aef339134 Mon Sep 17 00:00:00 2001 From: Zach Lewis Date: Mon, 5 Oct 2026 11:20:27 -0400 Subject: [PATCH 3/6] fix(color): describe color spaces through get_color_interop_id() get_color_space_info() now identifies a color space the way get_cicp() does, through get_color_interop_id(), so the two accept the same names: the legacy aliases of older configs, such as lin_ap1 and srgb_texture, now count, and so does a Color Interop ID the config doesn't define. The chromaticities and gamma move into the existing ColorInteropID table, which replaces the separate table. Assisted-by: Claude Code / Claude Opus 5.5 Signed-off-by: Zach Lewis --- src/doc/imageioapi.md | 20 +-- src/doc/pythonbindings.md | 7 +- src/include/OpenImageIO/color.h | 16 +-- src/libOpenImageIO/color_ocio.cpp | 213 ++++++++++++------------------ src/libOpenImageIO/color_test.cpp | 25 +++- 5 files changed, 131 insertions(+), 150 deletions(-) diff --git a/src/doc/imageioapi.md b/src/doc/imageioapi.md index 5530bbe1da..1f62b067e1 100644 --- a/src/doc/imageioapi.md +++ b/src/doc/imageioapi.md @@ -263,16 +263,16 @@ just exist in the OIIO namespace as general utilities. (See `ColorConfig::get_color_space_info()` and `ColorConfig::derive_color_space_info()` describe a color space, named by any -name `ColorConfig::resolve()` accepts, with a `ColorSpaceInfo`: its RGB -primaries and white point, and the exponent of its transfer function when that -is a pure power (1.0 for a linear one). Either property may be unavailable. -They are taken from what the config declares: a `scene-linear` or -`display-linear` encoding gives gamma 1.0, and a published Color Interop ID the -config declares for the color space, as its `interop_id` (OpenColorIO 2.5 or -newer) or as its name or one of its aliases, gives that ID's primaries and -transfer function. For example, a color space declared as `lin_ap1_scene` -reports gamma 1.0 and the ACES AP1 primaries, and one declared as -`g22_rec709_scene` gamma 2.2 and the Rec.709 primaries. The sRGB curve is +name `ColorConfig::get_color_interop_id()` and `ColorConfig::get_cicp()` +accept, with a `ColorSpaceInfo`: its RGB primaries and white point, and the +exponent of its transfer function when that is a pure power (1.0 for a linear +one). Either property may be unavailable. They are taken from what is already +known about the color space: a `scene-linear` or `display-linear` encoding +gives gamma 1.0, and the Color Interop ID that `get_color_interop_id()` finds +for it gives that ID's primaries and transfer function. For example, a color +space whose ID is `lin_ap1_scene` (or the legacy `lin_ap1`) reports gamma 1.0 +and the ACES AP1 primaries, and one whose ID is `g22_rec709_scene` gamma 2.2 +and the Rec.709 primaries. The sRGB curve is piecewise, so `srgb_rec709_scene` reports gamma 0. `derive_color_space_info()` may also derive properties the config does not declare by analyzing the color space's transforms; currently it derives nothing more. Neither call changes the diff --git a/src/doc/pythonbindings.md b/src/doc/pythonbindings.md index 4952e2209f..bfa946fc93 100644 --- a/src/doc/pythonbindings.md +++ b/src/doc/pythonbindings.md @@ -4041,9 +4041,10 @@ is provided for minimal color support. % TODO: The documentation for this class is incomplete. :::{py:method} get_color_space_info (colorspace) -Return a `ColorSpaceInfo` with the properties of the named color space that -the config declares, without analyzing its transforms. A name that does not -resolve returns an invalid `ColorSpaceInfo`. See {ref}`sec-colorspaceinfo`. +Return a `ColorSpaceInfo` with the properties already known for the named +color space, without analyzing its transforms. It accepts the same names as +`get_color_interop_id()` and `get_cicp()`; any other name returns an invalid +`ColorSpaceInfo`. See {ref}`sec-colorspaceinfo`. This function was added in OpenImageIO 3.3. ::: diff --git a/src/include/OpenImageIO/color.h b/src/include/OpenImageIO/color.h index 587967b762..900df54c36 100644 --- a/src/include/OpenImageIO/color.h +++ b/src/include/OpenImageIO/color.h @@ -475,14 +475,14 @@ class OIIO_API ColorConfig { /// Return the properties of a color space that are already known, /// without analyzing its transforms: gamma 1.0 for a color space whose /// config declares a scene-linear or display-linear encoding, and the - /// primaries and transfer function of a published Color Interop ID - /// (https://github.com/AcademySoftwareFoundation/ColorInterop) the config - /// declares for it, as its interop_id (OpenColorIO 2.5 or newer) or, - /// when it declares none, as its name or one of its aliases. An ID whose - /// transfer function contradicts a declared linear encoding is ignored. - /// A data color space is valid but has no properties. `colorspace` may be - /// any name `resolve()` accepts; a name that does not resolve to a color - /// space of this config returns an invalid ColorSpaceInfo. + /// primaries and transfer function of the Color Interop ID + /// (https://github.com/AcademySoftwareFoundation/ColorInterop) that + /// `get_color_interop_id()` finds for it. An ID whose transfer function + /// contradicts a declared linear encoding is ignored. A data color space + /// is valid but has no properties. `colorspace` may be any name + /// `get_color_interop_id()` and `get_cicp()` accept: a color space of + /// this config, or a Color Interop ID it need not define. Any other name + /// returns an invalid ColorSpaceInfo. /// /// @version 3.3 OIIO_NODISCARD ColorSpaceInfo diff --git a/src/libOpenImageIO/color_ocio.cpp b/src/libOpenImageIO/color_ocio.cpp index 28dfc6b834..d163e10065 100644 --- a/src/libOpenImageIO/color_ocio.cpp +++ b/src/libOpenImageIO/color_ocio.cpp @@ -2339,23 +2339,53 @@ enum class CICPRange : int { Full = 1, }; +// Chromaticities (Rx, Ry, Gx, Gy, Bx, By, Wx, Wy) of the published gamuts. +constexpr float xy_rec709[] = { .64f, .33f, .30f, .60f, + .15f, .06f, .3127f, .329f }; +constexpr float xy_p3d65[] = { .68f, .32f, .265f, .69f, + .15f, .06f, .3127f, .329f }; +constexpr float xy_rec2020[] = { .708f, .292f, .17f, .797f, + .131f, .046f, .3127f, .329f }; +constexpr float xy_adobergb[] = { .64f, .33f, .21f, .71f, + .15f, .06f, .3127f, .329f }; +constexpr float xy_ap0[] = { .7347f, .2653f, 0.0f, 1.0f, + .0001f, -.077f, .32168f, .33767f }; +constexpr float xy_ap1[] = { .713f, .293f, .165f, .83f, + .128f, .044f, .32168f, .33767f }; +// CIE XYZ taken as RGB, so R = G = B is illuminant E. +constexpr float xy_xyz[] = { 1.0f, 0.0f, 0.0f, 1.0f, + 0.0f, 0.0f, 1.0f / 3, 1.0f / 3 }; + +// A color interop ID with what OIIO knows about it: a legacy alias for older +// configs, its CICP code if it has one, and its chromaticities (Rx, Ry, Gx, Gy, +// Bx, By, Wx, Wy) and gamma if it has RGB primaries. Gamma is 1 for linear, +// the exponent of a pure power, or 0 for any other curve (sRGB, extended +// sRGB, PQ, HLG). struct ColorInteropID { - constexpr ColorInteropID(const char* interop_id, const char* legacy_alias) + constexpr ColorInteropID(const char* interop_id, const char* legacy_alias, + const float* chromaticities = nullptr, + float gamma = 0.0f) : interop_id(interop_id) , legacy_alias(legacy_alias) , cicp({ 0, 0, 0, 0 }) , has_cicp(false) + , chromaticities(chromaticities) + , gamma(gamma) { } constexpr ColorInteropID(const char* interop_id, const char* legacy_alias, CICPPrimaries primaries, CICPTransfer transfer, - CICPMatrix matrix) + CICPMatrix matrix, + const float* chromaticities = nullptr, + float gamma = 0.0f) : interop_id(interop_id) , legacy_alias(legacy_alias) , cicp({ int(primaries), int(transfer), int(matrix), int(CICPRange::Full) }) , has_cicp(true) + , chromaticities(chromaticities) + , gamma(gamma) { } @@ -2363,6 +2393,8 @@ struct ColorInteropID { const char* legacy_alias; std::array cicp; bool has_cicp; + const float* chromaticities; + float gamma; }; // Mapping between color interop ID and CICP, based on Color Interop Forum @@ -2371,28 +2403,28 @@ constexpr ColorInteropID color_interop_ids[] = { // Scene referred interop IDs first so they are the default in automatic // conversion from CICP to interop ID. Some are not display color spaces // at all, but can be represented by CICP anyway. - { "lin_ap1_scene", "lin_ap1" }, - { "lin_ap0_scene", "lin_ap0" }, + { "lin_ap1_scene", "lin_ap1", xy_ap1, 1.0f }, + { "lin_ap0_scene", "lin_ap0", xy_ap0, 1.0f }, { "lin_rec709_scene", "lin_rec709", CICPPrimaries::Rec709, - CICPTransfer::Linear, CICPMatrix::BT709 }, + CICPTransfer::Linear, CICPMatrix::BT709, xy_rec709, 1.0f }, { "lin_p3d65_scene", "lin_p3d65", CICPPrimaries::P3D65, - CICPTransfer::Linear, CICPMatrix::BT709 }, + CICPTransfer::Linear, CICPMatrix::BT709, xy_p3d65, 1.0f }, { "lin_rec2020_scene", "lin_rec2020", CICPPrimaries::Rec2020, - CICPTransfer::Linear, CICPMatrix::Rec2020_CL }, - { "lin_adobergb_scene", "lin_adobergb" }, + CICPTransfer::Linear, CICPMatrix::Rec2020_CL, xy_rec2020, 1.0f }, + { "lin_adobergb_scene", "lin_adobergb", xy_adobergb, 1.0f }, { "lin_ciexyzd65_scene", "cie_xyz_d65", CICPPrimaries::XYZD65, - CICPTransfer::Linear, CICPMatrix::Unspecified }, + CICPTransfer::Linear, CICPMatrix::Unspecified, xy_xyz, 1.0f }, { "srgb_rec709_scene", "srgb_texture", CICPPrimaries::Rec709, - CICPTransfer::sRGB, CICPMatrix::BT709 }, - { "g24_rec709_scene", "g24_rec709" }, + CICPTransfer::sRGB, CICPMatrix::BT709, xy_rec709, 0.0f }, + { "g24_rec709_scene", "g24_rec709", xy_rec709, 2.4f }, { "g22_rec709_scene", "g22_rec709", CICPPrimaries::Rec709, - CICPTransfer::Gamma22, CICPMatrix::BT709 }, - { "g18_rec709_scene", "g18_rec709" }, - { "srgb_ap1_scene", "srgb_ap1" }, - { "g22_ap1_scene", "g22_ap1" }, + CICPTransfer::Gamma22, CICPMatrix::BT709, xy_rec709, 2.2f }, + { "g18_rec709_scene", "g18_rec709", xy_rec709, 1.8f }, + { "srgb_ap1_scene", "srgb_ap1", xy_ap1, 0.0f }, + { "g22_ap1_scene", "g22_ap1", xy_ap1, 2.2f }, { "srgb_p3d65_scene", "srgb_p3d65", CICPPrimaries::P3D65, - CICPTransfer::sRGB, CICPMatrix::BT709 }, - { "g22_adobergb_scene", nullptr }, + CICPTransfer::sRGB, CICPMatrix::BT709, xy_p3d65, 0.0f }, + { "g22_adobergb_scene", nullptr, xy_adobergb, 563.0f / 256.0f }, { "data", nullptr }, { "unknown", nullptr }, @@ -2400,37 +2432,39 @@ constexpr ColorInteropID color_interop_ids[] = { // The linear displays share their scene rows' codes, which come first, // so CICP input still reads as the scene identity. { "lin_rec709_display", nullptr, CICPPrimaries::Rec709, - CICPTransfer::Linear, CICPMatrix::BT709 }, + CICPTransfer::Linear, CICPMatrix::BT709, xy_rec709, 1.0f }, { "lin_p3d65_display", nullptr, CICPPrimaries::P3D65, CICPTransfer::Linear, - CICPMatrix::BT709 }, + CICPMatrix::BT709, xy_p3d65, 1.0f }, { "lin_rec2020_display", nullptr, CICPPrimaries::Rec2020, - CICPTransfer::Linear, CICPMatrix::Rec2020_CL }, + CICPTransfer::Linear, CICPMatrix::Rec2020_CL, xy_rec2020, 1.0f }, { "srgb_rec709_display", "srgb_display", CICPPrimaries::Rec709, - CICPTransfer::sRGB, CICPMatrix::BT709 }, + CICPTransfer::sRGB, CICPMatrix::BT709, xy_rec709, 0.0f }, // Not all software interprets this CICP the same, see the // "QuickTime Gamma Shift" issue. We follow the CIF recommendation and // interpret it as BT.1886. { "g24_rec709_display", "rec1886_rec709_display", CICPPrimaries::Rec709, - CICPTransfer::BT709, CICPMatrix::BT709 }, + CICPTransfer::BT709, CICPMatrix::BT709, xy_rec709, 2.4f }, { "srgb_p3d65_display", "displayp3_display", CICPPrimaries::P3D65, - CICPTransfer::sRGB, CICPMatrix::BT709 }, + CICPTransfer::sRGB, CICPMatrix::BT709, xy_p3d65, 0.0f }, { "srgbe_p3d65_display", "displayp3_hdr_display", CICPPrimaries::P3D65, - CICPTransfer::sRGB, CICPMatrix::BT709 }, + CICPTransfer::sRGB, CICPMatrix::BT709, xy_p3d65, 0.0f }, { "pq_p3d65_display", "st2084_p3d65_display", CICPPrimaries::P3D65, - CICPTransfer::PQ, CICPMatrix::Rec2020_NCL }, + CICPTransfer::PQ, CICPMatrix::Rec2020_NCL, xy_p3d65, 0.0f }, { "pq_rec2020_display", "rec2100_pq_display", CICPPrimaries::Rec2020, - CICPTransfer::PQ, CICPMatrix::Rec2020_NCL }, + CICPTransfer::PQ, CICPMatrix::Rec2020_NCL, xy_rec2020, 0.0f }, { "hlg_rec2020_display", "rec2100_hlg_display", CICPPrimaries::Rec2020, - CICPTransfer::HLG, CICPMatrix::Rec2020_NCL }, - // No CICP mapping to keep previous behavior unchanged, as Gamma 2.2 - // display is more likely meant to be written as sRGB. On read the - // scene referred interop ID will be used. - { "g22_rec709_display", nullptr - /* CICPPrimaries::Rec709, CICPTransfer::Gamma22, CICPMatrix::BT709 */ }, + CICPTransfer::HLG, CICPMatrix::Rec2020_NCL, xy_rec2020, 0.0f }, + // No CICP mapping (Rec709, Gamma22, BT709) to keep previous behavior + // unchanged, as Gamma 2.2 display is more likely meant to be written as + // sRGB. On read the scene referred interop ID will be used. + { "g22_rec709_display", nullptr, xy_rec709, 2.2f }, // No CICP code for Adobe RGB primaries. - { "g22_adobergb_display", nullptr }, + { "g22_adobergb_display", nullptr, xy_adobergb, 563.0f / 256.0f }, { "g26_p3d65_display", "p3d65_display", CICPPrimaries::P3D65, - CICPTransfer::Gamma26, CICPMatrix::BT709 }, + CICPTransfer::Gamma26, CICPMatrix::BT709, xy_p3d65, 2.6f }, + // The DCDM XYZ encodings have no properties: neither curve is a pure + // power (DCI headroom scaling precedes the 2.6 power), and no RGB + // primaries are reported for display XYZ encodings. { "g26_xyzd65_display", nullptr, CICPPrimaries::XYZD65, CICPTransfer::Gamma26, CICPMatrix::Unspecified }, { "pq_xyzd65_display", nullptr, CICPPrimaries::XYZD65, CICPTransfer::PQ, @@ -2541,84 +2575,12 @@ ColorSpaceInfo::transfer_function_gamma() const noexcept -namespace { - -// Chromaticities (Rx, Ry, Gx, Gy, Bx, By, Wx, Wy) of the published gamuts. -constexpr float xy_rec709[] = { .64f, .33f, .30f, .60f, - .15f, .06f, .3127f, .329f }; -constexpr float xy_p3d65[] = { .68f, .32f, .265f, .69f, - .15f, .06f, .3127f, .329f }; -constexpr float xy_rec2020[] = { .708f, .292f, .17f, .797f, - .131f, .046f, .3127f, .329f }; -constexpr float xy_adobergb[] = { .64f, .33f, .21f, .71f, - .15f, .06f, .3127f, .329f }; -constexpr float xy_ap0[] = { .7347f, .2653f, 0.0f, 1.0f, - .0001f, -.077f, .32168f, .33767f }; -constexpr float xy_ap1[] = { .713f, .293f, .165f, .83f, - .128f, .044f, .32168f, .33767f }; -// CIE XYZ taken as RGB, so R = G = B is illuminant E. -constexpr float xy_xyz[] = { 1.0f, 0.0f, 0.0f, 1.0f, - 0.0f, 0.0f, 1.0f / 3, 1.0f / 3 }; - -// Properties of the Color Interop Forum's published (unnamespaced) IDs: -// gamma is 1 for linear, the exponent of a pure power, or 0 for any other -// curve (sRGB, extended sRGB, PQ, HLG). The DCDM XYZ encodings -// g26_xyzd65_display and pq_xyzd65_display have no entry: neither curve is a -// pure power (DCI headroom scaling precedes the 2.6 power), and no RGB -// primaries are reported for these display XYZ encodings. -struct PublishedColorSpace { - const char* interop_id; - const float* chromaticities; - float gamma; -}; -constexpr PublishedColorSpace published_color_spaces[] = { - { "lin_ap1_scene", xy_ap1, 1.0f }, - { "lin_ap0_scene", xy_ap0, 1.0f }, - { "lin_rec709_scene", xy_rec709, 1.0f }, - { "lin_p3d65_scene", xy_p3d65, 1.0f }, - { "lin_rec2020_scene", xy_rec2020, 1.0f }, - { "lin_adobergb_scene", xy_adobergb, 1.0f }, - { "lin_ciexyzd65_scene", xy_xyz, 1.0f }, - { "srgb_rec709_scene", xy_rec709, 0.0f }, - { "g24_rec709_scene", xy_rec709, 2.4f }, - { "g22_rec709_scene", xy_rec709, 2.2f }, - { "g18_rec709_scene", xy_rec709, 1.8f }, - { "srgb_ap1_scene", xy_ap1, 0.0f }, - { "g22_ap1_scene", xy_ap1, 2.2f }, - { "srgb_p3d65_scene", xy_p3d65, 0.0f }, - { "g22_adobergb_scene", xy_adobergb, 563.0f / 256.0f }, - { "srgb_rec709_display", xy_rec709, 0.0f }, - { "g24_rec709_display", xy_rec709, 2.4f }, - { "srgb_p3d65_display", xy_p3d65, 0.0f }, - { "srgbe_p3d65_display", xy_p3d65, 0.0f }, - { "pq_p3d65_display", xy_p3d65, 0.0f }, - { "pq_rec2020_display", xy_rec2020, 0.0f }, - { "hlg_rec2020_display", xy_rec2020, 0.0f }, - { "g22_rec709_display", xy_rec709, 2.2f }, - { "g22_adobergb_display", xy_adobergb, 563.0f / 256.0f }, - { "g26_p3d65_display", xy_p3d65, 2.6f }, - { "lin_rec709_display", xy_rec709, 1.0f }, - { "lin_p3d65_display", xy_p3d65, 1.0f }, - { "lin_rec2020_display", xy_rec2020, 1.0f }, -}; - -const PublishedColorSpace* -find_published_color_space(string_view interop_id) -{ - // Any case, as OCIO name lookups and get_color_interop_id() match names. - for (const auto& published : published_color_spaces) - if (Strutil::iequals(interop_id, published.interop_id)) - return &published; - return nullptr; -} - -} // namespace - - - ColorSpaceInfo ColorConfig::get_color_space_info(string_view colorspace) const { + // Identify the space as get_cicp() does, so that every name it accepts + // gives the same answer here. + const string_view interop_id = get_color_interop_id(colorspace); OCIO::ConstColorSpaceRcPtr cs; if (getImpl()->config_ && !disable_ocio && !colorspace.empty()) { try { @@ -2627,32 +2589,27 @@ ColorConfig::get_color_space_info(string_view colorspace) const DBG("OCIO exception in get_color_space_info: {}\n", e.what()); } } - if (!cs) + if (!cs && interop_id.empty()) return {}; ColorSpaceInfo result; auto impl = std::make_shared(); result.m_impl = impl; - if (cs->isData()) + if (cs && cs->isData()) return result; - const string_view encoding(cs->getEncoding()); + const string_view encoding(cs ? cs->getEncoding() : ""); const bool linear = encoding == "scene-linear" || encoding == "display-linear"; - // The ID the config declares for this space, else its name or an alias. - const PublishedColorSpace* published = nullptr; - string_view declared; -#if OCIO_VERSION_HEX >= MAKE_OCIO_VERSION_HEX(2, 5, 0) - declared = cs->getInteropID(); - published = find_published_color_space(declared); -#endif - if (declared.empty()) { - published = find_published_color_space(cs->getName()); - for (int i = 0, n = cs->getNumAliases(); !published && i < n; ++i) - published = find_published_color_space(cs->getAlias(i)); - } - if (published && !(linear && published->gamma != 1.0f)) { - std::copy_n(published->chromaticities, 8, impl->chromaticities.data()); - impl->has_chromaticities = true; - impl->gamma = published->gamma; + for (const ColorInteropID& interop : color_interop_ids) { + if (interop.chromaticities && interop_id == interop.interop_id) { + // An ID contradicting a declared linear encoding is ignored. + if (!(linear && interop.gamma != 1.0f)) { + std::copy_n(interop.chromaticities, 8, + impl->chromaticities.data()); + impl->has_chromaticities = true; + impl->gamma = interop.gamma; + } + break; + } } if (linear) impl->gamma = 1.0f; diff --git a/src/libOpenImageIO/color_test.cpp b/src/libOpenImageIO/color_test.cpp index 869d3c2a92..4ca526ce99 100644 --- a/src/libOpenImageIO/color_test.cpp +++ b/src/libOpenImageIO/color_test.cpp @@ -223,9 +223,11 @@ test_color_space_info() " - ! {name: Plain, encoding: scene-linear}\n" " - !\n name: Mislabeled\n encoding: scene-linear\n" " aliases: [g22_rec709_scene]\n" - " - ! {name: Data, isdata: true}\n")); + " - ! {name: Data, isdata: true}\n" + " - ! {name: Texture, aliases: [srgb_texture]}\n")); const float ap1[] = { .713f, .293f, .165f, .83f, .128f, .044f, .32168f, .33767f }; + const float p3d65[] = { .68f, .32f, .265f, .69f, .15f, .06f, .3127f, .329f }; ColorSpaceInfo saved; { ColorConfig config(filename); @@ -253,6 +255,27 @@ test_color_space_info() OIIO_CHECK_ASSERT(data.chromaticities().empty()); OIIO_CHECK_EQUAL(data.transfer_function_gamma(), 0.0f); OIIO_CHECK_FALSE(config.get_color_space_info("missing").valid()); + // A legacy alias, and IDs this config doesn't define, answer as they + // do for get_color_interop_id() and get_cicp(). + OIIO_CHECK_EQUAL(config.get_color_interop_id("Texture"), + "srgb_rec709_scene"); + auto texture = config.get_color_space_info("Texture"); + OIIO_CHECK_EQUAL(texture.transfer_function_gamma(), 0.0f); + OIIO_CHECK_EQUAL(texture.chromaticities().size(), 8); + auto p3 = config.get_color_space_info("lin_p3d65_display"); + OIIO_CHECK_EQUAL(p3.transfer_function_gamma(), 1.0f); + OIIO_CHECK_ASSERT(p3.chromaticities() == cspan(p3d65)); + OIIO_CHECK_ASSERT(config.get_color_space_info("data").valid()); + } + // OpenColorIO 2.3's built-in CG config gives its spaces only the legacy + // aliases, which identify them here too. + { + ColorConfig config("ocio://cg-config-v2.1.0_aces-v1.3_ocio-v2.3"); + OIIO_CHECK_ASSERT(config.get_color_space_info("ACEScg").chromaticities() + == cspan(ap1)); + auto texture = config.get_color_space_info("sRGB - Texture"); + OIIO_CHECK_EQUAL(texture.chromaticities().size(), 8); + OIIO_CHECK_EQUAL(texture.transfer_function_gamma(), 0.0f); } // The properties outlive the config; a move leaves its source invalid. auto copy = saved; From 36cbfe3cd02e280f37372830a0c9d33ac4580099 Mon Sep 17 00:00:00 2001 From: Zach Lewis Date: Mon, 5 Oct 2026 15:57:49 -0400 Subject: [PATCH 4/6] refactor(color): keep one get_color_space_info() and shorten its docs derive_color_space_info() returned exactly what get_color_space_info() returns, and two queries left callers guessing which to use. Transform analysis, when it arrives, extends get_color_space_info() instead, as it will get_color_interop_id() and get_cicp(). The descriptions now say what the function is for and leave the gamma details to transfer_function_gamma(). Also drops the ColorSpaceInfoAccess friend declaration, which named a struct this PR never defines. Assisted-by: Claude Code / Claude Opus 5.5 Signed-off-by: Zach Lewis --- src/doc/imageioapi.md | 23 ++------------- src/doc/pythonbindings.md | 19 ++++--------- src/include/OpenImageIO/color.h | 28 ++++--------------- src/libOpenImageIO/color_ocio.cpp | 9 ------ src/libOpenImageIO/color_test.cpp | 2 +- src/python/py_colorconfig.cpp | 6 ---- src/python/stubs/OpenImageIO/__init__.pyi | 1 - .../python-colorconfig/ref/out-ocio232.txt | 2 +- .../python-colorconfig/ref/out-ocio24.txt | 2 +- .../python-colorconfig/ref/out-ocio25.txt | 2 +- .../python-colorconfig/ref/out-ocio26.txt | 2 +- testsuite/python-colorconfig/ref/out.txt | 2 +- .../src/test_colorconfig.py | 4 +-- 13 files changed, 23 insertions(+), 79 deletions(-) diff --git a/src/doc/imageioapi.md b/src/doc/imageioapi.md index 1f62b067e1..001aafb21e 100644 --- a/src/doc/imageioapi.md +++ b/src/doc/imageioapi.md @@ -261,23 +261,9 @@ just exist in the OIIO namespace as general utilities. (See ## Color space properties: `ColorSpaceInfo` -`ColorConfig::get_color_space_info()` and -`ColorConfig::derive_color_space_info()` describe a color space, named by any -name `ColorConfig::get_color_interop_id()` and `ColorConfig::get_cicp()` -accept, with a `ColorSpaceInfo`: its RGB primaries and white point, and the -exponent of its transfer function when that is a pure power (1.0 for a linear -one). Either property may be unavailable. They are taken from what is already -known about the color space: a `scene-linear` or `display-linear` encoding -gives gamma 1.0, and the Color Interop ID that `get_color_interop_id()` finds -for it gives that ID's primaries and transfer function. For example, a color -space whose ID is `lin_ap1_scene` (or the legacy `lin_ap1`) reports gamma 1.0 -and the ACES AP1 primaries, and one whose ID is `g22_rec709_scene` gamma 2.2 -and the Rec.709 primaries. The sRGB curve is -piecewise, so `srgb_rec709_scene` reports gamma 0. `derive_color_space_info()` -may also derive properties the config does not declare by analyzing the color -space's transforms; currently it derives nothing more. Neither call changes the -config, an image, or its metadata. See {ref}`sec-pythoncolorconfig` for the -Python bindings. +`ColorConfig::get_color_space_info()` returns a `ColorSpaceInfo` describing a +color space's primaries and transfer function. See {ref}`sec-pythoncolorconfig` +for the Python bindings. :::{doxygenclass} OIIO::ColorSpaceInfo :members: @@ -286,9 +272,6 @@ Python bindings. :::{doxygenfunction} OIIO::ColorConfig::get_color_space_info ::: -:::{doxygenfunction} OIIO::ColorConfig::derive_color_space_info -::: - > (sec-startupshutdown)= ## Startup and Shutdown diff --git a/src/doc/pythonbindings.md b/src/doc/pythonbindings.md index bfa946fc93..92664eed6e 100644 --- a/src/doc/pythonbindings.md +++ b/src/doc/pythonbindings.md @@ -4041,23 +4041,16 @@ is provided for minimal color support. % TODO: The documentation for this class is incomplete. :::{py:method} get_color_space_info (colorspace) -Return a `ColorSpaceInfo` with the properties already known for the named -color space, without analyzing its transforms. It accepts the same names as -`get_color_interop_id()` and `get_cicp()`; any other name returns an invalid -`ColorSpaceInfo`. See {ref}`sec-colorspaceinfo`. - -This function was added in OpenImageIO 3.3. -::: - -:::{py:method} derive_color_space_info (colorspace) -Return a `ColorSpaceInfo` with the properties of the named color space, as -`get_color_space_info()` does, plus any that can be derived by analyzing its -transforms. Currently it derives nothing more. +Get color space information for a color space with a known Color Interop ID, +as `get_color_interop_id()` finds it. Properties that cannot be determined are +left unavailable. It accepts the same names as `get_color_interop_id()` and +`get_cicp()`; any other name returns an invalid `ColorSpaceInfo`. See +{ref}`sec-colorspaceinfo`. Example: ```python -info = colorconfig.derive_color_space_info("ACEScg") +info = colorconfig.get_color_space_info("ACEScg") if info.valid(): gamma = info.transfer_function_gamma() # 1.0 for a linear encoding xy = info.chromaticities() # [] if the primaries are unknown diff --git a/src/include/OpenImageIO/color.h b/src/include/OpenImageIO/color.h index 900df54c36..4543717626 100644 --- a/src/include/OpenImageIO/color.h +++ b/src/include/OpenImageIO/color.h @@ -64,8 +64,7 @@ using ColorProcessorHandle = std::shared_ptr; /// Immutable, owning properties of a color space, returned by -/// `ColorConfig::get_color_space_info()` and -/// `ColorConfig::derive_color_space_info()`. Copies share their storage and +/// `ColorConfig::get_color_space_info()`. Copies share their storage and /// remain usable after the originating ColorConfig resets or is destroyed. /// /// @version 3.3 @@ -95,7 +94,6 @@ class OIIO_API ColorSpaceInfo { private: friend class ColorConfig; - friend struct ColorSpaceInfoAccess; struct Impl; std::shared_ptr m_impl; }; @@ -472,31 +470,17 @@ class OIIO_API ColorConfig { OIIO_NODISCARD string_view get_color_interop_id(string_view colorspace) const; - /// Return the properties of a color space that are already known, - /// without analyzing its transforms: gamma 1.0 for a color space whose - /// config declares a scene-linear or display-linear encoding, and the - /// primaries and transfer function of the Color Interop ID - /// (https://github.com/AcademySoftwareFoundation/ColorInterop) that - /// `get_color_interop_id()` finds for it. An ID whose transfer function - /// contradicts a declared linear encoding is ignored. A data color space - /// is valid but has no properties. `colorspace` may be any name - /// `get_color_interop_id()` and `get_cicp()` accept: a color space of - /// this config, or a Color Interop ID it need not define. Any other name + /// Get color space information for a color space with a known Color + /// Interop ID (https://github.com/AcademySoftwareFoundation/ColorInterop), + /// as `get_color_interop_id()` finds it. Properties that cannot be + /// determined are left unavailable. `colorspace` may be any name + /// `get_color_interop_id()` and `get_cicp()` accept; any other name /// returns an invalid ColorSpaceInfo. /// /// @version 3.3 OIIO_NODISCARD ColorSpaceInfo get_color_space_info(string_view colorspace) const; - /// Return the properties of a color space, as `get_color_space_info()` - /// does, plus any the config does not declare that can be derived by - /// analyzing the color space's transforms. Currently it derives nothing - /// beyond what `get_color_space_info()` returns. - /// - /// @version 3.3 - OIIO_NODISCARD ColorSpaceInfo - derive_color_space_info(string_view colorspace) const; - /// Find color interop ID corresponding to the CICP code. /// Returns empty string if not found. /// diff --git a/src/libOpenImageIO/color_ocio.cpp b/src/libOpenImageIO/color_ocio.cpp index d163e10065..400d2e632c 100644 --- a/src/libOpenImageIO/color_ocio.cpp +++ b/src/libOpenImageIO/color_ocio.cpp @@ -2617,15 +2617,6 @@ ColorConfig::get_color_space_info(string_view colorspace) const } - -ColorSpaceInfo -ColorConfig::derive_color_space_info(string_view colorspace) const -{ - // No transform analysis yet, so there is nothing more to derive. - return get_color_space_info(colorspace); -} - - ////////////////////////////////////////////////////////////////////////// // // Image Processing Implementations diff --git a/src/libOpenImageIO/color_test.cpp b/src/libOpenImageIO/color_test.cpp index 4ca526ce99..33065feee9 100644 --- a/src/libOpenImageIO/color_test.cpp +++ b/src/libOpenImageIO/color_test.cpp @@ -243,7 +243,7 @@ test_color_space_info() OIIO_CHECK_EQUAL( config.get_color_space_info("sRGB").chromaticities().size(), 8); OIIO_CHECK_EQUAL( - config.derive_color_space_info("Adobe").transfer_function_gamma(), + config.get_color_space_info("Adobe").transfer_function_gamma(), 563.0f / 256.0f); for (auto name : { "Plain", "Mislabeled" }) { auto info = config.get_color_space_info(name); diff --git a/src/python/py_colorconfig.cpp b/src/python/py_colorconfig.cpp index a206c1d240..5773bfd086 100644 --- a/src/python/py_colorconfig.cpp +++ b/src/python/py_colorconfig.cpp @@ -212,12 +212,6 @@ declare_colorconfig(py_module& m) return self.get_color_space_info(colorspace); }, "colorspace"_a) - .def( - "derive_color_space_info", - [](const ColorConfig& self, const std::string& colorspace) { - return self.derive_color_space_info(colorspace); - }, - "colorspace"_a) .def("configname", &ColorConfig::configname) .def_static( "default_colorconfig", diff --git a/src/python/stubs/OpenImageIO/__init__.pyi b/src/python/stubs/OpenImageIO/__init__.pyi index a740c43a0b..954701f800 100644 --- a/src/python/stubs/OpenImageIO/__init__.pyi +++ b/src/python/stubs/OpenImageIO/__init__.pyi @@ -171,7 +171,6 @@ class ColorConfig: def configname(self) -> str: ... @staticmethod def default_colorconfig() -> ColorConfig: ... - def derive_color_space_info(self, colorspace: str) -> ColorSpaceInfo: ... def equivalent(self, color_space: str, other_color_space: str) -> bool: ... def filepathOnlyMatchesDefaultRule(self, filepath: str) -> bool: ... def getAliases(self, arg0: str, /) -> list[str]: ... diff --git a/testsuite/python-colorconfig/ref/out-ocio232.txt b/testsuite/python-colorconfig/ref/out-ocio232.txt index a9d1d949a4..b83c8b6c2b 100644 --- a/testsuite/python-colorconfig/ref/out-ocio232.txt +++ b/testsuite/python-colorconfig/ref/out-ocio232.txt @@ -70,6 +70,6 @@ get_color_space_info('g24_rec709_display'): valid=True gamma=2.4 chromaticities= get_color_space_info('srgb_tx'): valid=True gamma=0 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] get_color_space_info('Raw'): valid=True gamma=0 chromaticities=[] get_color_space_info('no_such_space'): valid=False gamma=0 chromaticities=[] -derive_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +get_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] Done. diff --git a/testsuite/python-colorconfig/ref/out-ocio24.txt b/testsuite/python-colorconfig/ref/out-ocio24.txt index 35697cf10d..90e7c7904f 100644 --- a/testsuite/python-colorconfig/ref/out-ocio24.txt +++ b/testsuite/python-colorconfig/ref/out-ocio24.txt @@ -70,6 +70,6 @@ get_color_space_info('g24_rec709_display'): valid=True gamma=2.4 chromaticities= get_color_space_info('srgb_tx'): valid=True gamma=0 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] get_color_space_info('Raw'): valid=True gamma=0 chromaticities=[] get_color_space_info('no_such_space'): valid=False gamma=0 chromaticities=[] -derive_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +get_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] Done. diff --git a/testsuite/python-colorconfig/ref/out-ocio25.txt b/testsuite/python-colorconfig/ref/out-ocio25.txt index 35ba307128..f2a35a0437 100644 --- a/testsuite/python-colorconfig/ref/out-ocio25.txt +++ b/testsuite/python-colorconfig/ref/out-ocio25.txt @@ -70,6 +70,6 @@ get_color_space_info('g24_rec709_display'): valid=True gamma=2.4 chromaticities= get_color_space_info('srgb_tx'): valid=True gamma=0 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] get_color_space_info('Raw'): valid=True gamma=0 chromaticities=[] get_color_space_info('no_such_space'): valid=False gamma=0 chromaticities=[] -derive_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +get_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] Done. diff --git a/testsuite/python-colorconfig/ref/out-ocio26.txt b/testsuite/python-colorconfig/ref/out-ocio26.txt index 8478a6e3ad..e9e78c83b5 100644 --- a/testsuite/python-colorconfig/ref/out-ocio26.txt +++ b/testsuite/python-colorconfig/ref/out-ocio26.txt @@ -70,6 +70,6 @@ get_color_space_info('g24_rec709_display'): valid=True gamma=2.4 chromaticities= get_color_space_info('srgb_tx'): valid=True gamma=0 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] get_color_space_info('Raw'): valid=True gamma=0 chromaticities=[] get_color_space_info('no_such_space'): valid=False gamma=0 chromaticities=[] -derive_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +get_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] Done. diff --git a/testsuite/python-colorconfig/ref/out.txt b/testsuite/python-colorconfig/ref/out.txt index a0aa70aa0f..5d65efd4bb 100644 --- a/testsuite/python-colorconfig/ref/out.txt +++ b/testsuite/python-colorconfig/ref/out.txt @@ -70,6 +70,6 @@ get_color_space_info('g24_rec709_display'): valid=True gamma=2.4 chromaticities= get_color_space_info('srgb_tx'): valid=True gamma=0 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] get_color_space_info('Raw'): valid=True gamma=0 chromaticities=[] get_color_space_info('no_such_space'): valid=False gamma=0 chromaticities=[] -derive_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +get_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] Done. diff --git a/testsuite/python-colorconfig/src/test_colorconfig.py b/testsuite/python-colorconfig/src/test_colorconfig.py index 82e15889c6..5cd93b77bf 100755 --- a/testsuite/python-colorconfig/src/test_colorconfig.py +++ b/testsuite/python-colorconfig/src/test_colorconfig.py @@ -110,9 +110,9 @@ for name in ("ACEScg", "g24_rec709_display", "srgb_tx", "Raw", "no_such_space"): info = config.get_color_space_info(name) print (f"get_color_space_info('{name}'): valid={info.valid()} gamma={info.transfer_function_gamma():g} chromaticities={[round(v, 5) for v in info.chromaticities()]}") - info = config.derive_color_space_info("lin_ap1_scene") + info = config.get_color_space_info("lin_ap1_scene") del config - print (f"derive_color_space_info('lin_ap1_scene') after del config: gamma={info.transfer_function_gamma():g} chromaticities={[round(v, 5) for v in info.chromaticities()]}") + print (f"get_color_space_info('lin_ap1_scene') after del config: gamma={info.transfer_function_gamma():g} chromaticities={[round(v, 5) for v in info.chromaticities()]}") print ("") print ("Done.") From 9baf224d4ef2adeb84650aa7e44c2f0fa67cde5e Mon Sep 17 00:00:00 2001 From: Zach Lewis Date: Wed, 7 Oct 2026 14:03:46 -0400 Subject: [PATCH 5/6] refactor(color): replace ColorSpaceInfo with two ColorConfig methods As Larry suggested in review, ColorConfig::get_chromaticities() and ColorConfig::get_transfer_gamma() replace the ColorSpaceInfo class and get_color_space_info(). get_chromaticities() returns a cspan of eight floats, like get_cicp(), pointing into the ColorInteropID table so it stays valid; get_transfer_gamma() returns the exponent. Both identify the color space as get_color_interop_id() does, so the answers are unchanged. In Python, get_chromaticities() returns None when they are unknown, as get_cicp() does. Assisted-by: Claude Code / Claude Opus 5.5 Signed-off-by: Zach Lewis --- src/doc/imageioapi.md | 15 ++- src/doc/pythonbindings.md | 53 +++++------ src/include/OpenImageIO/color.h | 62 ++++--------- src/libOpenImageIO/color_ocio.cpp | 93 ++++++++----------- src/libOpenImageIO/color_test.cpp | 77 ++++++--------- src/python/py_colorconfig.cpp | 27 +++--- src/python/stubs/OpenImageIO/__init__.pyi | 9 +- .../python-colorconfig/ref/out-ocio232.txt | 11 +-- .../python-colorconfig/ref/out-ocio24.txt | 11 +-- .../python-colorconfig/ref/out-ocio25.txt | 11 +-- .../python-colorconfig/ref/out-ocio26.txt | 11 +-- testsuite/python-colorconfig/ref/out.txt | 11 +-- .../src/test_colorconfig.py | 7 +- 13 files changed, 156 insertions(+), 242 deletions(-) diff --git a/src/doc/imageioapi.md b/src/doc/imageioapi.md index 001aafb21e..65a090848a 100644 --- a/src/doc/imageioapi.md +++ b/src/doc/imageioapi.md @@ -257,19 +257,18 @@ just exist in the OIIO namespace as general utilities. (See :::{doxygenfunction} OIIO::equivalent_colorspace ::: -(sec-colorspaceinfo)= +(sec-colorspaceproperties)= -## Color space properties: `ColorSpaceInfo` +## Color space properties -`ColorConfig::get_color_space_info()` returns a `ColorSpaceInfo` describing a -color space's primaries and transfer function. See {ref}`sec-pythoncolorconfig` -for the Python bindings. +`ColorConfig::get_chromaticities()` and `ColorConfig::get_transfer_gamma()` +report a color space's primaries and transfer function. See +{ref}`sec-pythoncolorconfig` for the Python bindings. -:::{doxygenclass} OIIO::ColorSpaceInfo -:members: +:::{doxygenfunction} OIIO::ColorConfig::get_chromaticities ::: -:::{doxygenfunction} OIIO::ColorConfig::get_color_space_info +:::{doxygenfunction} OIIO::ColorConfig::get_transfer_gamma ::: > (sec-startupshutdown)= diff --git a/src/doc/pythonbindings.md b/src/doc/pythonbindings.md index 92664eed6e..5464e0781e 100644 --- a/src/doc/pythonbindings.md +++ b/src/doc/pythonbindings.md @@ -4040,20 +4040,31 @@ is provided for minimal color support. % TODO: The documentation for this class is incomplete. -:::{py:method} get_color_space_info (colorspace) -Get color space information for a color space with a known Color Interop ID, -as `get_color_interop_id()` finds it. Properties that cannot be determined are -left unavailable. It accepts the same names as `get_color_interop_id()` and -`get_cicp()`; any other name returns an invalid `ColorSpaceInfo`. See -{ref}`sec-colorspaceinfo`. +:::{py:method} get_chromaticities (colorspace) +Get the RGB primaries and white point of a color space with a known Color +Interop ID, as `get_color_interop_id()` finds it: a sequence of eight floats +(Rx, Ry, Gx, Gy, Bx, By, Wx, Wy, CIE 1931 xy), or None if they are unknown. Example: ```python -info = colorconfig.get_color_space_info("ACEScg") -if info.valid(): - gamma = info.transfer_function_gamma() # 1.0 for a linear encoding - xy = info.chromaticities() # [] if the primaries are unknown +xy = colorconfig.get_chromaticities("ACEScg") +if xy: + rx, ry, gx, gy, bx, by, wx, wy = xy +``` + +This function was added in OpenImageIO 3.3. +::: + +:::{py:method} get_transfer_gamma (colorspace) +Get the exponent of a color space's transfer function if it decodes as a pure +power, 1.0 if it is linear, or 0 if it is neither or is unknown. Piecewise +curves such as sRGB's report 0. + +Example: + +```python +gamma = colorconfig.get_transfer_gamma("g22_rec709_scene") # 2.2 ``` This function was added in OpenImageIO 3.3. @@ -4103,28 +4114,6 @@ interop_id = colorconfig.get_color_interop_id([9, 16, 9, 1]) This function was added in OpenImageIO 3.1. ::: -:::{py:class} ColorSpaceInfo -Immutable properties of a color space. It remains usable after the -`ColorConfig` that returned it is destroyed. This class was added in -OpenImageIO 3.3. -::: - -:::{py:method} ColorSpaceInfo.valid () -Whether the color space name resolved, even if individual properties are -unavailable. -::: - -:::{py:method} ColorSpaceInfo.chromaticities () -The RGB primaries and white point as a list of eight floats (Rx, Ry, Gx, Gy, -Bx, By, Wx, Wy, CIE 1931 xy), or an empty list if they are unavailable. -::: - -:::{py:method} ColorSpaceInfo.transfer_function_gamma () -The exponent of a transfer function that decodes as a pure power, 1.0 for a -linear one, or 0 if the transfer function is not a pure power or is unknown. A -color space whose config declares a linear encoding reports 1.0. -::: - (sec-pythonmiscapi)= ## Miscellaneous Utilities diff --git a/src/include/OpenImageIO/color.h b/src/include/OpenImageIO/color.h index 4543717626..8715c453cb 100644 --- a/src/include/OpenImageIO/color.h +++ b/src/include/OpenImageIO/color.h @@ -63,42 +63,6 @@ using ColorProcessorHandle = std::shared_ptr; -/// Immutable, owning properties of a color space, returned by -/// `ColorConfig::get_color_space_info()`. Copies share their storage and -/// remain usable after the originating ColorConfig resets or is destroyed. -/// -/// @version 3.3 -class OIIO_API ColorSpaceInfo { -public: - ColorSpaceInfo() noexcept; - ColorSpaceInfo(const ColorSpaceInfo&) noexcept; - ColorSpaceInfo(ColorSpaceInfo&&) noexcept; - ColorSpaceInfo& operator=(const ColorSpaceInfo&) noexcept; - ColorSpaceInfo& operator=(ColorSpaceInfo&&) noexcept; - ~ColorSpaceInfo(); - - /// Whether the color space name resolved, even if individual properties - /// are unavailable. - bool valid() const noexcept; - /// The RGB primaries and white point as eight floats (Rx, Ry, Gx, Gy, Bx, - /// By, Wx, Wy, CIE 1931 xy), or an empty span if they are unavailable. - /// The span borrows from this object and expires when its last owning - /// copy is destroyed or reassigned. - cspan chromaticities() const noexcept; - /// The exponent of a transfer function that decodes as a pure power, - /// 1.0 for a linear one, or 0 if the transfer function is not a pure - /// power or is unknown. Piecewise curves such as sRGB's are never - /// approximated by a gamma, so they report 0. A color space whose config - /// declares a linear encoding reports 1.0. - float transfer_function_gamma() const noexcept; - -private: - friend class ColorConfig; - struct Impl; - std::shared_ptr m_impl; -}; - - /// Represents the set of all color transformations that are allowed. /// If OpenColorIO is enabled at build time, this configuration is loaded /// at runtime, allowing the user to have complete control of all color @@ -470,16 +434,25 @@ class OIIO_API ColorConfig { OIIO_NODISCARD string_view get_color_interop_id(string_view colorspace) const; - /// Get color space information for a color space with a known Color - /// Interop ID (https://github.com/AcademySoftwareFoundation/ColorInterop), - /// as `get_color_interop_id()` finds it. Properties that cannot be - /// determined are left unavailable. `colorspace` may be any name - /// `get_color_interop_id()` and `get_cicp()` accept; any other name - /// returns an invalid ColorSpaceInfo. + /// Get the RGB primaries and white point of a color space with a known + /// Color Interop ID + /// (https://github.com/AcademySoftwareFoundation/ColorInterop), as + /// `get_color_interop_id()` finds it: eight floats (Rx, Ry, Gx, Gy, Bx, + /// By, Wx, Wy, CIE 1931 xy), or an empty span if they are unknown. The + /// span points into OIIO's own table, so it stays valid. + /// + /// @version 3.3 + OIIO_NODISCARD cspan get_chromaticities(string_view colorspace) const; + + /// Get the exponent of a color space's transfer function if it decodes + /// as a pure power, 1.0 if it is linear, or 0 if it is neither or is + /// unknown. Piecewise curves such as sRGB's report 0. Like + /// `get_chromaticities()`, it answers for a known Color Interop ID, and + /// it reports 1.0 for any color space whose config declares a linear + /// encoding. /// /// @version 3.3 - OIIO_NODISCARD ColorSpaceInfo - get_color_space_info(string_view colorspace) const; + OIIO_NODISCARD float get_transfer_gamma(string_view colorspace) const; /// Find color interop ID corresponding to the CICP code. /// Returns empty string if not found. @@ -537,7 +510,6 @@ OIIO_NAMESPACE_3_1_END #ifndef OIIO_DOXYGEN OIIO_NAMESPACE_BEGIN using v3_1::ColorProcessorHandle; -using v3_1::ColorSpaceInfo; OIIO_NAMESPACE_END #endif diff --git a/src/libOpenImageIO/color_ocio.cpp b/src/libOpenImageIO/color_ocio.cpp index 400d2e632c..80b528c525 100644 --- a/src/libOpenImageIO/color_ocio.cpp +++ b/src/libOpenImageIO/color_ocio.cpp @@ -215,6 +215,13 @@ class ColorConfig::Impl { OCIO::ConfigRcPtr config_; OCIO::ConfigRcPtr builtinconfig_; + // A color space's chromaticities (in the ColorInteropID table, or null) + // and transfer function gamma. + struct Properties { + const float* xy = nullptr; + float gamma = 0.0f; + }; + private: std::vector colorspaces; std::string scene_linear_alias; // Alias for a scene-linear color space @@ -305,6 +312,9 @@ class ColorConfig::Impl { return handle; } + // The properties get_chromaticities() and get_transfer_gamma() report. + Properties properties(string_view colorspace) const; + int getNumColorSpaces() const { return (int)colorspaces.size(); } const char* getColorSpaceNameByIndex(int index) const @@ -2539,84 +2549,57 @@ ColorConfig::get_cicp(string_view colorspace) const -struct ColorSpaceInfo::Impl { - std::array chromaticities {}; - bool has_chromaticities = false; - float gamma = 0.0f; -}; - -ColorSpaceInfo::ColorSpaceInfo() noexcept = default; -ColorSpaceInfo::ColorSpaceInfo(const ColorSpaceInfo&) noexcept = default; -ColorSpaceInfo::ColorSpaceInfo(ColorSpaceInfo&&) noexcept = default; -ColorSpaceInfo& ColorSpaceInfo::operator=(const ColorSpaceInfo&) noexcept - = default; -ColorSpaceInfo& ColorSpaceInfo::operator=(ColorSpaceInfo&&) noexcept = default; -ColorSpaceInfo::~ColorSpaceInfo() = default; - -bool -ColorSpaceInfo::valid() const noexcept -{ - return bool(m_impl); -} - -cspan -ColorSpaceInfo::chromaticities() const noexcept -{ - return m_impl && m_impl->has_chromaticities - ? cspan(m_impl->chromaticities) - : cspan(); -} - -float -ColorSpaceInfo::transfer_function_gamma() const noexcept -{ - return m_impl ? m_impl->gamma : 0.0f; -} - - - -ColorSpaceInfo -ColorConfig::get_color_space_info(string_view colorspace) const +ColorConfig::Impl::Properties +ColorConfig::Impl::properties(string_view colorspace) const { // Identify the space as get_cicp() does, so that every name it accepts // gives the same answer here. - const string_view interop_id = get_color_interop_id(colorspace); + const string_view interop_id = m_self->get_color_interop_id(colorspace); OCIO::ConstColorSpaceRcPtr cs; - if (getImpl()->config_ && !disable_ocio && !colorspace.empty()) { + if (config_ && !disable_ocio && !colorspace.empty()) { try { - cs = getImpl()->config_->getColorSpace(c_str(resolve(colorspace))); + cs = config_->getColorSpace(c_str(m_self->resolve(colorspace))); } catch (const std::exception& e) { - DBG("OCIO exception in get_color_space_info: {}\n", e.what()); + DBG("OCIO exception looking up {}: {}\n", colorspace, e.what()); } } - if (!cs && interop_id.empty()) - return {}; - ColorSpaceInfo result; - auto impl = std::make_shared(); - result.m_impl = impl; if (cs && cs->isData()) - return result; + return {}; const string_view encoding(cs ? cs->getEncoding() : ""); const bool linear = encoding == "scene-linear" || encoding == "display-linear"; + Properties result; for (const ColorInteropID& interop : color_interop_ids) { if (interop.chromaticities && interop_id == interop.interop_id) { // An ID contradicting a declared linear encoding is ignored. - if (!(linear && interop.gamma != 1.0f)) { - std::copy_n(interop.chromaticities, 8, - impl->chromaticities.data()); - impl->has_chromaticities = true; - impl->gamma = interop.gamma; - } + if (!(linear && interop.gamma != 1.0f)) + result = { interop.chromaticities, interop.gamma }; break; } } if (linear) - impl->gamma = 1.0f; + result.gamma = 1.0f; return result; } + +cspan +ColorConfig::get_chromaticities(string_view colorspace) const +{ + const float* xy = getImpl()->properties(colorspace).xy; + return xy ? cspan(xy, 8) : cspan(); +} + + + +float +ColorConfig::get_transfer_gamma(string_view colorspace) const +{ + return getImpl()->properties(colorspace).gamma; +} + + ////////////////////////////////////////////////////////////////////////// // // Image Processing Implementations diff --git a/src/libOpenImageIO/color_test.cpp b/src/libOpenImageIO/color_test.cpp index 33065feee9..65bc4a280e 100644 --- a/src/libOpenImageIO/color_test.cpp +++ b/src/libOpenImageIO/color_test.cpp @@ -197,12 +197,8 @@ test_gamma_pair_conversion() static void -test_color_space_info() +test_color_space_properties() { - ColorSpaceInfo invalid; - OIIO_CHECK_FALSE(invalid.valid()); - OIIO_CHECK_ASSERT(invalid.chromaticities().empty()); - OIIO_CHECK_EQUAL(invalid.transfer_function_gamma(), 0.0f); if (!ColorConfig::supportsOpenColorIO()) return; @@ -228,61 +224,48 @@ test_color_space_info() const float ap1[] = { .713f, .293f, .165f, .83f, .128f, .044f, .32168f, .33767f }; const float p3d65[] = { .68f, .32f, .265f, .69f, .15f, .06f, .3127f, .329f }; - ColorSpaceInfo saved; + cspan saved; { ColorConfig config(filename); OIIO_CHECK_FALSE(config.has_error()); - saved = config.get_color_space_info("scene_linear"); - OIIO_CHECK_ASSERT(saved.valid()); - OIIO_CHECK_EQUAL(saved.transfer_function_gamma(), 1.0f); - OIIO_CHECK_ASSERT(saved.chromaticities() == cspan(ap1)); - auto srgb = config.get_color_space_info("srgb_rec709_scene"); - OIIO_CHECK_EQUAL(srgb.transfer_function_gamma(), 0.0f); - OIIO_CHECK_EQUAL(srgb.chromaticities().size(), 8); + saved = config.get_chromaticities("scene_linear"); + OIIO_CHECK_ASSERT(saved == cspan(ap1)); + OIIO_CHECK_EQUAL(config.get_transfer_gamma("scene_linear"), 1.0f); + OIIO_CHECK_EQUAL(config.get_transfer_gamma("srgb_rec709_scene"), 0.0f); + OIIO_CHECK_EQUAL(config.get_chromaticities("srgb_rec709_scene").size(), + 8); // OIIO's built-in names resolve as they do in the other queries. - OIIO_CHECK_EQUAL( - config.get_color_space_info("sRGB").chromaticities().size(), 8); - OIIO_CHECK_EQUAL( - config.get_color_space_info("Adobe").transfer_function_gamma(), - 563.0f / 256.0f); + OIIO_CHECK_EQUAL(config.get_chromaticities("sRGB").size(), 8); + OIIO_CHECK_EQUAL(config.get_transfer_gamma("Adobe"), 563.0f / 256.0f); for (auto name : { "Plain", "Mislabeled" }) { - auto info = config.get_color_space_info(name); - OIIO_CHECK_EQUAL(info.transfer_function_gamma(), 1.0f); - OIIO_CHECK_ASSERT(info.chromaticities().empty()); + OIIO_CHECK_EQUAL(config.get_transfer_gamma(name), 1.0f); + OIIO_CHECK_ASSERT(config.get_chromaticities(name).empty()); + } + for (auto name : { "Data", "data", "missing" }) { + OIIO_CHECK_EQUAL(config.get_transfer_gamma(name), 0.0f); + OIIO_CHECK_ASSERT(config.get_chromaticities(name).empty()); } - auto data = config.get_color_space_info("Data"); - OIIO_CHECK_ASSERT(data.valid()); - OIIO_CHECK_ASSERT(data.chromaticities().empty()); - OIIO_CHECK_EQUAL(data.transfer_function_gamma(), 0.0f); - OIIO_CHECK_FALSE(config.get_color_space_info("missing").valid()); // A legacy alias, and IDs this config doesn't define, answer as they // do for get_color_interop_id() and get_cicp(). OIIO_CHECK_EQUAL(config.get_color_interop_id("Texture"), "srgb_rec709_scene"); - auto texture = config.get_color_space_info("Texture"); - OIIO_CHECK_EQUAL(texture.transfer_function_gamma(), 0.0f); - OIIO_CHECK_EQUAL(texture.chromaticities().size(), 8); - auto p3 = config.get_color_space_info("lin_p3d65_display"); - OIIO_CHECK_EQUAL(p3.transfer_function_gamma(), 1.0f); - OIIO_CHECK_ASSERT(p3.chromaticities() == cspan(p3d65)); - OIIO_CHECK_ASSERT(config.get_color_space_info("data").valid()); + OIIO_CHECK_EQUAL(config.get_transfer_gamma("Texture"), 0.0f); + OIIO_CHECK_EQUAL(config.get_chromaticities("Texture").size(), 8); + OIIO_CHECK_EQUAL(config.get_transfer_gamma("lin_p3d65_display"), 1.0f); + OIIO_CHECK_ASSERT(config.get_chromaticities("lin_p3d65_display") + == cspan(p3d65)); } + // The span stays valid after the config is gone. + OIIO_CHECK_ASSERT(saved == cspan(ap1)); // OpenColorIO 2.3's built-in CG config gives its spaces only the legacy // aliases, which identify them here too. { ColorConfig config("ocio://cg-config-v2.1.0_aces-v1.3_ocio-v2.3"); - OIIO_CHECK_ASSERT(config.get_color_space_info("ACEScg").chromaticities() + OIIO_CHECK_ASSERT(config.get_chromaticities("ACEScg") == cspan(ap1)); - auto texture = config.get_color_space_info("sRGB - Texture"); - OIIO_CHECK_EQUAL(texture.chromaticities().size(), 8); - OIIO_CHECK_EQUAL(texture.transfer_function_gamma(), 0.0f); + OIIO_CHECK_EQUAL(config.get_chromaticities("sRGB - Texture").size(), 8); + OIIO_CHECK_EQUAL(config.get_transfer_gamma("sRGB - Texture"), 0.0f); } - // The properties outlive the config; a move leaves its source invalid. - auto copy = saved; - auto moved = std::move(saved); - OIIO_CHECK_FALSE(saved.valid()); - OIIO_CHECK_ASSERT(copy.chromaticities() == cspan(ap1)); - OIIO_CHECK_EQUAL(moved.transfer_function_gamma(), 1.0f); Filesystem::remove(filename); // OpenColorIO 2.5 reads a declared interop_id, which outranks the name. @@ -296,9 +279,9 @@ test_color_space_info() " interop_id: g22_ap1_scene\n")); ColorConfig config(filename); OIIO_CHECK_FALSE(config.has_error()); - auto info = config.get_color_space_info("g18_rec709_scene"); - OIIO_CHECK_EQUAL(info.transfer_function_gamma(), 2.2f); - OIIO_CHECK_ASSERT(info.chromaticities() == cspan(ap1)); + OIIO_CHECK_EQUAL(config.get_transfer_gamma("g18_rec709_scene"), 2.2f); + OIIO_CHECK_ASSERT(config.get_chromaticities("g18_rec709_scene") + == cspan(ap1)); Filesystem::remove(filename); } } @@ -323,7 +306,7 @@ main(int argc, char* argv[]) test_Rec709_conversion(); test_linear_display_cicp(); test_gamma_pair_conversion(); - test_color_space_info(); + test_color_space_properties(); return unit_test_failures != 0; } diff --git a/src/python/py_colorconfig.cpp b/src/python/py_colorconfig.cpp index 5773bfd086..b25391da49 100644 --- a/src/python/py_colorconfig.cpp +++ b/src/python/py_colorconfig.cpp @@ -14,17 +14,6 @@ namespace PyOpenImageIO { void declare_colorconfig(py_module& m) { - py::class_(m, "ColorSpaceInfo") - .def(py::init<>()) - .def("valid", &ColorSpaceInfo::valid) - .def("transfer_function_gamma", - &ColorSpaceInfo::transfer_function_gamma) - .def("chromaticities", [](const ColorSpaceInfo& self) { - auto xy = self.chromaticities(); - return std::vector(xy.begin(), xy.end()); - }); - - py::class_(m, "ColorConfig") .def(py::init<>()) @@ -207,9 +196,21 @@ declare_colorconfig(py_module& m) return std::nullopt; }) .def( - "get_color_space_info", + "get_chromaticities", + [](const ColorConfig& self, const std::string& colorspace) + -> std::optional> { + cspan xy = self.get_chromaticities(colorspace); + if (xy.empty()) + return std::nullopt; + std::array result; + std::copy(xy.begin(), xy.end(), result.begin()); + return result; + }, + "colorspace"_a) + .def( + "get_transfer_gamma", [](const ColorConfig& self, const std::string& colorspace) { - return self.get_color_space_info(colorspace); + return self.get_transfer_gamma(colorspace); }, "colorspace"_a) .def("configname", &ColorConfig::configname) diff --git a/src/python/stubs/OpenImageIO/__init__.pyi b/src/python/stubs/OpenImageIO/__init__.pyi index 954701f800..71a13bb637 100644 --- a/src/python/stubs/OpenImageIO/__init__.pyi +++ b/src/python/stubs/OpenImageIO/__init__.pyi @@ -208,23 +208,18 @@ class ColorConfig: def getRoles(self) -> list[str]: ... def getViewNameByIndex(self, display: str = ..., *, index: typing.SupportsInt) -> str: ... def getViewNames(self, display: str = ...) -> list[str]: ... + def get_chromaticities(self, colorspace: str) -> list[float] | None: ... def get_cicp(self, *args, **kwargs): ... @overload def get_color_interop_id(self, arg0: str, /) -> str: ... @overload def get_color_interop_id(self, arg0, /) -> str: ... - def get_color_space_info(self, colorspace: str) -> ColorSpaceInfo: ... + def get_transfer_gamma(self, colorspace: str) -> float: ... def geterror(self) -> str: ... def isColorSpaceActive(self, name: str) -> bool: ... def parseColorSpaceFromString(self, arg0: str, /) -> str: ... def resolve(self, name: str) -> str: ... -class ColorSpaceInfo: - def __init__(self) -> None: ... - def chromaticities(self) -> list[float]: ... - def transfer_function_gamma(self) -> float: ... - def valid(self) -> bool: ... - class CompareResults: def __init__(self) -> None: ... @property diff --git a/testsuite/python-colorconfig/ref/out-ocio232.txt b/testsuite/python-colorconfig/ref/out-ocio232.txt index b83c8b6c2b..d6cd123fb8 100644 --- a/testsuite/python-colorconfig/ref/out-ocio232.txt +++ b/testsuite/python-colorconfig/ref/out-ocio232.txt @@ -65,11 +65,10 @@ ociodisplay #2 (apply default display/view again): [[[-2.123 0.671 0.8037]] ociodisplay #3 (inverse look): [[[-2.252 0.6714 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #4 (forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #5 (inverse look + forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) -get_color_space_info('ACEScg'): valid=True gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] -get_color_space_info('g24_rec709_display'): valid=True gamma=2.4 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] -get_color_space_info('srgb_tx'): valid=True gamma=0 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] -get_color_space_info('Raw'): valid=True gamma=0 chromaticities=[] -get_color_space_info('no_such_space'): valid=False gamma=0 chromaticities=[] -get_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +'ACEScg': get_transfer_gamma=1 get_chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +'g24_rec709_display': get_transfer_gamma=2.4 get_chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +'srgb_tx': get_transfer_gamma=0 get_chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +'Raw': get_transfer_gamma=0 get_chromaticities=None +'no_such_space': get_transfer_gamma=0 get_chromaticities=None Done. diff --git a/testsuite/python-colorconfig/ref/out-ocio24.txt b/testsuite/python-colorconfig/ref/out-ocio24.txt index 90e7c7904f..8f8b5b5141 100644 --- a/testsuite/python-colorconfig/ref/out-ocio24.txt +++ b/testsuite/python-colorconfig/ref/out-ocio24.txt @@ -65,11 +65,10 @@ ociodisplay #2 (apply default display/view again): [[[-2.123 0.671 0.8037]] ociodisplay #3 (inverse look): [[[-2.252 0.6714 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #4 (forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #5 (inverse look + forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) -get_color_space_info('ACEScg'): valid=True gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] -get_color_space_info('g24_rec709_display'): valid=True gamma=2.4 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] -get_color_space_info('srgb_tx'): valid=True gamma=0 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] -get_color_space_info('Raw'): valid=True gamma=0 chromaticities=[] -get_color_space_info('no_such_space'): valid=False gamma=0 chromaticities=[] -get_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +'ACEScg': get_transfer_gamma=1 get_chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +'g24_rec709_display': get_transfer_gamma=2.4 get_chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +'srgb_tx': get_transfer_gamma=0 get_chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +'Raw': get_transfer_gamma=0 get_chromaticities=None +'no_such_space': get_transfer_gamma=0 get_chromaticities=None Done. diff --git a/testsuite/python-colorconfig/ref/out-ocio25.txt b/testsuite/python-colorconfig/ref/out-ocio25.txt index f2a35a0437..cdbecf4fc5 100644 --- a/testsuite/python-colorconfig/ref/out-ocio25.txt +++ b/testsuite/python-colorconfig/ref/out-ocio25.txt @@ -65,11 +65,10 @@ ociodisplay #2 (apply default display/view again): [[[-2.123 0.671 0.8037]] ociodisplay #3 (inverse look): [[[-2.252 0.6714 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #4 (forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #5 (inverse look + forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) -get_color_space_info('ACEScg'): valid=True gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] -get_color_space_info('g24_rec709_display'): valid=True gamma=2.4 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] -get_color_space_info('srgb_tx'): valid=True gamma=0 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] -get_color_space_info('Raw'): valid=True gamma=0 chromaticities=[] -get_color_space_info('no_such_space'): valid=False gamma=0 chromaticities=[] -get_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +'ACEScg': get_transfer_gamma=1 get_chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +'g24_rec709_display': get_transfer_gamma=2.4 get_chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +'srgb_tx': get_transfer_gamma=0 get_chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +'Raw': get_transfer_gamma=0 get_chromaticities=None +'no_such_space': get_transfer_gamma=0 get_chromaticities=None Done. diff --git a/testsuite/python-colorconfig/ref/out-ocio26.txt b/testsuite/python-colorconfig/ref/out-ocio26.txt index e9e78c83b5..7a05b1f38f 100644 --- a/testsuite/python-colorconfig/ref/out-ocio26.txt +++ b/testsuite/python-colorconfig/ref/out-ocio26.txt @@ -65,11 +65,10 @@ ociodisplay #2 (apply default display/view again): [[[-2.123 0.671 0.8037]] ociodisplay #3 (inverse look): [[[-2.252 0.6714 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #4 (forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #5 (inverse look + forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) -get_color_space_info('ACEScg'): valid=True gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] -get_color_space_info('g24_rec709_display'): valid=True gamma=2.4 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] -get_color_space_info('srgb_tx'): valid=True gamma=0 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] -get_color_space_info('Raw'): valid=True gamma=0 chromaticities=[] -get_color_space_info('no_such_space'): valid=False gamma=0 chromaticities=[] -get_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +'ACEScg': get_transfer_gamma=1 get_chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +'g24_rec709_display': get_transfer_gamma=2.4 get_chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +'srgb_tx': get_transfer_gamma=0 get_chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +'Raw': get_transfer_gamma=0 get_chromaticities=None +'no_such_space': get_transfer_gamma=0 get_chromaticities=None Done. diff --git a/testsuite/python-colorconfig/ref/out.txt b/testsuite/python-colorconfig/ref/out.txt index 5d65efd4bb..393fb1faeb 100644 --- a/testsuite/python-colorconfig/ref/out.txt +++ b/testsuite/python-colorconfig/ref/out.txt @@ -65,11 +65,10 @@ ociodisplay #2 (apply default display/view again): [[[-2.123 0.671 0.8037]] ociodisplay #3 (inverse look): [[[-2.252 0.6714 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #4 (forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) ociodisplay #5 (inverse look + forwards look): [[[-2.123 0.671 0.8037]]] (sRGB (~2.22) - Display) -get_color_space_info('ACEScg'): valid=True gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] -get_color_space_info('g24_rec709_display'): valid=True gamma=2.4 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] -get_color_space_info('srgb_tx'): valid=True gamma=0 chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] -get_color_space_info('Raw'): valid=True gamma=0 chromaticities=[] -get_color_space_info('no_such_space'): valid=False gamma=0 chromaticities=[] -get_color_space_info('lin_ap1_scene') after del config: gamma=1 chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +'ACEScg': get_transfer_gamma=1 get_chromaticities=[0.713, 0.293, 0.165, 0.83, 0.128, 0.044, 0.32168, 0.33767] +'g24_rec709_display': get_transfer_gamma=2.4 get_chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +'srgb_tx': get_transfer_gamma=0 get_chromaticities=[0.64, 0.33, 0.3, 0.6, 0.15, 0.06, 0.3127, 0.329] +'Raw': get_transfer_gamma=0 get_chromaticities=None +'no_such_space': get_transfer_gamma=0 get_chromaticities=None Done. diff --git a/testsuite/python-colorconfig/src/test_colorconfig.py b/testsuite/python-colorconfig/src/test_colorconfig.py index 5cd93b77bf..bae0ce9471 100755 --- a/testsuite/python-colorconfig/src/test_colorconfig.py +++ b/testsuite/python-colorconfig/src/test_colorconfig.py @@ -108,11 +108,8 @@ buf = oiio.ImageBufAlgo.ociodisplay(buf, "", "", colorconfig=str(TEST_CONFIG_PATH), looks="-ACES 1.3 Reference Gamut Compression, +ACES 1.3 Reference Gamut Compression") print (f"ociodisplay #5 (inverse look + forwards look): {buf.get_pixels(oiio.HALF)} ({buf.spec()['oiio:ColorSpace']})") for name in ("ACEScg", "g24_rec709_display", "srgb_tx", "Raw", "no_such_space"): - info = config.get_color_space_info(name) - print (f"get_color_space_info('{name}'): valid={info.valid()} gamma={info.transfer_function_gamma():g} chromaticities={[round(v, 5) for v in info.chromaticities()]}") - info = config.get_color_space_info("lin_ap1_scene") - del config - print (f"get_color_space_info('lin_ap1_scene') after del config: gamma={info.transfer_function_gamma():g} chromaticities={[round(v, 5) for v in info.chromaticities()]}") + xy = config.get_chromaticities(name) + print (f"'{name}': get_transfer_gamma={config.get_transfer_gamma(name):g} get_chromaticities={xy and [round(v, 5) for v in xy]}") print ("") print ("Done.") From c13433c4ed459a6f9a1b474be6f8d540fd1100fe Mon Sep 17 00:00:00 2001 From: Larry Gritz Date: Sat, 10 Oct 2026 12:56:22 -0700 Subject: [PATCH 6/6] Set version description of the new methods to 3.2, since we intend to backport Signed-off-by: Larry Gritz --- src/doc/pythonbindings.md | 4 ++-- src/include/OpenImageIO/color.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/src/doc/pythonbindings.md b/src/doc/pythonbindings.md index 5464e0781e..cb07c6c7d5 100644 --- a/src/doc/pythonbindings.md +++ b/src/doc/pythonbindings.md @@ -4053,7 +4053,7 @@ if xy: rx, ry, gx, gy, bx, by, wx, wy = xy ``` -This function was added in OpenImageIO 3.3. +This function was added in OpenImageIO 3.2. ::: :::{py:method} get_transfer_gamma (colorspace) @@ -4067,7 +4067,7 @@ Example: gamma = colorconfig.get_transfer_gamma("g22_rec709_scene") # 2.2 ``` -This function was added in OpenImageIO 3.3. +This function was added in OpenImageIO 3.2. ::: :::{py:method} get_cicp (colorspace) diff --git a/src/include/OpenImageIO/color.h b/src/include/OpenImageIO/color.h index 8715c453cb..db20767f2f 100644 --- a/src/include/OpenImageIO/color.h +++ b/src/include/OpenImageIO/color.h @@ -441,7 +441,7 @@ class OIIO_API ColorConfig { /// By, Wx, Wy, CIE 1931 xy), or an empty span if they are unknown. The /// span points into OIIO's own table, so it stays valid. /// - /// @version 3.3 + /// @version 3.2 OIIO_NODISCARD cspan get_chromaticities(string_view colorspace) const; /// Get the exponent of a color space's transfer function if it decodes @@ -451,7 +451,7 @@ class OIIO_API ColorConfig { /// it reports 1.0 for any color space whose config declares a linear /// encoding. /// - /// @version 3.3 + /// @version 3.2 OIIO_NODISCARD float get_transfer_gamma(string_view colorspace) const; /// Find color interop ID corresponding to the CICP code.