Skip to main content

ocio_rs/transform/
log.rs

1use std::ffi::c_void;
2use std::ptr::NonNull;
3
4use crate::{OcioError, Result, TransformDirection};
5use ocio_sys;
6
7/// Logarithmic OCIO transform with configurable base and direction.
8pub struct LogTransform {
9    pub(crate) handle: NonNull<c_void>,
10}
11
12impl LogTransform {
13    /// Create a new log transform.
14    pub fn create() -> Result<Self> {
15        crate::clear_last_error();
16        let handle = unsafe { ocio_sys::ocio_log_transform_create() };
17        crate::handle_result(handle).map(|handle| Self { handle })
18    }
19
20    /// Return the logarithm base.
21    pub fn base(&self) -> f64 {
22        unsafe { ocio_sys::ocio_log_transform_get_base(self.handle.as_ptr()) }
23    }
24
25    /// Set the logarithm base.
26    pub fn set_base(&self, base: f64) {
27        self.try_set_base(base)
28            .expect("failed to set logarithm base");
29    }
30
31    /// Set the logarithm base and surface any OCIO validation error.
32    pub fn try_set_base(&self, base: f64) -> Result<()> {
33        crate::clear_last_error();
34        unsafe { ocio_sys::ocio_log_transform_set_base(self.handle.as_ptr(), base) };
35        crate::ocio_call_status()
36    }
37
38    /// Return the transform direction.
39    pub fn direction(&self) -> TransformDirection {
40        let dir = unsafe { ocio_sys::ocio_log_transform_get_direction(self.handle.as_ptr()) };
41        match dir {
42            1 => TransformDirection::Inverse,
43            _ => TransformDirection::Forward,
44        }
45    }
46
47    /// Set the transform direction.
48    pub fn set_direction(&self, direction: TransformDirection) {
49        self.try_set_direction(direction)
50            .expect("failed to set log transform direction");
51    }
52
53    /// Set the transform direction and surface any OCIO validation error.
54    pub fn try_set_direction(&self, direction: TransformDirection) -> crate::Result<()> {
55        crate::clear_last_error();
56        unsafe {
57            ocio_sys::ocio_log_transform_set_direction(self.handle.as_ptr(), direction as i32);
58        }
59        crate::ocio_call_status()
60    }
61
62    /// Create an independent copy of this transform.
63    pub fn create_editable_copy(&self) -> Result<Self> {
64        crate::clear_last_error();
65        let handle = unsafe { ocio_sys::ocio_transform_create_editable_copy(self.handle.as_ptr()) };
66        crate::handle_result(handle).map(|handle| Self { handle })
67    }
68
69    /// Return format metadata attached to the transform, when available.
70    pub fn format_metadata(&self) -> Option<crate::FormatMetadata> {
71        let handle = unsafe { ocio_sys::ocio_transform_get_format_metadata(self.handle.as_ptr()) };
72        NonNull::new(handle).map(|h| crate::FormatMetadata { handle: h })
73    }
74
75    #[deprecated(since = "0.2.0", note = "compat alias; prefer format_metadata()")]
76    pub fn format_metadata_v1(&self) -> Option<crate::FormatMetadata> {
77        self.format_metadata()
78    }
79
80    #[deprecated(since = "0.2.0", note = "compat alias; prefer format_metadata()")]
81    pub fn format_metadata_v2(&self) -> Option<crate::FormatMetadata> {
82        self.format_metadata()
83    }
84
85    /// Return whether this transform is equivalent to `other`.
86    pub fn equals(&self, other: &Self) -> bool {
87        unsafe { ocio_sys::ocio_log_transform_equals(self.handle.as_ptr(), other.handle.as_ptr()) }
88    }
89}
90
91impl Drop for LogTransform {
92    fn drop(&mut self) {
93        unsafe { ocio_sys::ocio_log_transform_destroy(self.handle.as_ptr()) };
94    }
95}
96
97#[cfg(test)]
98mod tests {
99    use super::*;
100
101    #[test]
102    fn create_log() {
103        let lt = LogTransform::create();
104        assert!(lt.is_ok());
105    }
106
107    #[test]
108    fn base_no_crash() {
109        let lt = LogTransform::create().unwrap();
110        let _ = lt.base();
111        lt.try_set_base(10.0).unwrap();
112    }
113
114    #[test]
115    fn create_editable_copy_no_crash() {
116        let lt = LogTransform::create().unwrap();
117        let _ = lt.create_editable_copy();
118    }
119
120    #[test]
121    fn format_metadata_no_crash() {
122        let lt = LogTransform::create().unwrap();
123        let _ = lt.format_metadata();
124    }
125
126    #[test]
127    fn equals_no_crash() {
128        let a = LogTransform::create().unwrap();
129        let b = LogTransform::create().unwrap();
130        let _ = a.equals(&b);
131    }
132}