ocio 0.2.0

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
//! Color spaces and sets of color spaces (port of `ColorSpace.cpp` and
//! `ColorSpaceSet.cpp`).

use super::tokens::TokensManager;
use super::transform_display::format_transform;
use super::utils::{compare, contain, fmt_f32, join, lower, remove};
use crate::error::{Error, Result};
use crate::transforms::Transform;
use crate::types::{Allocation, BitDepth, ColorSpaceDirection, ReferenceSpaceType};
use std::collections::BTreeMap;
use std::fmt;

const KNOWN_INTERCHANGE_NAMES: [&str; 2] = ["amf_transform_ids", "icc_profile_name"];

/// Look up a known interchange attribute name (case insensitive).
pub(crate) fn find_interchange_key(known: &[&'static str], name: &str) -> Result<&'static str> {
    known
        .iter()
        .find(|k| compare(k, name))
        .copied()
        .ok_or_else(|| Error::msg(format!("Unknown attribute name '{name}'.")))
}

/// A color space: a named encoding of colors plus the transforms to and from
/// the reference space.
#[derive(Debug, Clone, PartialEq)]
pub struct ColorSpace {
    name: String,
    family: String,
    equality_group: String,
    description: String,
    encoding: String,
    interop_id: String,
    aliases: Vec<String>,
    interchange: BTreeMap<String, String>,
    bit_depth: BitDepth,
    is_data: bool,
    reference_space: ReferenceSpaceType,
    allocation: Allocation,
    allocation_vars: Vec<f32>,
    to_reference: Option<Transform>,
    from_reference: Option<Transform>,
    categories: TokensManager,
}

impl Default for ColorSpace {
    fn default() -> Self {
        Self::new(ReferenceSpaceType::Scene)
    }
}

impl ColorSpace {
    /// An empty color space using the given reference space.
    pub fn new(reference_space: ReferenceSpaceType) -> Self {
        Self {
            name: String::new(),
            family: String::new(),
            equality_group: String::new(),
            description: String::new(),
            encoding: String::new(),
            interop_id: String::new(),
            aliases: Vec::new(),
            interchange: BTreeMap::new(),
            bit_depth: BitDepth::Unknown,
            is_data: false,
            reference_space,
            allocation: Allocation::Uniform,
            allocation_vars: Vec::new(),
            to_reference: None,
            from_reference: None,
            categories: TokensManager::new(),
        }
    }

    /// A color space with a name, using the scene reference space.
    pub fn with_name(name: &str) -> Self {
        let mut cs = Self::new(ReferenceSpaceType::Scene);
        cs.set_name(name);
        cs
    }

    /// The name.
    pub fn name(&self) -> &str {
        &self.name
    }

    /// Set the name (an alias with the same name is removed).
    pub fn set_name(&mut self, name: &str) {
        self.name = name.to_string();
        let n = self.name.clone();
        remove(&mut self.aliases, &n);
    }

    /// Number of aliases.
    pub fn num_aliases(&self) -> usize {
        self.aliases.len()
    }

    /// Alias at `idx` (`""` if out of range).
    pub fn alias(&self, idx: usize) -> &str {
        self.aliases.get(idx).map(|s| s.as_str()).unwrap_or("")
    }

    /// All the aliases.
    pub fn aliases(&self) -> &[String] {
        &self.aliases
    }

    /// True if the alias exists (case insensitive).
    pub fn has_alias(&self, alias: &str) -> bool {
        self.aliases.iter().any(|a| compare(a, alias))
    }

    /// Add an alias (ignored if empty, equal to the name or already present).
    pub fn add_alias(&mut self, alias: &str) {
        if !alias.is_empty() && !compare(alias, &self.name) && !contain(&self.aliases, alias) {
            self.aliases.push(alias.to_string());
        }
    }

    /// Remove an alias (case insensitive).
    pub fn remove_alias(&mut self, alias: &str) {
        if !alias.is_empty() {
            remove(&mut self.aliases, alias);
        }
    }

    /// Remove all the aliases.
    pub fn clear_aliases(&mut self) {
        self.aliases.clear();
    }

    /// The family.
    pub fn family(&self) -> &str {
        &self.family
    }
    /// Set the family.
    pub fn set_family(&mut self, family: &str) {
        self.family = family.to_string();
    }
    /// The equality group.
    pub fn equality_group(&self) -> &str {
        &self.equality_group
    }
    /// Set the equality group.
    pub fn set_equality_group(&mut self, group: &str) {
        self.equality_group = group.to_string();
    }
    /// The description.
    pub fn description(&self) -> &str {
        &self.description
    }
    /// Set the description.
    pub fn set_description(&mut self, description: &str) {
        self.description = description.to_string();
    }

    /// The interop id.
    pub fn interop_id(&self) -> &str {
        &self.interop_id
    }

    /// Set the interop id, validating its syntax.
    pub fn set_interop_id(&mut self, id: &str) -> Result<()> {
        if !id.is_empty() {
            let allowed = |c: char| {
                c.is_ascii_digit()
                    || c.is_ascii_lowercase()
                    || matches!(
                        c,
                        '.' | '-'
                            | '_'
                            | '~'
                            | '/'
                            | '*'
                            | '#'
                            | '%'
                            | '^'
                            | '+'
                            | '('
                            | ')'
                            | '['
                            | ']'
                            | '|'
                            | ':'
                    )
            };
            if !id.chars().all(allowed) {
                return Err(Error::msg(format!(
                    "InteropID '{id}' contains invalid characters. Only lowercase a-z, 0-9 and . - _ ~ / * # % ^ + ( ) [ ] | are allowed.\n"
                )));
            }
            if let Some(pos) = id.find(':') {
                let ns = &id[..pos];
                let cs = &id[pos + 1..];
                if ns.is_empty() || cs.is_empty() {
                    return Err(Error::msg(format!(
                        "InteropID '{id}' is not valid. If ':' is used, both the namespace and the color space parts must be non-empty.\n"
                    )));
                }
                if cs.contains(':') {
                    return Err(Error::msg(format!(
                        "ERROR: InteropID '{id}' is not valid. Only one ':' is allowed to separate the namespace and the color space.\n"
                    )));
                }
            }
        }
        self.interop_id = id.to_string();
        Ok(())
    }

    /// Value of a known interchange attribute (`amf_transform_ids`,
    /// `icc_profile_name`).
    pub fn interchange_attribute(&self, name: &str) -> Result<&str> {
        let key = find_interchange_key(&KNOWN_INTERCHANGE_NAMES, name)?;
        Ok(self.interchange.get(key).map(|s| s.as_str()).unwrap_or(""))
    }

    /// Set (or remove if `value` is empty) a known interchange attribute.
    pub fn set_interchange_attribute(&mut self, name: &str, value: &str) -> Result<()> {
        let key = find_interchange_key(&KNOWN_INTERCHANGE_NAMES, name)?;
        if value.is_empty() {
            self.interchange.remove(key);
        } else {
            self.interchange.insert(key.to_string(), value.to_string());
        }
        Ok(())
    }

    /// All the interchange attributes.
    pub fn interchange_attributes(&self) -> &BTreeMap<String, String> {
        &self.interchange
    }

    /// The bit depth.
    pub fn bit_depth(&self) -> BitDepth {
        self.bit_depth
    }
    /// Set the bit depth.
    pub fn set_bit_depth(&mut self, bd: BitDepth) {
        self.bit_depth = bd;
    }
    /// The reference space (scene or display) this color space relates to.
    pub fn reference_space_type(&self) -> ReferenceSpaceType {
        self.reference_space
    }

    /// True if the category exists (case insensitive).
    pub fn has_category(&self, category: &str) -> bool {
        self.categories.has_token(category)
    }
    /// Add a category (trimmed, ignored if empty or already present).
    pub fn add_category(&mut self, category: &str) {
        self.categories.add_token(category);
    }
    /// Remove a category.
    pub fn remove_category(&mut self, category: &str) {
        self.categories.remove_token(category);
    }
    /// Number of categories.
    pub fn num_categories(&self) -> usize {
        self.categories.num_tokens()
    }
    /// Category at `index` (`None` if out of range).
    pub fn category(&self, index: usize) -> Option<&str> {
        self.categories.token(index)
    }
    /// All the categories.
    pub fn categories(&self) -> &[String] {
        self.categories.tokens()
    }
    /// Remove all the categories.
    pub fn clear_categories(&mut self) {
        self.categories.clear_tokens();
    }

    /// The encoding.
    pub fn encoding(&self) -> &str {
        &self.encoding
    }
    /// Set the encoding.
    pub fn set_encoding(&mut self, encoding: &str) {
        self.encoding = encoding.to_string();
    }
    /// True if the color space holds non-color data.
    pub fn is_data(&self) -> bool {
        self.is_data
    }
    /// Set whether the color space holds non-color data.
    pub fn set_is_data(&mut self, is_data: bool) {
        self.is_data = is_data;
    }
    /// The allocation.
    pub fn allocation(&self) -> Allocation {
        self.allocation
    }
    /// Set the allocation.
    pub fn set_allocation(&mut self, allocation: Allocation) {
        self.allocation = allocation;
    }
    /// Number of allocation variables.
    pub fn allocation_num_vars(&self) -> usize {
        self.allocation_vars.len()
    }
    /// The allocation variables.
    pub fn allocation_vars(&self) -> &[f32] {
        &self.allocation_vars
    }
    /// Set the allocation variables.
    pub fn set_allocation_vars(&mut self, vars: &[f32]) {
        self.allocation_vars = vars.to_vec();
    }

    /// The transform in the given direction, if any.
    pub fn transform(&self, dir: ColorSpaceDirection) -> Option<&Transform> {
        match dir {
            ColorSpaceDirection::ToReference => self.to_reference.as_ref(),
            ColorSpaceDirection::FromReference => self.from_reference.as_ref(),
        }
    }

    /// Set (or remove with `None`) the transform in the given direction.
    pub fn set_transform(&mut self, t: Option<Transform>, dir: ColorSpaceDirection) {
        match dir {
            ColorSpaceDirection::ToReference => self.to_reference = t,
            ColorSpaceDirection::FromReference => self.from_reference = t,
        }
    }
}

impl fmt::Display for ColorSpace {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "<ColorSpace referenceSpaceType=")?;
        match self.reference_space {
            ReferenceSpaceType::Scene => write!(f, "scene, ")?,
            ReferenceSpaceType::Display => write!(f, "display, ")?,
        }
        write!(f, "name={}, ", self.name)?;
        if self.aliases.len() == 1 {
            write!(f, "alias= {}, ", self.aliases[0])?;
        } else if self.aliases.len() > 1 {
            write!(f, "aliases=[{}], ", self.aliases.join(", "))?;
        }
        if !self.interop_id.is_empty() {
            write!(f, "interop_id={}, ", self.interop_id)?;
        }
        if !self.family.is_empty() {
            write!(f, "family={}, ", self.family)?;
        }
        if !self.equality_group.is_empty() {
            write!(f, "equalityGroup={}, ", self.equality_group)?;
        }
        if self.bit_depth != BitDepth::Unknown {
            write!(f, "bitDepth={}, ", self.bit_depth.as_str())?;
        }
        write!(f, "isData={}", crate::types::bool_to_string(self.is_data))?;
        if !self.allocation_vars.is_empty() {
            write!(f, ", allocation={}, ", self.allocation.as_str())?;
            let vars: Vec<String> = self.allocation_vars.iter().map(|v| fmt_f32(*v)).collect();
            write!(f, "vars={}", vars.join(" "))?;
        }
        if self.num_categories() > 0 {
            write!(f, ", categories={}", join(self.categories.tokens(), ','))?;
        }
        if !self.encoding.is_empty() {
            write!(f, ", encoding={}", self.encoding)?;
        }
        if !self.description.is_empty() {
            write!(f, ", description={}", self.description)?;
        }
        for (k, v) in &self.interchange {
            write!(f, ", {k}={v}")?;
        }
        if let Some(t) = &self.to_reference {
            write!(f, ",\n    {} --> Reference", self.name)?;
            write!(f, "\n        {}", format_transform(t))?;
        }
        if let Some(t) = &self.from_reference {
            write!(f, ",\n    Reference --> {}", self.name)?;
            write!(f, "\n        {}", format_transform(t))?;
        }
        write!(f, ">")
    }
}

