add useful Mat::mat_size() and Mat::mat_step()
MODIFY
gen_rust.py
gen_rust.py
@@ -97,9 +97,6 @@ decls_manual_pre = {
# key: module name
# value: list of declarations as supplied by hdr_parser
decls_manual_post = {
- "core": [
- ("cv.Mat.size", "Size", ["/C"], []),
- ],
"dnn": [
("cv.dnn.LayerParams.LayerParams", "", [], []),
],
@@ -171,9 +168,10 @@ func_rename = { # todo check if any "new" is required
"cv_Mat_reshape_const_int_int_const_int_X": "-", # duplicate of cv_Mat_reshape_const_int_VectorOfint, but with pointers
"cv_Mat_reshape_const_int_VectorOfint": "reshape_nd",
"cv_Mat_total_const_int_int": "total_slice",
- "cv_Mat_size": "-", # doesn't have any use, dims() and Size are already accessible through other methods
- "cv_Mat_set_size_MatSize": "-", # -"-
- "cv_Mat_set_step_MatStep": "-", # doesn't allow writing
+ "cv_Mat_size_const": "mat_size",
+ "cv_Mat_step_const": "mat_step",
+ "cv_Mat_set_size_MatSize": "-", # doesn't allow writing
+ "cv_Mat_set_step_MatStep": "-", # same as above
"cv_Mat_push_back__const_void_X": "-", # internal method
"cv_merge_const_Mat_size_t_Mat": "-", # duplicate of cv_merge_VectorOfMat_Mat, but with pointers
"cv_Matx_TOp_Matx_TOp_Matx_TOp": "copy",
@@ -3033,7 +3031,7 @@ class RustWrapperGenerator(object):
attrs = ["/ATTRGETTER"]
prop_type = self.get_type_info(prop.ctype)
is_const = prop_type.is_const or prop_type.is_copy
- if is_const:
+ if is_const or ci.fullname == "cv::Mat" and (prop.name == "size" or prop.name == "step"):
attrs.append("/C")
read_func = FuncInfo(
self,

MODIFY
src/hub/manual/core/mat.rs
src/hub/manual/core/mat.rs
-use std::{fmt, slice};
+use std::{
+ ffi::c_void,
+ fmt,
+ ops::Deref,
+ slice,
+};
+
+use libc::size_t;
use crate::{
Error,
- hub::core::{self, Mat},
+ hub::core::{self, Mat, MatSize, MatStep},
Result,
sys,
};
@@ -212,6 +219,16 @@ impl Mat {
self.at_mut_unchecked(row).map(|x| slice::from_raw_parts_mut(x, width))
}
+ pub fn size(&self) -> Result<core::Size> {
+ let me = self.as_raw_Mat();
+ cpp!(unsafe [me as "const cv::Mat*"] -> sys::cv_return_value_SizeWrapper as "cv_return_value_SizeWrapper" {
+ try {
+ cv::Size ret = me->size();
+ return { Error::Code::StsOk, NULL, *reinterpret_cast<SizeWrapper*>(&ret) };
+ } CVRS_CATCH(cv_return_value_SizeWrapper)
+ }).into_result()
+ }
+
pub fn data_typed<T: DataType>(&self) -> Result<&[T]> {
let total = self.total()?;
self._at(0).map(|x| unsafe { slice::from_raw_parts(x, total) })
@@ -307,6 +324,43 @@ impl Default for Mat {
}
}
+impl Deref for MatSize {
+ type Target = [i32];
+
+ fn deref(&self) -> &Self::Target {
+ let me = self.as_raw_MatSize();
+ let ptr = cpp!(unsafe [me as "const MatSize*"] -> &i32 as "const int*" {
+ return me->p;
+ });
+ unsafe { slice::from_raw_parts(ptr, self.dims().expect("Cannot get dims") as usize) }
+ }
+}
+
+impl fmt::Debug for MatSize {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ writeln!(f, "{:#?}", self.deref())
+ }
+}
+
+impl Deref for MatStep {
+ type Target = [size_t];
+
+ fn deref(&self) -> &Self::Target {
+ let me = self.as_raw_MatStep();
+ let ptr = cpp!(unsafe [me as "const MatStep*"] -> &size_t as "const size_t*" {
+ return me->p;
+ });
+ unsafe { slice::from_raw_parts(ptr, 2) }
+ }
+}
+
+impl fmt::Debug for MatStep {
+ fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
+ writeln!(f, "{:#?}", self.deref())
+ }
+}
+
+
mod private {
pub trait Sealed {}
}

MODIFY
tests/mat.rs
tests/mat.rs
@@ -14,19 +14,26 @@ const PIXEL: &[u8] = include_bytes!("pixel.png");
#[test]
fn mat_for_rows_and_cols() -> Result<()> {
- let typ = Vec3d::typ();
- let mat = unsafe { Mat::new_rows_cols(400, 300, typ) }?;
- assert_eq!(mat.typ()?, typ);
+ let mat = unsafe { Mat::new_rows_cols(400, 300, Vec3d::typ()) }?;
+ assert_eq!(Vec3d::typ(), mat.typ()?);
+ assert_eq!(Vec3d::depth(), mat.depth()?);
+ assert_eq!(Vec3d::channels(), mat.channels()?);
assert!(mat.is_continuous()?);
assert!(!mat.is_submatrix()?);
- assert_eq!(mat.size()?, Size::new(300, 400));
- assert_eq!(mat.rows()?, 400);
- assert_eq!(mat.cols()?, 300);
- assert_eq!(Vec3d::depth(), mat.depth()?);
+ assert_eq!(Size::new(300, 400), mat.size()?);
+ assert_eq!(400, mat.rows()?);
+ assert_eq!(300, mat.cols()?);
+ assert_eq!(2, mat.mat_size()?.len());
+ assert_eq!(400, mat.mat_size()?[0]);
+ assert_eq!(300, mat.mat_size()?[1]);
assert_eq!(2, mat.dims()?);
- assert_eq!(3, mat.channels()?);
+ assert_eq!(2, mat.mat_step()?.len());
+ assert_eq!(7200, mat.mat_step()?[0]);
+ assert_eq!(24, mat.mat_step()?[1]);
assert_eq!(24, mat.elem_size()?);
assert_eq!(8, mat.elem_size1()?);
+ assert_eq!(900, mat.step1(0)?);
+ assert_eq!(3, mat.step1(1)?);
assert_eq!(120000, mat.total()?);
Ok(())
}

add useful Mat::mat_size() and Mat::mat_step()