ocio 0.3.1

A pure Rust port of OpenColorIO, the color management framework for visual effects and animation.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
// SPDX-License-Identifier: BSD-3-Clause
// Copyright Contributors to the OpenColorIO Project.
// Copyright (c) 2026 Philippe Llerena.

//! Port of `Config_tests.cpp` (part 7: adding/removing color spaces and
//! inactive color spaces).

mod config_common;

use config_common::profiles::*;
use config_common::*;
use ocio::config::logging::LogGuard;
use ocio::config::ColorSpace;
use ocio::*;

#[test]
fn config_add_color_space() {
    let _lock = env_lock();
    let s = format!(
        "{}    from_scene_reference: !<MatrixTransform> {{offset: [-1, -2, -3, -4]}}\n",
        profile_v2_start()
    );
    let mut config = Config::create_from_str(&s).unwrap().create_editable_copy();
    config.validate().unwrap();
    assert_eq!(config.num_color_spaces(), 3);

    let mut cs = ColorSpace::default();
    cs.set_name("astéroïde");
    cs.set_description("é À Â Ç É È ç -- $ € 円 £ 元");
    cs.set_transform(
        Some(FixedFunctionTransform::new(FixedFunctionStyle::AcesRedMod03, &[]).into()),
        ColorSpaceDirection::ToReference,
    );

    let name = "astéroïde";
    assert_eq!(config.index_for_color_space(name), None);
    config.add_color_space(&cs).unwrap();
    assert_eq!(config.index_for_color_space(name), Some(3));

    let res = format!(
        "{s}\n  - !<ColorSpace>\n    name: {name}\n    family: \"\"\n    equalitygroup: \"\"\n    bitdepth: unknown\n    description: é À Â Ç É È ç -- $ € 円 £ 元\n    isdata: false\n    allocation: uniform\n    to_scene_reference: !<FixedFunctionTransform> {{style: ACES_RedMod03}}\n"
    );
    assert_eq!(config.serialize().unwrap(), res);

    config.remove_color_space(name);
    assert_eq!(config.num_color_spaces(), 3);
    assert_eq!(config.index_for_color_space(name), None);

    config.clear_color_spaces();
    assert_eq!(config.num_color_spaces(), 0);
}

#[test]
fn config_faulty_config_file() {
    let _lock = env_lock();
    assert_err!(
        Config::create_from_str("/usr/tmp/not_existing.ocio"),
        "Error: Loading the OCIO profile failed."
    );
}

#[test]
fn config_remove_color_space() {
    let _lock = env_lock();
    let s = format!(
        "{}    from_scene_reference: !<MatrixTransform> {{offset: [-1, -2, -3, -4]}}\n\n  - !<ColorSpace>\n    name: cs5\n    allocation: uniform\n    to_scene_reference: !<FixedFunctionTransform> {{style: ACES_RedMod03}}\n",
        profile_v2_start()
    );
    let mut config = Config::create_from_str(&s).unwrap().create_editable_copy();
    config.validate().unwrap();
    assert_eq!(config.num_color_spaces(), 4);

    assert_eq!(config.index_for_color_space("cs5"), Some(3));
    config.remove_color_space("cs5");
    assert_eq!(config.num_color_spaces(), 3);
    assert_eq!(config.index_for_color_space("cs5"), None);

    config.remove_color_space("cs5");
    config.validate().unwrap();

    config.remove_color_space("scene_linear");
    config.validate().unwrap();

    config.remove_color_space("raw");
    assert_err!(
        config.validate(),
        "Config failed role validation. The role 'default' refers to a color space, 'raw', which is not defined."
    );
}

const INACTIVE_START: &str = r#"ocio_profile_version: 2

environment:
  {}
search_path: luts
strictparsing: true
luma: [0.2126, 0.7152, 0.0722]

roles:
  default: raw
  scene_linear: lnh

file_rules:
  - !<Rule> {name: Default, colorspace: default}

displays:
  sRGB:
    - !<View> {name: Raw, colorspace: raw}
    - !<View> {name: Lnh, colorspace: lnh, looks: beauty}

active_displays: []
active_views: []
"#;

const INACTIVE_END: &str = r#"
looks:
  - !<Look>
    name: beauty
    process_space: lnh
    transform: !<CDLTransform> {slope: [1, 2, 1]}


