add comments for ToInput|OutputArray and implement them for references
MODIFY
build.rs
build.rs
@@ -264,6 +264,7 @@ fn build_wrapper(opencv_header_dir: &PathBuf) -> Result<(), Box<dyn Error>> {
{
let mut types = File::create(out_dir.join("common_opencv.h"))?;
writeln!(&mut types, "#define CERES_FOUND true")?; // for sfm module
+ writeln!(&mut types, "#define HAVE_OPENCV_OCL true")?; // for 3.2 opencl support
for m in modules {
writeln!(&mut types, "#include <opencv2/{}.hpp>", m.0)?;
if m.0 == "dnn" {

MODIFY
common.h
common.h
@@ -23,5 +23,3 @@ catch (cv::Exception& e) { \
}
#define VEC_CATCH(return_type) CODE_CATCH(return_type, std::out_of_range, Error::Code::StsOutOfRange, "index out of bounds")
-
-#define HAVE_OPENCV_OCL true // # for 3.2

MODIFY
gen_rust.py
gen_rust.py
@@ -2669,6 +2669,7 @@ class VectorTypeInfo(TypeInfo):
"input_output_array": template("""
impl core::ToInputArray for ${rust_local} {
+ #[inline]
fn input_array(&self) -> Result<core::InputArray> {
let me = self.as_raw_${rust_local}();
cpp!(unsafe [me as "${cpptype}*"] -> sys::cv_return_value_const_void_X as "cv_return_value_void_X" {
@@ -2680,7 +2681,15 @@ class VectorTypeInfo(TypeInfo):
}
}
+ impl core::ToInputArray for &${rust_local} {
+ #[inline]
+ fn input_array(&self) -> Result<core::InputArray> {
+ (*self).input_array()
+ }
+ }
+
impl core::ToOutputArray for ${rust_local} {
+ #[inline]
fn output_array(&mut self) -> Result<core::OutputArray> {
let me = self.as_raw_${rust_local}();
cpp!(unsafe [me as "${cpptype}*"] -> sys::cv_return_value_const_void_X as "cv_return_value_void_X" {
@@ -2692,7 +2701,15 @@ class VectorTypeInfo(TypeInfo):
}
}
+ impl core::ToOutputArray for &mut ${rust_local} {
+ #[inline]
+ fn output_array(&mut self) -> Result<core::OutputArray> {
+ (*self).output_array()
+ }
+ }
+
impl core::ToInputOutputArray for ${rust_local} {
+ #[inline]
fn input_output_array(&mut self) -> Result<core::InputOutputArray> {
let me = self.as_raw_${rust_local}();
cpp!(unsafe [me as "${cpptype}*"] -> sys::cv_return_value_const_void_X as "cv_return_value_void_X" {
@@ -2703,6 +2720,13 @@ class VectorTypeInfo(TypeInfo):
.map(|ptr| core::InputOutputArray { ptr })
}
}
+
+ impl core::ToInputOutputArray for &mut ${rust_local} {
+ #[inline]
+ fn input_output_array(&mut self) -> Result<core::InputOutputArray> {
+ (*self).input_output_array()
+ }
+ }
""")
}

MODIFY
src/manual/core/input_output_array.rs
src/manual/core/input_output_array.rs
@@ -93,6 +93,12 @@ impl Drop for InputArray {
}
}
+/// Trait to serve as a replacement for `InputArray` in C++ OpenCV
+///
+/// You can pass references to the types implementing this trait everywhere where OpenCV API expects
+/// `InputArray` or `InputArrayOfArrays`.
+///
+/// More info in [OpenCV docs](https://docs.opencv.org/master/d4/d32/classcv_1_1__InputArray.html#details).
pub trait ToInputArray {
fn input_array(&self) -> Result<InputArray>;
}
@@ -104,6 +110,13 @@ impl ToInputArray for f64 {
}
}
+impl ToInputArray for &f64 {
+ #[inline]
+ fn input_array(&self) -> Result<InputArray> {
+ (*self).input_array()
+ }
+}
+
pub struct OutputArray {
#[doc(hidden)] pub(crate) ptr: *mut c_void
}
@@ -175,12 +188,24 @@ impl Drop for OutputArray {
}
}
+/// Trait to serve as a replacement for `OutputArray` in C++ OpenCV
+///
+/// You can pass reference to the type implementing this trait everywhere where OpenCV API expects
+/// `OutputArray` or `OutputArrayOfArrays`.
+///
+/// More info in [OpenCV docs](https://docs.opencv.org/master/d2/d9e/classcv_1_1__OutputArray.html#details).
pub trait ToOutputArray {
fn output_array(&mut self) -> Result<OutputArray>;
}
pub type InputOutputArray = _InputOutputArray;
+/// Trait to serve as a replacement for `InputOutputArray` in C++ OpenCV
+///
+/// You can pass reference to the type implementing this trait everywhere where OpenCV API expects
+/// `InputOutputArray` or `InputOutputArrayOfArrays`.
+///
+/// For more info see comments for `ToInputArray` and `ToOutputArray`.
pub trait ToInputOutputArray {
fn input_output_array(&mut self) -> Result<InputOutputArray>;
}

