Blender V5.3
IMB_colormanagement_test.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#include <sstream>
6
7#include "testing/testing.h"
8
9#include "BLI_fileops.hh"
10#include "BLI_index_range.hh"
11#include "BLI_path_utils.hh"
12#include "BLI_string.hh"
13#include "BLI_vector.hh"
14
15#include "DNA_color_types.h"
16#include "DNA_image_types.h"
17
18#include "BKE_appdir.hh"
20#include "BKE_gtest_base.hh"
21#include "BKE_lib_id.hh"
22#include "BKE_main.hh"
23
25#include "IMB_imbuf.hh"
26
27#include "OCIO_colorspace.hh"
28#include "OCIO_config.hh"
29
31
33
34static std::string blender_config_path()
35{
36 char configfile[FILE_MAX];
37 BLI_path_join(configfile,
38 sizeof(configfile),
39 blender::tests::flags_test_release_dir().c_str(),
40 "datafiles",
41 "colormanagement",
43 return configfile;
44}
45
47 public:
48 static void SetUpTestSuite()
49 {
50 BLI_setenv("OCIO", blender_config_path().c_str());
52 BKE_tempdir_init(nullptr);
53 }
54
61
62 protected:
63 void TearDown() override
64 {
66 }
67};
68
69TEST_F(ColorManagementConfigSwitchTest, colorspace_pointers_survive_switch)
70{
71 const std::string config = blender_config_path();
72
73 struct TrackedBuffer {
74 ImBuf *ibuf;
75 const ColorSpace *byte_colorspace;
76 const ColorSpace *float_colorspace;
77 std::string byte_name;
78 std::string float_name;
79 };
80
82
84 for (const int float_role : {COLOR_ROLE_SCENE_LINEAR, COLOR_ROLE_DATA}) {
89
90 ASSERT_NE(ibuf->byte_buffer.colorspace, nullptr);
91 ASSERT_NE(ibuf->float_buffer.colorspace, nullptr);
92 buffers.append({ibuf,
96 ibuf->float_buffer.colorspace->name()});
97 }
98
99 /* Switch twice, the second time to verify repeated switching does not break anything either. */
100 for (int i = 0; i < 2; i++) {
101 SCOPED_TRACE("switch " + std::to_string(i));
102 ASSERT_TRUE(IMB_colormanagement_switch_config(config.c_str()));
103
104 for (const TrackedBuffer &tracked : buffers) {
105 EXPECT_EQ(tracked.ibuf->byte_buffer.colorspace, tracked.byte_colorspace);
106 EXPECT_EQ(tracked.ibuf->float_buffer.colorspace, tracked.float_colorspace);
107 EXPECT_EQ(tracked.byte_colorspace->name(), tracked.byte_name);
108 EXPECT_EQ(tracked.float_colorspace->name(), tracked.float_name);
109 EXPECT_EQ(IMB_colormanagement_space_get_named(tracked.byte_name.c_str()),
110 tracked.byte_colorspace);
111 EXPECT_EQ(IMB_colormanagement_space_get_named(tracked.float_name.c_str()),
112 tracked.float_colorspace);
113 }
114 }
115
116 for (const TrackedBuffer &tracked : buffers) {
117 IMB_freeImBuf(tracked.ibuf);
118 }
119}
120
121static const char *extra_colorspace_name = "test_extra_space";
122
123static std::string write_config_with_extra_colorspace(const std::string &config_path)
124{
125 /* Read config. */
126 fstream config_stream(config_path, std::ios::in);
127 if (!config_stream.is_open()) {
128 return "";
129 }
130
131 std::stringstream config_buffer;
132 config_buffer << config_stream.rdbuf();
133 std::string config_text = config_buffer.str();
134
135 /* Insert an extra colorspace at the start of the colorspaces: section.
136 * Simple colorspace that multiples scene linear by 2. */
137 const std::string marker = "\ncolorspaces:\n";
138 const size_t marker_pos = config_text.find(marker);
139 if (marker_pos == std::string::npos) {
140 return "";
141 }
142
143 const std::string extra_colorspace =
144 " - !<ColorSpace>\n"
145 " name: " +
146 std::string(extra_colorspace_name) +
147 "\n"
148 " family: Test\n"
149 " bitdepth: 32f\n"
150 " isdata: false\n"
151 " from_scene_reference: !<MatrixTransform> {matrix: [2, 0, 0, 0, 0, 2, 0, 0, 0, 0, 2, 0, "
152 "0, 0, 0, 1]}\n";
153 config_text.insert(marker_pos + marker.size(), extra_colorspace);
154
155 /* Write modified config to temporary directory. */
156 char extra_config_path[FILE_MAX];
157 BLI_path_join(extra_config_path,
158 sizeof(extra_config_path),
160 "test_extra_config.ocio");
161
162 fstream extra_config_stream(extra_config_path, std::ios::out | std::ios::trunc);
163 if (!extra_config_stream.is_open()) {
164 return "";
165 }
166 extra_config_stream << config_text;
167 return extra_config_path;
168}
169
170TEST_F(ColorManagementConfigSwitchTest, removed_colorspace_is_retained)
171{
172 const std::string config = blender_config_path();
173 const std::string extra_config = write_config_with_extra_colorspace(config);
174
175 /* Switch from blender config to a new config with an extra colorspace, and:
176 * - Create an imbuf with this color space
177 * - Compute the result of converting to this color space */
178 ASSERT_TRUE(IMB_colormanagement_switch_config(extra_config.c_str()));
180
184 ASSERT_NE(colorspace, nullptr);
185
186 float expected_pixel[3] = {0.5f, 0.25f, 0.125f};
188 ASSERT_NE(expected_pixel[0], 0.5f);
189
190 /* Switch back to the blender config and verify conversion still has:
191 * - A matching color space in the ImBuf
192 * - Converting to this color space continues to work as before, even though it
193 * does not exist in the blender config. */
194 ASSERT_TRUE(IMB_colormanagement_switch_config(config.c_str()));
195
198
199 float pixel[3] = {0.5f, 0.25f, 0.125f};
201 EXPECT_V3_NEAR(pixel, expected_pixel, 1e-6f);
202
203 IMB_freeImBuf(ibuf);
204}
205
206TEST_F(ColorManagementConfigSwitchTest, active_inactive_color_spaces_by_index)
207{
208 /* Check inactive color spaces have an index after active color spaces. */
210 ASSERT_GT(config.get_num_all_color_spaces(), config.get_num_active_color_spaces());
211
212 for (const int i : IndexRange(config.get_num_all_color_spaces())) {
214 ASSERT_NE(colorspace, nullptr);
215 EXPECT_EQ(colorspace->index, i);
216 }
218}
219
221 protected:
222 Main *bmain_ = nullptr;
223
224 void SetUp() override
225 {
227 }
228
234};
235
236TEST_F(ColorManagementInteropIDTest, stored_color_spaces_resolve_by_interop_id)
237{
238 Image *image = BKE_id_new<Image>(bmain_, "image");
239 ColorManagedColorspaceSettings &settings = image->colorspace_settings;
240
241 /* Resolve as if reading a file storing name and interop_id. */
242 auto resolve = [&](const char *name, const char *interop_id) -> std::string {
243 STRNCPY(settings.name, name);
244 STRNCPY(settings.interop_id, interop_id);
246 return settings.name;
247 };
248
249 /* Set along with the name, except for roles. */
250 IMB_colormanagement_colorspace_settings_set(&settings, "Non-Color");
251 EXPECT_STREQ(settings.interop_id, "data");
252 IMB_colormanagement_colorspace_settings_set(&settings, "scene_linear");
253 EXPECT_STREQ(settings.interop_id, "");
254 /* Color spaces with non-primary interop IDs don't store it. */
255 IMB_colormanagement_colorspace_settings_set(&settings, "Filmic sRGB");
256 EXPECT_STREQ(settings.interop_id, "");
257
258 /* The interop ID takes precedence name. */
259 EXPECT_EQ(resolve("sRGB", "lin_rec709_scene"), "Linear Rec.709");
260 /* Unknown interop IDs fall back to the name. */
261 EXPECT_EQ(resolve("sRGB", "unknown_scene"), "sRGB");
262 EXPECT_EQ(resolve("Unknown", "unknown_scene"), "");
263
264 /* Names of another config found by interop ID. */
265 EXPECT_EQ(resolve("Raw", "data"), "Non-Color");
266 EXPECT_STREQ(settings.interop_id, "data");
267 EXPECT_EQ(resolve("Linear Rec.709 (sRGB)", "lin_rec709_scene"), "Linear Rec.709");
268 /* Alternate interop ID is preserved. */
269 EXPECT_EQ(resolve("sRGB - Texture", "srgb_rec709_scene"), "sRGB");
270 EXPECT_STREQ(settings.interop_id, "srgb_rec709_scene");
271
272 /* Names set in the same config resolve to themselves. */
273 for (const char *name : {"Filmic sRGB", "AgX Base sRGB", "sRGB", "Non-Color"}) {
274 IMB_colormanagement_colorspace_settings_set(&settings, "Linear Rec.709");
276 const std::string interop_id = settings.interop_id;
277 EXPECT_EQ(resolve(name, interop_id.c_str()), name);
278 }
279
280 /* Interop ID index of named color spaces. */
288
289 /* Test the other way around. */
290 ASSERT_TRUE(IMB_colormanagement_switch_config("ocio://default"));
291 EXPECT_EQ(resolve("Linear Rec.2020", "lin_rec2020_scene"), "Linear Rec.2020");
292 EXPECT_EQ(resolve("ACEScg", "lin_ap1_scene"), "ACEScg");
293 EXPECT_EQ(resolve("Some ACEScct", "acescct_ap1"), "ACEScct");
294}
295
297{
298 auto alternate = [](const char *name) -> std::string {
300 };
301
302 /* Scene or display variant that is an alias of the color space. */
303 EXPECT_EQ(alternate("sRGB"), "srgb_rec709_scene");
304 EXPECT_EQ(alternate("Linear Rec.709"), "lin_rec709_display");
305
306 /* No alias, or a separate color space. */
307 EXPECT_EQ(alternate("ACEScg"), "");
308 EXPECT_EQ(alternate("Rec.1886"), "");
309
310 /* Separate color spaces for scene and display. */
311 EXPECT_EQ(IMB_colormanagement_space_from_interop_id("g24_rec709_display")->name(), "Rec.1886");
312 EXPECT_EQ(IMB_colormanagement_space_from_interop_id("g24_rec709_scene")->name(),
313 "Gamma 2.4 Encoded Rec.709");
314}
315
317 public:
318 static void SetUpTestSuite()
319 {
320 /* Point at the release datafiles, so startup finds the blender config. */
321 char datafiles[FILE_MAX];
322 BLI_path_join(datafiles,
323 sizeof(datafiles),
324 blender::tests::flags_test_release_dir().c_str(),
325 "datafiles");
326 BLI_setenv("BLENDER_SYSTEM_DATAFILES", datafiles);
328 BKE_tempdir_init(nullptr);
329 }
330
331 static void TearDownTestSuite()
332 {
335 BLI_setenv("BLENDER_SYSTEM_DATAFILES", nullptr);
336 }
337
338 protected:
339 Main *bmain_ = nullptr;
340
341 void SetUp() override
342 {
345
346 colormanage_environment_setup_for_test(std::nullopt, std::nullopt);
348 }
349
350 void TearDown() override
351 {
352 colormanage_environment_setup_for_test(std::nullopt, std::nullopt);
355 }
356
357 /* Switch to the config at #path, a file path or `ocio://` URI. */
365
366 /* Switch back to the blender config, by leaving the project. */
373
374 /* Expect #source and #unresolved_path to be active, with `OCIO` set to
375 * #resolved_path so that OpenColorIO and subprocesses see the same config. */
377 const StringRef unresolved_path,
378 const StringRef resolved_path)
379 {
382
383 const char *ocio_env = BLI_getenv("OCIO");
384 ASSERT_NE(ocio_env, nullptr);
385 EXPECT_EQ(BLI_path_cmp_normalized(ocio_env, std::string(resolved_path).c_str()), 0)
386 << ocio_env << " != " << resolved_path;
387 }
388
389 static void expect_active_config(const ColorManagedConfigSource source, const StringRef path)
390 {
391 expect_active_config(source, path, path);
392 }
393};
394
395TEST_F(ColorManagementProjectConfigTest, blender_config_without_environment)
396{
398}
399
401{
402 colormanage_environment_setup_for_test(std::nullopt, "ocio://default");
404 expect_active_config(ColorManagedConfigSource::EnvOCIO, "ocio://default");
405}
406
407TEST_F(ColorManagementProjectConfigTest, blender_ocio_env_replaces_ocio_env)
408{
409 /* Not tried alongside `OCIO`, so a broken `BLENDER_OCIO` does not reach it. */
410 colormanage_environment_setup_for_test("ocio://default", "/does/not/exist/config.ocio");
412 expect_active_config(ColorManagedConfigSource::EnvBlenderOCIO, "ocio://default");
413
414 colormanage_environment_setup_for_test("/does/not/exist/config.ocio", "ocio://default");
417}
418
419TEST_F(ColorManagementProjectConfigTest, empty_blender_ocio_env_hides_ocio_env)
420{
421 colormanage_environment_setup_for_test("", "ocio://default");
424}
425
426TEST_F(ColorManagementProjectConfigTest, broken_ocio_env_falls_back_to_blender)
427{
428 colormanage_environment_setup_for_test(std::nullopt, "/does/not/exist/config.ocio");
431}
432
433TEST_F(ColorManagementProjectConfigTest, broken_ocio_env_falls_back_to_project)
434{
435 /* A stale `OCIO` must not cost the user their project's config. */
436 colormanage_environment_setup_for_test(std::nullopt, "/does/not/exist/config.ocio");
437 ASSERT_TRUE(switch_to_project_config("ocio://default"));
438 expect_active_config(ColorManagedConfigSource::Project, "ocio://default");
439}
440
441TEST_F(ColorManagementProjectConfigTest, project_config_through_project_root)
442{
443 /* A config shipped inside the project. */
444 char color_dir[FILE_MAX];
445 BLI_path_join(color_dir, sizeof(color_dir), BKE_tempdir_session(), "color");
446 char config_path[FILE_MAX];
447 BLI_path_join(config_path, sizeof(config_path), color_dir, BCM_CONFIG_FILE);
448 ASSERT_TRUE(BLI_dir_create_recursive(color_dir));
449 ASSERT_EQ(BLI_copy(blender_config_path().c_str(), config_path), 0);
450
451 ASSERT_TRUE(switch_to_project_config("{project_root}/color/" BCM_CONFIG_FILE));
452 expect_active_config(
453 ColorManagedConfigSource::Project, "{project_root}/color/" BCM_CONFIG_FILE, config_path);
454}
455
456TEST_F(ColorManagementProjectConfigTest, broken_project_config_falls_back_to_blender)
457{
458 EXPECT_FALSE(switch_to_project_config("{project_root}/missing/config.ocio"));
460}
461
463{
464 ASSERT_TRUE(switch_to_project_config("ocio://default"));
465 expect_active_config(ColorManagedConfigSource::Project, "ocio://default");
466
467 ASSERT_TRUE(switch_to_project_config(blender_config_path()));
469
470 ASSERT_TRUE(switch_to_blender_config());
472}
473
474TEST_F(ColorManagementProjectConfigTest, reload_only_when_config_changes)
475{
476 /* Check we don't do unnecessary reloads. */
478
479 ASSERT_TRUE(switch_to_project_config("ocio://default"));
481 EXPECT_FALSE(switch_to_project_config("ocio://default"));
482}
483
484} // namespace blender::imbuf::tests
File and directory operations.
#define FILE_MAX
#define BLI_path_join(...)
#define BCM_CONFIG_FILE
constexpr IndexRange()=default
void append(const T &value)
static void expect_active_config(const ColorManagedConfigSource source, const StringRef path)
static void expect_active_config(const ColorManagedConfigSource source, const StringRef unresolved_path, const StringRef resolved_path)
virtual StringRefNull name() const =0
virtual StringRefNull alternate_interop_id() const =0
virtual const ColorSpace * get_color_space_by_index(int index) const =0
virtual int get_num_active_color_spaces() const =0
virtual int get_num_all_color_spaces() const =0
#define EXPECT_FALSE(result)
#define EXPECT_TRUE(result)
static std::string blender_config_path()
TEST_F(ColorManagementConfigSwitchTest, colorspace_pointers_survive_switch)
static std::string write_config_with_extra_colorspace(const std::string &config_path)
static const char * extra_colorspace_name
void IMB_colormanagement_assign_float_colorspace(ImBuf *ibuf, const char *name)
void BKE_tempdir_session_purge()
Definition appdir.cc:1293
char * STRNCPY(char(&dst)[N], const char *src)
int IMB_colormanagement_colorspace_get_named_index(const char *name)
StringRefNull IMB_colormanagement_config_path_get()
bool BLI_dir_create_recursive(const char *dirname) ATTR_NONNULL()
Definition fileops_c.cc:416
Main * BKE_main_new()
Definition main.cc:93
void BLI_setenv(const char *env, const char *val) ATTR_NONNULL(1)
const char * BKE_tempdir_session() ATTR_WARN_UNUSED_RESULT ATTR_RETURNS_NONNULL
Definition appdir.cc:1283
void colormanage_environment_setup_for_test(std::optional< std::string > blender_ocio_env, std::optional< std::string > ocio_env)
void BKE_blender_project_clear()
void IMB_colormanagement_colorspace_to_scene_linear_v3(float pixel[3], const ColorSpace *colorspace)
void IMB_freeImBuf(ImBuf *ibuf)
bool IMB_colormanagement_switch_config(const char *filepath)
bool colormanage_config_reload(Main *bmain)
const char * IMB_colormanagement_role_colorspace_name_get(int role)
int IMB_colormanagement_colorspace_get_interop_id_index(const char *name, const char *interop_id)
int BLI_path_cmp_normalized(const char *p1, const char *p2) ATTR_NONNULL(1
void IMB_colormanagement_colorspace_settings_set(ColorManagedColorspaceSettings *settings, const char *name)
ColorManagedConfigSource IMB_colormanagement_config_source_get()
void IMB_colormanagement_assign_byte_colorspace(ImBuf *ibuf, const char *name)
ImBuf * IMB_allocImBuf(unsigned int x, unsigned int y, ImBufFlags flags)
void BKE_main_free(Main *bmain)
Definition main.cc:202
EXPECT_EQ(BLI_expr_pylike_eval(expr, nullptr, 0, &result), EXPR_PYLIKE_INVALID)
bool BKE_blender_project_init(blender::StringRef name, blender::StringRef root_path)
int BLI_copy(const char *path_src, const char *path_dst) ATTR_NONNULL()
const ColorSpace * IMB_colormanagement_space_from_interop_id(StringRefNull interop_id)
ocio::Config ColorManagedConfig
const ColorSpace * IMB_colormanagement_space_get_named(const char *name)
ColorManagedConfig & IMB_colormanagement_get_config()
void * BKE_id_new(Main *bmain, short type, const char *name)
Definition lib_id.cc:1612
void IMB_colormanagement_check_file_config(Main *bmain)
void BKE_tempdir_init(const char *userdir)
Definition appdir.cc:1245
const char * BLI_getenv(const char *env) ATTR_NONNULL(1) ATTR_WARN_UNUSED_RESULT
auto BKE_blender_project_write_callback(const Main *bmain, Fn lambda)
ocio::ColorSpace ColorSpace
Definition BLF_api.hh:42
const ColorSpace * colorspace
const ColorSpace * colorspace
ImBufFloatBuffer float_buffer
ImBufByteBuffer byte_buffer