ocio 0.2.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
//! Helpers for (display, view) pairs (port of
//! `apphelpers/DisplayViewHelpers.cpp`, namespace `DisplayViewHelpers`).

use super::category_helpers::{extract_items, find_color_space_names};
use super::legacy_viewing_pipeline::LegacyViewingPipeline;
use crate::config::utils::{compare, contain, join, remove, split, trim};
use crate::config::{ColorSpace, Config};
use crate::context::Context;
use crate::error::{Error, Result};
use crate::processor::Processor;
use crate::transforms::{
    DisplayViewTransform, ExposureContrastTransform, FileTransform, GroupTransform,
    MatrixTransform, Transform,
};
use crate::types::{
    ColorSpaceDirection, ExposureContrastStyle, TransformDirection, OCIO_ACTIVE_DISPLAYS_ENVVAR,
    OCIO_ACTIVE_VIEWS_ENVVAR,
};

/// Exposure / contrast transform with dynamic exposure and contrast (linear
/// style, 0.18 pivot).
fn dynamic_exposure_contrast() -> ExposureContrastTransform {
    ExposureContrastTransform {
        style: ExposureContrastStyle::Linear,
        pivot: 0.18,
        exposure_dynamic: true,
        contrast_dynamic: true,
        ..Default::default()
    }
}

/// Exposure / contrast transform with a dynamic gamma (video style, 1.0
/// pivot).
fn dynamic_gamma() -> ExposureContrastTransform {
    ExposureContrastTransform {
        style: ExposureContrastStyle::Video,
        pivot: 1.0,
        gamma_dynamic: true,
        ..Default::default()
    }
}

/// Processor from the working color space (a color space or role name) to
/// the (display, view) pair (forward) or from the (display, view) pair to the
/// working color space (inverse), using the current context of the config.
///
/// If not already present, exposure / contrast transforms are added to allow
/// changing exposure, contrast and gamma through dynamic properties. The
/// channel view is optional.
pub fn get_processor(
    config: &Config,
    working_name: &str,
    display_name: &str,
    view_name: &str,
    channel_view: Option<&MatrixTransform>,
    direction: TransformDirection,
) -> Result<Processor> {
    get_processor_with_context(
        config,
        config.current_context(),
        working_name,
        display_name,
        view_name,
        channel_view,
        direction,
    )
}

/// Same as [`get_processor`] using an explicit context.
pub fn get_processor_with_context(
    config: &Config,
    context: &Context,
    working_name: &str,
    display_name: &str,
    view_name: &str,
    channel_view: Option<&MatrixTransform>,
    direction: TransformDirection,
) -> Result<Processor> {
    let mut display_transform = DisplayViewTransform::new(working_name, display_name, view_name);
    display_transform.direction = direction;

    let processor = config.get_processor_with_context(
        context,
        &Transform::DisplayView(display_transform.clone()),
        TransformDirection::Forward,
    )?;

    let mut need_exposure = true;
    let mut need_gamma = true;

    if processor.is_dynamic() {
        let grp = processor.create_group_transform();
        for tr in &grp.transforms {
            if let Transform::ExposureContrast(ex) = tr {
                if ex.exposure_dynamic {
                    need_exposure = false;
                }
                if ex.gamma_dynamic {
                    need_gamma = false;
                }
            }
        }
    }

    if !need_exposure && !need_gamma && channel_view.is_none() {
        return Ok(processor);
    }

    let mut pipeline = LegacyViewingPipeline::new();
    pipeline.set_display_view_transform(Some(&display_transform));

    // Upstream OCIO note: Need to update to allow for apps that are viewing non-scene-linear
    // images.
    if need_exposure {
        pipeline.set_linear_cc(Some(&Transform::ExposureContrast(
            dynamic_exposure_contrast(),
        )));
    }

    if need_gamma {
        pipeline.set_display_cc(Some(&Transform::ExposureContrast(dynamic_gamma())));
    }

    if let Some(cv) = channel_view {
        pipeline.set_channel_view(Some(&Transform::Matrix(cv.clone())));
    }

    pipeline.get_processor_with_context(config, context)
}

/// Identity processor containing only the exposure / contrast transforms
/// with dynamic properties.
pub fn get_identity_processor(config: &Config) -> Result<Processor> {
    let mut group = GroupTransform::new();
    group.append(dynamic_exposure_contrast());
    group.append(dynamic_gamma());
    config.get_processor_for_transform(&Transform::Group(group), TransformDirection::Forward)
}