colorspaces:
  - !<ColorSpace>
    name: raw
    family: ""
    equalitygroup: ""
    bitdepth: unknown
    isdata: false
    allocation: uniform

  - !<ColorSpace>
    name: lnh
    family: ""
    equalitygroup: ""
    bitdepth: unknown
    isdata: false
    allocation: uniform

  - !<ColorSpace>
    name: cs1
    aliases: [alias1]
    family: ""
    equalitygroup: ""
    bitdepth: unknown
    isdata: false
    categories: [file-io]
    allocation: uniform
    from_scene_reference: !<CDLTransform> {offset: [0.1, 0.1, 0.1]}

  - !<ColorSpace>
    name: cs2
    family: ""
    equalitygroup: ""
    bitdepth: unknown
    isdata: false
    categories: [working-space]
    allocation: uniform
    from_scene_reference: !<CDLTransform> {offset: [0.2, 0.2, 0.2]}

  - !<ColorSpace>
    name: cs3
    family: ""
    equalitygroup: ""
    bitdepth: unknown
    isdata: false
    categories: [cat3]
    allocation: uniform
    from_scene_reference: !<CDLTransform> {offset: [0.3, 0.3, 0.3]}
"#;

use ColorSpaceVisibility as Vis;
use SearchReferenceSpaceType as Ref;

fn n(c: &Config, r: Ref, v: Vis) -> usize {
    c.num_color_spaces_filtered(r, v)
}

fn name(c: &Config, r: Ref, v: Vis, i: usize) -> String {
    c.color_space_name_by_index_filtered(r, v, i).to_string()
}

fn active_names(c: &Config) -> Vec<String> {
    (0..c.num_color_spaces())
        .map(|i| c.color_space_name_by_index(i).to_string())
        .collect()
}

#[track_caller]
fn check_counts(c: &Config, all: [usize; 3], scene: [usize; 3], display: [usize; 3]) {
    for (r, e) in [
        (Ref::All, all),
        (Ref::Scene, scene),
        (Ref::Display, display),
    ] {
        assert_eq!(n(c, r, Vis::Inactive), e[0], "{r:?} inactive");
        assert_eq!(n(c, r, Vis::Active), e[1], "{r:?} active");
        assert_eq!(n(c, r, Vis::All), e[2], "{r:?} all");
    }
}

fn inactive_config(extra: &str) -> Config {
    Config::create_from_str(&format!("{INACTIVE_START}{extra}{INACTIVE_END}"))
        .unwrap()
        .create_editable_copy()
}

