Skip to main content

ocio_rs/transform/
allocation.rs

1use std::ffi::c_void;
2use std::ptr::NonNull;
3
4use crate::{Allocation, OcioError, Result, TransformDirection};
5use ocio_sys;
6
7/// Maps values into or out of an OCIO allocation domain.
8pub struct AllocationTransform {
9    pub(crate) handle: NonNull<c_void>,
10}
11
12impl AllocationTransform {
13    pub fn create() -> Result<Self> {
14        crate::clear_last_error();
15        let handle = unsafe { ocio_sys::ocio_allocation_transform_create() };
16        crate::handle_result(handle).map(|handle| Self { handle })
17    }
18
19    pub fn try_allocation(&self) -> Result<Allocation> {
20        crate::clear_last_error();
21        let v = unsafe { ocio_sys::ocio_allocation_transform_get_allocation(self.handle.as_ptr()) };
22        crate::ocio_call_status()?;
23        Ok(match v {
24            1 => Allocation::Uniform,
25            2 => Allocation::Lg2,
26            _ => Allocation::Unknown,
27        })
28    }
29
30    pub fn allocation(&self) -> Allocation {
31        self.try_allocation().unwrap_or(Allocation::Unknown)
32    }
33
34    pub fn set_allocation(&self, allocation: Allocation) {
35        self.try_set_allocation(allocation)
36            .expect("failed to set allocation mode");
37    }
38
39    /// Set the allocation mode and surface any OCIO validation error.
40    pub fn try_set_allocation(&self, allocation: Allocation) -> Result<()> {
41        crate::clear_last_error();
42        unsafe {
43            ocio_sys::ocio_allocation_transform_set_allocation(
44                self.handle.as_ptr(),
45                allocation as i32,
46            );
47        }
48        crate::ocio_call_status()
49    }
50
51    pub fn try_num_vars(&self) -> Result<i32> {
52        crate::clear_last_error();
53        let v = unsafe { ocio_sys::ocio_allocation_transform_get_num_vars(self.handle.as_ptr()) };
54        crate::ocio_call_status()?;
55        Ok(v)
56    }
57
58    pub fn num_vars(&self) -> i32 {
59        self.try_num_vars().unwrap_or(0)
60    }
61
62    pub fn try_vars(&self) -> Result<Vec<f32>> {
63        let n = super::checked_native_count(self.try_num_vars()?, "AllocationTransform::vars")?;
64        if n == 0 {
65            return Ok(vec![]);
66        }
67        let mut v = super::reserve_native_items(n, "AllocationTransform::vars")?;
68        v.resize(n, 0.0f32);
69        self.read_vars(&mut v)?;
70        Ok(v)
71    }
72
73    /// Copy variables into an exactly sized buffer without allocating.
74    /// A length mismatch leaves the buffer untouched.
75    pub fn try_vars_into(&self, output: &mut [f32]) -> Result<()> {
76        let count = super::checked_native_count(
77            self.try_num_vars()?,
78            "AllocationTransform::try_vars_into",
79        )?;
80        if output.len() != count {
81            return Err(OcioError::InvalidInput(
82                "allocation variable buffer length mismatch".into(),
83            ));
84        }
85        if count == 0 {
86            return Ok(());
87        }
88        self.read_vars(output)
89    }
90
91    fn read_vars(&self, output: &mut [f32]) -> Result<()> {
92        crate::clear_last_error();
93        unsafe {
94            ocio_sys::ocio_allocation_transform_get_vars(
95                self.handle.as_ptr(),
96                output.as_mut_ptr() as *mut c_void,
97            );
98        }
99        crate::ocio_call_status()
100    }
101
102    pub fn vars(&self) -> Vec<f32> {
103        self.try_vars().unwrap_or_default()
104    }
105
106    /// Set allocation-domain parameters.
107    pub fn set_vars(&self, vars: &[f32]) -> Result<()> {
108        let num_vars = i32::try_from(vars.len()).map_err(|_| {
109            OcioError::InvalidInput(
110                "AllocationTransform::set_vars: too many allocation values".to_owned(),
111            )
112        })?;
113        crate::clear_last_error();
114        unsafe {
115            ocio_sys::ocio_allocation_transform_set_vars(
116                self.handle.as_ptr(),
117                num_vars,
118                vars.as_ptr() as *mut c_void,
119            );
120        }
121        crate::ocio_call_status()
122    }
123
124    pub fn try_direction(&self) -> Result<TransformDirection> {
125        crate::clear_last_error();
126        let dir =
127            unsafe { ocio_sys::ocio_allocation_transform_get_direction(self.handle.as_ptr()) };
128        crate::ocio_call_status()?;
129        super::direction_from_raw(dir)
130    }
131
132    pub fn direction(&self) -> TransformDirection {
133        self.try_direction().unwrap_or(TransformDirection::Forward)
134    }
135
136    pub fn create_editable_copy(&self) -> Result<Self> {
137        crate::clear_last_error();
138        let handle = unsafe {
139            ocio_sys::ocio_allocation_transform_create_editable_copy(self.handle.as_ptr())
140        };
141        crate::handle_result(handle).map(|handle| Self { handle })
142    }
143
144    pub fn format_metadata(&self) -> Option<crate::FormatMetadata> {
145        let handle = unsafe { ocio_sys::ocio_transform_get_format_metadata(self.handle.as_ptr()) };
146        NonNull::new(handle).map(|h| crate::FormatMetadata { handle: h })
147    }
148
149    pub fn set_direction(&self, direction: TransformDirection) {
150        self.try_set_direction(direction)
151            .expect("failed to set allocation transform direction");
152    }
153
154    /// Set the transform direction and surface any OCIO validation error.
155    pub fn try_set_direction(&self, direction: TransformDirection) -> Result<()> {
156        crate::clear_last_error();
157        unsafe {
158            ocio_sys::ocio_allocation_transform_set_direction(
159                self.handle.as_ptr(),
160                direction as i32,
161            );
162        }
163        crate::ocio_call_status()
164    }
165
166    pub fn validate(&self) -> Result<()> {
167        crate::clear_last_error();
168        unsafe { ocio_sys::ocio_allocation_transform_validate(self.handle.as_ptr()) };
169        crate::validation_status()
170    }
171}
172
173impl Drop for AllocationTransform {
174    fn drop(&mut self) {
175        unsafe { ocio_sys::ocio_allocation_transform_destroy(self.handle.as_ptr()) };
176    }
177}
178
179#[cfg(test)]
180mod tests {
181    use super::*;
182
183    #[test]
184    fn create_allocation_transform() {
185        let t = AllocationTransform::create();
186        assert!(t.is_ok());
187    }
188
189    #[test]
190    fn allocation_transform_methods_no_crash() {
191        let t = AllocationTransform::create().unwrap();
192        let _ = t.allocation();
193        let _ = t.num_vars();
194        let _ = t.vars();
195        let _ = t.direction();
196    }
197
198    #[test]
199    fn set_allocation_no_crash() {
200        let t = AllocationTransform::create().unwrap();
201        t.try_set_allocation(Allocation::Uniform).unwrap();
202        t.try_set_allocation(Allocation::Lg2).unwrap();
203    }
204
205    #[test]
206    fn set_vars_no_crash() {
207        let t = AllocationTransform::create().unwrap();
208        let vars: [f32; 3] = [1.0, 2.0, 3.0];
209        t.set_vars(&vars).unwrap();
210    }
211
212    #[test]
213    fn direction_no_crash() {
214        let t = AllocationTransform::create().unwrap();
215        let _ = t.direction();
216        t.try_set_direction(TransformDirection::Inverse).unwrap();
217    }
218
219    #[test]
220    fn create_editable_copy_no_crash() {
221        let t = AllocationTransform::create().unwrap();
222        let _ = t.create_editable_copy();
223    }
224
225    #[test]
226    fn format_metadata_no_crash() {
227        let t = AllocationTransform::create().unwrap();
228        let _ = t.format_metadata();
229    }
230
231    #[test]
232    fn validate_no_crash() {
233        let t = AllocationTransform::create().unwrap();
234        let _ = t.validate();
235    }
236}