Skip to main content

ocio_rs/transform/
look_transform.rs

1use std::ffi::c_void;
2use std::ptr::NonNull;
3
4use crate::{cstr_from_mut, cstr_to_opt_string, cstring, OcioError, Result, TransformDirection};
5use ocio_sys;
6
7/// Applies an OCIO look string between source and destination color spaces.
8pub struct LookTransform {
9    pub(crate) handle: NonNull<c_void>,
10}
11
12impl LookTransform {
13    /// Resolve the final process space of a look expression, including OCIO's fallbacks.
14    /// An empty expression returns an empty string, as in native OCIO.
15    pub fn result_color_space(
16        config: &crate::Config,
17        context: &crate::ConstContext,
18        looks: impl AsRef<str>,
19    ) -> Result<String> {
20        let looks = cstring(looks)?;
21        crate::clear_last_error();
22        let result = unsafe {
23            ocio_sys::ocio_look_transform_get_result_color_space(
24                config.handle.as_ptr(),
25                context.handle.as_ptr(),
26                looks.as_ptr(),
27            )
28        };
29        crate::ocio_call_status()?;
30        unsafe { cstr_to_opt_string(result) }
31            .ok_or_else(|| OcioError::Ocio("null look result color space".into()))
32    }
33
34    /// Create a look transform with default settings.
35    pub fn create() -> Result<Self> {
36        crate::clear_last_error();
37        let handle = unsafe { ocio_sys::ocio_look_transform_create() };
38        crate::handle_result(handle).map(|handle| Self { handle })
39    }
40
41    /// Return the source color space name.
42    pub fn src(&self) -> Option<String> {
43        unsafe { cstr_from_mut(ocio_sys::ocio_look_transform_get_src(self.handle.as_ptr())) }
44    }
45
46    /// Set the source color space name.
47    pub fn set_src(&self, src: impl AsRef<str>) -> Result<()> {
48        let s = cstring(src)?;
49        crate::clear_last_error();
50        unsafe { ocio_sys::ocio_look_transform_set_src(self.handle.as_ptr(), s.as_ptr().cast()) };
51        crate::ocio_call_status()
52    }
53
54    /// Return the destination color space name.
55    pub fn dst(&self) -> Option<String> {
56        unsafe { cstr_from_mut(ocio_sys::ocio_look_transform_get_dst(self.handle.as_ptr())) }
57    }
58
59    /// Set the destination color space name.
60    pub fn set_dst(&self, dst: impl AsRef<str>) -> Result<()> {
61        let d = cstring(dst)?;
62        crate::clear_last_error();
63        unsafe { ocio_sys::ocio_look_transform_set_dst(self.handle.as_ptr(), d.as_ptr().cast()) };
64        crate::ocio_call_status()
65    }
66
67    /// Return the comma-separated look names.
68    pub fn looks(&self) -> Option<String> {
69        unsafe {
70            cstr_from_mut(ocio_sys::ocio_look_transform_get_looks(
71                self.handle.as_ptr(),
72            ))
73        }
74    }
75
76    /// Set the comma-separated look names.
77    pub fn set_looks(&self, looks: impl AsRef<str>) -> Result<()> {
78        let l = cstring(looks)?;
79        crate::clear_last_error();
80        unsafe { ocio_sys::ocio_look_transform_set_looks(self.handle.as_ptr(), l.as_ptr().cast()) };
81        crate::ocio_call_status()
82    }
83
84    /// Return the evaluation direction.
85    pub fn direction(&self) -> TransformDirection {
86        let dir = unsafe { ocio_sys::ocio_look_transform_get_direction(self.handle.as_ptr()) };
87        match dir {
88            1 => TransformDirection::Inverse,
89            _ => TransformDirection::Forward,
90        }
91    }
92
93    /// Set the evaluation direction, panicking on validation error.
94    pub fn set_direction(&self, direction: TransformDirection) {
95        self.try_set_direction(direction)
96            .expect("failed to set look transform direction");
97    }
98
99    /// Set evaluation direction and surface any OCIO validation error.
100    pub fn try_set_direction(&self, direction: TransformDirection) -> Result<()> {
101        crate::clear_last_error();
102        unsafe {
103            ocio_sys::ocio_look_transform_set_direction(self.handle.as_ptr(), direction as i32);
104        }
105        crate::ocio_call_status()
106    }
107
108    /// Create an editable copy that is independent from the original transform.
109    pub fn create_editable_copy(&self) -> Result<Self> {
110        crate::clear_last_error();
111        let handle =
112            unsafe { ocio_sys::ocio_look_transform_create_editable_copy(self.handle.as_ptr()) };
113        crate::handle_result(handle).map(|handle| Self { handle })
114    }
115
116    /// Return format metadata attached to the transform, when available.
117    pub fn format_metadata(&self) -> Option<crate::FormatMetadata> {
118        let handle = unsafe { ocio_sys::ocio_transform_get_format_metadata(self.handle.as_ptr()) };
119        NonNull::new(handle).map(|h| crate::FormatMetadata { handle: h })
120    }
121
122    /// Return whether color-space conversion is skipped for this look.
123    pub fn skip_color_space_conversion(&self) -> bool {
124        unsafe {
125            ocio_sys::ocio_look_transform_get_skip_color_space_conversion(self.handle.as_ptr())
126        }
127    }
128
129    /// Set whether to skip color-space conversion, panicking on validation error.
130    pub fn set_skip_color_space_conversion(&self, skip: bool) {
131        self.try_set_skip_color_space_conversion(skip)
132            .expect("failed to set look color space conversion bypass");
133    }
134
135    /// Set color-space conversion bypass and surface any OCIO validation error.
136    pub fn try_set_skip_color_space_conversion(&self, skip: bool) -> Result<()> {
137        crate::clear_last_error();
138        unsafe {
139            ocio_sys::ocio_look_transform_set_skip_color_space_conversion(
140                self.handle.as_ptr(),
141                skip,
142            );
143        }
144        crate::ocio_call_status()
145    }
146
147    /// Validate the transform configuration and return any errors.
148    pub fn validate(&self) -> Result<()> {
149        crate::clear_last_error();
150        unsafe { ocio_sys::ocio_look_transform_validate(self.handle.as_ptr()) };
151        crate::validation_status()
152    }
153}
154
155impl Drop for LookTransform {
156    fn drop(&mut self) {
157        unsafe { ocio_sys::ocio_look_transform_destroy(self.handle.as_ptr()) };
158    }
159}
160
161#[cfg(test)]
162mod tests {
163    use super::*;
164
165    #[test]
166    fn create_look_transform() {
167        let t = LookTransform::create();
168        assert!(t.is_ok());
169    }
170
171    #[test]
172    fn look_transform_methods_no_crash() {
173        let t = LookTransform::create().unwrap();
174        let _ = t.src();
175        let _ = t.dst();
176        let _ = t.looks();
177        let _ = t.direction();
178    }
179
180    #[test]
181    fn set_src_dst_looks_no_crash() {
182        let t = LookTransform::create().unwrap();
183        assert!(t.set_src("ACEScg").is_ok());
184        assert!(t.set_dst("Output").is_ok());
185        assert!(t.set_looks("look1,look2").is_ok());
186    }
187
188    #[test]
189    fn direction_no_crash() {
190        let t = LookTransform::create().unwrap();
191        let _ = t.direction();
192        t.try_set_direction(TransformDirection::Inverse).unwrap();
193    }
194
195    #[test]
196    fn create_editable_copy_no_crash() {
197        let t = LookTransform::create().unwrap();
198        let _ = t.create_editable_copy();
199    }
200
201    #[test]
202    fn skip_cs_conversion_no_crash() {
203        let t = LookTransform::create().unwrap();
204        let _ = t.skip_color_space_conversion();
205        t.set_skip_color_space_conversion(true);
206        t.set_skip_color_space_conversion(false);
207    }
208
209    #[test]
210    fn format_metadata_no_crash() {
211        let t = LookTransform::create().unwrap();
212        let _ = t.format_metadata();
213    }
214
215    #[test]
216    fn validate_no_crash() {
217        let t = LookTransform::create().unwrap();
218        let _ = t.validate();
219    }
220}