enable mutable output parameters
MODIFY
gen_rust.py
gen_rust.py
@@ -297,7 +297,7 @@ renamed_funcs = { # todo check if any "new" is required
### imgcodecs ###
"cv_imdecode_Mat_buf_int_flags": "decode",
- "cv_imdecode_Mat_buf_int_flags_Mat_dst": "decode_to", # fixme, make sure dst is &mut
+ "cv_imdecode_Mat_buf_int_flags_Mat_dst": "decode_to",
### imgproc ###
"cv_Canny_Mat_dx_Mat_dy_Mat_edges_double_threshold1_double_threshold2_bool_L2gradient": "canny_derivative",
@@ -2146,8 +2146,6 @@ class RawPtrTypeInfo(TypeInfo):
self.rust_local = self.inner.rust_local
self.rust_full = self.inner.rust_full
self.rust_extern = self.inner.rust_extern
- if self.is_const:
- pass
else:
# self.rust_lifetimes = "'b"
self.rust_safe_id = self.inner.rust_safe_id + "_X"
@@ -2183,7 +2181,7 @@ class RawPtrTypeInfo(TypeInfo):
def rust_arg_func_decl(self, var_name, is_output=False):
if self.is_string():
return "{}: &{}str".format(var_name, "mut " if is_output else "")
- return super(RawPtrTypeInfo, self).rust_arg_func_decl(var_name, is_output)
+ return super(RawPtrTypeInfo, self).rust_arg_func_decl(var_name, is_output or not self.is_const)
def rust_arg_pre_call(self, var_name, is_output=False):
if self.is_string():

ADD
pixel.png
pixel.png
Binary files /dev/null and b/pixel.png differ

ADD
tests/imgcodecs.rs
+use std::mem::transmute;
+
+use opencv::{
+ core::{self, Mat},
+ core::{Size, Vec3b},
+ imgcodecs,
+};
+
+const PIXEL: &[u8] = include_bytes!("../pixel.png");
+
+#[test]
+fn decode() {
+ {
+ 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 dest = imgcodecs::decode(&src, imgcodecs::IMREAD_COLOR).unwrap();
+ assert_eq!(dest.size().unwrap(), Size::new(1, 1));
+ assert_eq!(dest.channels().unwrap(), 3);
+ assert_eq!(*dest.at_2d::<Vec3b>(0, 0).unwrap(), Vec3b::from([56u8, 56, 191]));
+ }
+
+ {
+ 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 mut dest = Mat::default();
+ imgcodecs::decode_to(&src, imgcodecs::IMREAD_COLOR, &mut dest).unwrap();
+ assert_eq!(dest.size().unwrap(), Size::new(1, 1));
+ assert_eq!(dest.channels().unwrap(), 3);
+ assert_eq!(*dest.at_2d::<Vec3b>(0, 0).unwrap(), Vec3b::from([56u8, 56, 191]));
+ }
+}

MODIFY
tests/mat.rs
tests/mat.rs
-use opencv::core::{self, Mat, Scalar, Vec2b};
+use std::mem::transmute;
+
+use opencv::core::{self, Mat, Scalar, Size, Vec2b};
use opencv::types::VectorOfMat;
+const PIXEL: &[u8] = include_bytes!("../pixel.png");
+
#[test]
fn mat_for_rows_and_cols() {
let typ = core::CV_8UC3;
let mat = unsafe { Mat::new_rows_cols(400, 300, typ) }.unwrap();
- let size = mat.size().unwrap();
assert_eq!(mat.typ().unwrap(), typ);
- assert_eq!(
- core::Size {
- width: 300,
- height: 400
- },
- size
- );
+ assert_eq!(mat.size().unwrap(), Size::new(300, 400));
assert_eq!(core::CV_8U, mat.depth().unwrap());
assert_eq!(3, mat.channels().unwrap());
}
@@ -73,3 +70,16 @@ fn mat_operations() {
assert_eq!(dst.at_2d::<Vec2b>(0, 2).unwrap()[1], 2);
}
}
+
+
+#[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();
+ 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();
+ assert_eq!(row[0], 0x89);
+ assert_eq!(row[11], 0x0D);
+ assert_eq!(row[89], 0x82);
+}

enable mutable output parameters