/// An ordered set of color spaces (names and aliases are unique, case
/// insensitive).
#[derive(Debug, Clone, Default)]
pub struct ColorSpaceSet {
    color_spaces: Vec<ColorSpace>,
}

impl PartialEq for ColorSpaceSet {
    /// Only the names are compared.
    fn eq(&self, other: &Self) -> bool {
        self.color_spaces.len() == other.color_spaces.len()
            && self
                .color_spaces
                .iter()
                .all(|cs| other.has_color_space(cs.name()))
    }
}

impl ColorSpaceSet {
    /// An empty set.
    pub fn new() -> Self {
        Self::default()
    }

    /// Number of color spaces.
    pub fn num_color_spaces(&self) -> usize {
        self.color_spaces.len()
    }

    /// Name of the color space at `index`.
    pub fn color_space_name_by_index(&self, index: usize) -> Option<&str> {
        self.color_spaces.get(index).map(|c| c.name())
    }

    /// Color space at `index`.
    pub fn color_space_by_index(&self, index: usize) -> Option<&ColorSpace> {
        self.color_spaces.get(index)
    }

    /// Color space by name or alias.
    pub fn color_space(&self, name: &str) -> Option<&ColorSpace> {
        self.color_space_index(name)
            .and_then(|i| self.color_spaces.get(i))
    }

