Skip to main content

ocio_rs/
app_helpers.rs

1//! Native OpenColorIO color-space menus.
2
3use std::{
4    ffi::{c_char, c_void, CStr},
5    ptr::NonNull,
6};
7
8use crate::transform::{
9    transform_from_raw_handle, ConstTransform, DisplayViewTransform, Transform, TransformHandle,
10};
11use crate::Context;
12use crate::{Config, ConstConfig, Processor, Result, SearchReferenceSpaceType, TransformDirection};
13
14unsafe fn text<'a>(ptr: *const c_char) -> &'a CStr {
15    if ptr.is_null() {
16        c""
17    } else {
18        unsafe { CStr::from_ptr(ptr) }
19    }
20}
21
22pub(crate) fn serialize_app_helper(handle: NonNull<c_void>, kind: i32) -> Result<String> {
23    crate::clear_last_error();
24    let ptr = unsafe { ocio_sys::ocio_app_helper_serialize(handle.as_ptr(), kind) };
25    crate::ocio_call_status()?;
26    if ptr.is_null() {
27        return Err(crate::OcioError::Ocio(
28            "null app helper serialization".into(),
29        ));
30    }
31    Ok(unsafe { CStr::from_ptr(ptr) }
32        .to_string_lossy()
33        .into_owned())
34}
35
36/// Mutable native options used to construct a color-space menu.
37pub struct ColorSpaceMenuParameters {
38    handle: NonNull<c_void>,
39}
40
41impl ColorSpaceMenuParameters {
42    /// Serialize using OCIO's diagnostic stream operator.
43    pub fn serialize(&self) -> Result<String> {
44        serialize_app_helper(self.handle, 0)
45    }
46    /// Create options for a configuration. The native parameters retain the config.
47    pub fn create(config: &Config) -> Result<Self> {
48        crate::clear_last_error();
49        let handle =
50            unsafe { ocio_sys::ocio_color_space_menu_parameters_create(config.handle.as_ptr()) };
51        Ok(Self {
52            handle: crate::handle_result(handle)?,
53        })
54    }
55
56    /// Create options retaining a read-only config without copying it.
57    pub fn create_from_const(config: &ConstConfig) -> Result<Self> {
58        crate::clear_last_error();
59        let handle = unsafe {
60            ocio_sys::ocio_color_space_menu_parameters_create_from_const(config.handle.as_ptr())
61        };
62        Ok(Self {
63            handle: crate::handle_result(handle)?,
64        })
65    }
66
67    /// Replace the retained configuration.
68    pub fn set_config(&mut self, config: &Config) {
69        unsafe {
70            ocio_sys::ocio_color_space_menu_parameters_set_config(
71                self.handle.as_ptr(),
72                config.handle.as_ptr(),
73            )
74        }
75    }
76
77    /// Replace the retained configuration with a read-only shared config.
78    pub fn set_const_config(&mut self, config: &ConstConfig) {
79        unsafe {
80            ocio_sys::ocio_color_space_menu_parameters_set_const_config(
81                self.handle.as_ptr(),
82                config.handle.as_ptr(),
83            )
84        }
85    }
86
87    /// Return an editable copy of the retained native configuration. Native
88    /// group-transform copies may retain children shared with the source.
89    pub fn config(&self) -> Result<Config> {
90        crate::clear_last_error();
91        let handle =
92            unsafe { ocio_sys::ocio_color_space_menu_parameters_get_config(self.handle.as_ptr()) };
93        Ok(Config {
94            handle: crate::handle_result(handle)?,
95        })
96    }
97
98    /// Retain the native configuration for read-only access without copying it.
99    pub fn const_config(&self) -> Result<ConstConfig> {
100        crate::clear_last_error();
101        let handle = unsafe {
102            ocio_sys::ocio_color_space_menu_parameters_get_const_config(self.handle.as_ptr())
103        };
104        Ok(ConstConfig {
105            handle: crate::handle_result(handle)?,
106        })
107    }
108
109    /// Return the native role setting.
110    pub fn role(&self) -> &CStr {
111        unsafe {
112            text(ocio_sys::ocio_color_space_menu_parameters_get_role(
113                self.handle.as_ptr(),
114            ))
115        }
116    }
117
118    /// Update the native role setting.
119    pub fn set_role(&mut self, value: &CStr) {
120        unsafe {
121            ocio_sys::ocio_color_space_menu_parameters_set_role(
122                self.handle.as_ptr(),
123                value.as_ptr(),
124            )
125        }
126    }
127
128    /// Return the native include color spaces setting.
129    pub fn include_color_spaces(&self) -> bool {
130        unsafe {
131            ocio_sys::ocio_color_space_menu_parameters_get_include_color_spaces(
132                self.handle.as_ptr(),
133            )
134        }
135    }
136
137    /// Update the native include color spaces setting.
138    pub fn set_include_color_spaces(&mut self, value: bool) {
139        unsafe {
140            ocio_sys::ocio_color_space_menu_parameters_set_include_color_spaces(
141                self.handle.as_ptr(),
142                value,
143            )
144        }
145    }
146
147    /// Return the native include named transforms setting.
148    pub fn include_named_transforms(&self) -> bool {
149        unsafe {
150            ocio_sys::ocio_color_space_menu_parameters_get_include_named_transforms(
151                self.handle.as_ptr(),
152            )
153        }
154    }
155
156    /// Update the native include named transforms setting.
157    pub fn set_include_named_transforms(&mut self, value: bool) {
158        unsafe {
159            ocio_sys::ocio_color_space_menu_parameters_set_include_named_transforms(
160                self.handle.as_ptr(),
161                value,
162            )
163        }
164    }
165
166    #[cfg(feature = "v2_5")]
167    /// Return the native treat no category as any setting.
168    pub fn treat_no_category_as_any(&self) -> bool {
169        unsafe {
170            ocio_sys::ocio_color_space_menu_parameters_get_treat_no_category_as_any(
171                self.handle.as_ptr(),
172            )
173        }
174    }
175
176    #[cfg(feature = "v2_5")]
177    /// Update the native treat no category as any setting.
178    pub fn set_treat_no_category_as_any(&mut self, value: bool) {
179        unsafe {
180            ocio_sys::ocio_color_space_menu_parameters_set_treat_no_category_as_any(
181                self.handle.as_ptr(),
182                value,
183            )
184        }
185    }
186
187    /// Return the native app categories setting.
188    pub fn app_categories(&self) -> &CStr {
189        unsafe {
190            text(
191                ocio_sys::ocio_color_space_menu_parameters_get_app_categories(self.handle.as_ptr()),
192            )
193        }
194    }
195
196    /// Update the native app categories setting.
197    pub fn set_app_categories(&mut self, value: &CStr) {
198        unsafe {
199            ocio_sys::ocio_color_space_menu_parameters_set_app_categories(
200                self.handle.as_ptr(),
201                value.as_ptr(),
202            )
203        }
204    }
205
206    /// Return the native encodings setting.
207    pub fn encodings(&self) -> &CStr {
208        unsafe {
209            text(ocio_sys::ocio_color_space_menu_parameters_get_encodings(
210                self.handle.as_ptr(),
211            ))
212        }
213    }
214
215    /// Update the native encodings setting.
216    pub fn set_encodings(&mut self, value: &CStr) {
217        unsafe {
218            ocio_sys::ocio_color_space_menu_parameters_set_encodings(
219                self.handle.as_ptr(),
220                value.as_ptr(),
221            )
222        }
223    }
224
225    /// Return the native user categories setting.
226    pub fn user_categories(&self) -> &CStr {
227        unsafe {
228            text(
229                ocio_sys::ocio_color_space_menu_parameters_get_user_categories(
230                    self.handle.as_ptr(),
231                ),
232            )
233        }
234    }
235
236    /// Update the native user categories setting.
237    pub fn set_user_categories(&mut self, value: &CStr) {
238        unsafe {
239            ocio_sys::ocio_color_space_menu_parameters_set_user_categories(
240                self.handle.as_ptr(),
241                value.as_ptr(),
242            )
243        }
244    }
245
246    /// Return the native include roles setting.
247    pub fn include_roles(&self) -> bool {
248        unsafe {
249            ocio_sys::ocio_color_space_menu_parameters_get_include_roles(self.handle.as_ptr())
250        }
251    }
252
253    /// Update the native include roles setting.
254    pub fn set_include_roles(&mut self, value: bool) {
255        unsafe {
256            ocio_sys::ocio_color_space_menu_parameters_set_include_roles(
257                self.handle.as_ptr(),
258                value,
259            )
260        }
261    }
262
263    /// Return the native search reference space type setting.
264    pub fn search_reference_space_type(&self) -> SearchReferenceSpaceType {
265        match unsafe {
266            ocio_sys::ocio_color_space_menu_parameters_get_search_reference_space_type(
267                self.handle.as_ptr(),
268            )
269        } {
270            0 => SearchReferenceSpaceType::Scene,
271            1 => SearchReferenceSpaceType::Display,
272            _ => SearchReferenceSpaceType::All,
273        }
274    }
275
276    /// Update the native search reference space type setting.
277    pub fn set_search_reference_space_type(&mut self, value: SearchReferenceSpaceType) {
278        unsafe {
279            ocio_sys::ocio_color_space_menu_parameters_set_search_reference_space_type(
280                self.handle.as_ptr(),
281                value as i32,
282            )
283        }
284    }
285
286    /// Include an additional color space or named transform by name.
287    pub fn add_color_space(&mut self, name: &CStr) {
288        unsafe {
289            ocio_sys::ocio_color_space_menu_parameters_add_color_space(
290                self.handle.as_ptr(),
291                name.as_ptr(),
292            )
293        }
294    }
295
296    /// Count explicitly added entries before native menu construction.
297    pub fn num_added_color_spaces(&self) -> usize {
298        unsafe {
299            ocio_sys::ocio_color_space_menu_parameters_get_num_added_color_spaces(
300                self.handle.as_ptr(),
301            )
302        }
303    }
304
305    /// Return an explicitly added name, or an empty string for an invalid index.
306    pub fn added_color_space(&self, index: usize) -> &CStr {
307        unsafe {
308            text(
309                ocio_sys::ocio_color_space_menu_parameters_get_added_color_space(
310                    self.handle.as_ptr(),
311                    index,
312                ),
313            )
314        }
315    }
316
317    /// Clear explicitly added entries.
318    pub fn clear_added_color_spaces(&mut self) {
319        unsafe {
320            ocio_sys::ocio_color_space_menu_parameters_clear_added_color_spaces(
321                self.handle.as_ptr(),
322            )
323        }
324    }
325}
326
327impl Drop for ColorSpaceMenuParameters {
328    fn drop(&mut self) {
329        unsafe { ocio_sys::ocio_color_space_menu_parameters_destroy(self.handle.as_ptr()) }
330    }
331}
332
333/// Read-only native color-space menu. Strings borrow the menu's storage.
334pub struct ColorSpaceMenuHelper {
335    handle: NonNull<c_void>,
336}
337
338impl ColorSpaceMenuHelper {
339    /// Serialize using OCIO's diagnostic stream operator.
340    pub fn serialize(&self) -> Result<String> {
341        serialize_app_helper(self.handle, 1)
342    }
343    /// Build a native menu. Upstream evaluates filtering and hierarchy once.
344    pub fn create(parameters: &ColorSpaceMenuParameters) -> Result<Self> {
345        crate::clear_last_error();
346        let handle =
347            unsafe { ocio_sys::ocio_color_space_menu_helper_create(parameters.handle.as_ptr()) };
348        Ok(Self {
349            handle: crate::handle_result(handle)?,
350        })
351    }
352
353    /// Native menu num color spaces.
354    pub fn num_color_spaces(&self) -> usize {
355        unsafe { ocio_sys::ocio_color_space_menu_helper_get_num_color_spaces(self.handle.as_ptr()) }
356    }
357
358    /// Native menu name. Returns an empty string if not found.
359    pub fn name(&self, index: usize) -> &CStr {
360        unsafe {
361            text(ocio_sys::ocio_color_space_menu_helper_get_name(
362                self.handle.as_ptr(),
363                index,
364            ))
365        }
366    }
367
368    /// Native menu ui name. Returns an empty string if not found.
369    pub fn ui_name(&self, index: usize) -> &CStr {
370        unsafe {
371            text(ocio_sys::ocio_color_space_menu_helper_get_ui_name(
372                self.handle.as_ptr(),
373                index,
374            ))
375        }
376    }
377
378    /// Native menu index from name.
379    pub fn index_from_name(&self, name: &CStr) -> Option<usize> {
380        let index = unsafe {
381            ocio_sys::ocio_color_space_menu_helper_get_index_from_name(
382                self.handle.as_ptr(),
383                name.as_ptr(),
384            )
385        };
386        (index != usize::MAX).then_some(index)
387    }
388
389    /// Native menu index from ui name.
390    pub fn index_from_ui_name(&self, name: &CStr) -> Option<usize> {
391        let index = unsafe {
392            ocio_sys::ocio_color_space_menu_helper_get_index_from_ui_name(
393                self.handle.as_ptr(),
394                name.as_ptr(),
395            )
396        };
397        (index != usize::MAX).then_some(index)
398    }
399
400    /// Native menu description. Returns an empty string if not found.
401    pub fn description(&self, index: usize) -> &CStr {
402        unsafe {
403            text(ocio_sys::ocio_color_space_menu_helper_get_description(
404                self.handle.as_ptr(),
405                index,
406            ))
407        }
408    }
409
410    /// Native menu family. Returns an empty string if not found.
411    pub fn family(&self, index: usize) -> &CStr {
412        unsafe {
413            text(ocio_sys::ocio_color_space_menu_helper_get_family(
414                self.handle.as_ptr(),
415                index,
416            ))
417        }
418    }
419
420    /// Native menu num hierarchy levels.
421    pub fn num_hierarchy_levels(&self, index: usize) -> usize {
422        unsafe {
423            ocio_sys::ocio_color_space_menu_helper_get_num_hierarchy_levels(
424                self.handle.as_ptr(),
425                index,
426            )
427        }
428    }
429
430    /// Native menu hierarchy level. Returns an empty string if not found.
431    pub fn hierarchy_level(&self, index: usize, level: usize) -> &CStr {
432        unsafe {
433            text(ocio_sys::ocio_color_space_menu_helper_get_hierarchy_level(
434                self.handle.as_ptr(),
435                index,
436                level,
437            ))
438        }
439    }
440
441    /// Native menu name from ui name. Returns an empty string if not found.
442    pub fn name_from_ui_name(&self, name: &CStr) -> &CStr {
443        unsafe {
444            text(
445                ocio_sys::ocio_color_space_menu_helper_get_name_from_ui_name(
446                    self.handle.as_ptr(),
447                    name.as_ptr(),
448                ),
449            )
450        }
451    }
452
453    /// Native menu ui name from name. Returns an empty string if not found.
454    pub fn ui_name_from_name(&self, name: &CStr) -> &CStr {
455        unsafe {
456            text(
457                ocio_sys::ocio_color_space_menu_helper_get_ui_name_from_name(
458                    self.handle.as_ptr(),
459                    name.as_ptr(),
460                ),
461            )
462        }
463    }
464}
465
466impl Drop for ColorSpaceMenuHelper {
467    fn drop(&mut self) {
468        unsafe { ocio_sys::ocio_color_space_menu_helper_destroy(self.handle.as_ptr()) }
469    }
470}
471
472/// Native mixing space and encoding selection.
473pub struct MixingColorSpaceManager {
474    handle: NonNull<c_void>,
475}
476
477impl MixingColorSpaceManager {
478    /// Serialize using OCIO's diagnostic stream operator.
479    pub fn serialize(&self) -> Result<String> {
480        serialize_app_helper(self.handle, 2)
481    }
482    /// Create using the native implementation.
483    pub fn create(config: &Config) -> Result<Self> {
484        crate::clear_last_error();
485        let value =
486            unsafe { ocio_sys::ocio_mixing_color_space_manager_create(config.handle.as_ptr()) };
487        Ok(Self {
488            handle: crate::handle_result(value)?,
489        })
490    }
491
492    /// Create from a retained read-only config without copying the config.
493    pub fn create_from_const(config: &ConstConfig) -> Result<Self> {
494        crate::clear_last_error();
495        let value = unsafe {
496            ocio_sys::ocio_mixing_color_space_manager_create_from_const(config.handle.as_ptr())
497        };
498        Ok(Self {
499            handle: crate::handle_result(value)?,
500        })
501    }
502
503    /// Num mixing spaces using the native implementation.
504    pub fn num_mixing_spaces(&self) -> usize {
505        unsafe {
506            ocio_sys::ocio_mixing_color_space_manager_get_num_mixing_spaces(self.handle.as_ptr())
507        }
508    }
509
510    /// Mixing space ui name using the native implementation.
511    pub fn mixing_space_ui_name(&self, index: usize) -> Result<&CStr> {
512        crate::clear_last_error();
513        let value = unsafe {
514            ocio_sys::ocio_mixing_color_space_manager_get_mixing_space_ui_name(
515                self.handle.as_ptr(),
516                index,
517            )
518        };
519        crate::ocio_call_status()?;
520        Ok(unsafe { text(value) })
521    }
522
523    /// Selected mixing space idx using the native implementation.
524    pub fn selected_mixing_space_idx(&self) -> usize {
525        unsafe {
526            ocio_sys::ocio_mixing_color_space_manager_get_selected_mixing_space_idx(
527                self.handle.as_ptr(),
528            )
529        }
530    }
531
532    /// Set selected mixing space idx using the native implementation.
533    pub fn set_selected_mixing_space_idx(&mut self, index: usize) -> Result<()> {
534        crate::clear_last_error();
535        unsafe {
536            ocio_sys::ocio_mixing_color_space_manager_set_selected_mixing_space_idx(
537                self.handle.as_ptr(),
538                index,
539            )
540        };
541        crate::ocio_call_status()
542    }
543
544    /// Set selected mixing space using the native implementation.
545    pub fn set_selected_mixing_space(&mut self, name: &CStr) -> Result<()> {
546        crate::clear_last_error();
547        unsafe {
548            ocio_sys::ocio_mixing_color_space_manager_set_selected_mixing_space(
549                self.handle.as_ptr(),
550                name.as_ptr(),
551            )
552        };
553        crate::ocio_call_status()
554    }
555
556    /// Is perceptually uniform using the native implementation.
557    pub fn is_perceptually_uniform(&self) -> bool {
558        unsafe {
559            ocio_sys::ocio_mixing_color_space_manager_is_perceptually_uniform(self.handle.as_ptr())
560        }
561    }
562
563    /// Num mixing encodings using the native implementation.
564    pub fn num_mixing_encodings(&self) -> usize {
565        unsafe {
566            ocio_sys::ocio_mixing_color_space_manager_get_num_mixing_encodings(self.handle.as_ptr())
567        }
568    }
569
570    /// Mixing encoding name using the native implementation.
571    pub fn mixing_encoding_name(&self, index: usize) -> Result<&CStr> {
572        crate::clear_last_error();
573        let value = unsafe {
574            ocio_sys::ocio_mixing_color_space_manager_get_mixing_encoding_name(
575                self.handle.as_ptr(),
576                index,
577            )
578        };
579        crate::ocio_call_status()?;
580        Ok(unsafe { text(value) })
581    }
582
583    /// Selected mixing encoding idx using the native implementation.
584    pub fn selected_mixing_encoding_idx(&self) -> usize {
585        unsafe {
586            ocio_sys::ocio_mixing_color_space_manager_get_selected_mixing_encoding_idx(
587                self.handle.as_ptr(),
588            )
589        }
590    }
591
592    /// Set selected mixing encoding idx using the native implementation.
593    pub fn set_selected_mixing_encoding_idx(&mut self, index: usize) -> Result<()> {
594        crate::clear_last_error();
595        unsafe {
596            ocio_sys::ocio_mixing_color_space_manager_set_selected_mixing_encoding_idx(
597                self.handle.as_ptr(),
598                index,
599            )
600        };
601        crate::ocio_call_status()
602    }
603
604    /// Set selected mixing encoding using the native implementation.
605    pub fn set_selected_mixing_encoding(&mut self, name: &CStr) -> Result<()> {
606        crate::clear_last_error();
607        unsafe {
608            ocio_sys::ocio_mixing_color_space_manager_set_selected_mixing_encoding(
609                self.handle.as_ptr(),
610                name.as_ptr(),
611            )
612        };
613        crate::ocio_call_status()
614    }
615
616    /// Refresh using the native implementation.
617    pub fn refresh(&mut self, config: &Config) -> Result<()> {
618        crate::clear_last_error();
619        unsafe {
620            ocio_sys::ocio_mixing_color_space_manager_refresh(
621                self.handle.as_ptr(),
622                config.handle.as_ptr(),
623            )
624        };
625        crate::ocio_call_status()
626    }
627
628    /// Refresh from a retained read-only config without copying the config.
629    pub fn refresh_from_const(&mut self, config: &ConstConfig) -> Result<()> {
630        crate::clear_last_error();
631        unsafe {
632            ocio_sys::ocio_mixing_color_space_manager_refresh_from_const(
633                self.handle.as_ptr(),
634                config.handle.as_ptr(),
635            )
636        };
637        crate::ocio_call_status()
638    }
639
640    /// Processor using the native implementation.
641    pub fn processor(
642        &self,
643        working: &CStr,
644        display: &CStr,
645        view: &CStr,
646        direction: TransformDirection,
647    ) -> Result<Processor> {
648        crate::clear_last_error();
649        let value = unsafe {
650            ocio_sys::ocio_mixing_color_space_manager_get_processor(
651                self.handle.as_ptr(),
652                working.as_ptr(),
653                display.as_ptr(),
654                view.as_ptr(),
655                direction as i32,
656            )
657        };
658        Ok(Processor {
659            handle: crate::handle_result(value)?,
660        })
661    }
662
663    /// Slider using the native implementation.
664    pub fn slider(&mut self) -> MixingSlider<'_> {
665        MixingSlider {
666            handle: NonNull::new(unsafe {
667                ocio_sys::ocio_mixing_color_space_manager_get_slider(self.handle.as_ptr())
668            })
669            .expect("native manager owns a slider"),
670            owner: std::marker::PhantomData,
671        }
672    }
673
674    /// Slider with edges using the native implementation.
675    pub fn slider_with_edges(&mut self, min: f32, max: f32) -> MixingSlider<'_> {
676        MixingSlider {
677            handle: NonNull::new(unsafe {
678                ocio_sys::ocio_mixing_color_space_manager_get_slider_with_edges(
679                    self.handle.as_ptr(),
680                    min,
681                    max,
682                )
683            })
684            .expect("native manager owns a slider"),
685            owner: std::marker::PhantomData,
686        }
687    }
688}
689
690impl Drop for MixingColorSpaceManager {
691    fn drop(&mut self) {
692        unsafe { ocio_sys::ocio_mixing_color_space_manager_destroy(self.handle.as_ptr()) }
693    }
694}
695
696/// An exclusive borrow of the native manager's slider, without an allocation.
697///
698/// The manager cannot be mutated or dropped while the slider is in use:
699///
700/// ```compile_fail
701/// use ocio_rs::{Config, MixingColorSpaceManager};
702/// let mut manager = MixingColorSpaceManager::create(&Config::raw().unwrap()).unwrap();
703/// let slider = manager.slider();
704/// drop(manager);
705/// let _ = slider.slider_to_mixing(0.5);
706/// ```
707pub struct MixingSlider<'a> {
708    handle: NonNull<c_void>,
709    owner: std::marker::PhantomData<&'a mut MixingColorSpaceManager>,
710}
711
712impl MixingSlider<'_> {
713    /// Serialize using OCIO's diagnostic stream operator.
714    pub fn serialize(&self) -> Result<String> {
715        serialize_app_helper(self.handle, 3)
716    }
717    /// Set slider min edge using the native implementation.
718    pub fn set_slider_min_edge(&mut self, value: f32) {
719        unsafe { ocio_sys::ocio_mixing_slider_set_slider_min_edge(self.handle.as_ptr(), value) }
720    }
721
722    /// Set slider max edge using the native implementation.
723    pub fn set_slider_max_edge(&mut self, value: f32) {
724        unsafe { ocio_sys::ocio_mixing_slider_set_slider_max_edge(self.handle.as_ptr(), value) }
725    }
726
727    /// Slider min edge using the native implementation.
728    pub fn slider_min_edge(&self) -> f32 {
729        unsafe { ocio_sys::ocio_mixing_slider_get_slider_min_edge(self.handle.as_ptr()) }
730    }
731
732    /// Slider max edge using the native implementation.
733    pub fn slider_max_edge(&self) -> f32 {
734        unsafe { ocio_sys::ocio_mixing_slider_get_slider_max_edge(self.handle.as_ptr()) }
735    }
736
737    /// Slider to mixing using the native implementation.
738    pub fn slider_to_mixing(&self, value: f32) -> f32 {
739        unsafe { ocio_sys::ocio_mixing_slider_slider_to_mixing(self.handle.as_ptr(), value) }
740    }
741
742    /// Mixing to slider using the native implementation.
743    pub fn mixing_to_slider(&self, value: f32) -> f32 {
744        unsafe { ocio_sys::ocio_mixing_slider_mixing_to_slider(self.handle.as_ptr(), value) }
745    }
746}
747
748/** Native OCIO v1-style viewing pipeline for applications that need its correction stages. */
749pub struct LegacyViewingPipeline {
750    handle: NonNull<c_void>,
751}
752
753/// Transform slots exposed by the native legacy viewing pipeline.
754#[derive(Debug, Clone, Copy, PartialEq, Eq)]
755#[repr(i32)]
756pub enum LegacyViewingStage {
757    DisplayView = 0,
758    LinearCorrection = 1,
759    ColorTimingCorrection = 2,
760    ChannelView = 3,
761    DisplayCorrection = 4,
762}
763
764impl LegacyViewingPipeline {
765    /// Retain the native stage without creating an editable copy. The handle
766    /// remains usable after the stage is replaced or the pipeline is dropped.
767    /// The display stage reflects the state stored by OCIO, including its
768    /// internal looks-bypass setting, not the original caller-supplied state.
769    pub fn const_stage(&self, stage: LegacyViewingStage) -> Result<Option<ConstTransform>> {
770        crate::clear_last_error();
771        let handle = unsafe {
772            ocio_sys::ocio_legacy_viewing_pipeline_get_const_stage(
773                self.handle.as_ptr(),
774                stage as i32,
775            )
776        };
777        crate::ocio_call_status()?;
778        Ok(NonNull::new(handle).map(|handle| ConstTransform { handle }))
779    }
780
781    /// Pass a retained transform directly to OCIO, which makes its own copy.
782    /// `None` clears the stage. `DisplayView` accepts only a display-view transform.
783    pub fn set_const_stage(
784        &mut self,
785        stage: LegacyViewingStage,
786        transform: Option<&ConstTransform>,
787    ) -> Result<()> {
788        let transform =
789            transform.map_or(std::ptr::null(), |value| value.handle.as_ptr().cast_const());
790        crate::clear_last_error();
791        unsafe {
792            ocio_sys::ocio_legacy_viewing_pipeline_set_const_stage(
793                self.handle.as_ptr(),
794                stage as i32,
795                transform,
796            )
797        };
798        crate::ocio_call_status()
799    }
800
801    /// Serialize using OCIO's diagnostic stream operator.
802    pub fn serialize(&self) -> Result<String> {
803        serialize_app_helper(self.handle, 4)
804    }
805    /// Create a native pipeline with no correction stages.
806    pub fn create() -> Result<Self> {
807        crate::clear_last_error();
808        let handle = unsafe { ocio_sys::ocio_legacy_viewing_pipeline_create() };
809        Ok(Self {
810            handle: crate::handle_result(handle)?,
811        })
812    }
813
814    /// Return an independent editable copy, if this stage is set.
815    pub fn display_view_transform(&self) -> Result<Option<DisplayViewTransform>> {
816        crate::clear_last_error();
817        let handle = unsafe {
818            ocio_sys::ocio_legacy_viewing_pipeline_get_display_view_transform(self.handle.as_ptr())
819        };
820        crate::ocio_call_status()?;
821        match transform_from_raw_handle(handle) {
822            Some(Transform::DisplayView(transform)) => Ok(Some(transform)),
823            None => Ok(None),
824            _ => Err(crate::OcioError::Ocio(
825                "Unexpected legacy display transform type".into(),
826            )),
827        }
828    }
829
830    /// Set this stage using OCIO's native copy semantics, or clear it with `None`.
831    pub fn set_display_view_transform(&mut self, transform: Option<&DisplayViewTransform>) {
832        let transform = transform.map_or(std::ptr::null_mut(), TransformHandle::as_ptr);
833        unsafe {
834            ocio_sys::ocio_legacy_viewing_pipeline_set_display_view_transform(
835                self.handle.as_ptr(),
836                transform,
837            )
838        }
839    }
840
841    /// Return an editable copy, if this stage is set. A copied group still shares its children.
842    pub fn linear_cc(&self) -> Result<Option<Transform>> {
843        crate::clear_last_error();
844        let handle =
845            unsafe { ocio_sys::ocio_legacy_viewing_pipeline_get_linear_cc(self.handle.as_ptr()) };
846        crate::ocio_call_status()?;
847        Ok(transform_from_raw_handle(handle))
848    }
849
850    /// Set this stage using OCIO's native copy semantics, or clear it with `None`.
851    pub fn set_linear_cc(&mut self, transform: Option<&Transform>) {
852        let transform = transform.map_or(std::ptr::null_mut(), TransformHandle::as_ptr);
853        unsafe {
854            ocio_sys::ocio_legacy_viewing_pipeline_set_linear_cc(self.handle.as_ptr(), transform)
855        }
856    }
857
858    /// Return an editable copy, if this stage is set. A copied group still shares its children.
859    pub fn color_timing_cc(&self) -> Result<Option<Transform>> {
860        crate::clear_last_error();
861        let handle = unsafe {
862            ocio_sys::ocio_legacy_viewing_pipeline_get_color_timing_cc(self.handle.as_ptr())
863        };
864        crate::ocio_call_status()?;
865        Ok(transform_from_raw_handle(handle))
866    }
867
868    /// Set this stage using OCIO's native copy semantics, or clear it with `None`.
869    pub fn set_color_timing_cc(&mut self, transform: Option<&Transform>) {
870        let transform = transform.map_or(std::ptr::null_mut(), TransformHandle::as_ptr);
871        unsafe {
872            ocio_sys::ocio_legacy_viewing_pipeline_set_color_timing_cc(
873                self.handle.as_ptr(),
874                transform,
875            )
876        }
877    }
878
879    /// Return an editable copy, if this stage is set. A copied group still shares its children.
880    pub fn channel_view(&self) -> Result<Option<Transform>> {
881        crate::clear_last_error();
882        let handle = unsafe {
883            ocio_sys::ocio_legacy_viewing_pipeline_get_channel_view(self.handle.as_ptr())
884        };
885        crate::ocio_call_status()?;
886        Ok(transform_from_raw_handle(handle))
887    }
888
889    /// Set this stage using OCIO's native copy semantics, or clear it with `None`.
890    pub fn set_channel_view(&mut self, transform: Option<&Transform>) {
891        let transform = transform.map_or(std::ptr::null_mut(), TransformHandle::as_ptr);
892        unsafe {
893            ocio_sys::ocio_legacy_viewing_pipeline_set_channel_view(self.handle.as_ptr(), transform)
894        }
895    }
896
897    /// Return an editable copy, if this stage is set. A copied group still shares its children.
898    pub fn display_cc(&self) -> Result<Option<Transform>> {
899        crate::clear_last_error();
900        let handle =
901            unsafe { ocio_sys::ocio_legacy_viewing_pipeline_get_display_cc(self.handle.as_ptr()) };
902        crate::ocio_call_status()?;
903        Ok(transform_from_raw_handle(handle))
904    }
905
906    /// Set this stage using OCIO's native copy semantics, or clear it with `None`.
907    pub fn set_display_cc(&mut self, transform: Option<&Transform>) {
908        let transform = transform.map_or(std::ptr::null_mut(), TransformHandle::as_ptr);
909        unsafe {
910            ocio_sys::ocio_legacy_viewing_pipeline_set_display_cc(self.handle.as_ptr(), transform)
911        }
912    }
913
914    /// Read the native looks override setting.
915    pub fn looks_override(&self) -> Result<&CStr> {
916        crate::clear_last_error();
917        let value = unsafe {
918            ocio_sys::ocio_legacy_viewing_pipeline_get_looks_override(self.handle.as_ptr())
919        };
920        crate::ocio_call_status()?;
921        Ok(unsafe { text(value) })
922    }
923
924    /// Update the native looks override setting.
925    pub fn set_looks_override(&mut self, looks: &CStr) -> Result<()> {
926        crate::clear_last_error();
927        unsafe {
928            ocio_sys::ocio_legacy_viewing_pipeline_set_looks_override(
929                self.handle.as_ptr(),
930                looks.as_ptr(),
931            )
932        };
933        crate::ocio_call_status()
934    }
935
936    /// Read the native looks override enabled setting.
937    pub fn looks_override_enabled(&self) -> Result<bool> {
938        crate::clear_last_error();
939        let value = unsafe {
940            ocio_sys::ocio_legacy_viewing_pipeline_get_looks_override_enabled(self.handle.as_ptr())
941        };
942        crate::ocio_call_status()?;
943        Ok(value)
944    }
945
946    /// Update the native looks override enabled setting.
947    pub fn set_looks_override_enabled(&mut self, enabled: bool) -> Result<()> {
948        crate::clear_last_error();
949        unsafe {
950            ocio_sys::ocio_legacy_viewing_pipeline_set_looks_override_enabled(
951                self.handle.as_ptr(),
952                enabled,
953            )
954        };
955        crate::ocio_call_status()
956    }
957
958    /// Build the native viewing processor with the config's context.
959    pub fn processor(&self, config: &Config) -> Result<Processor> {
960        crate::clear_last_error();
961        let handle = unsafe {
962            ocio_sys::ocio_legacy_viewing_pipeline_get_processor(
963                self.handle.as_ptr(),
964                config.handle.as_ptr(),
965            )
966        };
967        Ok(Processor {
968            handle: crate::handle_result(handle)?,
969        })
970    }
971
972    /// Build the native viewing processor with an explicit context.
973    pub fn processor_with_context(&self, config: &Config, context: &Context) -> Result<Processor> {
974        crate::clear_last_error();
975        let handle = unsafe {
976            ocio_sys::ocio_legacy_viewing_pipeline_get_processor_with_context(
977                self.handle.as_ptr(),
978                config.handle.as_ptr(),
979                context.handle.as_ptr(),
980            )
981        };
982        Ok(Processor {
983            handle: crate::handle_result(handle)?,
984        })
985    }
986
987    /// Build with a retained config and an explicit or config-owned context.
988    /// Static processors use the native config directly. Dynamic processors
989    /// are rebuilt from an independent copy to isolate mutable properties.
990    pub fn processor_with_const_config(
991        &self,
992        config: &ConstConfig,
993        context: Option<&crate::ConstContext>,
994    ) -> Result<Processor> {
995        crate::clear_last_error();
996        let handle = unsafe {
997            ocio_sys::ocio_legacy_viewing_pipeline_get_processor_with_const_config(
998                self.handle.as_ptr(),
999                config.handle.as_ptr(),
1000                context.map_or(std::ptr::null(), |ctx| ctx.handle.as_ptr().cast_const()),
1001            )
1002        };
1003        Ok(Processor {
1004            handle: crate::handle_result(handle)?,
1005        })
1006    }
1007}
1008
1009impl Drop for LegacyViewingPipeline {
1010    fn drop(&mut self) {
1011        unsafe { ocio_sys::ocio_legacy_viewing_pipeline_destroy(self.handle.as_ptr()) }
1012    }
1013}
1014
1015impl Config {
1016    /// Build a display processor with OCIO's dynamic adjustment stages.
1017    /// If `context` is absent, use the configuration's current context.
1018    pub fn application_display_processor(
1019        &self,
1020        context: Option<&Context>,
1021        working: &CStr,
1022        display: &CStr,
1023        view: &CStr,
1024        channel: Option<&crate::transform::MatrixTransform>,
1025        direction: TransformDirection,
1026    ) -> Result<Processor> {
1027        crate::clear_last_error();
1028        let handle = unsafe {
1029            ocio_sys::ocio_config_get_application_display_processor(
1030                self.handle.as_ptr(),
1031                context.map_or(std::ptr::null_mut(), |c| c.handle.as_ptr()),
1032                working.as_ptr(),
1033                display.as_ptr(),
1034                view.as_ptr(),
1035                channel.map_or(std::ptr::null_mut(), TransformHandle::as_ptr),
1036                direction as i32,
1037            )
1038        };
1039        Ok(Processor {
1040            handle: crate::handle_result(handle)?,
1041        })
1042    }
1043
1044    /// Author a display view and its associated color space from a transform file.
1045    pub fn add_display_view_from_file(&mut self, view: DisplayViewFromFile<'_>) -> Result<()> {
1046        crate::clear_last_error();
1047        unsafe {
1048            ocio_sys::ocio_config_add_display_view_from_file(
1049                self.handle.as_ptr(),
1050                view.display.as_ptr(),
1051                view.view.as_ptr(),
1052                view.looks.as_ptr(),
1053                view.color_space.as_ptr(),
1054                view.family.as_ptr(),
1055                view.description.as_ptr(),
1056                view.categories.as_ptr(),
1057                view.file.as_ptr(),
1058                view.connection_space.as_ptr(),
1059            )
1060        };
1061        crate::ocio_call_status()
1062    }
1063
1064    /// Remove the view, its now-empty display, and its color space if unused.
1065    pub fn remove_display_view_and_unused_color_space(
1066        &mut self,
1067        display: &CStr,
1068        view: &CStr,
1069    ) -> Result<()> {
1070        crate::clear_last_error();
1071        unsafe {
1072            ocio_sys::ocio_config_remove_display_view_and_unused_color_space(
1073                self.handle.as_ptr(),
1074                display.as_ptr(),
1075                view.as_ptr(),
1076            )
1077        };
1078        crate::ocio_call_status()
1079    }
1080
1081    /// Add a color space using a file transform whose output is the connection space.
1082    pub fn add_color_space_from_file(
1083        &mut self,
1084        name: &CStr,
1085        file: &CStr,
1086        categories: &CStr,
1087        connection_space: &CStr,
1088    ) -> Result<()> {
1089        crate::clear_last_error();
1090        unsafe {
1091            ocio_sys::ocio_config_add_color_space_from_file(
1092                self.handle.as_ptr(),
1093                name.as_ptr(),
1094                file.as_ptr(),
1095                categories.as_ptr(),
1096                connection_space.as_ptr(),
1097            )
1098        };
1099        crate::ocio_call_status()
1100    }
1101
1102    /// Create an identity viewing processor with native dynamic adjustment stages.
1103    pub fn display_view_helper_identity_processor(&self) -> Result<Processor> {
1104        crate::clear_last_error();
1105        let handle = unsafe {
1106            ocio_sys::ocio_config_get_display_view_helper_identity_processor(self.handle.as_ptr())
1107        };
1108        Ok(Processor {
1109            handle: crate::handle_result(handle)?,
1110        })
1111    }
1112}
1113
1114impl ConstConfig {
1115    /// Build a display processor using this retained config and an optional
1116    /// retained context. Dynamic processors are isolated from shared caches,
1117    /// matching the other `ConstConfig` processor methods.
1118    pub fn application_display_processor(
1119        &self,
1120        context: Option<&crate::ConstContext>,
1121        working: &CStr,
1122        display: &CStr,
1123        view: &CStr,
1124        channel: Option<&crate::transform::MatrixTransform>,
1125        direction: TransformDirection,
1126    ) -> Result<Processor> {
1127        crate::clear_last_error();
1128        let handle = unsafe {
1129            ocio_sys::ocio_const_config_application_display_processor(
1130                self.handle.as_ptr(),
1131                context.map_or(std::ptr::null(), |c| c.handle.as_ptr().cast_const()),
1132                working.as_ptr(),
1133                display.as_ptr(),
1134                view.as_ptr(),
1135                channel.map_or(std::ptr::null_mut(), TransformHandle::as_ptr),
1136                direction as i32,
1137                true,
1138            )
1139        };
1140        Ok(Processor {
1141            handle: crate::handle_result(handle)?,
1142        })
1143    }
1144
1145    /// Create an identity viewing processor with isolated dynamic adjustments.
1146    pub fn display_view_helper_identity_processor(&self) -> Result<Processor> {
1147        crate::clear_last_error();
1148        let handle = unsafe {
1149            ocio_sys::ocio_const_config_display_view_identity_processor(self.handle.as_ptr(), true)
1150        };
1151        Ok(Processor {
1152            handle: crate::handle_result(handle)?,
1153        })
1154    }
1155}
1156
1157/** Inputs to the native helper that authors a display view from a transform file. */
1158pub struct DisplayViewFromFile<'a> {
1159    /// Display name.
1160    pub display: &'a CStr,
1161    /// View name.
1162    pub view: &'a CStr,
1163    /// Look definition, or an empty string.
1164    pub looks: &'a CStr,
1165    /// Name for the new color space.
1166    pub color_space: &'a CStr,
1167    /// Color-space family, or an empty string.
1168    pub family: &'a CStr,
1169    /// Color-space description, or an empty string.
1170    pub description: &'a CStr,
1171    /// Comma-separated categories, or an empty string.
1172    pub categories: &'a CStr,
1173    /// Transform file path.
1174    pub file: &'a CStr,
1175    /// Color space expected by the file transform.
1176    pub connection_space: &'a CStr,
1177}