kornelski / dssim

Image similarity comparison simulating human perception (multiscale SSIM in Rust)
https://kornel.ski/dssim
GNU Affero General Public License v3.0
1.07k stars 69 forks source link

Can v2 be used in c++? #91

Closed Ben-Mack closed 3 years ago

Ben-Mack commented 3 years ago

I have no experience with Rust so it would be great if there's some guidance on how to intergrate dssim v2 with c++ codebase.

kornelski commented 3 years ago

Theoretically, yes: https://doc.rust-lang.org/nomicon/ffi.html#calling-rust-code-from-c

Rust can make C-compatible static libraries and export C ABI structs and functions, which you can then pick up on the C++ side. There's even tooling like https://lib.rs/cxx that automates a lot of this.

However, I haven't prepared an interface for it, so it needs a bit of DIY. If you'd like to do it, then:

  1. dssim-core is the library interface with minimal dependencies (no image I/O) so it's the best one to expose. https://github.com/kornelski/dssim/tree/main/dssim-core
  2. Create C-compatible functions that have #[no_mangle] pub extern "C" fn type for the functionality you need.
  3. Write their equivalent as a C header (or use https://lib.rs/cbindgen to automate this)
  4. Add [lib]\ncrate-type = ["lib", "staticlib"] to https://github.com/kornelski/dssim/blob/main/dssim-core/Cargo.toml
  5. cargo build --release will then make target/release/libdssim-core.a that you can link with a C or C++ project
Ben-Mack commented 3 years ago

Thank you for the quick and detailed response!

Please consider adding such interface to the library as I think there'll be more demand to use this lib with C/C++.

kornelski commented 3 years ago

Done in b5c4195f2779cac60888bc0f8a5dba9a65b0a5fe

Ben-Mack commented 3 years ago

Wow, that's really quick, these code comments are so detailed too, thanks a lot!