Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
twistedfall committed Apr 22, 2024
1 parent b9565e7 commit 84f76c2
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 77 deletions.
3 changes: 2 additions & 1 deletion CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
semantics.
* Some constructors have received `_mut` suffix where appropriate.
* Unsafe `Mat` constructors (`*_with_data`) have received `_unsafe` suffix and safe versions have been introduced that return
`BoxedRef`/`BoxedRefMut`
`BoxedRef`/`BoxedRefMut`. Consequently, `Mat::from_slice_rows_cols` was replaced by `Mat::new_rows_cols_with_data`, note the
different order and types of the arguments.
* `MatSize::new` is now unsafe and accepts a pointer.

* 0.90.0
Expand Down
3 changes: 2 additions & 1 deletion binding-generator/src/element.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ impl DefaultElement {
| EntityKind::ClassTemplate
| EntityKind::ClassTemplatePartialSpecialization
| EntityKind::FunctionTemplate
| EntityKind::Method => {
| EntityKind::Method
| EntityKind::FunctionDecl => {
// handle anonymous enums inside classes and anonymous namespaces
if let Some(parent_name) = parent.get_name() {
parts.push(parent_name);
Expand Down
2 changes: 1 addition & 1 deletion binding-generator/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ impl Element for Func<'_, '_> {
};
is_unavailable
|| settings::FUNC_EXCLUDE.contains(identifier.as_str())
|| (self.is_generic())
|| self.is_generic()
|| self.arguments().iter().any(|a| a.type_ref().exclude_kind().is_ignored())
|| kind.as_operator().map_or(false, |(_, kind)| match kind {
OperatorKind::Unsupported => true,
Expand Down
2 changes: 2 additions & 0 deletions binding-generator/src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@ pub static PRIMITIVE_TYPEDEFS: Lazy<HashMap<&str, (&str, &str)>> = Lazy::new(||

pub static STATIC_MODULES: Lazy<BTreeSet<&str>> = Lazy::new(|| BTreeSet::from(["core", "sys", "types"]));

/// Types that can be used as `Mat` element
/// cpp_name(Reference)
pub static DATA_TYPES: Lazy<HashSet<&str>> = Lazy::new(|| {
HashSet::from([
"unsigned char",
Expand Down
1 change: 0 additions & 1 deletion ci/msrv.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@ rustc --version
rustc --print=cfg

cargo update
rm -vf examples/cuda.rs # no CUDA support in CI
cargo check -vv --all-targets --all-features --workspace --tests
2 changes: 0 additions & 2 deletions ci/script.sh
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,6 @@ if [[ "${OPENCV_VERSION:-}" != "4.9.0" ]]; then
rm -vf tests/*_only_latest_opencv.rs
rm -vf examples/dnn_face_detect.rs examples/gapi_api_example.rs examples/text_detection.rs
fi
# the following examples don't work in CI
rm -vf examples/cuda.rs

echo "=== Current directory: $(pwd)"
echo "=== Environment variable dump:"
Expand Down
35 changes: 16 additions & 19 deletions examples/cuda.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
use std::{env, time};
use time::Instant;

use opencv::core::{GpuMat, Size};
use opencv::core::Size;
use opencv::prelude::*;
use opencv::{core, cudafilters, cudaimgproc, imgcodecs, imgproc, Result};
use opencv::{core, imgcodecs, imgproc, Result};

const ITERATIONS: usize = 100;

Expand All @@ -25,40 +26,36 @@ fn main() -> Result<()> {
}
);
println!("Timing CPU implementation... ");
let img = imgcodecs::imread(&img_file, imgcodecs::IMREAD_COLOR)?;
let start = time::Instant::now();
let img = imgcodecs::imread_def(&img_file)?;
let start = Instant::now();
for _ in 0..ITERATIONS {
let mut gray = Mat::default();
imgproc::cvt_color(&img, &mut gray, imgproc::COLOR_BGR2GRAY, 0)?;
imgproc::cvt_color_def(&img, &mut gray, imgproc::COLOR_BGR2GRAY)?;
let mut blurred = Mat::default();
imgproc::gaussian_blur(&gray, &mut blurred, Size::new(7, 7), 1.5, 0., core::BORDER_DEFAULT)?;
imgproc::gaussian_blur_def(&gray, &mut blurred, Size::new(7, 7), 1.5)?;
let mut edges = Mat::default();
imgproc::canny(&blurred, &mut edges, 0., 50., 3, false)?;
imgproc::canny_def(&blurred, &mut edges, 0., 50.)?;
}
println!("{:#?}", start.elapsed());
#[cfg(all(ocvrs_has_module_cudafilters, ocvrs_has_module_cudaimgproc))]
if cuda_available {
use opencv::core::GpuMat;
use opencv::{cudafilters, cudaimgproc};

println!("Timing CUDA implementation... ");
let img = imgcodecs::imread(&img_file, imgcodecs::IMREAD_COLOR)?;
let img = imgcodecs::imread_def(&img_file)?;
let mut img_gpu = GpuMat::new_def()?;
img_gpu.upload(&img)?;
let mut stream = core::Stream::default()?;
let start = time::Instant::now();
let start = Instant::now();
for _ in 0..ITERATIONS {
let mut gray = GpuMat::new_def()?;
cudaimgproc::cvt_color(&img_gpu, &mut gray, imgproc::COLOR_BGR2GRAY, 0, &mut stream)?;
let mut blurred = GpuMat::new_def()?;
let mut filter = cudafilters::create_gaussian_filter(
gray.typ()?,
blurred.typ()?,
Size::new(7, 7),
1.5,
0.,
core::BORDER_DEFAULT,
core::BORDER_DEFAULT,
)?;
let mut filter = cudafilters::create_gaussian_filter_def(gray.typ()?, blurred.typ()?, Size::new(7, 7), 1.5)?;
filter.apply(&gray, &mut blurred, &mut stream)?;
let mut edges = GpuMat::new_def()?;
let mut detector = cudaimgproc::create_canny_edge_detector(0., 50., 3, false)?;
let mut detector = cudaimgproc::create_canny_edge_detector_def(0., 50.)?;
detector.detect(&blurred, &mut edges, &mut stream)?;
stream.wait_for_completion()?;
}
Expand Down
40 changes: 8 additions & 32 deletions src/manual/core/input_output_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -194,80 +194,56 @@ impl<const N: usize> ToInputArray for [u8; N] {
}
}

impl<T> ToInputArray for BoxedRef<'_, T>
where
T: Boxed + ToInputArray,
{
impl<T: Boxed + ToInputArray> ToInputArray for BoxedRef<'_, T> {
#[inline]
fn input_array(&self) -> Result<BoxedRef<_InputArray>> {
self.reference.input_array()
}
}

impl<T> ToInputArray for &BoxedRef<'_, T>
where
T: Boxed + ToInputArray,
{
impl<T: Boxed + ToInputArray> ToInputArray for &BoxedRef<'_, T> {
#[inline]
fn input_array(&self) -> Result<BoxedRef<_InputArray>> {
(*self).input_array()
}
}

impl<T> ToInputArray for BoxedRefMut<'_, T>
where
T: Boxed + ToInputArray,
{
impl<T: Boxed + ToInputArray> ToInputArray for BoxedRefMut<'_, T> {
#[inline]
fn input_array(&self) -> Result<BoxedRef<_InputArray>> {
self.reference.input_array()
}
}

impl<T> ToInputArray for &BoxedRefMut<'_, T>
where
T: Boxed + ToInputArray,
{
impl<T: Boxed + ToInputArray> ToInputArray for &BoxedRefMut<'_, T> {
#[inline]
fn input_array(&self) -> Result<BoxedRef<_InputArray>> {
(*self).input_array()
}
}

impl<T> ToOutputArray for BoxedRefMut<'_, T>
where
T: Boxed + ToOutputArray,
{
impl<T: Boxed + ToOutputArray> ToOutputArray for BoxedRefMut<'_, T> {
#[inline]
fn output_array(&mut self) -> Result<BoxedRefMut<_OutputArray>> {
self.reference.output_array()
}
}

impl<T> ToOutputArray for &mut BoxedRefMut<'_, T>
where
T: Boxed + ToOutputArray,
{
impl<T: Boxed + ToOutputArray> ToOutputArray for &mut BoxedRefMut<'_, T> {
#[inline]
fn output_array(&mut self) -> Result<BoxedRefMut<_OutputArray>> {
(*self).output_array()
}
}

impl<T> ToInputOutputArray for BoxedRefMut<'_, T>
where
T: Boxed + ToInputOutputArray,
{
impl<T: Boxed + ToInputOutputArray> ToInputOutputArray for BoxedRefMut<'_, T> {
#[inline]
fn input_output_array(&mut self) -> Result<BoxedRefMut<_InputOutputArray>> {
self.reference.input_output_array()
}
}

impl<T> ToInputOutputArray for &mut BoxedRefMut<'_, T>
where
T: Boxed + ToInputOutputArray,
{
impl<T: Boxed + ToInputOutputArray> ToInputOutputArray for &mut BoxedRefMut<'_, T> {
#[inline]
fn input_output_array(&mut self) -> Result<BoxedRefMut<_InputOutputArray>> {
(*self).input_output_array()
Expand Down
12 changes: 10 additions & 2 deletions src/manual/core/mat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,14 @@ fn match_is_continuous(mat: &(impl MatTraitConst + ?Sized)) -> Result<()> {

#[inline]
fn match_length(sizes: &[i32], len: usize) -> Result<()> {
if sizes.is_empty() {
return Err(Error::new(core::StsUnmatchedSizes, "Dimensions must not be empty"));
}
let data_len = i32::try_from(len)?;
if sizes.iter().product::<i32>() != data_len {
let msg = if sizes.len() == 2 {
format!(
"The length of the slice: {data_len} must match the passed row count: {rows} and column count: {cols} exactly",
"The length of the slice: {data_len} must match the passed row: {rows} and column: {cols} counts exactly",
rows = sizes[0],
cols = sizes[1],
)
Expand Down Expand Up @@ -204,7 +207,7 @@ impl Mat {
Ok(out)
}

/// Create a new `Mat` from a single-dimensional slice with custom shape
/// Create a new `Mat` that references a single-dimensional slice with custom shape
#[inline]
pub fn new_rows_cols_with_data<T: DataType>(rows: i32, cols: i32, data: &[T]) -> Result<BoxedRef<Self>> {
match_length(&[rows, cols], data.len())?;
Expand All @@ -214,6 +217,7 @@ impl Mat {
Ok(<BoxedRef<Mat>>::from(m))
}

/// Create a new `Mat` that references a single-dimensional slice with custom shape
#[inline]
pub fn new_rows_cols_with_data_mut<T: DataType>(rows: i32, cols: i32, data: &mut [T]) -> Result<BoxedRefMut<Self>> {
match_length(&[rows, cols], data.len())?;
Expand All @@ -222,27 +226,31 @@ impl Mat {
Ok(<BoxedRefMut<Mat>>::from(m))
}

/// Create a new `Mat` that references a single-dimensional slice with custom shape
#[inline]
pub fn new_size_with_data<T: DataType>(size: Size, data: &[T]) -> Result<BoxedRef<Self>> {
match_length(&[size.width, size.height], data.len())?;
let m = unsafe { Self::new_size_with_data_unsafe_def(size, T::opencv_type(), data.as_ptr().cast::<c_void>().cast_mut()) }?;
Ok(<BoxedRef<Mat>>::from(m))
}

/// Create a new `Mat` that references a single-dimensional slice with custom shape
#[inline]
pub fn new_size_with_data_mut<T: DataType>(size: Size, data: &mut [T]) -> Result<BoxedRefMut<Self>> {
match_length(&[size.width, size.height], data.len())?;
let m = unsafe { Self::new_size_with_data_unsafe_def(size, T::opencv_type(), data.as_mut_ptr().cast::<c_void>()) }?;
Ok(<BoxedRefMut<Mat>>::from(m))
}

/// Create a new `Mat` that references a single-dimensional slice with custom shape
#[inline]
pub fn new_nd_with_data<'data, T: DataType>(sizes: &[i32], data: &'data [T]) -> Result<BoxedRef<'data, Self>> {
match_length(sizes, data.len())?;
let m = unsafe { Self::new_nd_with_data_unsafe_def(sizes, T::opencv_type(), data.as_ptr().cast::<c_void>().cast_mut()) }?;
Ok(<BoxedRef<Mat>>::from(m))
}

/// Create a new `Mat` that references a single-dimensional slice with custom shape
#[inline]
pub fn new_nd_with_data_mut<'data, T: DataType>(sizes: &[i32], data: &'data mut [T]) -> Result<BoxedRefMut<'data, Self>> {
match_length(sizes, data.len())?;
Expand Down
2 changes: 1 addition & 1 deletion src/manual/core/vector/vector_extern.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::core::Vector;
use crate::platform_types::size_t;
use crate::traits::OpenCVType;
use crate::{extern_arg_send, extern_container_send, extern_receive, extern_send};
use crate::{extern_arg_send, extern_container_send, extern_receive};

/// This trait is implemented by any type that can be stored inside `Vector`.
///
Expand Down
8 changes: 4 additions & 4 deletions src/traits/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,22 @@ macro_rules! opencv_type_boxed {
($type: ty) => {
impl $crate::traits::Boxed for $type {
#[inline]
unsafe fn from_raw(ptr: extern_receive!($type)) -> Self {
unsafe fn from_raw(ptr: $crate::extern_receive!($type)) -> Self {
Self { ptr }
}

#[inline]
fn into_raw(self) -> extern_send!(mut $type) {
fn into_raw(self) -> $crate::extern_send!(mut $type) {
::std::mem::ManuallyDrop::new(self).ptr
}

#[inline]
fn as_raw(&self) -> extern_send!($type) {
fn as_raw(&self) -> $crate::extern_send!($type) {
self.ptr
}

#[inline]
fn as_raw_mut(&mut self) -> extern_send!(mut $type) {
fn as_raw_mut(&mut self) -> $crate::extern_send!(mut $type) {
self.ptr
}
}
Expand Down
14 changes: 7 additions & 7 deletions src/traits/opencv_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ pub trait OpenCVTypeExternContainerMove: OpenCVTypeExternContainer {
#[macro_export]
macro_rules! extern_receive {
($typ: ty) => {
extern_receive!($typ: '_)
$crate::extern_receive!($typ: '_)
};
($typ: ty: $lt: lifetime) => {
<$typ as $crate::traits::OpenCVType<$lt>>::ExternReceive
Expand All @@ -96,16 +96,16 @@ macro_rules! extern_send {
#[macro_export]
macro_rules! extern_container_send {
(mut $typ: ty: $lt: lifetime) => {
extern_send!(mut <$typ as $crate::traits::OpenCVTypeArg<$lt>>::ExternContainer)
$crate::extern_send!(mut <$typ as $crate::traits::OpenCVTypeArg<$lt>>::ExternContainer)
};
($typ: ty: $lt: lifetime) => {
extern_send!(<$typ as $crate::traits::OpenCVTypeArg<$lt>>::ExternContainer)
$crate::extern_send!(<$typ as $crate::traits::OpenCVTypeArg<$lt>>::ExternContainer)
};
(mut $typ: ty) => {
extern_container_send!(mut $typ: '_)
$crate::extern_container_send!(mut $typ: '_)
};
($typ: ty) => {
extern_container_send!($typ: '_)
$crate::extern_container_send!($typ: '_)
};
}

Expand All @@ -114,10 +114,10 @@ macro_rules! extern_container_send {
#[macro_export]
macro_rules! extern_arg_send {
(mut $typ: ty: $lt: lifetime) => {
extern_container_send!(mut <$typ as $crate::traits::OpenCVType<$lt>>::Arg: $lt)
$crate::extern_container_send!(mut <$typ as $crate::traits::OpenCVType<$lt>>::Arg: $lt)
};
($typ: ty: $lt: lifetime) => {
extern_container_send!(<$typ as $crate::traits::OpenCVType<$lt>>::Arg: $lt)
$crate::extern_container_send!(<$typ as $crate::traits::OpenCVType<$lt>>::Arg: $lt)
};
}

Expand Down
8 changes: 3 additions & 5 deletions tests/marshalling_only_latest_opencv.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
//! Tests that will not be run in CI on OpenCV 4.2.0, 4.5.4 and 3.4.16 due to missing classes
use opencv::prelude::*;
use opencv::Result;

/// Setting and getting fields through Ptr
#[test]
fn field_access_on_ptr() -> Result<()> {
#![cfg(all(ocvrs_has_module_aruco, any(ocvrs_opencv_branch_34, ocvrs_opencv_branch_4)))]
#[cfg(all(ocvrs_has_module_aruco, any(ocvrs_opencv_branch_34, ocvrs_opencv_branch_4)))]
fn field_access_on_ptr() -> opencv::Result<()> {
use opencv::aruco::EstimateParameters;
use opencv::prelude::*;
// the location and parameters are wildly different between even the minor release in the OpenCV branches, so for now
// let's just limit to only those fields that are stable
// #[cfg(ocvrs_opencv_branch_34)]
Expand Down
Loading

0 comments on commit 84f76c2

Please sign in to comment.