/// Split a comma separated list and trim the items (`StringUtils::Split` +
/// `StringUtils::Trim`).
fn split_trim(list: &str) -> Vec<String> {
    split(list, ',')
        .iter()
        .map(|s| trim(s).to_string())
        .collect()
}

/// A non-empty value of the environment variable.
fn non_empty_env(name: &str) -> Option<String> {
    std::env::var(name).ok().filter(|v| !v.is_empty())
}

fn add_active_display_view(config: &mut Config, display_name: &str, view_name: &str) -> Result<()> {
    // Add the display to the active display list only if possible.

    if let Some(env) = non_empty_env(OCIO_ACTIVE_DISPLAYS_ENVVAR) {
        let displays = split_trim(&env);
        let accept_all = displays.len() == 1 && displays[0].is_empty();
        if !accept_all {
            return Err(Error::msg(format!(
                "Forbidden to add an active display as '{OCIO_ACTIVE_DISPLAYS_ENVVAR}' controls the active list."
            )));
        }
    } else {
        let active = config.active_displays();
        if !active.is_empty() {
            let mut displays = split_trim(&active);
            let accept_all = displays.len() == 1 && displays[0].is_empty();
            if !accept_all && !contain(&displays, display_name) {
                displays.push(display_name.to_string());
                config.set_active_displays(&join(&displays, ','))?;
            }
        }
    }

    // Add the view to the active view list, only if needed.

    if let Some(env) = non_empty_env(OCIO_ACTIVE_VIEWS_ENVVAR) {
        let views = split_trim(&env);
        let accept_all = views.len() == 1 && views[0].is_empty();
        if !accept_all {
            return Err(Error::msg(format!(
                "Forbidden to add an active view as '{OCIO_ACTIVE_VIEWS_ENVVAR}' controls the active list."
            )));
        }
    } else {
        let active = config.active_views();
        if !active.is_empty() {
            let mut views = split_trim(&active);
            let accept_all = views.len() == 1 && views[0].is_empty();
            if !accept_all && !contain(&views, view_name) {
                views.push(view_name.to_string());
                config.set_active_views(&join(&views, ','))?;
            }
        }
    }
    Ok(())
}

fn remove_active_display_view(
    config: &mut Config,
    display_name: &str,
    view_name: &str,
) -> Result<()> {
    // Remove the display from the active display list only if possible.

    if let Some(env) = non_empty_env(OCIO_ACTIVE_DISPLAYS_ENVVAR) {
        let displays = split_trim(&env);
        let accept_all = displays.len() == 1 && displays[0].is_empty();
        if !accept_all {
            return Err(Error::msg(format!(
                "Forbidden to remove an active display as '{OCIO_ACTIVE_DISPLAYS_ENVVAR}' controls the active list."
            )));
        }
    } else {
        let active = config.active_displays();
        if !active.is_empty() {
            let mut displays = split_trim(&active);
            let accept_all = displays.len() == 1 && displays[0].is_empty();
            if !accept_all && contain(&displays, display_name) {
                // As the display is an active one, not finding it in the list means that it
                // could be removed from the 'active_displays' list.
                let to_remove =
                    !(0..config.num_displays()).any(|i| compare(&config.display(i), display_name));
                if to_remove {
                    remove(&mut displays, display_name);
                    config.set_active_displays(&join(&displays, ','))?;
                }
            }
        }
    }

    // Remove the view from the active view list only if possible.

    if let Some(env) = non_empty_env(OCIO_ACTIVE_VIEWS_ENVVAR) {
        let views = split_trim(&env);
        let accept_all = views.len() == 1 && views[0].is_empty();
        if !accept_all {
            return Err(Error::msg(format!(
                "Forbidden to remove an active view as '{OCIO_ACTIVE_VIEWS_ENVVAR}' controls the active list."
            )));
        }
    } else {
        let active = config.active_views();
        if !active.is_empty() {
            let mut views = split_trim(&active);
            let accept_all = views.len() == 1 && views[0].is_empty();
            if !accept_all && contain(&views, view_name) {
                // As the view is an active one, not finding it in the list means that it could
                // be removed. But the code needs to loop over all the displays i.e. active and
                // inactive ones, before validating the removal.

                // Enable all the displays while looking for the view.
                let saved_active_displays = config.active_displays();
                config.set_active_displays("")?;

                let mut to_remove = true;
                for disp_idx in 0..config.num_displays() {
                    if !to_remove {
                        break;
                    }
                    let disp = config.display(disp_idx);
                    for view_idx in 0..config.num_views(&disp) {
                        if compare(&config.view(&disp, view_idx), view_name) {
                            to_remove = false;
                            break;
                        }
                    }
                }

                config.set_active_displays(&saved_active_displays)?;

                if to_remove {
                    remove(&mut views, view_name);
                    config.set_active_views(&join(&views, ','))?;
                }
            }
        }
    }
    Ok(())
}

