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
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
// SPDX-License-Identifier: BSD-3-Clause
// Copyright Contributors to the OpenColorIO Project.
// Copyright (c) 2026 Philippe Llerena.

//! Config validation (`Config::validate`) and version consistency checks.

use super::utils::compare;
use super::*;

impl Config {
    fn fail(&self, msg: String) -> Error {
        if let Ok(mut v) = self.validation.lock() {
            v.1 = msg.clone();
        }
        Error::msg(msg)
    }

    fn validate_view(
        &self,
        display: &str,
        view: &View,
        check_use_display_name: bool,
    ) -> Result<()> {
        if view.name.is_empty() {
            return Err(self.fail(prefix_error_msg(display, view)));
        }
        let shared_with_vt = display.is_empty() && !view.view_transform.is_empty();
        if view.colorspace.is_empty() {
            return Err(self.fail(format!(
                "{}does not refer to a color space.",
                prefix_error_msg(display, view)
            )));
        }
        if check_use_display_name && !shared_with_vt && view.use_display_name_for_colorspace() {
            return Err(self.fail(format!(
                "{}can not use '{}' keyword for the color space name.",
                prefix_error_msg(display, view),
                OCIO_VIEW_USE_DISPLAY_NAME
            )));
        }
        if !view.use_display_name_for_colorspace()
            && !self.all_color_spaces.has_color_space(&view.colorspace)
            && self.get_named_transform(&view.colorspace).is_none()
        {
            return Err(self.fail(format!(
                "{}that refers to a color space or a named transform, '{}', which is not defined.",
                prefix_error_msg(display, view),
                view.colorspace
            )));
        }
        if !view.view_transform.is_empty() {
            if self.get_named_transform(&view.view_transform).is_none()
                && self.view_transform(&view.view_transform).is_none()
            {
                return Err(self.fail(format!(
                    "{}that refers to a view transform, '{}', which is neither a view transform nor a named transform.",
                    prefix_error_msg(display, view),
                    view.view_transform
                )));
            }
            let display_cs = if view.use_display_name_for_colorspace() {
                display
            } else {
                view.colorspace.as_str()
            };
            if let Some(cs) = self.get_color_space(display_cs) {
                if cs.reference_space_type() != ReferenceSpaceType::Display {
                    return Err(self.fail(format!(
                        "{}refers to a color space, '{}', that is not a display-referred color space.",
                        prefix_error_msg(display, view),
                        display_cs
                    )));
                }
            }
        }
        let mut looks = LookParseResult::new();
        for option in looks.parse(&view.looks) {
            for token in option {
                if !token.name.is_empty() && self.look(&token.name).is_none() {
                    return Err(self.fail(format!(
                        "{}refers to a look, '{}', which is not defined.",
                        prefix_error_msg(display, view),
                        token.name
                    )));
                }
            }
        }
        if !view.rule.is_empty() && self.viewing_rules.find_rule(&view.rule).is_none() {
            return Err(self.fail(format!(
                "{}refers to a viewing rule, '{}', which is not defined.",
                prefix_error_msg(display, view),
                view.rule
            )));
        }
        Ok(())
    }

    fn validate_shared_view(
        &self,
        display: &str,
        views_of_display: &[View],
        shared_view: &str,
        check_use_display_name: bool,
    ) -> Result<()> {
        if find_view(views_of_display, shared_view).is_some() {
            return Err(self.fail(format!(
                "Config failed view validation. The display '{display}' contains a shared view '{shared_view}' that is already defined as a view."
            )));
        }
        match find_view(&self.shared_views, shared_view) {
            None => Err(self.fail(format!(
                "Config failed view validation. The display '{display}' contains a shared view '{shared_view}' that is not defined."
            ))),
            Some(i) => {
                let view = &self.shared_views[i];
                if check_use_display_name && !view.view_transform.is_empty() && view.use_display_name_for_colorspace() {
                    match self.get_color_space(display) {
                        None => {
                            return Err(self.fail(format!(
                                "Config failed view validation. The display '{}' contains a shared view '{}' which does not define a color space and there is no color space that matches the display name.",
                                display, view.name
                            )))
                        }
                        Some(cs) if cs.reference_space_type() != ReferenceSpaceType::Display => {
                            return Err(self.fail(format!(
                                "Config failed view validation. The display '{}' contains a shared view '{}' that refers to a color space, '{}', that is not a display-referred color space.",
                                display, view.name, display
                            )))
                        }
                        _ => {}
                    }
                }
                Ok(())
            }
        }
    }

