Skip to content

Commit 87f5f21

Browse files
committed
Support OpenCV 5, drop support for 3.2
1 parent 8fddd16 commit 87f5f21

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+869
-431
lines changed

.github/workflows/opencv-rust.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ jobs:
2222
- os-image: ubuntu-22.04
2323
opencv-version: 4.11.0
2424
linkage: static
25+
- os-image: ubuntu-22.04
26+
opencv-version: 5.0.0-alpha
27+
linkage: dynamic
2528
- os-image: ubuntu-22.04
2629
opencv-version: 3.4.20
2730
linkage: dynamic

Cargo.toml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ maintenance = { status = "actively-developed" }
2222
members = ["binding-generator"]
2323

2424
[dependencies]
25+
half = { version = "2", optional = true }
2526
libc = "0.2"
2627
num-traits = "0.2"
2728
once_cell = "1"
@@ -61,29 +62,35 @@ vcpkg = "0.2.9"
6162

6263
[features]
6364
default = [
65+
"3d",
6466
"alphamat",
6567
"aruco",
6668
"aruco_detector",
6769
"barcode",
6870
"bgsegm",
6971
"bioinspired",
72+
"calib",
7073
"calib3d",
74+
#"cannops",
7175
"ccalib",
7276
"cudaarithm",
7377
"cudabgsegm",
7478
"cudacodec",
7579
"cudafeatures2d",
7680
"cudafilters",
7781
"cudaimgproc",
82+
"cudalegacy",
7883
"cudaobjdetect",
7984
"cudaoptflow",
8085
"cudastereo",
8186
"cudawarping",
87+
#"cudev",
8288
"cvv",
8389
"dnn",
8490
"dnn_superres",
8591
"dpm",
8692
"face",
93+
"features",
8794
"features2d",
8895
"flann",
8996
"freetype",
@@ -128,32 +135,39 @@ default = [
128135
"ximgproc",
129136
"xobjdetect",
130137
"xphoto",
138+
"xstereo",
131139
]
132140

133141
# OpenCV module features
142+
3d = []
134143
alphamat = []
135144
aruco = []
136145
aruco_detector = ["aruco"]
137146
barcode = []
138147
bgsegm = ["video"]
139148
bioinspired = []
149+
calib = ["features"]
140150
calib3d = ["features2d"]
151+
#cannops = []
141152
ccalib = ["features2d"]
142153
cudaarithm = []
143154
cudabgsegm = ["video"]
144155
cudacodec = []
145156
cudafeatures2d = ["features2d"]
146157
cudafilters = []
147158
cudaimgproc = ["imgproc"]
159+
cudalegacy = []
148160
cudaobjdetect = ["objdetect"]
149161
cudaoptflow = []
150162
cudastereo = ["calib3d"]
151163
cudawarping = []
164+
#cudev = []
152165
cvv = []
153166
dnn = []
154167
dnn_superres = []
155168
dpm = []
156169
face = ["objdetect"]
170+
features = ["flann"]
157171
features2d = ["flann"]
158172
flann = []
159173
freetype = []
@@ -193,14 +207,16 @@ video = []
193207
videoio = []
194208
videostab = ["features2d"]
195209
viz = []
210+
wechat_qrcode = []
196211
xfeatures2d = ["features2d"]
197212
ximgproc = ["calib3d"]
198213
xobjdetect = []
199214
xphoto = ["photo"]
200-
wechat_qrcode = []
215+
xstereo = []
201216

202217
# General features
203218
clang-runtime = ["opencv-binding-generator/clang-runtime"]
219+
f16 = ["dep:half"]
204220

205221
[package.metadata.docs.rs]
206222
no-default-features = true

README.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ The API is usable, but unstable and not very battle-tested; use at your own risk
1414

1515
## Quickstart
1616

17-
Make sure the supported OpenCV version (3.4 or 4.x) and Clang (part of LLVM, needed for automatic binding
17+
Make sure the supported OpenCV version (3.4, 4.x or 5.x) and Clang (part of LLVM, needed for automatic binding
1818
generation) are installed in your system.
1919

2020
Update your Cargo.toml
@@ -145,6 +145,7 @@ The following variables affect the building the of the `opencv` crate, but belon
145145
* `clang-runtime` - enables the runtime detection of libclang (`runtime` feature of `clang-sys`). Useful as a
146146
workaround for when your dependencies (like `bindgen`) pull in `clang-sys` with hard `runtime` feature.
147147
* `rgb` - allow using [`rgb`](https://crates.io/crates/rgb) crate types as `Mat` elements
148+
* `f16` - add intergration with `f16` type from the `half` crate
148149

149150
## API details
150151

@@ -158,6 +159,7 @@ The following OpenCV versions are supported at the moment:
158159

159160
* 3.4
160161
* 4.x
162+
* 5.x (preliminary)
161163

162164
### Minimum rustc version (MSRV)
163165

@@ -279,7 +281,7 @@ version because the crate has gone through the considerable rewrite since.
279281
### OpenCV 3.2
280282

281283
The last version with confirmed OpenCV 3.2 support is 0.75.0, after that this branch of OpenCV is no longer
282-
tested and supported. It may still work though.
284+
tested and supported. Since version 0.94.0 the support of OpenCV 3.2 is removed from the codebase.
283285

284286
## Contributor's Guide
285287

binding-generator/src/bin/binding-generator.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,5 +82,5 @@ fn main() {
8282
.map(Path::new)
8383
.collect::<Vec<_>>();
8484
let bindings_writer = RustNativeBindingWriter::new(&src_cpp_dir, &out_dir, module, &version, debug);
85-
Generator::new(&opencv_header_dir, &additional_include_dirs, &src_cpp_dir).generate(module, bindings_writer);
85+
Generator::new(&opencv_header_dir, &additional_include_dirs, &src_cpp_dir).generate(module, !debug, bindings_writer);
8686
}

binding-generator/src/class/desc.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,4 +156,9 @@ impl<'tu, 'ge> ClassDesc<'tu, 'ge> {
156156
pub fn cv_dnn_dict_value() -> Class<'tu, 'ge> {
157157
Class::new_desc(Self::boxed("cv::dnn::DictValue", "dnn"))
158158
}
159+
160+
/// `cv::Feature2D`
161+
pub fn cv_keypoint() -> Class<'tu, 'ge> {
162+
Class::new_desc(Self::boxed("cv::KeyPoint", "core"))
163+
}
159164
}

binding-generator/src/constant.rs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,21 @@ pub fn render_constant_rust(tokens: &[Token]) -> Option<Value> {
4444
let spelling = t.get_spelling();
4545
if spelling.contains(['"', '\'']) {
4646
out.kind = ValueKind::String;
47+
out.value += &spelling;
4748
} else if spelling.contains('.') {
48-
out.kind = ValueKind::Float;
49+
if let Some(float) = spelling.strip_suffix(['F', 'f']) {
50+
out.kind = ValueKind::Float;
51+
out.value += float;
52+
} else {
53+
out.kind = ValueKind::Double;
54+
out.value += &spelling;
55+
}
4956
} else if let Some(unsigned_value) = spelling.strip_suffix(['U', 'u']) {
5057
out.kind = ValueKind::UnsignedInteger;
5158
out.value += unsigned_value;
52-
continue;
59+
} else {
60+
out.value += &spelling;
5361
}
54-
out.value += &spelling;
5562
}
5663
TokenKind::Punctuation => {
5764
let spelling = t.get_spelling();
@@ -85,7 +92,7 @@ pub fn render_evaluation_result_rust(result: EvaluationResult) -> Value {
8592
value: x.to_string(),
8693
},
8794
EvaluationResult::Float(x) => Value {
88-
kind: ValueKind::Float,
95+
kind: ValueKind::Double,
8996
value: x.to_string(),
9097
},
9198
EvaluationResult::String(x)
@@ -181,6 +188,7 @@ pub enum ValueKind {
181188
Integer,
182189
UnsignedInteger,
183190
Float,
191+
Double,
184192
String,
185193
}
186194

@@ -192,7 +200,7 @@ pub struct Value {
192200

193201
impl fmt::Display for Value {
194202
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
195-
if self.kind == ValueKind::Float && !self.value.contains('.') {
203+
if self.kind == ValueKind::Double && !self.value.contains('.') {
196204
write!(f, "{}.", self.value)
197205
} else {
198206
write!(f, "{}", self.value)

binding-generator/src/generator.rs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,12 @@ impl<'tu, V: GeneratorVisitor<'tu>> EntityWalkerVisitor<'tu> for OpenCvWalker<'t
104104
"GAPI_EXPORTS_W",
105105
"GAPI_WRAP",
106106
];
107-
const SIMPLE: [&str; 3] = ["CV_EXPORTS_W_SIMPLE", "CV_EXPORTS_W_MAP", "GAPI_EXPORTS_W_SIMPLE"];
107+
const SIMPLE: [&str; 4] = [
108+
"CV_EXPORTS_W_SIMPLE",
109+
"CV_EXPORTS_W_PARAMS",
110+
"CV_EXPORTS_W_MAP",
111+
"GAPI_EXPORTS_W_SIMPLE",
112+
];
108113
const RENAME: [&str; 2] = ["CV_EXPORTS_AS", "CV_WRAP_AS"];
109114
if BOXED.contains(&name.as_str()) {
110115
self.gen_env.make_export_config(entity);
@@ -455,8 +460,8 @@ impl Generator {
455460
}
456461

457462
/// Runs the full binding generation process using the supplied `visitor`
458-
pub fn generate(&self, module: &str, visitor: impl for<'tu> GeneratorVisitor<'tu>) {
459-
self.pre_process(module, true, |root_entity| {
463+
pub fn generate(&self, module: &str, panic_on_error: bool, visitor: impl for<'tu> GeneratorVisitor<'tu>) {
464+
self.pre_process(module, panic_on_error, |root_entity| {
460465
let gen_env = GeneratorEnv::global(module, root_entity);
461466
let opencv_walker = OpenCvWalker::new(module, &self.opencv_module_header_dir, visitor, gen_env);
462467
root_entity.walk_opencv_entities(opencv_walker);

binding-generator/src/settings.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,7 @@ pub static RESERVED_RENAME: Lazy<HashMap<&str, &str>> = Lazy::new(|| {
187187
("impl", "impl_"),
188188
("loop", "loop_"),
189189
("yield", "yield_"),
190+
("where", "where_"),
190191
])
191192
});
192193

@@ -275,6 +276,7 @@ pub static NO_SKIP_NAMESPACE_IN_LOCALNAME: Lazy<HashMap<&str, HashMap<&str, &str
275276
("cudacodec", HashMap::from([("cudacodec", "CUDA")])),
276277
("cudafeatures2d", HashMap::from([("cuda", "CUDA")])),
277278
("cudaimgproc", HashMap::from([("cuda", "CUDA")])),
279+
("cudalegacy", HashMap::from([("cuda", "CUDA")])),
278280
("cudaobjdetect", HashMap::from([("cuda", "CUDA")])),
279281
("cudaoptflow", HashMap::from([("cuda", "CUDA")])),
280282
("cudastereo", HashMap::from([("cuda", "CUDA")])),
@@ -297,7 +299,9 @@ pub static NO_SKIP_NAMESPACE_IN_LOCALNAME: Lazy<HashMap<&str, HashMap<&str, &str
297299

298300
pub static PREVENT_VECTOR_TYPEDEF_GENERATION: Lazy<HashSet<&str>> = Lazy::new(|| {
299301
HashSet::from([
300-
// `MatShape` is an alias to `Vector<i32>` and this leads to duplication of definition for that type
302+
// `MatShape` is an alias to `Vector<i32>` and this leads to duplication of definition for the `Vector<Vector<i32>>` type
301303
"cv::dnn::MatShape",
304+
// `MatType` is an alias `i32` and we don't want to duplicate `Vector<i32>` with `Vector<MatType>`
305+
"cv::dnn::MatType",
302306
])
303307
});

binding-generator/src/settings/argument_override.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub type PropertyOverride = HashMap<&'static str, TypeRefTypeHint>;
1313

1414
pub fn arg_override_factory(module: &str) -> ArgOverride {
1515
match module {
16-
"calib3d" => calib3d_arg_override_factory(),
16+
"calib3d" | "calib" | "3d" => calib3d_arg_override_factory(),
1717
"core" => core_arg_override_factory(),
1818
"freetype" => freetype_arg_override_factory(),
1919
"highgui" => highgui_arg_override_factory(),

binding-generator/src/settings/element_exclude_kind.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@ use crate::element::ExcludeKind;
77
/// cpp_name(Reference)
88
pub static ELEMENT_EXCLUDE_KIND: Lazy<HashMap<&str, ExcludeKind>> = Lazy::new(|| {
99
HashMap::from([
10-
("cv::internal::format", ExcludeKind::Excluded), // 3.2 duplicate definition
1110
("cv::face::FacemarkLBF::BBox", ExcludeKind::Excluded), // not used, not exported in windows dll
1211
("CV_DEPRECATED", ExcludeKind::Ignored),
1312
("CV_EXPORTS", ExcludeKind::Ignored),
14-
("CV_IMPL", ExcludeKind::Ignored), // 3.2
13+
("CV_IMPL", ExcludeKind::Ignored),
1514
("CV_MAKE_TYPE", ExcludeKind::Ignored),
16-
("CvFileNode", ExcludeKind::Ignored), // 3.2 3.4 C struct
17-
("CvSeq", ExcludeKind::Ignored), // 3.2 C struct
1815
("FILE", ExcludeKind::Ignored),
19-
("HG_AUTOSIZE", ExcludeKind::Ignored), // 3.2
16+
("HG_AUTOSIZE", ExcludeKind::Ignored), // 3.4
2017
("cv::ErrorCallback", ExcludeKind::Ignored),
2118
("cv::MatAllocator", ExcludeKind::Ignored), // doesn't handle cpp part too well
2219
("cv::MatExpr::op", ExcludeKind::Ignored), // fixme implement PointerOf types

0 commit comments

Comments
 (0)