    /// Index of the color space by name or alias.
    pub fn color_space_index(&self, name: &str) -> Option<usize> {
        if name.is_empty() {
            return None;
        }
        let s = lower(name);
        self.color_spaces
            .iter()
            .position(|cs| compare(cs.name(), &s) || cs.aliases.iter().any(|a| compare(a, &s)))
    }

    /// True if a color space has this name or alias.
    pub fn has_color_space(&self, name: &str) -> bool {
        self.color_space_index(name).is_some()
    }

    /// Add a copy of the color space, replacing a color space with the same
    /// name.
    pub fn add_color_space(&mut self, cs: &ColorSpace) -> Result<()> {
        let cs_name = cs.name();
        if cs_name.is_empty() {
            return Err(Error::msg("Cannot add a color space with an empty name."));
        }
        let mut replace = None;
        if let Some(idx) = self.color_space_index(cs_name) {
            if !compare(self.color_spaces[idx].name(), cs_name) {
                return Err(Error::msg(format!(
                    "Cannot add '{}' color space, existing color space, '{}' is using this name as an alias.",
                    cs_name,
                    self.color_spaces[idx].name()
                )));
            }
            replace = Some(idx);
        }
        for alias in &cs.aliases {
            if let Some(idx) = self.color_space_index(alias) {
                if Some(idx) != replace {
                    return Err(Error::msg(format!(
                        "Cannot add '{}' color space, it has '{}' alias and existing color space, '{}' is using the same alias.",
                        cs_name,
                        alias,
                        self.color_spaces[idx].name()
                    )));
                }
            }
        }
        match replace {
            Some(i) => self.color_spaces[i] = cs.clone(),
            None => self.color_spaces.push(cs.clone()),
        }
        Ok(())
    }