    /// Validate the config (the result is cached until the config changes).
    pub fn validate(&self) -> Result<()> {
        {
            let v = self
                .validation
                .lock()
                .map(|v| v.clone())
                .unwrap_or((Validation::Unknown, String::new()));
            match v.0 {
                Validation::Passed => return Ok(()),
                Validation::Failed => return Err(Error::msg(v.1)),
                Validation::Unknown => {}
            }
        }
        if let Ok(mut v) = self.validation.lock() {
            *v = (Validation::Failed, String::new());
        }
        self.validate_impl()?;
        if let Ok(mut v) = self.validation.lock() {
            v.0 = Validation::Passed;
        }
        Ok(())
    }

    fn validate_impl(&self) -> Result<()> {
        // Predefined context variables.
        if self.major_version >= 2
            && self.context.environment_mode() == EnvironmentMode::LoadPredefined
        {
            for (k, v) in &self.env {
                if contains_context_variables(v) {
                    let valid = *v == format!("${k}")
                        || *v == format!("${{{k}}}")
                        || *v == format!("%{k}%");
                    if !valid {
                        return Err(Error::msg(format!(
                            "Unresolved context variable in environment declaration '{k} = {v}'."
                        )));
                    }
                }
            }
        }

        // Color spaces.
        let mut has_display_referred = false;
        let mut has_scene_referred = false;
        for cs in self.all_color_spaces.iter() {
            let name = cs.name();
            if self.major_version >= 2 && contains_context_variable_token(name) {
                return Err(self.fail(format!(
                    "Config failed color space validation. A color space name '{name}' cannot contain a context variable reserved token i.e. % or $."
                )));
            }
            if cs.num_aliases() > 0 && self.major_version < 2 {
                return Err(self.fail(format!(
                    "Config failed color space validation. Aliases may not be used in a v1 config.  Color space name: '{name}'."
                )));
            }
            let interop = cs.interop_id();
            if !interop.is_empty() && self.get_color_space(interop).is_none() {
                return Err(self.fail(format!(
                    "Config failed color space validation. The color space '{name}' refers to an interop ID, '{interop}', which is not a color space name or alias."
                )));
            }
            match cs.reference_space_type() {
                ReferenceSpaceType::Display => has_display_referred = true,
                ReferenceSpaceType::Scene => has_scene_referred = true,
            }
        }

        // Roles.
        for (role, cs) in &self.roles {
            if self.major_version >= 2 && contains_context_variable_token(role) {
                return Err(self.fail(format!(
                    "Config failed role validation. A role name '{role}' cannot contain a context variable reserved token i.e. % or $."
                )));
            }
            if !self.all_color_spaces.has_color_space(cs) {
                return Err(self.fail(format!(
                    "Config failed role validation. The role '{role}' refers to a color space, '{cs}', which is not defined."
                )));
            }
        }
        if self.version_hex() >= 0x0202_0000 {
            let mut scene_linear = false;
            let mut compositing_log = false;
            let mut color_timing = false;
            let mut aces_interchange = false;
            let mut aces_scene = false;
            let mut cie_interchange = false;
            let mut cie_display = false;
            for (role, csname) in &self.roles {
                if compare(role, ROLE_SCENE_LINEAR) {
                    scene_linear = true;
                } else if compare(role, ROLE_COMPOSITING_LOG) {
                    compositing_log = true;
                } else if compare(role, ROLE_COLOR_TIMING) {
                    color_timing = true;
                } else if compare(role, ROLE_INTERCHANGE_SCENE) {
                    aces_interchange = true;
                    aces_scene = self
                        .get_color_space(csname)
                        .map(|c| c.reference_space_type() == ReferenceSpaceType::Scene)
                        .unwrap_or(false);
                } else if compare(role, ROLE_INTERCHANGE_DISPLAY) {
                    cie_interchange = true;
                    cie_display = self
                        .get_color_space(csname)
                        .map(|c| c.reference_space_type() == ReferenceSpaceType::Display)
                        .unwrap_or(false);
                }
            }
            if !scene_linear {
                log_error("The scene_linear role is required for a config version 2.2 or higher.");
            }
            if !compositing_log {
                log_error(
                    "The compositing_log role is required for a config version 2.2 or higher.",
                );
            }
            if !color_timing {
                log_error("The color_timing role is required for a config version 2.2 or higher.");
            }
            if has_scene_referred && !aces_interchange {
                log_error(
                    "The aces_interchange role is required when there are scene-referred color spaces and the config version is 2.2 or higher.",
                );
            } else if aces_interchange && !aces_scene {
                log_error("The aces_interchange role must be a scene-referred color space.");
            }
            if has_display_referred && !cie_interchange {
                log_error(
                    "The cie_xyz_d65_interchange role is required when there are display-referred color spaces and the config version is 2.2 or higher.",
                );
            } else if cie_interchange && !cie_display {
                log_error(
                    "The cie_xyz_d65_interchange role must be a display-referred color space.",
                );
            }
        }

        // Inactive color spaces.
        for name in self.build_inactive_names_list(InactiveType::All) {
            if self.get_color_space(&name).is_none() && self.get_named_transform(&name).is_none() {
                log_info(&format!(
                    "Inactive '{name}' is neither a color space nor a named transform."
                ));
            }
        }

        // Viewing rules.
        let accessor = |n: &str| self.get_color_space(n);
        if let Err(e) = self
            .viewing_rules
            .validate(&accessor, &self.all_color_spaces)
        {
            return Err(self.fail(format!(
                "Config failed validation. Viewing rules failed validation with: {}",
                e.message()
            )));
        }

        // Shared views.
        for view in &self.shared_views {
            self.validate_view("", view, true)?;
        }

        // Displays.
        let mut num_displays = 0;
        for (display, d) in &self.displays {
            if d.views.is_empty() && d.shared_views.is_empty() {
                return Err(self.fail(format!(
                    "Config failed display validation. The display '{display}' does not define any views."
                )));
            }
            num_displays += 1;
            for sv in &d.shared_views {
                self.validate_shared_view(display, &d.views, sv, true)?;
            }
            for view in &d.views {
                self.validate_view(display, view, true)?;
            }
        }
        if num_displays == 0 {
            return Err(self
                .fail("Config failed display validation. No displays are specified.".to_string()));
        }

        // Virtual display.
        if self.major_version >= 2 {
            for sv in &self.virtual_display.shared_views {
                self.validate_shared_view(
                    "virtual_display",
                    &self.virtual_display.views,
                    sv,
                    false,
                )?;
            }
            for view in &self.virtual_display.views {
                self.validate_view("virtual_display", view, false)?;
            }
        }

        // Active displays.
        let displays: Vec<String> = self.displays.iter().map(|d| d.0.clone()).collect();
        if !self.active_displays_env_override.is_empty() {
            let all = self.active_displays_env_override.len() == 1
                && self.active_displays_env_override[0].is_empty();
            if !all {
                let ordered = intersect_case_ignore(&self.active_displays_env_override, &displays);
                if ordered.is_empty() {
                    return Err(self.fail(format!(
                        "The content of the env. variable for the list of active displays [{}] is invalid.",
                        join_string_env_style(&self.active_displays_env_override)
                    )));
                }
                if ordered.len() != self.active_displays_env_override.len() {
                    return Err(self.fail(format!(
                        "The content of the env. variable for the list of active displays [{}] contains invalid display name(s).",
                        join_string_env_style(&self.active_displays_env_override)
                    )));
                }
            }
        } else if !self.active_displays.is_empty() {
            let all = self.active_displays.len() == 1 && self.active_displays[0].is_empty();
            if !all {
                let ordered = intersect_case_ignore(&self.active_displays, &displays);
                if ordered.is_empty() {
                    return Err(self.fail(format!(
                        "The list of active displays [{}] from the config file is invalid.",
                        join_string_env_style(&self.active_displays)
                    )));
                }
                if ordered.len() != self.active_displays.len() {
                    return Err(self.fail(format!(
                        "The list of active displays [{}] from the config file contains invalid display name(s).",
                        join_string_env_style(&self.active_displays)
                    )));
                }
            }
        }

        // Transforms.
        {
            let mut names = BTreeSet::new();
            for t in self.all_internal_transforms() {
                t.validate()?;
                get_color_space_references(&mut names, t, &self.context);
            }
            for name in &names {
                if !self.all_color_spaces.has_color_space(name) {
                    if contains_context_variables(name) {
                        return Err(self.fail(format!(
                            "Config failed transform validation. This config references a color space '{name}' using an unknown context variable."
                        )));
                    }
                    let csname = self.lookup_role(name);
                    if csname.is_empty() {
                        if self.get_named_transform(name).is_none() {
                            return Err(self.fail(format!(
                                "Config failed transform validation. This config references a color space, '{name}', which is not defined."
                            )));
                        }
                    } else if !self.all_color_spaces.has_color_space(csname) {
                        return Err(self.fail(format!(
                            "Config failed transform validation. This config references a color space, '{csname}' (for role '{name}'), which is not defined."
                        )));
                    }
                }
            }
        }

        // Looks.
        for (i, look) in self.looks.iter().enumerate() {
            let name = look.name();
            if name.is_empty() {
                return Err(self.fail(format!(
                    "Config failed Look validation. The look at index '{i}' does not specify a name."
                )));
            }
            let ps = look.process_space();
            if ps.is_empty() {
                return Err(self.fail(format!(
                    "Config failed Look validation. The look '{name}' does not specify a process space."
                )));
            }
            if !self.all_color_spaces.has_color_space(ps) {
                let csname = self.lookup_role(ps);
                if csname.is_empty() {
                    return Err(self.fail(format!(
                        "Config failed Look validation. The look '{name}' specifies a process color space, '{ps}', which is not defined."
                    )));
                } else if !self.all_color_spaces.has_color_space(csname) {
                    return Err(self.fail(format!(
                        "Config failed Look validation. The look '{name}' specifies a process color space, '{csname}' (for role '{ps}'), which is not defined."
                    )));
                }
            }
        }

        // View transforms.
        if !self.view_transforms.is_empty() {
            if self.default_scene_to_display_view_transform().is_none() {
                return Err(self.fail(
                    "Config failed validation. If there are view_transforms, at least one must use the scene reference space."
                        .to_string(),
                ));
            }
        } else if has_display_referred {
            return Err(self.fail(
                "Config failed validation. If there are display-referred color spaces, there must be view_transforms."
                    .to_string(),
            ));
        }
        if !self.default_view_transform.is_empty() {
            let ok = self
                .default_scene_to_display_view_transform()
                .map(|vt| compare(vt.name(), &self.default_view_transform))
                .unwrap_or(false);
            if !ok {
                return Err(self.fail(format!(
                    "Config failed validation. Default view transform is defined as: '{}' but this does not correspond to an existing scene-referred view transform.",
                    self.default_view_transform
                )));
            }
        }

        // File rules.
        if let Err(e) = self.file_rules.validate(self) {
            return Err(self.fail(format!(
                "Config failed validation. File rules failed with: {}",
                e.message()
            )));
        }

        // File transforms.
        {
            let mut files = BTreeSet::new();
            for t in self.all_internal_transforms() {
                get_file_references(&mut files, t);
            }
            if !files.is_empty() {
                let mut found_one = false;
                let mut err = String::from("Config failed search path validation.");
                for idx in 0..self.context.num_search_paths() {
                    let path = self.context.search_path_by_index(idx).unwrap_or("");
                    if path.is_empty() {
                        err.push_str(" The search_path must not be an empty string if there are FileTransforms.");
                        continue;
                    }
                    let resolved = self.context.resolve_string_var(path);
                    if contains_context_variables(&resolved) {
                        err.push_str(&format!("  The search_path '{path}' cannot be resolved"));
                        if path != resolved {
                            err.push_str(&format!(" by '{resolved}'"));
                        }
                        err.push('.');
                        continue;
                    }
                    found_one = true;
                }
                if !found_one {
                    if self.context.num_search_paths() == 0 {
                        err.push_str(
                            " The search_path must not be empty if there are FileTransforms.",
                        );
                    }
                    return Err(self.fail(err));
                }
            }
            for file in &files {
                let resolved = self.context.resolve_string_var(file);
                if resolved.is_empty() || contains_context_variables(&resolved) {
                    let mut msg = String::from(
                        "Config failed validation expanding file transform paths. The file transform source cannot be resolved: '",
                    );
                    if *file != resolved {
                        msg.push_str(&format!("{file}' vs. '{resolved}'."));
                    } else {
                        msg.push_str(&format!("{file}'."));
                    }
                    return Err(self.fail(msg));
                }
            }
        }

        // Named transforms.
        for nt in &self.all_named_transforms {
            let name = nt.name();
            if self.look(name).is_some() {
                return Err(self.fail(format!(
                    "Config failed validation. NamedTransform can't be named '{name}'. This name is already used for a look."
                )));
            }
            if self.view_transform(name).is_some() {
                return Err(self.fail(format!(
                    "Config failed validation. NamedTransform can't be named '{name}'. This name is already used for a view transform."
                )));
            }
        }

        self.check_version_consistency()
    }

