add set() and is_allocated() Mat methods
MODIFY
gen_rust.py
gen_rust.py
@@ -145,10 +145,10 @@ func_rename = { # todo check if any "new" is required
"cv_Mat_colRange_const_Range": "colrange",
"cv_Mat_colRange_const_int_int": "colbounds",
"cv_Mat_copyTo_const_Mat_Mat": "copy_to_masked",
- "cv_Mat_create_int_int_int": "create_rows_cols",
- "cv_Mat_create_Size_int": "create_size",
+ "cv_Mat_create_int_int_int": "+_rows_cols",
+ "cv_Mat_create_Size_int": "+_size",
+ "cv_Mat_create_VectorOfint_int": "+_nd",
"cv_Mat_create_int_const_int_X_int": "-", # duplicate of cv_Mat_create_VectorOfint_int, but with pointers
- "cv_Mat_create_VectorOfint_int": "create_nd",
"cv_Mat_diag_Mat": "diag_new_mat",
"cv_Mat_ptr_int": "ptr_mut",
"cv_Mat_ptr_int_int": "ptr_2d_mut",
@@ -419,7 +419,12 @@ func_unsafe_list = {
"cv_Mat_Mat_int_int_int",
"cv_Mat_Mat_Size_int",
"cv_Mat_Mat_VectorOfint_int",
+ "cv_Mat_create_int_int_int",
+ "cv_Mat_create_Size_int",
+ "cv_Mat_create_VectorOfint_int",
+ # allows passing arbitrary data
"cv_Mat_set_data_uchar_X",
+ # no bounds checking
"cv_Mat_ptr_int",
"cv_Mat_ptr_const_int",
"cv_Mat_ptr_int_int",

MODIFY
src/hub/manual/core/mat.rs
src/hub/manual/core/mat.rs
-use std::{
- ffi::c_void,
- fmt,
- ops::Deref,
- slice,
-};
+use std::{fmt, ops::Deref, slice};
use libc::size_t;
use crate::{
Error,
- hub::core::{self, Mat, MatSize, MatStep},
+ hub::core::{self, Mat, MatSize, MatStep, Scalar},
Result,
sys,
};
@@ -229,6 +224,27 @@ impl Mat {
}).into_result()
}
+ pub fn is_allocated(&self) -> bool {
+ let me = self.as_raw_Mat();
+ cpp!(unsafe [me as "const cv::Mat*"] -> bool as "bool" {
+ return me->data != NULL;
+ })
+ }
+
+ /// Sets all or some of the array elements to the specified value.
+ ///
+ /// ## Parameters
+ /// * s: Assigned scalar converted to the actual array type.
+ pub fn set(&mut self, s: Scalar) -> Result<()> {
+ let me = self.as_raw_Mat();
+ cpp!(unsafe [me as "cv::Mat*", s as "cv::Scalar"] -> sys::cv_return_value_void as "cv_return_value_void" {
+ try {
+ *me = s;
+ return { Error::Code::StsOk, NULL };
+ } CVRS_CATCH(cv_return_value_void)
+ }).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) })

MODIFY
tests/mat.rs
tests/mat.rs
@@ -12,6 +12,35 @@ use opencv::{
const PIXEL: &[u8] = include_bytes!("pixel.png");
+#[test]
+fn mat_default() -> Result<()> {
+ let mat = Mat::new()?;
+ assert_eq!(u8::typ(), mat.typ()?);
+ assert_eq!(u8::depth(), mat.depth()?);
+ assert_eq!(u8::channels(), mat.channels()?);
+ assert_eq!(Size::new(0, 0), mat.size()?);
+ assert_eq!(0, mat.dims()?);
+ assert!(!mat.is_allocated());
+ assert_matches!(mat.data(), Err(Error { code: core::StsNullPtr, ..}));
+ Ok(())
+}
+
+#[test]
+fn mat_create() -> Result<()> {
+ let mut mat = Mat::new()?;
+ unsafe { mat.create_rows_cols(10, 10, u16::typ())? };
+ assert!(mat.is_allocated());
+ mat.set(Scalar::all(7.))?;
+ assert_eq!(7, *mat.at_2d::<u16>(0, 0)?);
+ assert_eq!(7, *mat.at_2d::<u16>(3, 3)?);
+ assert_eq!(7, *mat.at_2d::<u16>(9, 9)?);
+ mat.release()?;
+ assert!(!mat.is_allocated());
+ assert_eq!(Size::new(0, 0), mat.size()?);
+ assert_eq!(2, mat.dims()?);
+ Ok(())
+}
+
#[test]
fn mat_for_rows_and_cols() -> Result<()> {
let mat = unsafe { Mat::new_rows_cols(400, 300, Vec3d::typ()) }?;

add set() and is_allocated() Mat methods