Blender V5.3
libocio_colorspace.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2025 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
7#include "error_handling.hh"
9
10#include "CLG_log.h"
11
12#include "../description.hh"
14#include "libocio_processor.hh"
15
16namespace blender {
17
18static CLG_LogRef LOG = {"color_management"};
19
20namespace ocio {
21
23 const OCIO_NAMESPACE::ConstConfigRcPtr &ocio_config,
24 const OCIO_NAMESPACE::ConstColorSpaceRcPtr &ocio_color_space,
25 Set<StringRef> &primary_interop_ids)
26 : ocio_config_(ocio_config),
27 ocio_color_space_(ocio_color_space),
28 clean_description_(cleanup_description(ocio_color_space->getDescription())),
29 scene_linear_config_(ocio_config)
30{
31 const char *family = ocio_color_space->getFamily();
32 this->family_ = (family) ? family : "";
33 this->index = index;
34
35#if OCIO_VERSION_HEX >= 0x02050000
36 interop_id_ = ocio_color_space->getInteropID();
37#endif
38 bool is_legacy_interop_id = false;
39
40 if (interop_id_.is_empty()) {
41 /* For older configs and older OpenColorIO versions, check the aliases as fallback.
42 * This is a convention used in the Blender and ACES 2.0 configs. */
43 const int num_aliases = ocio_color_space->getNumAliases();
44 for (int i = 0; interop_id_.is_empty() && i < num_aliases; i++) {
45 StringRefNull alias = ocio_color_space->getAlias(i);
46 if (alias == "srgb_display") {
47 interop_id_ = "srgb_rec709_display";
48 }
49 else if (alias == "displayp3_display") {
50 interop_id_ = "srgb_p3d65_display";
51 }
52 else if (alias == "displayp3_hdr_display") {
53 interop_id_ = "srgbe_p3d65_display";
54 }
55 else if (alias == "p3d65_display") {
56 interop_id_ = "g26_p3d65_display";
57 }
58 else if (alias == "rec1886_rec709_display") {
59 interop_id_ = "g24_rec709_display";
60 }
61 else if (alias == "g24_rec2020_display") {
62 interop_id_ = "blender:g24_rec2020_display";
63 }
64 else if (alias == "rec2100_pq_display") {
65 interop_id_ = "pq_rec2020_display";
66 }
67 else if (alias == "rec2100_hlg_display") {
68 interop_id_ = "hlg_rec2020_display";
69 }
70 else if (alias == "st2084_p3d65_display") {
71 interop_id_ = "pq_p3d65_display";
72 }
73 else if (ELEM(alias, "lin_rec709_srgb", "lin_rec709")) {
74 interop_id_ = "lin_rec709_scene";
75 }
76 else if (alias == "lin_rec2020") {
77 interop_id_ = "lin_rec2020_scene";
78 }
79 else if (ELEM(alias, "lin_p3d65", "lin_displayp3")) {
80 interop_id_ = "lin_p3d65_scene";
81 }
82 else if ((alias.startswith("lin_") || alias.startswith("srgb_") ||
83 alias.startswith("g18_") || alias.startswith("g22_") || alias.startswith("g24_") ||
84 alias.startswith("g26_") || alias.startswith("pq_") || alias.startswith("hlg_")) &&
85 (alias.endswith("_scene") || alias.endswith("_display")))
86 {
87 interop_id_ = alias;
88 }
89 }
90 is_legacy_interop_id = !interop_id_.is_empty();
91 }
92
93 if (!interop_id_.is_empty()) {
94 /* Detect if this is the primary interop ID, either because the colorspace
95 * name or an alias is the same, or because it's a legacy interop ID and
96 * there is no other color space that has it as a name or alias. */
97 const OCIO_NAMESPACE::ConstColorSpaceRcPtr owner = ocio_config->getColorSpace(
98 interop_id_.c_str());
99 const bool is_owner = owner && name() == owner->getName();
100 is_primary_interop_id_ = is_owner || (is_legacy_interop_id && !owner);
101 }
102
103 /* Special case that we can not handle as an alias, because it's a role too. */
104 if (interop_id_.is_empty() || interop_id_ == "data") {
105 const char *data_name = ocio_config->getRoleColorSpace(OCIO_NAMESPACE::ROLE_DATA);
106 if (data_name && STREQ(ocio_color_space->getName(), data_name)) {
107 interop_id_ = "data";
108 is_primary_interop_id_ = true;
109 }
110 }
111
112 /* If multiple legacy aliases are found for the same interop ID, the first one wins. */
113 if (is_primary_interop_id_ && !primary_interop_ids.add(interop_id_)) {
114 is_primary_interop_id_ = false;
115 }
116
117 initialize_alternate_interop_id();
118
120 "Add colorspace: %s (interop ID: %s)",
121 name().c_str(),
122 interop_id_.is_empty() ? "<none>" : interop_id_.c_str());
123}
124
126{
127 return is_primary_interop_id_;
128}
129
130void LibOCIOColorSpace::initialize_alternate_interop_id()
131{
132 if (!is_primary_interop_id_) {
133 return;
134 }
135
136 const StringRef interop_id = interop_id_;
137 std::string other_interop_id;
138 if (interop_id.endswith("_scene")) {
139 other_interop_id = interop_id.drop_known_suffix("_scene") + "_display";
140 }
141 else if (interop_id.endswith("_display")) {
142 other_interop_id = interop_id.drop_known_suffix("_display") + "_scene";
143 }
144 else {
145 return;
146 }
147
148 const int num_aliases = ocio_color_space_->getNumAliases();
149 for (int i = 0; i < num_aliases; i++) {
150 if (ocio_color_space_->getAlias(i) == other_interop_id) {
151 alternate_interop_id_ = std::move(other_interop_id);
152 return;
153 }
154 }
155}
156
158{
159#if OCIO_VERSION_HEX >= 0x02050000
160 try {
161 /* Both these methods can throw exceptions. */
162 const char *profile_name = ocio_color_space_->getInterchangeAttribute("icc_profile_name");
163 if (profile_name && profile_name[0]) {
164 return ocio_config_->getCurrentContext()->resolveFileLocation(profile_name);
165 }
166 return profile_name;
167 }
168 catch (OCIO_NAMESPACE::Exception &exception) {
169 report_exception(exception);
170 }
171#endif
172
173 return "";
174}
175
177{
178 /* The color space is the scene linear working space when the conversion is a no-op. */
179 const CPUProcessor *cpu_processor = get_to_scene_linear_cpu_processor();
180 return cpu_processor && cpu_processor->is_noop();
181}
182
184{
185 /* Detected from the primary interop ID. For additional interop IDs it's not necessarily
186 * sRGB, but rather a color space that can be saved as sRGB. */
187 return is_primary_interop_id_ && ELEM(interop_id_, "srgb_rec709_scene", "srgb_rec709_display");
188}
189
191{
192 return to_scene_linear_cpu_processor_.get([&]() -> std::unique_ptr<CPUProcessor> {
193 OCIO_NAMESPACE::ConstProcessorRcPtr ocio_processor;
194 if (scene_linear_config_ != ocio_config_) {
195 ocio_processor = create_ocio_processor_between_configs(ocio_config_,
196 ocio_color_space_->getName(),
197 scene_linear_config_,
198 OCIO_NAMESPACE::ROLE_SCENE_LINEAR);
199 }
200 if (!ocio_processor) {
201 ocio_processor = create_ocio_processor(
202 ocio_config_, ocio_color_space_->getName(), OCIO_NAMESPACE::ROLE_SCENE_LINEAR);
203 }
204 if (!ocio_processor) {
205 return nullptr;
206 }
207 return std::make_unique<LibOCIOCPUProcessor>(ocio_processor->getDefaultCPUProcessor());
208 });
209}
210
212{
213 return from_scene_linear_cpu_processor_.get([&]() -> std::unique_ptr<CPUProcessor> {
214 OCIO_NAMESPACE::ConstProcessorRcPtr ocio_processor;
215 if (scene_linear_config_ != ocio_config_) {
216 ocio_processor = create_ocio_processor_between_configs(scene_linear_config_,
217 OCIO_NAMESPACE::ROLE_SCENE_LINEAR,
218 ocio_config_,
219 ocio_color_space_->getName());
220 }
221 if (!ocio_processor) {
222 ocio_processor = create_ocio_processor(
223 ocio_config_, OCIO_NAMESPACE::ROLE_SCENE_LINEAR, ocio_color_space_->getName());
224 }
225 if (!ocio_processor) {
226 return nullptr;
227 }
228 return std::make_unique<LibOCIOCPUProcessor>(ocio_processor->getDefaultCPUProcessor());
229 });
230}
231
233 const OCIO_NAMESPACE::ConstConfigRcPtr &ocio_config)
234{
235 if (scene_linear_config_ == ocio_config) {
236 return;
237 }
238 scene_linear_config_ = ocio_config;
239 clear_caches();
240}
241
243{
244 from_scene_linear_cpu_processor_ = CPUProcessorCache();
245 to_scene_linear_cpu_processor_ = CPUProcessorCache();
246}
247
248} // namespace ocio
249} // namespace blender
#define CLOG_TRACE(clg_ref,...)
Definition CLG_log.h:192
#define ELEM(...)
constexpr StringRef drop_known_suffix(StringRef suffix) const
constexpr bool endswith(StringRef suffix) const
virtual bool is_noop() const =0
StringRefNull family() const override
void switch_scene_linear_config(const OCIO_NAMESPACE::ConstConfigRcPtr &ocio_config)
std::string icc_profile_path() const override
const CPUProcessor * get_from_scene_linear_cpu_processor() const override
LibOCIOColorSpace(int index, const OCIO_NAMESPACE::ConstConfigRcPtr &ocio_config, const OCIO_NAMESPACE::ConstColorSpaceRcPtr &ocio_color_space, Set< StringRef > &primary_interop_ids)
const OCIO_NAMESPACE::ConstColorSpaceRcPtr & ocio_color_space() const
bool is_primary_interop_id() const override
const CPUProcessor * get_to_scene_linear_cpu_processor() const override
OCIO_NAMESPACE::ConstProcessorRcPtr create_ocio_processor(const OCIO_NAMESPACE::ConstConfigRcPtr &ocio_config, const StringRefNull from_colorspace, const StringRefNull to_colorspace)
OCIO_NAMESPACE::ConstProcessorRcPtr create_ocio_processor_between_configs(const OCIO_NAMESPACE::ConstConfigRcPtr &from_config, const StringRefNull from_colorspace, const OCIO_NAMESPACE::ConstConfigRcPtr &to_config, const StringRefNull to_colorspace)
void report_exception(const OCIO_NAMESPACE::Exception &exception)
std::string cleanup_description(const StringRef description)
BMesh Iterators.
static CLG_LogRef LOG
Definition logging.cpp:12