#[test]
fn config_inactive_color_space() {
    let _lock = env_lock();
    let _g = EnvGuard::set(OCIO_INACTIVE_COLORSPACES_ENVVAR, None);
    let mut config = inactive_config("");
    config.validate().unwrap();

    // Step 1 - No inactive color spaces.
    check_counts(&config, [0, 5, 5], [0, 5, 5], [0, 0, 0]);
    let all: Vec<String> = (0..6)
        .map(|i| name(&config, Ref::All, Vis::All, i))
        .collect();
    assert_eq!(all, ["raw", "lnh", "cs1", "cs2", "cs3", ""]);
    assert_eq!(active_names(&config), ["raw", "lnh", "cs1", "cs2", "cs3"]);
    assert_eq!(config.color_space_name_by_index(5), "");
    assert_eq!(config.color_spaces("").num_color_spaces(), 5);
    assert_eq!(
        config.get_color_space("scene_linear").unwrap().name(),
        "lnh"
    );
    assert_eq!(config.index_for_color_space("scene_linear"), Some(1));
    assert_eq!(config.index_for_color_space("lnh"), Some(1));

    // Step 2 - Some inactive color spaces.
    config.set_inactive_color_spaces("lnh, cs1");
    assert_eq!(config.inactive_color_spaces(), "lnh, cs1");
    check_counts(&config, [2, 3, 5], [2, 3, 5], [0, 0, 0]);
    let all: Vec<String> = (0..5)
        .map(|i| name(&config, Ref::All, Vis::All, i))
        .collect();
    assert_eq!(all, ["raw", "lnh", "cs1", "cs2", "cs3"]);
    assert_eq!(active_names(&config), ["raw", "cs2", "cs3"]);
    assert_eq!(config.color_spaces("").num_color_spaces(), 3);
    assert_eq!(config.color_spaces("file-io").num_color_spaces(), 0);
    assert_eq!(config.color_spaces("working-space").num_color_spaces(), 1);
    assert_eq!(config.get_color_space("cs2").unwrap().name(), "cs2");
    assert_eq!(config.get_color_space("cs1").unwrap().name(), "cs1");
    assert_eq!(config.get_color_space("default").unwrap().name(), "raw");
    assert_eq!(
        config.get_color_space("scene_linear").unwrap().name(),
        "lnh"
    );
    assert_eq!(config.index_for_color_space("scene_linear"), None);
    assert_eq!(config.index_for_color_space("lnh"), None);
    assert_eq!(config.color_space_name_by_index(3), "");
    assert!(config.get_color_space("cs1").is_some());

    // Step 3 - Same as 2, but using role name.
    config.set_inactive_color_spaces("scene_linear, cs1");
    assert_eq!(config.inactive_color_spaces(), "scene_linear, cs1");
    check_counts(&config, [2, 3, 5], [2, 3, 5], [0, 0, 0]);
    assert_eq!(active_names(&config), ["raw", "cs2", "cs3"]);
    assert!(config.has_role("scene_linear"));

    // Step 4 - Same as 2, but using an alias.
    config.set_inactive_color_spaces("lnh, alias1");
    assert_eq!(config.inactive_color_spaces(), "lnh, alias1");
    check_counts(&config, [2, 3, 5], [2, 3, 5], [0, 0, 0]);
    assert_eq!(active_names(&config), ["raw", "cs2", "cs3"]);

    // Step 5 & 6 - No inactive color spaces.
    config.set_inactive_color_spaces("");
    assert_eq!(config.inactive_color_spaces(), "");
    assert_eq!(n(&config, Ref::All, Vis::All), 5);
    assert_eq!(config.num_color_spaces(), 5);
    assert_eq!(n(&config, Ref::Scene, Vis::All), 5);
    assert_eq!(n(&config, Ref::Display, Vis::All), 0);

    // Step 7 - Add display color spaces.
    for d in ["display0", "display1", "display2"] {
        let mut dcs = ColorSpace::new(ReferenceSpaceType::Display);
        dcs.set_name(d);
        config.add_color_space(&dcs).unwrap();
    }
    assert_eq!(n(&config, Ref::All, Vis::All), 8);
    assert_eq!(n(&config, Ref::Scene, Vis::All), 5);
    assert_eq!(n(&config, Ref::Display, Vis::All), 3);

    // Step 8 - Some inactive color spaces.
    config.set_inactive_color_spaces("cs1, display1");
    assert_eq!(config.inactive_color_spaces(), "cs1, display1");
    check_counts(&config, [2, 6, 8], [1, 4, 5], [1, 2, 3]);
    assert_eq!(name(&config, Ref::Scene, Vis::Inactive, 0), "cs1");
    assert_eq!(name(&config, Ref::Display, Vis::Inactive, 0), "display1");
    assert_eq!(name(&config, Ref::Scene, Vis::Inactive, 1), "");
    assert_eq!(name(&config, Ref::Display, Vis::Inactive, 1), "");
    assert_eq!(name(&config, Ref::Scene, Vis::Active, 2), "cs2");
    assert_eq!(name(&config, Ref::Display, Vis::Active, 1), "display2");
    assert_eq!(name(&config, Ref::Scene, Vis::All, 0), "raw");
    assert_eq!(name(&config, Ref::Scene, Vis::All, 3), "cs2");
    assert_eq!(name(&config, Ref::Scene, Vis::All, 10), "");
    assert_eq!(name(&config, Ref::Display, Vis::All, 1), "display1");
}

#[test]
fn config_inactive_color_space_processors() {
    let _lock = env_lock();
    let _g = EnvGuard::set(OCIO_INACTIVE_COLORSPACES_ENVVAR, None);
    let mut config = inactive_config("");
    config.set_inactive_color_spaces("lnh, cs1");
    let dst = config
        .display_view_color_space_name("sRGB", "Lnh")
        .to_string();
    let lt: Transform = LookTransform::new("raw", &dst, "beauty").into();
    config
        .get_processor_for_transform(&lt, TransformDirection::Forward)
        .unwrap();
    config.get_processor("lnh", "cs1").unwrap();
    config.get_processor("raw", "cs1").unwrap();
    config.get_processor("lnh", "cs2").unwrap();
    config.get_processor("cs2", "scene_linear").unwrap();
}

#[test]
fn config_is_inactive() {
    let _lock = env_lock();
    let config =
        Config::create_from_builtin_config("studio-config-v1.0.0_aces-v1.3_ocio-v2.1").unwrap();
    config.validate().unwrap();
    assert!(!config.is_inactive_color_space(""));
    assert!(!config.is_inactive_color_space("fake-colorspace-name"));
    assert!(!config.is_inactive_color_space("Linear P3-D65"));
    assert!(config.is_inactive_color_space("Rec.1886 Rec.2020 - Display"));
}

#[test]
fn config_is_inactive_local() {
    let _lock = env_lock();
    // Same checks as `config_is_inactive` using a local config.
    let _g = EnvGuard::set(OCIO_INACTIVE_COLORSPACES_ENVVAR, None);
    let config = inactive_config("inactive_colorspaces: [cs2]\n");
    assert!(!config.is_inactive_color_space(""));
    assert!(!config.is_inactive_color_space("fake-colorspace-name"));
    assert!(!config.is_inactive_color_space("cs1"));
    assert!(config.is_inactive_color_space("cs2"));
}

