Skip to main content

ocio_rs/transform/
color_space.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/// Converts between two named color spaces inside a config.
8pub struct ColorSpaceTransform {
9    pub(crate) handle: NonNull<c_void>,
10}
11
12impl ColorSpaceTransform {
13    /// Create a color-space transform with default source and destination.
14    pub fn create() -> Result<Self> {
15        crate::clear_last_error();
16        let handle = unsafe { ocio_sys::ocio_color_space_transform_create() };
17        crate::handle_result(handle).map(|handle| Self { handle })
18    }
19
20    /// Return the source color space name.
21    pub fn src(&self) -> Option<String> {
22        unsafe {
23            cstr_from_mut(ocio_sys::ocio_color_space_transform_get_src(
24                self.handle.as_ptr(),
25            ))
26        }
27    }
28
29    /// Set the source color space name.
30    pub fn set_src(&self, src: impl AsRef<str>) -> Result<()> {
31        let s = cstring(src)?;
32        crate::clear_last_error();
33        unsafe {
34            ocio_sys::ocio_color_space_transform_set_src(self.handle.as_ptr(), s.as_ptr().cast())
35        };
36        crate::ocio_call_status()
37    }
38
39    /// Return the destination color space name.
40    pub fn dst(&self) -> Option<String> {
41        unsafe {
42            cstr_from_mut(ocio_sys::ocio_color_space_transform_get_dst(
43                self.handle.as_ptr(),
44            ))
45        }
46    }
47
48    /// Set the destination color space name.
49    pub fn set_dst(&self, dst: impl AsRef<str>) -> Result<()> {
50        let d = cstring(dst)?;
51        crate::clear_last_error();
52        unsafe {
53            ocio_sys::ocio_color_space_transform_set_dst(self.handle.as_ptr(), d.as_ptr().cast())
54        };
55        crate::ocio_call_status()
56    }
57
58    /// Return whether data channel bypass is enabled.
59    pub fn data_bypass(&self) -> bool {
60        unsafe { ocio_sys::ocio_color_space_transform_get_data_bypass(self.handle.as_ptr()) }
61    }
62
63    /// Set data bypass behavior, panicking on validation error.
64    pub fn set_data_bypass(&self, bypass: bool) {
65        self.try_set_data_bypass(bypass)
66            .expect("failed to set color space data bypass");
67    }
68
69    /// Set data bypass behavior and surface any OCIO validation error.
70    pub fn try_set_data_bypass(&self, bypass: bool) -> Result<()> {
71        crate::clear_last_error();
72        unsafe {
73            ocio_sys::ocio_color_space_transform_set_data_bypass(self.handle.as_ptr(), bypass)
74        };
75        crate::ocio_call_status()
76    }
77
78    /// Return the evaluation direction.
79    pub fn direction(&self) -> TransformDirection {
80        let dir =
81            unsafe { ocio_sys::ocio_color_space_transform_get_direction(self.handle.as_ptr()) };
82        match dir {
83            1 => TransformDirection::Inverse,
84            _ => TransformDirection::Forward,
85        }
86    }
87
88    /// Set the evaluation direction, panicking on validation error.
89    pub fn set_direction(&self, direction: TransformDirection) {
90        self.try_set_direction(direction)
91            .expect("failed to set color space transform direction");
92    }
93
94    /// Set evaluation direction and surface any OCIO validation error.
95    pub fn try_set_direction(&self, direction: TransformDirection) -> Result<()> {
96        crate::clear_last_error();
97        unsafe {
98            ocio_sys::ocio_color_space_transform_set_direction(
99                self.handle.as_ptr(),
100                direction as i32,
101            );
102        }
103        crate::ocio_call_status()
104    }
105
106    /// Create an editable copy that is independent from the original transform.
107    pub fn create_editable_copy(&self) -> Result<Self> {
108        crate::clear_last_error();
109        let handle = unsafe {
110            ocio_sys::ocio_color_space_transform_create_editable_copy(self.handle.as_ptr())
111        };
112        crate::handle_result(handle).map(|handle| Self { handle })
113    }
114
115    /// Return format metadata attached to the transform, when available.
116    pub fn format_metadata(&self) -> Option<crate::FormatMetadata> {
117        let handle = unsafe { ocio_sys::ocio_transform_get_format_metadata(self.handle.as_ptr()) };
118        NonNull::new(handle).map(|h| crate::FormatMetadata { handle: h })
119    }
120
121    /// Validate the transform configuration and return any errors.
122    pub fn validate(&self) -> Result<()> {
123        crate::clear_last_error();
124        unsafe { ocio_sys::ocio_color_space_transform_validate(self.handle.as_ptr()) };
125        crate::validation_status()
126    }
127}
128
129impl Drop for ColorSpaceTransform {
130    fn drop(&mut self) {
131        unsafe { ocio_sys::ocio_color_space_transform_destroy(self.handle.as_ptr()) };
132    }
133}
134
135#[cfg(test)]
136mod tests {
137    use super::*;
138
139    #[test]
140    fn create_color_space_transform() {
141        let t = ColorSpaceTransform::create();
142        assert!(t.is_ok());
143    }
144
145    #[test]
146    fn color_space_transform_methods_no_crash() {
147        let t = ColorSpaceTransform::create().unwrap();
148        let _ = t.src();
149        let _ = t.dst();
150        let _ = t.data_bypass();
151        let _ = t.direction();
152    }
153
154    #[test]
155    fn set_src_dst_no_crash() {
156        let t = ColorSpaceTransform::create().unwrap();
157        assert!(t.set_src("ACEScg").is_ok());
158        assert!(t.set_dst("Output").is_ok());
159    }
160
161    #[test]
162    fn set_data_bypass_no_crash() {
163        let t = ColorSpaceTransform::create().unwrap();
164        t.try_set_data_bypass(true).unwrap();
165    }
166
167    #[test]
168    fn direction_no_crash() {
169        let t = ColorSpaceTransform::create().unwrap();
170        let _ = t.direction();
171        t.try_set_direction(TransformDirection::Inverse).unwrap();
172    }
173
174    #[test]
175    fn create_editable_copy_no_crash() {
176        let t = ColorSpaceTransform::create().unwrap();
177        let _ = t.create_editable_copy();
178    }
179
180    #[test]
181    fn format_metadata_no_crash() {
182        let t = ColorSpaceTransform::create().unwrap();
183        let _ = t.format_metadata();
184    }
185
186    #[test]
187    fn validate_no_crash() {
188        let t = ColorSpaceTransform::create().unwrap();
189        let _ = t.validate();
190    }
191}