    fn check_transform_version(&self, t: &Transform) -> Result<()> {
        let (maj, min) = (self.major_version, self.minor_version);
        let style_in = |style: &str, list: &[&str]| list.iter().any(|s| compare(s, style));
        match t {
            Transform::Builtin(b) => {
                if maj < 2 {
                    bail!("Only config version 2 (or higher) can have BuiltinInTransform.");
                }
                let st = b.style.as_str();
                if maj == 2
                    && min < 1
                    && compare(st, "ACES-LMT - ACES 1.3 Reference Gamut Compression")
                {
                    bail!("Only config version 2.1 (or higher) can have BuiltinTransform style 'ACES-LMT - ACES 1.3 Reference Gamut Compression'.");
                }
                if maj == 2
                    && min < 2
                    && style_in(
                        st,
                        &[
                            "ARRI_LOGC4_to_ACES2065-1",
                            "CURVE - CANON_CLOG2_to_LINEAR",
                            "CURVE - CANON_CLOG3_to_LINEAR",
                        ],
                    )
                {
                    bail!(
                        "Only config version 2.2 (or higher) can have BuiltinTransform style '{}'.",
                        st
                    );
                }
                if maj == 2 && min < 3 && compare(st, "DISPLAY - CIE-XYZ-D65_to_DisplayP3") {
                    bail!("Only config version 2.3 (or higher) can have BuiltinTransform style 'DISPLAY - CIE-XYZ-D65_to_DisplayP3'.");
                }
                const V24: &[&str] = &[
                    "APPLE_LOG_to_ACES2065-1",
                    "CURVE - APPLE_LOG_to_LINEAR",
                    "CURVE - HLG-OETF",
                    "CURVE - HLG-OETF-INVERSE",
                    "DISPLAY - CIE-XYZ-D65_to_DCDM-D65",
                    "DISPLAY - CIE-XYZ-D65_to_ST2084-DCDM-D65",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - SDR-100nit-REC709_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - SDR-100nit-P3-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-108nit-P3-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-300nit-P3-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-500nit-P3-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-1000nit-P3-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-2000nit-P3-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-4000nit-P3-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-500nit-REC2020_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-1000nit-REC2020_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-2000nit-REC2020_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-4000nit-REC2020_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - SDR-100nit-REC709-D60-in-REC709-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - SDR-100nit-REC709-D60-in-P3-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - SDR-100nit-REC709-D60-in-REC2020-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - SDR-100nit-P3-D60-in-P3-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - SDR-100nit-P3-D60-in-XYZ-E_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-108nit-P3-D60-in-P3-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-300nit-P3-D60-in-XYZ-E_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-500nit-P3-D60-in-P3-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-1000nit-P3-D60-in-P3-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-2000nit-P3-D60-in-P3-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-4000nit-P3-D60-in-P3-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-500nit-P3-D60-in-REC2020-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-1000nit-P3-D60-in-REC2020-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-2000nit-P3-D60-in-REC2020-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-4000nit-P3-D60-in-REC2020-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-500nit-REC2020-D60-in-REC2020-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-1000nit-REC2020-D60-in-REC2020-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-2000nit-REC2020-D60-in-REC2020-D65_2.0",
                    "ACES-OUTPUT - ACES2065-1_to_CIE-XYZ-D65 - HDR-4000nit-REC2020-D60-in-REC2020-D65_2.0",
                    "DISPLAY - CIE-XYZ-D65_to_DisplayP3-HDR",
                ];
                if maj == 2 && min < 4 && style_in(st, V24) {
                    bail!(
                        "Only config version 2.4 (or higher) can have BuiltinTransform style '{}'.",
                        st
                    );
                }
                const V25: &[&str] = &[
                    "DISPLAY - CIE-XYZ-D65_to_REC.1886-REC.709 - MIRROR NEGS",
                    "DISPLAY - CIE-XYZ-D65_to_REC.1886-REC.2020 - MIRROR NEGS",
                    "DISPLAY - CIE-XYZ-D65_to_G2.2-REC.709 - MIRROR NEGS",
                    "DISPLAY - CIE-XYZ-D65_to_sRGB - MIRROR NEGS",
                    "DISPLAY - CIE-XYZ-D65_to_G2.6-P3-D65 - MIRROR NEGS",
                ];
                if maj == 2 && min < 5 && style_in(st, V25) {
                    bail!(
                        "Only config version 2.5 (or higher) can have BuiltinTransform style '{}'.",
                        st
                    );
                }
                if maj == 2 && min < 6 && compare(st, "APPLE_LOG-APPLEWG_to_ACES2065-1") {
                    bail!(
                        "Only config version 2.6 (or higher) can have BuiltinTransform style '{}'.",
                        st
                    );
                }
            }
            Transform::Cdl(c) => {
                if maj < 2 && c.style != CdlStyle::NoClamp {
                    bail!("Only config version 2 (or higher) can have style for CDLTransform.");
                }
            }
            Transform::DisplayView(_) => {
                if maj < 2 {
                    bail!("Only config version 2 (or higher) can have DisplayViewTransform.");
                }
            }
            Transform::Exponent(e) => {
                if maj < 2 && e.negative_style != NegativeStyle::Clamp {
                    bail!("Config version 1 only supports ExponentTransform clamping negative values.");
                }
            }
            Transform::ExponentWithLinear(_) => {
                if maj < 2 {
                    bail!(
                        "Only config version 2 (or higher) can have ExponentWithLinearTransform."
                    );
                }
            }
            Transform::ExposureContrast(_) => {
                if maj < 2 {
                    bail!("Only config version 2 (or higher) can have ExposureContrastTransform.");
                }
            }
            Transform::File(f) => {
                if maj < 2 {
                    if f.interpolation == Interpolation::Cubic {
                        bail!("Only config version 2 (or higher) can use 'cubic' interpolation with FileTransform.");
                    }
                    if f.cdl_style != CdlStyle::NoClamp {
                        bail!("Only config version 2 (or higher) can use CDL style' for FileTransform.");
                    }
                }
            }
            Transform::FixedFunction(ff) => {
                use FixedFunctionStyle as S;
                let s = ff.style;
                if maj < 2 {
                    bail!("Only config version 2 (or higher) can have FixedFunctionTransform.");
                }
                if maj == 2 && min < 1 && s == S::AcesGamutComp13 {
                    bail!("Only config version 2.1 (or higher) can have FixedFunctionTransform style 'ACES_GAMUT_COMP_13'.");
                }
                if maj == 2
                    && min < 4
                    && matches!(
                        s,
                        S::LinToPq
                            | S::LinToGammaLog
                            | S::LinToDoubleLog
                            | S::AcesOutputTransform20
                            | S::AcesRgbToJmh20
                            | S::AcesTonescaleCompress20
                            | S::AcesGamutCompress20
                    )
                {
                    bail!("Only config version 2.4 (or higher) can have FixedFunctionTransform style '{}'.", s.as_str());
                }
                if maj == 2
                    && min < 5
                    && matches!(s, S::RgbToHsyLin | S::RgbToHsyLog | S::RgbToHsyVid)
                {
                    bail!("Only config version 2.5 (or higher) can have FixedFunctionTransform style '{}'.", s.as_str());
                }
                if maj == 2 && min < 6 && s == S::AcesRgbToHmj20 {
                    bail!("Only config version 2.6 (or higher) can have FixedFunctionTransform style '{}'.", s.as_str());
                }
            }
            Transform::GradingPrimary(_) => {
                if maj < 2 {
                    bail!("Only config version 2 (or higher) can have GradingPrimaryTransform.");
                }
            }
            Transform::GradingRgbCurve(_) => {
                if maj < 2 {
                    bail!("Only config version 2 (or higher) can have GradingRGBCurveTransform.");
                }
            }
            Transform::GradingHueCurve(_) => {
                if maj == 2 && min < 5 {
                    bail!("Only config version 2.5 (or higher) can have GradingHueCurveTransform.");
                }
            }
            Transform::GradingTone(_) => {
                if maj < 2 {
                    bail!("Only config version 2 (or higher) can have GradingToneTransform.");
                }
            }
            Transform::LogAffine(_) => {
                if maj < 2 {
                    bail!("Only config version 2 (or higher) can have LogAffineTransform.");
                }
            }
            Transform::LogCamera(_) => {
                if maj < 2 {
                    bail!("Only config version 2 (or higher) can have LogCameraTransform.");
                }
            }
            Transform::Range(_) => {
                if maj < 2 {
                    bail!("Only config version 2 (or higher) can have RangeTransform.");
                }
            }
            Transform::Group(g) => {
                for c in &g.transforms {
                    self.check_transform_version(c)?;
                }
            }
            _ => {}
        }
        Ok(())
    }

