rename ValidMatElement to DataType to be closer to upstream, seal trait and require Copy, impl for more types
MODIFY
gen_rust.py
gen_rust.py
@@ -370,11 +370,11 @@ renamed_funcs = { # todo check if any "new" is required
}
_templ_const = template("""
-${doc_comment}${visibility}fn ${r_name}<T: core::ValidMatElement>(${args}) -> Result<&T> { ${pre_call_args}self._${r_name}(${forward_args}) }
+${doc_comment}${visibility}fn ${r_name}<T: core::DataType>(${args}) -> Result<&T> { ${pre_call_args}self._${r_name}(${forward_args}) }
""")
_templ_mut = template("""
-${doc_comment}${visibility}fn ${r_name}<T: core::ValidMatElement>(${args}) -> Result<&mut T> { ${pre_call_args}self._${r_name}(${forward_args}) }
+${doc_comment}${visibility}fn ${r_name}<T: core::DataType>(${args}) -> Result<&mut T> { ${pre_call_args}self._${r_name}(${forward_args}) }
""")

MODIFY
src/hub/manual/core/mat.rs
src/hub/manual/core/mat.rs
@@ -7,71 +7,72 @@ use crate::{
sys,
};
-// todo, make sealed
-pub trait ValidMatElement {
+/// This sealed trait is implemented for types that are valid to use as Mat elements
+pub trait DataType: Copy + private::Sealed {
+ fn depth() -> i32;
+ fn channels() -> i32;
fn typ() -> i32;
}
-macro_rules! valid_mat_element {
- ($rust_type: ty, $mat_type: expr) => {
- impl ValidMatElement for $rust_type {
- fn typ() -> i32 { $mat_type }
+macro_rules! data_type {
+ ($rust_type: ty, $mat_depth: expr, $channels: expr) => {
+ impl DataType for $rust_type {
+ #[inline(always)]
+ fn depth() -> i32 { $mat_depth }
+
+ #[inline(always)]
+ fn channels() -> i32 { $channels }
+
+ #[inline(always)]
+ fn typ() -> i32 { $crate::hub::core::MAKETYPE($mat_depth, $channels) }
}
+
+ impl private::Sealed for $rust_type {}
};
}
// int
-valid_mat_element!(u8, core::CV_8U);
-valid_mat_element!(i8, core::CV_8S);
-valid_mat_element!(u16, core::CV_16U);
-valid_mat_element!(i16, core::CV_16S);
-valid_mat_element!(i32, core::CV_32S);
+data_type!(u8, core::CV_8U, 1);
+data_type!(i8, core::CV_8S, 1);
+data_type!(u16, core::CV_16U, 1);
+data_type!(i16, core::CV_16S, 1);
+data_type!(i32, core::CV_32S, 1);
// float
-valid_mat_element!(f32, core::CV_32F);
-valid_mat_element!(f64, core::CV_64F);
+data_type!(f32, core::CV_32F, 1);
+data_type!(f64, core::CV_64F, 1);
// vec int
-valid_mat_element!(core::Vec2b, core::CV_8UC2);
-valid_mat_element!(core::Vec3b, core::CV_8UC3);
-valid_mat_element!(core::Vec4b, core::CV_8UC4);
-valid_mat_element!(core::Vec2s, core::CV_16SC2);
-valid_mat_element!(core::Vec3s, core::CV_16SC3);
-valid_mat_element!(core::Vec4s, core::CV_16SC4);
-valid_mat_element!(core::Vec2i, core::CV_32SC2);
-valid_mat_element!(core::Vec3i, core::CV_32SC3);
-valid_mat_element!(core::Vec4i, core::CV_32SC4);
+data_type!(core::Vec2b, core::CV_8U, 2);
+data_type!(core::Vec3b, core::CV_8U, 3);
+data_type!(core::Vec4b, core::CV_8U, 4);
+data_type!(core::Vec2<i8>, core::CV_8S, 2);
+data_type!(core::Vec3<i8>, core::CV_8S, 3);
+data_type!(core::Vec4<i8>, core::CV_8S, 4);
+data_type!(core::Vec2<u16>, core::CV_16U, 2);
+data_type!(core::Vec3<u16>, core::CV_16U, 3);
+data_type!(core::Vec4<u16>, core::CV_16U, 4);
+data_type!(core::Vec2s, core::CV_16S, 2);
+data_type!(core::Vec3s, core::CV_16S, 3);
+data_type!(core::Vec4s, core::CV_16S, 4);
+data_type!(core::Vec2i, core::CV_32S, 2);
+data_type!(core::Vec3i, core::CV_32S, 3);
+data_type!(core::Vec4i, core::CV_32S, 4);
// vec float
-valid_mat_element!(core::Vec2f, core::CV_32FC2);
-valid_mat_element!(core::Vec3f, core::CV_32FC3);
-valid_mat_element!(core::Vec4f, core::CV_32FC4);
-valid_mat_element!(core::Vec2d, core::CV_64FC2);
-valid_mat_element!(core::Vec3d, core::CV_64FC3);
-valid_mat_element!(core::Vec4d, core::CV_64FC4);
+data_type!(core::Vec2f, core::CV_32F, 2);
+data_type!(core::Vec3f, core::CV_32F, 3);
+data_type!(core::Vec4f, core::CV_32F, 4);
+data_type!(core::Vec2d, core::CV_64F, 2);
+data_type!(core::Vec3d, core::CV_64F, 3);
+data_type!(core::Vec4d, core::CV_64F, 4);
// scalar
-valid_mat_element!(core::Scalar, core::CV_64FC4);
-
-
-//struct Mat_<T> {
-// mat: Mat,
-// _d: PhantomData<T>,
-//}
-
-//impl<T: MatElement> Mat_<T> {
-// pub fn try_from_mat(mat: Mat) -> Result<Self> {
-// if mat.typ()? == T::typ() {
-// Ok(Self { mat, _d: PhantomData })
-// } else {
-// Err(Error { code: -1, message: "".into() }) // fixme
-// }
-// }
-//}
+data_type!(core::Scalar, core::CV_64F, 4);
impl Mat {
#[inline(always)]
- fn match_format<T: ValidMatElement>(&self) -> Result<()> {
+ fn match_format<T: DataType>(&self) -> Result<()> {
let mat_type = self.typ()?;
let out_type = T::typ();
if mat_type == out_type {
@@ -82,61 +83,61 @@ impl Mat {
}
#[inline(always)]
- pub(crate) fn _at<T: ValidMatElement>(&self, i0: i32) -> Result<&T> {
+ pub(crate) fn _at<T: DataType>(&self, i0: i32) -> Result<&T> {
self.match_format::<T>().and_then(|_| self.ptr(i0).map(|x| unsafe { &*(x as *const _ as *const T) }))
}
#[inline(always)]
- pub(crate) fn _at_mut<T: ValidMatElement>(&mut self, i0: i32) -> Result<&mut T> {
+ pub(crate) fn _at_mut<T: DataType>(&mut self, i0: i32) -> Result<&mut T> {
self.match_format::<T>().and_then(|_| self.ptr_mut(i0).map(|x| unsafe { &mut *(x as *mut _ as *mut T) }))
}
#[inline(always)]
- pub(crate) fn _at_2d<T: ValidMatElement>(&self, row: i32, col: i32) -> Result<&T> {
+ pub(crate) fn _at_2d<T: DataType>(&self, row: i32, col: i32) -> Result<&T> {
self.match_format::<T>().and_then(|_| self.ptr_2d(row, col).map(|x| unsafe { &*(x as *const _ as *const T) }))
}
#[inline(always)]
- pub(crate) fn _at_2d_mut<T: ValidMatElement>(&mut self, row: i32, col: i32) -> Result<&mut T> {
+ pub(crate) fn _at_2d_mut<T: DataType>(&mut self, row: i32, col: i32) -> Result<&mut T> {
self.match_format::<T>().and_then(|_| self.ptr_2d_mut(row, col).map(|x| unsafe { &mut *(x as *mut _ as *mut T) }))
}
#[inline(always)]
- pub(crate) fn _at_3d<T: ValidMatElement>(&self, i0: i32, i1: i32, i2: i32) -> Result<&T> {
+ pub(crate) fn _at_3d<T: DataType>(&self, i0: i32, i1: i32, i2: i32) -> Result<&T> {
self.match_format::<T>().and_then(|_| self.ptr_3d(i0, i1, i2).map(|x| unsafe { &*(x as *const _ as *const T) }))
}
#[inline(always)]
- pub(crate) fn _at_3d_mut<T: ValidMatElement>(&mut self, i0: i32, i1: i32, i2: i32) -> Result<&mut T> {
+ pub(crate) fn _at_3d_mut<T: DataType>(&mut self, i0: i32, i1: i32, i2: i32) -> Result<&mut T> {
self.match_format::<T>().and_then(|_| self.ptr_3d_mut(i0, i1, i2).map(|x| unsafe { &mut *(x as *mut _ as *mut T) }))
}
- pub fn at_row<T: ValidMatElement>(&self, i0: i32) -> Result<&[T]> {
+ pub fn at_row<T: DataType>(&self, i0: i32) -> Result<&[T]> {
let width = self.size()?.width as usize;
self._at(i0).map(|x| unsafe { slice::from_raw_parts(x, width) })
}
- pub fn at_row_mut<T: ValidMatElement>(&mut self, i0: i32) -> Result<&mut [T]> {
+ pub fn at_row_mut<T: DataType>(&mut self, i0: i32) -> Result<&mut [T]> {
let width = self.size()?.width as usize;
self._at_mut(i0).map(|x| unsafe { slice::from_raw_parts_mut(x, width) })
}
- pub fn data<T: ValidMatElement>(&self) -> Result<&[T]> {
+ pub fn data<T: DataType>(&self) -> Result<&[T]> {
let total = self.total()?;
self._at(0).map(|x| unsafe { slice::from_raw_parts(x, total) })
}
- pub fn data_mut<T: ValidMatElement>(&mut self) -> Result<&mut [T]> {
+ pub fn data_mut<T: DataType>(&mut self) -> Result<&mut [T]> {
let total = self.total()?;
self._at_mut(0).map(|x| unsafe { slice::from_raw_parts_mut(x, total) })
}
#[inline]
- pub fn from_slice<T: ValidMatElement + Copy>(s: &[T]) -> Result<Mat> {
+ pub fn from_slice<T: DataType>(s: &[T]) -> Result<Mat> {
Self::from_slice_2d(&[s])
}
- pub fn from_slice_2d<T: ValidMatElement + Copy>(s: &[impl AsRef<[T]>]) -> Result<Mat> {
+ pub fn from_slice_2d<T: DataType>(s: &[impl AsRef<[T]>]) -> Result<Mat> {
let row_count: i32 = s.len() as _;
let col_count: i32 = if row_count > 0 {
s[0].as_ref().len() as _
@@ -155,7 +156,7 @@ impl Mat {
Ok(out)
}
- pub fn to_vec_2d<T: ValidMatElement + Copy>(&self) -> Result<Vec<Vec<T>>> {
+ pub fn to_vec_2d<T: DataType>(&self) -> Result<Vec<Vec<T>>> {
self.match_format::<T>().and_then(|_| {
let size = self.size()?;
let width = size.width as usize;
@@ -196,7 +197,6 @@ impl Default for Mat {
}
}
-
mod private {
pub trait Sealed {}
}

MODIFY
tests/mat.rs
tests/mat.rs
use std::mem::transmute;
-use opencv::core::{self, Mat, Scalar, Size, Vec2b};
+use opencv::core::{self, Mat, Scalar, Size, Vec2b, DataType};
use opencv::types::VectorOfMat;
const PIXEL: &[u8] = include_bytes!("../pixel.png");
@@ -17,7 +17,7 @@ fn mat_for_rows_and_cols() {
#[test]
fn mat_at() {
- let mut mat = Mat::new_rows_cols_with_default(100, 100, core::CV_32F, Scalar::all(0.)).unwrap();
+ let mut mat = Mat::new_rows_cols_with_default(100, 100, f32::typ(), Scalar::all(0.)).unwrap();
assert_eq!(*mat.at_2d::<f32>(0, 0).unwrap(), 0.);
*mat.at_2d_mut::<f32>(0, 0).unwrap() = 1.;
assert_eq!(*mat.at_2d::<f32>(0, 0).unwrap(), 1.);
@@ -61,11 +61,11 @@ fn mat_vec() {
fn mat_operations() {
{
let mut src = VectorOfMat::new();
- src.push(Mat::new_rows_cols_with_default(1, 3, core::CV_8U, Scalar::all(1.)).unwrap());
- src.push(Mat::new_rows_cols_with_default(1, 3, core::CV_8U, Scalar::all(2.)).unwrap());
+ src.push(Mat::new_rows_cols_with_default(1, 3, u8::typ(), Scalar::all(1.)).unwrap());
+ src.push(Mat::new_rows_cols_with_default(1, 3, u8::typ(), Scalar::all(2.)).unwrap());
let mut dst = Mat::default();
core::merge(&src, &mut dst).unwrap();
- assert_eq!(dst.typ().unwrap(), core::CV_8UC2);
+ assert_eq!(dst.typ().unwrap(), Vec2b::typ());
assert_eq!(dst.at_2d::<Vec2b>(0, 1).unwrap()[0], 1);
assert_eq!(dst.at_2d::<Vec2b>(0, 2).unwrap()[1], 2);
}
@@ -75,7 +75,7 @@ fn mat_operations() {
#[test]
fn mat_from_data() {
let mut bytes = PIXEL.to_vec();
- let src = Mat::new_rows_cols_with_data(1, PIXEL.len() as _, core::CV_8U, unsafe { transmute(bytes.as_mut_ptr()) }, core::Mat_AUTO_STEP).unwrap();
+ let src = Mat::new_rows_cols_with_data(1, PIXEL.len() as _, u8::typ(), unsafe { transmute(bytes.as_mut_ptr()) }, core::Mat_AUTO_STEP).unwrap();
assert_eq!(src.size().unwrap(), Size::new(PIXEL.len() as _, 1));
assert_eq!(src.total().unwrap(), PIXEL.len());
let row = src.at_row::<u8>(0).unwrap();

MODIFY
tests/types.rs
tests/types.rs
use opencv::{
- core,
- core::Mat,
+ core::{self, DataType, Mat, Point, Scalar, Size},
imgproc,
- types
+ types,
};
+use opencv::core::Vec3b;
#[test]
fn simple_struct() {
- let res = imgproc::get_structuring_element(imgproc::MORPH_CROSS, core::Size { width: 100, height: 100 }, core::Point { x: 50, y: 50 }).unwrap();
+ let res = imgproc::get_structuring_element(imgproc::MORPH_CROSS, Size { width: 100, height: 100 }, Point { x: 50, y: 50 }).unwrap();
assert_eq!(res.typ().unwrap(), 0);
let size = res.size().unwrap();
assert_eq!(size.width, 100);
@@ -16,16 +16,16 @@ fn simple_struct() {
#[test]
fn vec() {
- let mut m = Mat::new_rows_cols_with_default(10, 10, core::CV_8UC3, core::Scalar::default()).unwrap();
+ let mut m = Mat::new_rows_cols_with_default(10, 10, Vec3b::typ(), Scalar::default()).unwrap();
let mut ps = types::VectorOfMat::new();
assert_eq!(ps.len(), 0);
- let mut p1 = unsafe { Mat::new_rows_cols(3, 2, core::CV_32S) }.unwrap();
+ let mut p1 = unsafe { Mat::new_rows_cols(3, 2, i32::typ()) }.unwrap();
p1.at_row_mut::<i32>(0).unwrap().copy_from_slice(&[0, 0]);
p1.at_row_mut::<i32>(1).unwrap().copy_from_slice(&[0, 9]);
p1.at_row_mut::<i32>(2).unwrap().copy_from_slice(&[9, 9]);
ps.push(p1);
assert_eq!(ps.len(), 1);
- imgproc::fill_poly(&mut m, &ps, core::Scalar::new(127., 127., 127., 0.), core::LINE_8, 0, core::Point::default()).unwrap();
+ imgproc::fill_poly(&mut m, &ps, Scalar::new(127., 127., 127., 0.), core::LINE_8, 0, Point::default()).unwrap();
assert_eq!(*m.at_2d::<core::Vec3b>(0, 0).unwrap(), core::Vec3b::from([127, 127, 127]));
assert_eq!(*m.at_2d::<core::Vec3b>(0, 9).unwrap(), core::Vec3b::default());
assert_eq!(*m.at_2d::<core::Vec3b>(9, 9).unwrap(), core::Vec3b::from([127, 127, 127]));
@@ -33,7 +33,7 @@ fn vec() {
#[test]
fn scalar() {
- let mut m = Mat::new_rows_cols_with_default(1, 3, core::CV_8U, core::Scalar::new(2., 0., 0., 0.)).unwrap();
+ let mut m = Mat::new_rows_cols_with_default(1, 3, u8::typ(), Scalar::new(2., 0., 0., 0.)).unwrap();
let sum = core::sum(&m).unwrap();
assert_eq!(sum[0], 6.);
let s = m.at_row_mut::<u8>(0).unwrap();

rename ValidMatElement to DataType to be closer to upstream, seal trait and require Copy, impl for more types