#[test]
fn config_inactive_color_space_precedence() {
    let _lock = env_lock();
    let _g = EnvGuard::set(OCIO_INACTIVE_COLORSPACES_ENVVAR, None);
    let config = inactive_config("inactive_colorspaces: [cs2]\n");
    config.validate().unwrap();
    assert_eq!(n(&config, Ref::All, Vis::Inactive), 1);
    assert_eq!(n(&config, Ref::All, Vis::Active), 4);
    assert_eq!(n(&config, Ref::All, Vis::All), 5);
    assert_eq!(active_names(&config), ["raw", "lnh", "cs1", "cs3"]);

    // Env. variable supersedes the config content.
    let _g2 = EnvGuard::set(OCIO_INACTIVE_COLORSPACES_ENVVAR, Some("cs3, cs1, lnh"));
    let mut config = inactive_config("inactive_colorspaces: [cs2]\n");
    config.validate().unwrap();
    assert_eq!(n(&config, Ref::All, Vis::Inactive), 3);
    assert_eq!(n(&config, Ref::All, Vis::Active), 2);
    assert_eq!(n(&config, Ref::All, Vis::All), 5);
    assert_eq!(active_names(&config), ["raw", "cs2"]);

    // An API request supersedes the lists from the env. variable and the config file.
    config.set_inactive_color_spaces("cs1, lnh");
    assert_eq!(n(&config, Ref::All, Vis::Inactive), 2);
    assert_eq!(n(&config, Ref::All, Vis::Active), 3);
    assert_eq!(n(&config, Ref::All, Vis::All), 5);
    assert_eq!(active_names(&config), ["raw", "cs2", "cs3"]);
}

#[test]
fn config_inactive_color_space_read_write() {
    let _lock = env_lock();
    let _g = EnvGuard::set(OCIO_INACTIVE_COLORSPACES_ENVVAR, None);
    {
        let s = format!("{INACTIVE_START}inactive_colorspaces: [cs2]\n{INACTIVE_END}");
        let config = Config::create_from_str(&s).unwrap();
        config.validate().unwrap();
        assert_eq!(n(&config, Ref::All, Vis::All), 5);
        assert_eq!(config.num_color_spaces(), 4);
        assert_eq!(config.serialize().unwrap(), s);
    }
    {
        let _g2 = EnvGuard::set(OCIO_INACTIVE_COLORSPACES_ENVVAR, Some("cs3, cs1, lnh"));
        let s = format!("{INACTIVE_START}inactive_colorspaces: [cs2]\n{INACTIVE_END}");
        let config = Config::create_from_str(&s).unwrap();
        {
            let _log = LogGuard::new();
            config.validate().unwrap();
        }
        assert_eq!(n(&config, Ref::All, Vis::All), 5);
        assert_eq!(config.num_color_spaces(), 2);
        assert_eq!(config.serialize().unwrap(), s);
    }
    {
        let s = format!(
            "{INACTIVE_START}inactive_colorspaces: [cs1\t\n   \n,   \ncs2]\n{INACTIVE_END}"
        );
        let config = Config::create_from_str(&s).unwrap();
        config.validate().unwrap();
        assert_eq!(n(&config, Ref::All, Vis::All), 5);
        assert_eq!(config.num_color_spaces(), 3);
        assert_eq!(
            config.serialize().unwrap(),
            format!("{INACTIVE_START}inactive_colorspaces: [cs1, cs2]\n{INACTIVE_END}")
        );
    }
    {
        let s = format!("{INACTIVE_START}inactive_colorspaces: []\n{INACTIVE_END}");
        let config = Config::create_from_str(&s).unwrap();
        config.validate().unwrap();
        assert_eq!(config.num_color_spaces(), 5);
        assert_eq!(
            config.serialize().unwrap(),
            format!("{INACTIVE_START}{INACTIVE_END}")
        );
    }
    {
        let s = format!("{INACTIVE_START}inactive_colorspaces: [unknown]\n{INACTIVE_END}");
        let config = Config::create_from_str(&s).unwrap();
        {
            let log = LogGuard::new();
            config.validate().unwrap();
            assert_eq!(
                log.output(),
                "[OpenColorIO Info]: Inactive 'unknown' is neither a color space nor a named transform.\n"
            );
        }
        assert_eq!(n(&config, Ref::All, Vis::All), 5);
        assert_eq!(config.num_color_spaces(), 5);
        assert_eq!(config.serialize().unwrap(), s);
    }
}