Introduce conditional macros as a way to perform conditional compilation based on the OpenCV branch for external crates
MODIFY
examples/opencl.rs
examples/opencl.rs
@@ -8,10 +8,12 @@ use opencv::{
Result,
types,
};
-#[cfg(ocvrs_opencv_branch_4)]
+opencv::opencv_branch_4! {
use opencv::core::AccessFlag::ACCESS_READ;
-#[cfg(not(ocvrs_opencv_branch_4))]
+}
+opencv::not_opencv_branch_4! {
use opencv::core::ACCESS_READ;
+}
const ITERATIONS: usize = 100;

MODIFY
examples/video_capture.rs
examples/video_capture.rs
@@ -8,10 +8,12 @@ use opencv::{
fn main() -> Result<()> {
let window = "video capture";
highgui::named_window(window, highgui::WINDOW_AUTOSIZE)?;
- #[cfg(ocvrs_opencv_branch_32)]
+ opencv::opencv_branch_32! {
let mut cam = videoio::VideoCapture::new_default(0)?; // 0 is the default camera
- #[cfg(not(ocvrs_opencv_branch_32))]
+ }
+ opencv::not_opencv_branch_32! {
let mut cam = videoio::VideoCapture::new(0, videoio::CAP_ANY)?; // 0 is the default camera
+ }
let opened = videoio::VideoCapture::is_opened(&cam)?;
if !opened {
panic!("Unable to open default camera!");

MODIFY
examples/video_facedetect.rs
examples/video_facedetect.rs
@@ -14,20 +14,22 @@ use opencv::{
fn main() -> Result<()> {
let window = "video capture";
highgui::named_window(window, 1)?;
- #[cfg(ocvrs_opencv_branch_32)]
+ opencv::opencv_branch_32! {
let (xml, mut cam) = {
(
"/usr/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml".to_owned(),
videoio::VideoCapture::new_default(0)?, // 0 is the default camera
)
};
- #[cfg(not(ocvrs_opencv_branch_32))]
+ }
+ opencv::not_opencv_branch_32! {
let (xml, mut cam) = {
(
core::find_file("haarcascades/haarcascade_frontalface_alt.xml", true, false)?,
videoio::VideoCapture::new(0, videoio::CAP_ANY)?, // 0 is the default camera
)
};
+ }
let opened = videoio::VideoCapture::is_opened(&cam)?;
if !opened {
panic!("Unable to open default camera!");

MODIFY
examples/video_features.rs
examples/video_features.rs
@@ -11,10 +11,12 @@ use opencv::{
fn main() -> Result<()> {
let window = "video capture";
highgui::named_window(window, 1)?;
- #[cfg(ocvrs_opencv_branch_32)]
+ opencv::opencv_branch_32! {
let mut cam = videoio::VideoCapture::new_default(0)?; // 0 is the default camera
- #[cfg(not(ocvrs_opencv_branch_32))]
+ }
+ opencv::not_opencv_branch_32! {
let mut cam = videoio::VideoCapture::new(0, videoio::CAP_ANY)?; // 0 is the default camera
+ }
let opened = videoio::VideoCapture::is_opened(&cam)?;
if !opened {
panic!("Unable to open default camera!");
@@ -35,10 +37,12 @@ fn main() -> Result<()> {
let mask = Mat::default();
orb.detect(&gray, &mut kps, &mask)?;
let mut display = Mat::default();
- #[cfg(ocvrs_opencv_branch_4)]
+ opencv::opencv_branch_4! {
let default_draw_matches_flags = features2d::DrawMatchesFlags::DEFAULT;
- #[cfg(not(ocvrs_opencv_branch_4))]
+ }
+ opencv::not_opencv_branch_4! {
let default_draw_matches_flags = features2d::DrawMatchesFlags_DEFAULT;
+ }
features2d::draw_keypoints(
&gray,
&kps,

MODIFY
examples/video_to_gray.rs
examples/video_to_gray.rs
@@ -9,10 +9,12 @@ use opencv::{
fn main() -> Result<()> {
let window = "video capture";
highgui::named_window(window, 1)?;
- #[cfg(ocvrs_opencv_branch_32)]
+ opencv::opencv_branch_32! {
let mut cam = videoio::VideoCapture::new_default(0)?; // 0 is the default camera
- #[cfg(not(ocvrs_opencv_branch_32))]
+ }
+ opencv::not_opencv_branch_32! {
let mut cam = videoio::VideoCapture::new(0, videoio::CAP_ANY)?; // 0 is the default camera
+ }
let opened = videoio::VideoCapture::is_opened(&cam)?;
if !opened {
panic!("Unable to open default camera!");

MODIFY
examples/warp_perspective_demo.rs
examples/warp_perspective_demo.rs
@@ -18,10 +18,12 @@ use opencv::{
types::VectorOfPoint,
};
-#[cfg(ocvrs_opencv_branch_4)]
+opencv::opencv_branch_4! {
use self::imgproc::LINE_8;
-#[cfg(not(ocvrs_opencv_branch_4))]
+}
+opencv::not_opencv_branch_4! {
use self::core::LINE_8;
+}
fn help() {
// print a welcome message, and the OpenCV version
@@ -52,8 +54,9 @@ fn main() -> Result<(), Box<dyn Error>> {
help();
let filename = env::args().skip(1).next().unwrap_or("data/right.jpg".into());
- #[cfg(not(ocvrs_opencv_branch_32))]
+ opencv::not_opencv_branch_32! {
let filename = core::find_file(&filename, true, false)?;
+ }
let original_image = imgcodecs::imread(&filename, imgcodecs::IMREAD_COLOR)?;
let mut image;
let original_image_cols = original_image.cols() as f32;

ADD
src/cond_macros.rs
+/// Conditional compilation macro based on OpenCV branch version for usage in external crates.
+/// # Examples
+///
+/// Alternative import:
+/// ```
+/// opencv::opencv_branch_4! {
+/// use opencv::imgproc::LINE_8;
+/// }
+/// opencv::not_opencv_branch_4! {
+/// use opencv::core::LINE_8;
+/// }
+/// ```
+///
+/// Alternative function call:
+/// ```
+/// opencv::opencv_branch_32! {
+/// let mut cam = opencv::videoio::VideoCapture::new_default(0)?;
+/// }
+/// opencv::not_opencv_branch_32! {
+/// let mut cam = opencv::videoio::VideoCapture::new(0, videoio::CAP_ANY)?;
+/// }
+/// ```
+#[cfg(ocvrs_opencv_branch_32)]
+#[macro_export]
+macro_rules! opencv_branch_32 {
+ ($($tt:tt)*) => { $($tt)* }
+}
+
+#[cfg(not(ocvrs_opencv_branch_32))]
+#[doc(hidden)]
+#[macro_export]
+macro_rules! opencv_branch_32 {
+ ($($tt:tt)*) => {}
+}
+
+/// See [`opencv::opencv_branch_32`]
+#[cfg(not(ocvrs_opencv_branch_32))]
+#[macro_export]
+macro_rules! not_opencv_branch_32 {
+ ($($tt:tt)*) => { $($tt)* }
+}
+
+#[cfg(ocvrs_opencv_branch_32)]
+#[doc(hidden)]
+#[macro_export]
+macro_rules! not_opencv_branch_32 {
+ ($($tt:tt)*) => {}
+}
+
+/// See [`opencv::opencv_branch_32`]
+#[cfg(ocvrs_opencv_branch_34)]
+#[macro_export]
+macro_rules! opencv_branch_34 {
+ ($($tt:tt)*) => { $($tt)* }
+}
+
+#[cfg(not(ocvrs_opencv_branch_34))]
+#[doc(hidden)]
+#[macro_export]
+macro_rules! opencv_branch_34 {
+ ($($tt:tt)*) => {}
+}
+
+/// See [`opencv::opencv_branch_32`]
+#[cfg(not(ocvrs_opencv_branch_34))]
+#[macro_export]
+macro_rules! not_opencv_branch_34 {
+ ($($tt:tt)*) => { $($tt)* }
+}
+
+#[cfg(ocvrs_opencv_branch_34)]
+#[doc(hidden)]
+#[macro_export]
+macro_rules! not_opencv_branch_34 {
+ ($($tt:tt)*) => {}
+}
+
+/// See [`opencv::opencv_branch_32`]
+#[cfg(ocvrs_opencv_branch_4)]
+#[macro_export]
+macro_rules! opencv_branch_4 {
+ ($($tt:tt)*) => { $($tt)* }
+}
+
+#[cfg(not(ocvrs_opencv_branch_4))]
+#[doc(hidden)]
+#[macro_export]
+macro_rules! opencv_branch_4 {
+ ($($tt:tt)*) => {}
+}
+
+/// See [`opencv::opencv_branch_32`]
+#[cfg(not(ocvrs_opencv_branch_4))]
+#[macro_export]
+macro_rules! not_opencv_branch_4 {
+ ($($tt:tt)*) => { $($tt)* }
+}
+
+#[cfg(ocvrs_opencv_branch_4)]
+#[doc(hidden)]
+#[macro_export]
+macro_rules! not_opencv_branch_4 {
+ ($($tt:tt)*) => {}
+}

MODIFY
src/lib.rs
src/lib.rs
#![allow(broken_intra_doc_links)]
+pub use cond_macros::*;
pub use error::{Error, Result};
pub use crate::opencv::hub::*;
@@ -62,3 +63,4 @@ pub(crate) mod mod_prelude {
#[cfg(test)]
mod test;
+mod cond_macros;

Introduce conditional macros as a way to perform conditional compilation based on the OpenCV branch for external crates