    /// Add all the color spaces of another set.
    pub fn add_color_spaces(&mut self, other: &ColorSpaceSet) -> Result<()> {
        for cs in &other.color_spaces {
            self.add_color_space(cs)?;
        }
        Ok(())
    }

    /// Remove a color space by name (aliases are not considered).
    pub fn remove_color_space(&mut self, name: &str) {
        if name.is_empty() {
            return;
        }
        let n = lower(name);
        if let Some(pos) = self
            .color_spaces
            .iter()
            .position(|cs| lower(cs.name()) == n)
        {
            self.color_spaces.remove(pos);
        }
    }

    /// Remove all the color spaces of another set.
    pub fn remove_color_spaces(&mut self, other: &ColorSpaceSet) {
        for cs in &other.color_spaces {
            self.remove_color_space(cs.name());
        }
    }

    /// Remove all the color spaces.
    pub fn clear_color_spaces(&mut self) {
        self.color_spaces.clear();
    }

    /// Iterate over the color spaces.
    pub fn iter(&self) -> impl Iterator<Item = &ColorSpace> {
        self.color_spaces.iter()
    }

    /// Union (`operator||`).
    pub fn union(&self, other: &ColorSpaceSet) -> Result<ColorSpaceSet> {
        let mut s = self.clone();
        s.add_color_spaces(other)?;
        Ok(s)
    }

    /// Intersection (`operator&&`): the color spaces of `other` present in `self`.
    pub fn intersection(&self, other: &ColorSpaceSet) -> Result<ColorSpaceSet> {
        let mut s = ColorSpaceSet::new();
        for cs in &other.color_spaces {
            if self.has_color_space(cs.name()) {
                s.add_color_space(cs)?;
            }
        }
        Ok(s)
    }

    /// Difference (`operator-`): the color spaces of `self` not in `other`.
    pub fn difference(&self, other: &ColorSpaceSet) -> Result<ColorSpaceSet> {
        let mut s = ColorSpaceSet::new();
        for cs in &self.color_spaces {
            if !other.has_color_space(cs.name()) {
                s.add_color_space(cs)?;
            }
        }
        Ok(s)
    }
}