1use crate::{OcioError, Result};
3use std::ffi::CStr;
4
5fn native_to_string(kind: i32, value: i32) -> Result<String> {
6 crate::clear_last_error();
7 let ptr = unsafe { ocio_sys::ocio_enum_to_string(kind, value) };
8 let result = unsafe { crate::cstr_to_opt_string(ptr) };
9 crate::ocio_call_status()?;
10 result.ok_or_else(|| OcioError::Ocio("null native enum string".into()))
11}
12fn native_write_string(kind: i32, value: i32, output: &mut [u8]) -> Result<usize> {
13 crate::clear_last_error();
14 let ptr = unsafe { ocio_sys::ocio_enum_to_string(kind, value) };
15 crate::ocio_call_status()?;
16 if ptr.is_null() {
17 return Err(OcioError::Ocio("null native enum string".into()));
18 }
19 let bytes = unsafe { CStr::from_ptr(ptr) }.to_bytes();
20 if output.len() < bytes.len() {
21 return Err(OcioError::InvalidInput(format!(
22 "enum string buffer requires {} bytes",
23 bytes.len()
24 )));
25 }
26 output[..bytes.len()].copy_from_slice(bytes);
27 Ok(bytes.len())
28}
29fn native_from_string(kind: i32, value: &CStr) -> Result<i32> {
30 crate::clear_last_error();
31 let result = unsafe { ocio_sys::ocio_enum_from_string(kind, value.as_ptr()) };
32 crate::ocio_call_status()?;
33 Ok(result)
34}
35
36pub trait OcioEnumText: Sized + Copy {
41 fn to_ocio_string(self) -> Result<String>;
43 fn write_ocio_string(self, output: &mut [u8]) -> Result<usize>;
45 fn from_ocio_string(value: &CStr) -> Result<Self>;
47}
48
49impl OcioEnumText for bool {
50 fn to_ocio_string(self) -> Result<String> {
51 native_to_string(0, i32::from(self))
52 }
53 fn from_ocio_string(value: &CStr) -> Result<Self> {
54 Ok(native_from_string(0, value)? != 0)
55 }
56 fn write_ocio_string(self, output: &mut [u8]) -> Result<usize> {
57 native_write_string(0, i32::from(self), output)
58 }
59}
60
61macro_rules! native_enum {
62 ($ty:ident, $kind:expr, [$($(#[$attr:meta])* $variant:ident),+ $(,)?]) => {
63 impl OcioEnumText for crate::$ty {
64 fn to_ocio_string(self) -> Result<String> {
65 let raw = self as i32;
66 let mut valid = false;
67 $( $(#[$attr])* { valid |= raw == crate::$ty::$variant as i32; } )+
68 if !valid { return Err(OcioError::InvalidInput("value is not supported by OpenColorIO".into())); }
69 native_to_string($kind, raw)
70 }
71 fn write_ocio_string(self, output: &mut [u8]) -> Result<usize> {
72 let raw = self as i32;
73 let mut valid = false;
74 $( $(#[$attr])* { valid |= raw == crate::$ty::$variant as i32; } )+
75 if !valid { return Err(OcioError::InvalidInput("value is not supported by OpenColorIO".into())); }
76 native_write_string($kind, raw, output)
77 }
78 fn from_ocio_string(value: &CStr) -> Result<Self> {
79 let raw = native_from_string($kind, value)?;
80 $( $(#[$attr])* if raw == crate::$ty::$variant as i32 { return Ok(crate::$ty::$variant); } )+
81 Err(OcioError::Ocio(format!("native {} value {raw} is not exposed by the selected interface", stringify!($ty))))
82 }
83 }
84 };
85}
86
87native_enum!(LoggingLevel, 1, [None, Warning, Info, Debug, Unknown]);
88native_enum!(TransformDirection, 2, [Forward, Inverse]);
89native_enum!(
90 BitDepth,
91 3,
92 [Unknown, Uint8, Uint10, Uint12, Uint14, Uint16, Uint32, F16, F32]
93);
94native_enum!(Allocation, 4, [Unknown, Uniform, Lg2]);
95native_enum!(
96 Interpolation,
97 5,
98 [Unknown, Nearest, Linear, Tetrahedral, Cubic, Default, Best]
99);
100native_enum!(
101 GpuLanguage,
102 6,
103 [
104 Cg,
105 Glsl1_2,
106 Glsl1_3,
107 Glsl4_0,
108 #[cfg(feature = "v2_5")]
109 GlslVk4_6,
110 HlslSm5_0,
111 Osl1,
112 GlslEs1_0,
113 GlslEs3_0,
114 Msl2_0
115 ]
116);
117native_enum!(EnvironmentMode, 7, [Unknown, LoadPredefined, LoadAll]);
118native_enum!(CDLStyle, 8, [Asc, NoClamp]);
119native_enum!(RangeStyle, 9, [NoClamp, Clamp]);
120native_enum!(
121 FixedFunctionStyle,
122 10,
123 [
124 AcesRedMod03,
125 AcesRedMod10,
126 AcesGlow03,
127 AcesGlow10,
128 AcesDarkToDim10,
129 Rec2100Surround,
130 RgbToHsv,
131 XyzToxyY,
132 XyzTouvY,
133 XyzToLuv,
134 AcesGamutMap02,
135 AcesGamutMap07,
136 AcesGamutCompress13,
137 LinToPq,
138 LinToGammaLog,
139 LinToDoubleLog,
140 AcesOutputTransform20,
141 AcesRgbToJmh20,
142 AcesTonescaleCompress20,
143 AcesGamutCompress20,
144 #[cfg(feature = "v2_5")]
145 RgbToHsyLin,
146 #[cfg(feature = "v2_5")]
147 RgbToHsyLog,
148 #[cfg(feature = "v2_5")]
149 RgbToHsyVid,
150 #[cfg(feature = "v2_6")]
151 AcesRgbToHmj20
152 ]
153);
154native_enum!(GradingStyle, 11, [Log, Lin, Video]);
155native_enum!(ExposureContrastStyle, 12, [Linear, Video, Logarithmic]);
156native_enum!(NegativeStyle, 13, [Clamp, Mirror, PassThru, Linear]);
157
158impl crate::BitDepth {
159 pub fn ocio_is_float(self) -> Result<bool> {
161 Ok(self.ocio_utility(false)? != 0)
162 }
163 pub fn ocio_bit_count(self) -> Result<i32> {
165 self.ocio_utility(true)
166 }
167 fn ocio_utility(self, as_integer: bool) -> Result<i32> {
168 crate::clear_last_error();
169 let value = unsafe { ocio_sys::ocio_bit_depth_utility(self as i32, as_integer) };
170 crate::ocio_call_status()?;
171 Ok(value)
172 }
173}
174impl crate::TransformDirection {
175 pub fn ocio_inverse(self) -> Result<Self> {
177 self.ocio_direction(self, false)
178 }
179 pub fn ocio_combine(self, other: Self) -> Result<Self> {
181 self.ocio_direction(other, true)
182 }
183 fn ocio_direction(self, other: Self, combine: bool) -> Result<Self> {
184 crate::clear_last_error();
185 let value = unsafe { ocio_sys::ocio_direction_utility(self as i32, other as i32, combine) };
186 crate::ocio_call_status()?;
187 match value {
188 0 => Ok(Self::Forward),
189 1 => Ok(Self::Inverse),
190 _ => Err(OcioError::Ocio("unknown native transform direction".into())),
191 }
192 }
193}