ocio 0.1.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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! Port of `MergeConfigsHelpers_tests.cpp` (part 5: merges driven by OCIOM
//! files, OCIOZ archives, in memory configs and single color spaces).

use super::tests::*;
use super::*;
use crate::transforms::Transform;
use crate::types::*;

/// First value of the matrix of the first transform of the processor.
#[track_caller]
fn first_matrix_value(config: &Config, ctx: &crate::Context, src: &str, dst: &str) -> f64 {
    let processor = config
        .get_processor_with_context_names(ctx, src, dst)
        .unwrap();
    match processor.create_group_transform().transforms.first() {
        Some(Transform::Matrix(m)) => m.matrix[0],
        other => panic!("expected a matrix transform, got {other:?}"),
    }
}

#[test]
fn merge_configs_merges_with_ociom_file() {
    {
        let ociom_path = merge_file("merged1/merged1.ociom");

        // PreferInput, Input first.
        let merger = ConfigMerger::create_from_file(&ociom_path).unwrap();
        let mut new_merger = None;
        check_for_log_or_exception(
            LogType::Warning,
            || {
                new_merger = Some(merger.merge_configs()?);
                Ok(())
            },
            &[
                "The Input config contains a value that would override the Base config: file_rules: Default",
                "Merged color space 'ACES2065-1' has a conflict with alias 'aces' in color space 'ACEScg'",
                "Equivalent input color space 'sRGB - Display' replaces 'sRGB - Display' in the base config, preserving aliases.",
                "Equivalent input color space 'CIE-XYZ-D65' replaces 'CIE-XYZ-D65' in the base config, preserving aliases.",
                "Equivalent input color space 'ACES2065-1' replaces 'ap0' in the base config, preserving aliases.",
                "Equivalent input color space 'sRGB' replaces 'sRGB - Texture' in the base config, preserving aliases.",
            ],
        );
        let new_merger = new_merger.unwrap();
        let merged = new_merger.merged_config().unwrap();

        // This test is essentially the same as the first sub-test of
        // colorspaces_section_common_reference_and_duplicates, so just do a quick sanity test.
        assert_eq!(merged.name(), "Merged1");
        assert_eq!(merged.description(), "Basic merge with default strategy");
        assert_eq!(
            merged.num_color_spaces_filtered(
                SearchReferenceSpaceType::All,
                ColorSpaceVisibility::All
            ),
            7
        );
    }

    // Test is similar to the previous one but it has two merges in the OCIOM file and it is
    // using the output of the first merged config as the input for the second merge.
    {
        let ociom_path = merge_file("merged2/merged.ociom");
        let merger = ConfigMerger::create_from_file(&ociom_path).unwrap();
        let mut new_merger = None;
        check_for_log_or_exception(
            LogType::Warning,
            || {
                new_merger = Some(merger.merge_configs()?);
                Ok(())
            },
            &[
                "The Input config contains a value that would override the Base config: file_rules: Default",
                "The Input config contains a role that would override Base config role 'cie_xyz_d65_interchange'",
                "Color space 'sRGB - Display' was not merged as it's already present in the base config",
                "Color space 'CIE-XYZ-D65' was not merged as it's already present in the base config",
                "Merged color space 'ACES2065-1' has a conflict with alias 'aces' in color space 'ACEScg'",
                "Color space 'sRGB' was not merged as it conflicts with an alias in color space 'sRGB - Texture'",
                "Equivalent base color space 'ap0' overrides 'rec709' in the input config, preserving aliases",
            ],
        );
        let new_merger = new_merger.unwrap();
        assert_eq!(new_merger.num_merged_configs(), 2);

        let merged = new_merger.merged_config().unwrap();
        merged.validate().unwrap();

        assert_eq!(merged.name(), "Merged2");
        assert_eq!(merged.description(), "Description override");
        assert_eq!(
            merged.num_color_spaces_filtered(
                SearchReferenceSpaceType::All,
                ColorSpaceVisibility::All
            ),
            7
        );

        // The second merge replaces the file rules of the first merge with the input config.
        let fr = merged.file_rules();
        assert_eq!(fr.num_entries(), 1);
        assert_eq!(fr.name(0).unwrap(), "Default");
        assert_eq!(fr.color_space(0).unwrap(), "sRGB");

        // The second merge replaces the roles of the first merge with the input config.
        assert_eq!(merged.num_roles(), 1);
        assert_eq!(
            merged.role_color_space("cie_xyz_d65_interchange"),
            "CIE-XYZ-D65"
        );

        // The rest of the config should be the result of the first merge.
        assert_eq!(merged.num_displays_all(), 1);
        assert_eq!(merged.display(0), "sRGB - Display");
        assert_eq!(
            merged.num_views_by_type(ViewType::DisplayDefined, "sRGB - Display"),
            2
        );

        let cs = check_color_space(
            merged,
            "sRGB - Display",
            0,
            SearchReferenceSpaceType::Display,
        );
        assert_eq!(cs.num_aliases(), 1);
        assert_eq!(cs.alias(0), "srgb_display");
        assert_eq!(cs.family(), "Display-Basic");
        assert_eq!(cs.description(), "from base");

        let cs = check_color_space(merged, "ACES2065-1", 0, SearchReferenceSpaceType::Scene);
        assert_eq!(cs.num_aliases(), 0);
        assert_eq!(cs.family(), "ACES~Linear");
        assert_eq!(cs.description(), "from input");

        assert_eq!(merged.inactive_color_spaces(), "ACES2065-1");
    }

    // Test with external LUT files.
    {
        let ociom_path = merge_file("merged3/merged.ociom");

        // PreferInput, Input first.
        let merger = ConfigMerger::create_from_file(&ociom_path).unwrap();
        let mut new_merger = None;
        check_for_log_or_exception(
            LogType::Warning,
            || {
                new_merger = Some(merger.merge_configs()?);
                Ok(())
            },
            &["Color space 'raw' will replace a color space in the base config."],
        );
        let new_merger = new_merger.unwrap();
        let merged = new_merger.merged_config().unwrap();
        merged.validate().unwrap();

        assert_eq!(merged.search_path(), "./$SHOT:./shot1:shot2:.");
        let cs = merged.get_color_space("shot1_lut1_cs").unwrap();
        let tf = cs.transform(ColorSpaceDirection::ToReference).unwrap();
        match tf {
            Transform::File(f) => assert_eq!(f.src, "shot1/lut1.clf"),
            other => panic!("expected a file transform, got {other:?}"),
        }
        merged
            .get_processor_for_transform(tf, TransformDirection::Forward)
            .unwrap();

        let look = merged.look("shot_look").unwrap();
        let ltf = look.transform().unwrap();
        merged
            .get_processor_for_transform(ltf, TransformDirection::Forward)
            .unwrap();
    }

    // Test that a merge could go wrong if the search_paths are merged with a different strategy
    // than the other sections.
    {
        let ociom_path = merge_file("merged3/merged.ociom");
        let mut merger = ConfigMerger::create_from_file(&ociom_path).unwrap();
        // Changing the strategy for colorspace merger to INPUT_ONLY. This will break the look
        // "shot_look" (from base) as it needs the search paths from the base config
        // (search_paths are managed by the colorspace merger).
        merger
            .params_mut(0)
            .unwrap()
            .set_colorspaces(MergeStrategy::InputOnly);
        // The rest of the merges uses PreferInput strategy.

        let new_merger = merger.merge_configs().unwrap();
        let merged = new_merger.merged_config().unwrap();

        let look = merged.look("shot_look").unwrap();
        let ltf = look.transform().unwrap();

        // Expected to fail as the search_paths were merged following the InputOnly strategy and
        // the looks were merged following the PreferInput (see OCIOM file default strategy).
        // Therefore, the look's FileTransform can not find "look.cdl".
        assert!(merged
            .get_processor_for_transform(ltf, TransformDirection::Forward)
            .is_err());

        // It can happen with any section that uses the search_paths such as looks, named
        // transforms, and colorspaces.
    }

    // Test with a built-in config.
    {
        let ociom_path = merge_file("merged4/merged.ociom");

        // InputOnly.
        let merger = ConfigMerger::create_from_file(&ociom_path).unwrap();
        let new_merger = merger.merge_configs().unwrap();
        let merged = new_merger.merged_config().unwrap();

        merged.validate().unwrap();

        assert_eq!(merged.name(), "cg-config-v1.0.0_aces-v1.3_ocio-v2.1");
        assert_eq!(
            merged.num_color_spaces_filtered(
                SearchReferenceSpaceType::All,
                ColorSpaceVisibility::All
            ),
            20
        );
    }
}

#[test]
fn merge_configs_merges_with_ocioz_file() {
    let archive = format!(
        "{}/configs/context_test1/context_test1_linux.ocioz",
        test_files_dir()
    );
    let base = Config::create_from_file(&archive).unwrap();
    let input = get_config("merged3/input.ocio");

    let mut params = ConfigMergingParameters::new();
    params.set_input_first(false);
    let strategy = MergeStrategy::PreferInput;
    params.set_roles(strategy);
    params.set_colorspaces(strategy);
    params.set_named_transforms(strategy);
    params.set_default_strategy(strategy);
    params.set_input_family_prefix("Input/");
    params.set_base_family_prefix("Base/");
    params.set_adjust_input_reference_space(false);
    params.set_avoid_duplicates(false);

    let mut merged = None;
    check_for_log_or_exception(
        LogType::Warning,
        || {
            merged = Some(merge_configs(&params, &base, &input)?);
            Ok(())
        },
        &[
            "Color space 'reference' will replace a color space in the base config.",
            "Color space 'raw' will replace a color space in the base config.",
            "Color space 'plain_lut1_cs' will replace a color space in the base config.",
            "Color space 'shot1_lut1_cs' will replace a color space in the base config.",
        ],
    );
    let merged = merged.unwrap();

    // The working dir is the one of the base (OCIO: empty for an archive; this port extracts
    // the archive in a directory used as the working dir).
    assert_eq!(merged.working_dir(), base.working_dir());
    // Note: OCIO also checks that the config IO proxy (i.e. the archive) is kept by the merge,
    // the resolution of the files below from the archive checks it.

    let mut ctx = merged.current_context().clone();

    // This is resolved in the OCIOZ base.
    // Note: the $SHOT = shot4 search path takes precedence for this color space.
    // It is 40 in the base config, 20 in input.
    assert_eq!(
        first_matrix_value(&merged, &ctx, "plain_lut1_cs", "reference"),
        40.0
    );
    // It is 10 in the base config, 100 in input.
    assert_eq!(
        first_matrix_value(&merged, &ctx, "shot1_lut1_cs", "reference"),
        10.0
    );
    // Will try to resolve it using relative paths and won't find it.
    assert!(merged
        .get_processor_with_context_names(&ctx, "shot1_lut2_cs", "reference")
        .is_err());

    // Add an absolute search path for the input config.
    let search_path_input = crate::path_utils::normpath(&format!(
        "{}/configs/mergeconfigs/merged3",
        test_files_dir()
    ));
    ctx.clear_search_paths();
    ctx.add_search_path(&search_path_input);
    for i in 0..merged.num_search_paths() {
        ctx.add_search_path(merged.search_path_by_index(i));
    }

    // It doesn't exist in the base config.
    assert_eq!(
        first_matrix_value(&merged, &ctx, "shot1_lut2_cs", "reference"),
        42.0
    );

    merged.clear_processor_cache();

    // It is 10 in the base config, 100 in input.
    assert_eq!(
        first_matrix_value(&merged, &ctx, "shot1_lut1_cs", "reference"),
        100.0
    );
}

#[test]
fn merge_configs_merge_in_memory_configs() {
    const BASE: &str = r#"ocio_profile_version: 2.1

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

roles:
    a: colorspace_a

colorspaces:
- !<ColorSpace>
    name: colorspace_a
    family: utility
"#;

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

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

colorspaces:
- !<ColorSpace>
    name: B
    family: aces
"#;

    const RESULT: &str = r#"ocio_profile_version: 2.1

roles:
  a: colorspace_a

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

colorspaces:
  - !<ColorSpace>
    name: colorspace_a
    family: Base/utility
    equalitygroup: ""
    bitdepth: unknown
    isdata: false
    allocation: uniform

  - !<ColorSpace>
    name: B
    family: Input/aces
    equalitygroup: ""
    bitdepth: unknown
    isdata: false
    allocation: uniform"#;

    let base = from_str(BASE);
    let input = from_str(INPUT);

    let mut params = ConfigMergingParameters::new();
    params.set_input_first(false);
    let strategy = MergeStrategy::PreferInput;
    params.set_roles(strategy);
    params.set_colorspaces(strategy);
    params.set_named_transforms(strategy);
    params.set_default_strategy(strategy);
    params.set_input_family_prefix("Input/");
    params.set_base_family_prefix("Base/");
    params.set_adjust_input_reference_space(false);
    params.set_avoid_duplicates(false);

    let mut merged = None;
    check_for_log_or_exception(
        LogType::Warning,
        || {
            merged = Some(merge_configs(&params, &base, &input)?);
            Ok(())
        },
        &["The Input config contains a value that would override the Base config: file_rules: Default"],
    );
    let merged = merged.unwrap();

    let result = from_str(RESULT);
    assert_eq!(merged.serialize().unwrap(), result.serialize().unwrap());
}

#[test]
fn merge_configs_merge_single_colorspace() {
    const BASE: &str = r#"ocio_profile_version: 2

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

roles:
    a: colorspace_a

colorspaces:
- !<ColorSpace>
    name: colorspace_a
    family: utility
"#;

    const INPUT: &str = r#"ocio_profile_version: 2.1

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

colorspaces:
- !<ColorSpace>
    name: B
    family: aces
"#;

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

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

roles:
  a: colorspace_a

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

displays:
  {}

active_displays: []
active_views: []

colorspaces:
  - !<ColorSpace>
    name: colorspace_a
    family: Base/utility
    equalitygroup: ""
    bitdepth: unknown
    isdata: false
    allocation: uniform

  - !<ColorSpace>
    name: B
    family: Input/aces
    equalitygroup: ""
    bitdepth: unknown
    isdata: false
    allocation: uniform"#;

    let base = from_str(BASE);
    let input = from_str(INPUT);
    let colorspace = input.get_color_space("B").unwrap();

    let mut params = ConfigMergingParameters::new();
    params.set_input_first(false);
    let strategy = MergeStrategy::PreferInput;
    params.set_roles(strategy);
    params.set_colorspaces(strategy);
    params.set_named_transforms(strategy);
    params.set_default_strategy(strategy);
    params.set_input_family_prefix("Input/");
    params.set_base_family_prefix("Base/");
    params.set_adjust_input_reference_space(false);
    params.set_avoid_duplicates(false);

    let merged = merge_color_space(&params, &base, colorspace).unwrap();

    let result = from_str(RESULT);
    assert_eq!(merged.serialize().unwrap(), result.serialize().unwrap());
}