fn add_display_view_impl(
    config: &mut Config,
    display_name: &str,
    view_name: &str,
    look_definition: &str,
    color_space: &mut ColorSpace,
    user_transform: FileTransform,
    connection_color_space_name: &str,
) -> Result<()> {
    if display_name.is_empty() {
        return Err(Error::msg("Invalid display name."));
    }
    if view_name.is_empty() {
        return Err(Error::msg("Invalid view name."));
    }

    // Step 1 - Create the color transformation.

    let mut grp = GroupTransform::new();

    // Add the 'reference' to connection color space.
    {
        // Check for an active or inactive color space.
        let connection_cs = config
            .get_color_space(connection_color_space_name)
            .ok_or_else(|| {
                Error::msg(format!(
                    "Connection color space name '{connection_color_space_name}' does not exist."
                ))
            })?;

        if let Some(tr) = connection_cs.transform(ColorSpaceDirection::FromReference) {
            grp.append(tr.clone());
        } else if let Some(tr) = connection_cs.transform(ColorSpaceDirection::ToReference) {
            grp.append(tr.inverted());
        }
    }

    // Add the 'LUT' transform.
    grp.append(user_transform);

    let grp = Transform::Group(grp);
    grp.validate()?;

    // Step 2 - Add active display and view.

    add_active_display_view(config, display_name, view_name)?;

    // Step 3 - Add the color space to the config.

    color_space.set_transform(Some(grp), ColorSpaceDirection::FromReference);
    config.add_color_space(color_space)?;

    // Step 4 - Add a new active (display, view) pair.

    config.add_display_view(display_name, view_name, color_space.name(), look_definition)
}

/// Add a new (display, view) pair and its new color space to a config
/// (`DisplayViewHelpers::AddDisplayView`). The input of the user transform
/// (a file) must be in the connection color space. The look definition,
/// color space family, description and categories may be empty. Categories
/// are only added if they are already used by the config.
#[allow(clippy::too_many_arguments)]
pub fn add_display_view(
    config: &mut Config,
    display_name: &str,
    view_name: &str,
    look_definition: &str,
    color_space_name: &str,
    color_space_family: &str,
    color_space_description: &str,
    categories: &str,
    transform_file_path: &str,
    connection_color_space_name: &str,
) -> Result<()> {
    let mut color_space = ColorSpace::default();
    color_space.set_name(color_space_name);
    color_space.set_family(color_space_family);
    color_space.set_description(color_space_description);

    // Check if the name is already a color space or a role name.
    if config.get_color_space(color_space.name()).is_some() {
        return Err(Error::msg(format!(
            "Color space name '{}' already exists.",
            color_space.name()
        )));
    }

    // Add categories if any.
    if !categories.is_empty() {
        let cats = extract_items(categories);
        // Only add the categories if already used.
        if !find_color_space_names(config, &cats).is_empty() {
            for cat in &cats {
                color_space.add_category(cat);
            }
        }
    }

    let file = FileTransform::new(transform_file_path);

    add_display_view_impl(
        config,
        display_name,
        view_name,
        look_definition,
        &mut color_space,
        file,
        connection_color_space_name,
    )
}

/// Remove a (display, view) pair and its color space if not used anymore
/// (`DisplayViewHelpers::RemoveDisplayView`). The view is always removed but
/// the display is only removed if empty.
pub fn remove_display_view(config: &mut Config, display_name: &str, view_name: &str) -> Result<()> {
    let name = config
        .display_view_color_space_name(display_name, view_name)
        .to_string();
    let cs_name = if name.is_empty() {
        display_name.to_string()
    } else {
        name
    };
    if cs_name.is_empty() {
        return Err(Error::msg(format!(
            "Missing color space for '{display_name}' and '{view_name}'."
        )));
    }

    // Step 1 - Remove the (display, view) pair.

    config.remove_display_view(display_name, view_name)?;

    // Step 2 - Remove the (display, view) pair from active lists if possible.

    remove_active_display_view(config, display_name, view_name)?;

    // Step 3 - Remove the associated color space if not used.

    if !config.is_color_space_used(&cs_name) {
        config.remove_color_space(&cs_name);
    }
    Ok(())
}