    /// Check that no feature newer than the config version is used.
    pub(crate) fn check_version_consistency(&self) -> Result<()> {
        let hex = self.version_hex();
        for t in self.all_internal_transforms() {
            self.check_transform_version(t)?;
        }
        let maj = self.major_version;
        if maj < 2 && self.family_separator != '/' {
            bail!("Only version 2 (or higher) can have a family separator.");
        }
        if maj < 2 && self.file_rules.num_entries() > 2 {
            bail!("Only version 2 (or higher) can have file rules.");
        }
        if maj < 2 && !self.inactive_names_conf.is_empty() {
            bail!("Only version 2 (or higher) can have inactive color spaces.");
        }
        if maj < 2 && self.viewing_rules.num_entries() != 0 {
            bail!("Only version 2 (or higher) can have viewing rules.");
        }
        if maj < 2 {
            if !self.shared_views.is_empty() {
                bail!("Only version 2 (or higher) can have shared views.");
            }
            for (name, d) in &self.displays {
                if !d.shared_views.is_empty() {
                    bail!(
                        "Config failed validation. The display '{}' uses shared views and config version is less than 2.",
                        name
                    );
                }
            }
            if !self.virtual_display.views.is_empty()
                || !self.virtual_display.shared_views.is_empty()
            {
                bail!("Only version 2 (or higher) can have a virtual display.");
            }
        }
        for cs in self.all_color_spaces.iter() {
            if maj < 2 {
                if cs.reference_space_type() == ReferenceSpaceType::Display {
                    bail!("Only version 2 (or higher) can have DisplayColorSpaces.");
                }
                if !cs.interop_id().is_empty() {
                    bail!(
                        "Config failed validation. The color space '{}' has non-empty InteropID and config version is less than 2.0.",
                        cs.name()
                    );
                }
            }
            if hex < 0x0205_0000 && !cs.interchange_attributes().is_empty() {
                bail!(
                    "Config failed validation. The color space '{}' has non-empty interchange attributes and config version is less than 2.5.",
                    cs.name()
                );
            }
        }
        if maj < 2 && (!self.view_transforms.is_empty() || !self.default_view_transform.is_empty())
        {
            bail!("Only version 2 (or higher) can have ViewTransforms.");
        }
        if hex < 0x0205_0000 {
            for vt in &self.view_transforms {
                if !vt.interchange_attributes().is_empty() {
                    bail!(
                        "Config failed validation. The view transform '{}' has non-empty interchange attributes and config version is less than 2.5.",
                        vt.name()
                    );
                }
            }
            for l in &self.looks {
                if !l.interchange_attributes().is_empty() {
                    bail!(
                        "Config failed validation. The look '{}' has non-empty interchange attributes and config version is less than 2.5.",
                        l.name()
                    );
                }
            }
        }
        if maj < 2 && !self.all_named_transforms.is_empty() {
            bail!("Only version 2 (or higher) can have NamedTransforms.");
        }
        Ok(())
    }
}