opencv/manual/core/
data_type.rs

1use crate::core;
2use crate::core::{Point3_, Point_, Rect_, Size_, VecN, CV_16S, CV_16U, CV_32F, CV_32S, CV_64F, CV_8S, CV_8U};
3
4#[inline]
5pub const fn CV_MAT_DEPTH(flags: i32) -> i32 {
6	#![allow(non_snake_case)]
7	flags & core::CV_MAT_DEPTH_MASK
8}
9
10#[inline]
11pub const fn CV_MAKETYPE(depth: i32, cn: i32) -> i32 {
12	#![allow(non_snake_case)]
13	CV_MAT_DEPTH(depth) + ((cn - 1) << core::CV_CN_SHIFT)
14}
15
16pub use CV_MAKETYPE as CV_MAKE_TYPE;
17
18#[cfg(ocvrs_opencv_branch_5)]
19#[inline]
20pub const fn CV_IS_INT_TYPE(flags: i32) -> bool {
21	#![allow(non_snake_case)]
22	((1 << CV_MAT_DEPTH(flags)) & 0x1e1f) != 0
23}
24
25#[cfg(ocvrs_opencv_branch_5)]
26#[inline]
27pub const fn CV_IS_FLOAT_TYPE(flags: i32) -> bool {
28	#![allow(non_snake_case)]
29	((1 << CV_MAT_DEPTH(flags)) & 0x1e0) != 0
30}
31
32/// Implement this trait for types that are valid to use as Mat elements.
33///
34/// # Safety
35/// Types implementing this trait must adhere to the memory layout declared by the values returned
36/// by [Self::opencv_depth()] and [Self::opencv_channels()] functions. In most cases that means that the type
37/// must also be `#[repr(C)]`.
38pub unsafe trait DataType: Copy {
39	/// The shape of bytes occupied by the single layer/channel of the element. E.g., for an 8-bit BGR
40	/// image it's [CV_8U] because a single channel for a pixel is unsigned 8 bits. You should use one
41	/// of the depth constants for this like [CV_8U], [CV_8S], [CV_32F], etc.
42	fn opencv_depth() -> i32;
43
44	/// Number of layers/channels per element. E.g., for an 8-bit BGR image it's 3 because one pixel
45	/// consists of 3 channels: B, G and R.
46	fn opencv_channels() -> i32;
47
48	/// OpenCV value for this type as produced by [CV_MAKETYPE()] function
49	#[inline]
50	fn opencv_type() -> i32 {
51		CV_MAKETYPE(Self::opencv_depth(), Self::opencv_channels())
52	}
53}
54
55macro_rules! data_type {
56	($rust_type: ty, $mat_depth: expr, $channels: expr) => {
57		unsafe impl $crate::core::DataType for $rust_type {
58			#[inline]
59			fn opencv_depth() -> i32 {
60				$mat_depth
61			}
62
63			#[inline]
64			fn opencv_channels() -> i32 {
65				$channels
66			}
67		}
68	};
69}
70
71macro_rules! mchan_fun {
72	($name: ident, $depth: expr) => {
73		#[inline]
74		pub const fn $name(n: i32) -> i32 {
75			#![allow(non_snake_case)]
76			$crate::core::CV_MAKETYPE($depth, n)
77		}
78	};
79}
80
81// int
82data_type!(u8, CV_8U, 1);
83data_type!(i8, CV_8S, 1);
84data_type!(u16, CV_16U, 1);
85data_type!(i16, CV_16S, 1);
86data_type!(i32, CV_32S, 1);
87
88mchan_fun!(CV_8UC, CV_8U);
89mchan_fun!(CV_8SC, CV_8S);
90mchan_fun!(CV_16UC, CV_16U);
91mchan_fun!(CV_16SC, CV_16S);
92mchan_fun!(CV_32SC, CV_32S);
93
94#[cfg(ocvrs_opencv_branch_5)]
95mod opencv5 {
96	use crate::core;
97
98	data_type!(u32, core::CV_32U, 1);
99	data_type!(u64, core::CV_64U, 1);
100	data_type!(i64, core::CV_64S, 1);
101	data_type!(bool, core::CV_Bool, 1);
102	data_type!(core::bfloat, core::CV_16BF, 1);
103
104	mchan_fun!(CV_32UC, core::CV_32U);
105	mchan_fun!(CV_64SC, core::CV_64S);
106	mchan_fun!(CV_64UC, core::CV_64U);
107	mchan_fun!(CV_BoolC, core::CV_Bool);
108	mchan_fun!(CV_16BFC, core::CV_16BF);
109}
110#[cfg(ocvrs_opencv_branch_5)]
111pub use opencv5::*;
112
113// float
114data_type!(f32, CV_32F, 1);
115data_type!(f64, CV_64F, 1);
116
117mchan_fun!(CV_32FC, CV_32F);
118mchan_fun!(CV_64FC, CV_64F);
119
120#[cfg(not(ocvrs_opencv_branch_34))]
121mod half_builtin {
122	use crate::core;
123
124	#[cfg(ocvrs_has_inherent_feature_hfloat)]
125	data_type!(core::hfloat, core::CV_16F, 1);
126	mchan_fun!(CV_16FC, core::CV_16F);
127}
128#[cfg(not(ocvrs_opencv_branch_34))]
129pub use half_builtin::*;
130
131#[cfg(all(feature = "f16", not(ocvrs_opencv_branch_34)))]
132mod half {
133	use half::f16;
134
135	use crate::core;
136
137	data_type!(f16, core::CV_16F, 1);
138
139	#[cfg(ocvrs_opencv_branch_5)]
140	mod opencv5 {
141		use half::bf16;
142
143		use crate::core;
144
145		data_type!(bf16, core::CV_16BF, 1);
146	}
147}
148
149#[cfg(feature = "rgb")]
150mod rgb {
151	use crate::core;
152
153	data_type!(rgb::RGB8, core::CV_8U, 3);
154	data_type!(rgb::RGBA8, core::CV_8U, 4);
155	data_type!(rgb::alt::Gray<u8>, core::CV_8U, 1);
156	data_type!(rgb::alt::GrayAlpha<u8>, core::CV_8U, 2);
157	data_type!(rgb::alt::BGR8, core::CV_8U, 3);
158	data_type!(rgb::alt::ARGB8, core::CV_8U, 4);
159	data_type!(rgb::alt::BGRA8, core::CV_8U, 4);
160	data_type!(rgb::alt::ABGR8, core::CV_8U, 4);
161}
162
163unsafe impl<T: DataType, const N: usize> DataType for VecN<T, N> {
164	#[inline]
165	fn opencv_depth() -> i32 {
166		T::opencv_depth()
167	}
168
169	#[inline]
170	fn opencv_channels() -> i32 {
171		i32::try_from(N).expect("Number of channels is out of range for i32")
172	}
173}
174
175unsafe impl<T: DataType> DataType for Point_<T> {
176	#[inline]
177	fn opencv_depth() -> i32 {
178		T::opencv_depth()
179	}
180
181	#[inline]
182	fn opencv_channels() -> i32 {
183		2
184	}
185}
186
187unsafe impl<T: DataType> DataType for Point3_<T> {
188	#[inline]
189	fn opencv_depth() -> i32 {
190		T::opencv_depth()
191	}
192
193	#[inline]
194	fn opencv_channels() -> i32 {
195		3
196	}
197}
198
199unsafe impl<T: DataType> DataType for Size_<T> {
200	#[inline]
201	fn opencv_depth() -> i32 {
202		T::opencv_depth()
203	}
204
205	#[inline]
206	fn opencv_channels() -> i32 {
207		2
208	}
209}
210
211unsafe impl<T: DataType> DataType for Rect_<T> {
212	#[inline]
213	fn opencv_depth() -> i32 {
214		T::opencv_depth()
215	}
216
217	#[inline]
218	fn opencv_channels() -> i32 {
219		4
220	}
221}