Skip to main content

ocio_rs/
const_config_item.rs

1use crate::transform::ConstTransform;
2use crate::{
3    Config, Look, NamedTransform, Result, TransformDirection, ViewTransform, ViewTransformDirection,
4};
5use std::ffi::{c_void, CStr};
6use std::ptr::NonNull;
7
8pub(crate) fn serialize_named_item(
9    handle: NonNull<c_void>,
10    kind: i32,
11    is_const: bool,
12) -> Result<String> {
13    crate::clear_last_error();
14    let text = unsafe {
15        crate::cstr_to_opt_string(ocio_sys::ocio_named_item_serialize_text(
16            handle.as_ptr(),
17            kind,
18            is_const,
19        ))
20    };
21    crate::ocio_call_status()?;
22    text.ok_or_else(|| {
23        crate::OcioError::Ocio("OCIO returned a null named item representation".into())
24    })
25}
26
27struct Item {
28    handle: NonNull<c_void>,
29    kind: i32,
30}
31impl Item {
32    fn text(&self, field: i32, index: usize, argument: Option<&CStr>) -> Result<Option<&CStr>> {
33        crate::clear_last_error();
34        let ptr = unsafe {
35            ocio_sys::ocio_const_named_item_text(
36                self.handle.as_ptr(),
37                self.kind,
38                field,
39                index,
40                argument.map_or(std::ptr::null(), CStr::as_ptr),
41            )
42        };
43        crate::ocio_call_status()?;
44        Ok(if ptr.is_null() {
45            None
46        } else {
47            Some(unsafe { CStr::from_ptr(ptr) })
48        })
49    }
50    fn number(&self, field: i32) -> Result<usize> {
51        crate::clear_last_error();
52        let value = unsafe {
53            ocio_sys::ocio_const_named_item_number(self.handle.as_ptr(), self.kind, field)
54        };
55        crate::ocio_call_status()?;
56        Ok(value)
57    }
58    fn contains(&self, field: i32, value: &CStr) -> Result<bool> {
59        crate::clear_last_error();
60        let found = unsafe {
61            ocio_sys::ocio_const_named_item_contains(
62                self.handle.as_ptr(),
63                self.kind,
64                field,
65                value.as_ptr(),
66            )
67        };
68        crate::ocio_call_status()?;
69        Ok(found)
70    }
71    fn name(&self) -> Result<Option<String>> {
72        crate::clear_last_error();
73        let ptr = unsafe { ocio_sys::ocio_const_named_item_name(self.handle.as_ptr(), self.kind) };
74        let name = unsafe { crate::cstr_to_opt_string(ptr) };
75        crate::ocio_call_status()?;
76        Ok(name)
77    }
78    fn copy(&self) -> Result<NonNull<c_void>> {
79        crate::clear_last_error();
80        let handle = unsafe {
81            ocio_sys::ocio_const_named_item_editable_copy(self.handle.as_ptr(), self.kind)
82        };
83        crate::handle_result(handle)
84    }
85    fn transform(&self, kind: i32, direction: i32) -> Result<Option<ConstTransform>> {
86        ConstTransform::from_parent(self.handle.as_ptr(), kind, direction)
87    }
88}
89impl Drop for Item {
90    fn drop(&mut self) {
91        unsafe { ocio_sys::ocio_const_named_item_destroy(self.handle.as_ptr(), self.kind) }
92    }
93}
94
95/// Shared read-only native look. Borrowed text remains valid while this object
96/// is alive, provided external native code does not mutate the same object.
97pub struct ConstLook(Item);
98/// Shared read-only native named transform. Borrowed text remains valid while
99/// this object is alive, provided external native code does not mutate it.
100pub struct ConstNamedTransform(Item);
101/// Shared read-only native view transform. Borrowed text remains valid while
102/// this object is alive, provided external native code does not mutate it.
103pub struct ConstViewTransform(Item);
104
105macro_rules! accessors {
106    ($ty:ident, $editable:ident) => {
107        impl $ty {
108            /// Copy the native name as owned text.
109            pub fn name(&self) -> Result<Option<String>> {
110                self.0.name()
111            }
112            /// Return OCIO's diagnostic stream output as owned text. It is not
113            /// a stable interchange format and may contain sensitive paths.
114            pub fn serialize(&self) -> Result<String> {
115                serialize_named_item(self.0.handle, self.0.kind, true)
116            }
117            /// Borrow the native name without allocating.
118            pub fn name_ref(&self) -> Result<Option<&CStr>> {
119                self.0.text(0, 0, None)
120            }
121            /// Borrow the native description without allocating.
122            pub fn description(&self) -> Result<Option<&CStr>> {
123                self.0.text(1, 0, None)
124            }
125            /// Create an editable copy. Native group-transform copies may
126            /// retain children shared with the original.
127            pub fn create_editable_copy(&self) -> Result<$editable> {
128                Ok($editable {
129                    handle: self.0.copy()?,
130                })
131            }
132        }
133    };
134}
135accessors!(ConstLook, Look);
136accessors!(ConstNamedTransform, NamedTransform);
137accessors!(ConstViewTransform, ViewTransform);
138
139impl ConstLook {
140    /// Return an owned snapshot of all interchange attributes.
141    #[cfg(feature = "v2_5")]
142    pub fn try_interchange_attributes(&self) -> Result<std::collections::BTreeMap<String, String>> {
143        crate::colorspace::interchange_attributes_snapshot(self.0.handle.as_ptr(), true, Some(0))
144    }
145    /// Borrow the process-space name.
146    pub fn process_space(&self) -> Result<Option<&CStr>> {
147        self.0.text(2, 0, None)
148    }
149    /// Borrow an interchange attribute; invalid attribute names return an error.
150    #[cfg(feature = "v2_5")]
151    pub fn interchange_attribute(&self, name: &CStr) -> Result<Option<&CStr>> {
152        self.0.text(7, 0, Some(name))
153    }
154    /// Share the forward transform without copying it.
155    pub fn try_const_transform(&self) -> Result<Option<ConstTransform>> {
156        self.0.transform(6, 0)
157    }
158    /// Share the inverse transform without copying it.
159    pub fn try_const_inverse_transform(&self) -> Result<Option<ConstTransform>> {
160        self.0.transform(7, 0)
161    }
162}
163impl ConstNamedTransform {
164    /// Borrow the family string.
165    pub fn family(&self) -> Result<Option<&CStr>> {
166        self.0.text(3, 0, None)
167    }
168    /// Borrow the encoding string.
169    pub fn encoding(&self) -> Result<Option<&CStr>> {
170        self.0.text(4, 0, None)
171    }
172    /// Number of aliases.
173    pub fn num_aliases(&self) -> Result<usize> {
174        self.0.number(0)
175    }
176    /// Borrow an alias; an out-of-range index returns `None`.
177    pub fn alias(&self, index: usize) -> Result<Option<&CStr>> {
178        self.0.text(5, index, None)
179    }
180    /// Check an alias without allocating a C string.
181    pub fn has_alias(&self, value: &CStr) -> Result<bool> {
182        self.0.contains(0, value)
183    }
184    /// Number of categories.
185    pub fn num_categories(&self) -> Result<usize> {
186        self.0.number(1)
187    }
188    /// Borrow a category; an out-of-range index returns `None`.
189    pub fn category(&self, index: usize) -> Result<Option<&CStr>> {
190        self.0.text(6, index, None)
191    }
192    /// Check a category without allocating a C string.
193    pub fn has_category(&self, value: &CStr) -> Result<bool> {
194        self.0.contains(1, value)
195    }
196    /// Resolve a transform, synthesizing it from the opposite direction if needed.
197    /// Returns an error if neither direction is defined.
198    pub fn resolve_const_transform(
199        &self,
200        direction: TransformDirection,
201    ) -> Result<Option<ConstTransform>> {
202        self.0.transform(10, direction as i32)
203    }
204    pub(crate) fn raw(&self) -> *mut c_void {
205        self.0.handle.as_ptr()
206    }
207    /// Share the transform in the requested direction.
208    pub fn try_const_transform(
209        &self,
210        direction: TransformDirection,
211    ) -> Result<Option<ConstTransform>> {
212        self.0.transform(8, direction as i32)
213    }
214}
215impl ConstViewTransform {
216    /// Return an owned snapshot of all interchange attributes.
217    #[cfg(feature = "v2_5")]
218    pub fn try_interchange_attributes(&self) -> Result<std::collections::BTreeMap<String, String>> {
219        crate::colorspace::interchange_attributes_snapshot(self.0.handle.as_ptr(), true, Some(2))
220    }
221    /// Borrow the family string.
222    pub fn family(&self) -> Result<Option<&CStr>> {
223        self.0.text(3, 0, None)
224    }
225    /// Number of categories.
226    pub fn num_categories(&self) -> Result<usize> {
227        self.0.number(1)
228    }
229    /// Borrow a category; an out-of-range index returns `None`.
230    pub fn category(&self, index: usize) -> Result<Option<&CStr>> {
231        self.0.text(6, index, None)
232    }
233    /// Check a category without allocating a C string.
234    pub fn has_category(&self, value: &CStr) -> Result<bool> {
235        self.0.contains(1, value)
236    }
237    /// Return the native reference-space type.
238    pub fn reference_space_type(&self) -> Result<crate::ReferenceSpaceType> {
239        match self.0.number(2)? {
240            0 => Ok(crate::ReferenceSpaceType::Scene),
241            1 => Ok(crate::ReferenceSpaceType::Display),
242            _ => Err(crate::OcioError::Ocio(
243                "unknown reference space type".into(),
244            )),
245        }
246    }
247    /// Borrow an interchange attribute; invalid attribute names return an error.
248    #[cfg(feature = "v2_5")]
249    pub fn interchange_attribute(&self, name: &CStr) -> Result<Option<&CStr>> {
250        self.0.text(7, 0, Some(name))
251    }
252    /// Share the transform in the requested direction.
253    pub fn try_const_transform(
254        &self,
255        direction: ViewTransformDirection,
256    ) -> Result<Option<ConstTransform>> {
257        self.0.transform(9, direction as i32)
258    }
259}
260
261fn lookup(config: *mut c_void, name: Option<&CStr>, kind: i32) -> Result<Option<Item>> {
262    crate::clear_last_error();
263    let handle = unsafe {
264        ocio_sys::ocio_config_get_const_named_item(
265            config,
266            name.map_or(std::ptr::null(), CStr::as_ptr),
267            kind,
268        )
269    };
270    crate::ocio_call_status()?;
271    Ok(NonNull::new(handle).map(|handle| Item {
272        handle,
273        kind: if kind == 3 { 2 } else { kind },
274    }))
275}
276
277fn lookup_const(config: *mut c_void, name: Option<&CStr>, kind: i32) -> Result<Option<Item>> {
278    crate::clear_last_error();
279    let handle = unsafe {
280        ocio_sys::ocio_const_config_get_named_item(
281            config,
282            name.map_or(std::ptr::null(), CStr::as_ptr),
283            kind,
284        )
285    };
286    crate::ocio_call_status()?;
287    Ok(NonNull::new(handle).map(|handle| Item {
288        handle,
289        kind: if kind == 3 { 2 } else { kind },
290    }))
291}
292
293impl crate::ConstConfig {
294    /// Share a native look without making an editable copy.
295    pub fn try_const_look(&self, name: &CStr) -> Result<Option<ConstLook>> {
296        Ok(lookup_const(self.handle.as_ptr(), Some(name), 0)?.map(ConstLook))
297    }
298    /// Share a native named transform without making an editable copy.
299    pub fn try_const_named_transform(&self, name: &CStr) -> Result<Option<ConstNamedTransform>> {
300        Ok(lookup_const(self.handle.as_ptr(), Some(name), 1)?.map(ConstNamedTransform))
301    }
302    /// Share a native view transform without making an editable copy.
303    pub fn try_const_view_transform(&self, name: &CStr) -> Result<Option<ConstViewTransform>> {
304        Ok(lookup_const(self.handle.as_ptr(), Some(name), 2)?.map(ConstViewTransform))
305    }
306    /// Share the default scene-to-display view transform.
307    pub fn try_const_default_scene_to_display_view_transform(
308        &self,
309    ) -> Result<Option<ConstViewTransform>> {
310        Ok(lookup_const(self.handle.as_ptr(), None, 3)?.map(ConstViewTransform))
311    }
312}
313
314impl Config {
315    /// Share a named native look without making an editable copy.
316    pub fn try_const_look(&self, name: &CStr) -> Result<Option<ConstLook>> {
317        Ok(lookup(self.handle.as_ptr(), Some(name), 0)?.map(ConstLook))
318    }
319    /// Share a native named transform without making an editable copy.
320    pub fn try_const_named_transform(&self, name: &CStr) -> Result<Option<ConstNamedTransform>> {
321        Ok(lookup(self.handle.as_ptr(), Some(name), 1)?.map(ConstNamedTransform))
322    }
323    /// Share a native view transform without making an editable copy.
324    pub fn try_const_view_transform(&self, name: &CStr) -> Result<Option<ConstViewTransform>> {
325        Ok(lookup(self.handle.as_ptr(), Some(name), 2)?.map(ConstViewTransform))
326    }
327    /// Share OCIO's default scene-to-display view transform, if one exists.
328    pub fn try_const_default_scene_to_display_view_transform(
329        &self,
330    ) -> Result<Option<ConstViewTransform>> {
331        Ok(lookup(self.handle.as_ptr(), None, 3)?.map(ConstViewTransform))
332    }
333}