MODIFY
src/manual/core/mat.rs
src/manual/core/mat.rs
@@ -447,6 +447,13 @@ impl ToInputArray for Mat {
}
}
+impl ToInputArray for &Mat {
+ #[inline]
+ fn input_array(&self) -> Result<InputArray> {
+ (*self).input_array()
+ }
+}
+
impl ToOutputArray for Mat {
#[inline]
fn output_array(&mut self) -> Result<OutputArray> {
@@ -454,6 +461,13 @@ impl ToOutputArray for Mat {
}
}
+impl ToOutputArray for &mut Mat {
+ #[inline]
+ fn output_array(&mut self) -> Result<OutputArray> {
+ (*self).output_array()
+ }
+}
+
impl ToInputOutputArray for Mat {
#[inline]
fn input_output_array(&mut self) -> Result<InputOutputArray> {
@@ -461,6 +475,13 @@ impl ToInputOutputArray for Mat {
}
}
+impl ToInputOutputArray for &mut Mat {
+ #[inline]
+ fn input_output_array(&mut self) -> Result<InputOutputArray> {
+ (*self).input_output_array()
+ }
+}
+
impl fmt::Debug for Mat {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
let typ = self.typ();
@@ -507,6 +528,13 @@ impl ToInputArray for UMat {
}
}
+impl ToInputArray for &UMat {
+ #[inline]
+ fn input_array(&self) -> Result<InputArray> {
+ (*self).input_array()
+ }
+}
+
impl ToOutputArray for UMat {
#[inline]
fn output_array(&mut self) -> Result<OutputArray> {
@@ -514,6 +542,13 @@ impl ToOutputArray for UMat {
}
}
+impl ToOutputArray for &mut UMat {
+ #[inline]
+ fn output_array(&mut self) -> Result<OutputArray> {
+ (*self).output_array()
+ }
+}
+
impl ToInputOutputArray for UMat {
#[inline]
fn input_output_array(&mut self) -> Result<InputOutputArray> {
@@ -521,6 +556,13 @@ impl ToInputOutputArray for UMat {
}
}
+impl ToInputOutputArray for &mut UMat {
+ #[inline]
+ fn input_output_array(&mut self) -> Result<InputOutputArray> {
+ (*self).input_output_array()
+ }
+}
+
#[cfg(feature = "opencv-32")]
impl MatSize {
#[inline]
@@ -617,6 +659,13 @@ impl ToInputArray for MatExpr {
}
}
+impl ToInputArray for &MatExpr {
+ #[inline]
+ fn input_array(&self) -> Result<InputArray> {
+ (*self).input_array()
+ }
+}
+
mod private {
pub trait Sealed {}
}

MODIFY
tests/input_output_array.rs
tests/input_output_array.rs
@@ -4,7 +4,6 @@ use opencv::{
Result,
types::{VectorOfdouble, VectorOfuchar},
};
-
#[cfg(not(feature = "opencv-41"))]
use opencv::core::ACCESS_READ;
#[cfg(feature = "opencv-41")]
@@ -15,6 +14,7 @@ fn input_output_array() -> Result<()> {
{
let mat_expr = Mat::ones(1, 3, u8::typ())?;
let umat = Mat::new_rows_cols_with_default(1, 3, u8::typ(), Scalar::all(3.))?.get_umat(ACCESS_READ, UMatUsageFlags::USAGE_DEFAULT)?;
+ {
let mut trg = VectorOfuchar::new();
core::add(&mat_expr, &umat, &mut trg, &Mat::default()?, -1)?;
assert_eq!(3, trg.len());
@@ -23,6 +23,16 @@ fn input_output_array() -> Result<()> {
assert_eq!(4, trg.get(2)?);
}
+ {
+ let mut trg = VectorOfuchar::new();
+ core::add(&&mat_expr, &&umat, &mut &mut trg, &Mat::default()?, -1)?;
+ assert_eq!(3, trg.len());
+ assert_eq!(4, trg.get(0)?);
+ assert_eq!(4, trg.get(1)?);
+ assert_eq!(4, trg.get(2)?);
+ }
+ }
+
{
let mut t = VectorOfdouble::new();
core::add(&2.5, &4., &mut t, &Mat::default()?, -1)?;

add comments for ToInput|OutputArray and implement them for references