Browse Source

Externalizing

Added pub extern methods to the library to expose as a c api
set cargo to build a dynamic library as well as an rlib for the included binary.
develop
Drew Short 9 years ago
parent
commit
23f0eec0f4
  1. 3
      Cargo.toml
  2. 37
      src/lib.rs

3
Cargo.toml

@ -9,6 +9,9 @@ keywords = ["phash", "perceptual", "image", "comparison"]
license-file = "LICENSE" license-file = "LICENSE"
exclude = ["test_images/*"] exclude = ["test_images/*"]
[lib]
crate-type = ["dylib", "rlib"]
[dependencies] [dependencies]
docopt = "0.6.78" docopt = "0.6.78"
rustc-serialize = "0.3.16" rustc-serialize = "0.3.16"

37
src/lib.rs

@ -8,13 +8,15 @@ mod cache;
use std::path::Path; use std::path::Path;
use hash::PerceptualHash; use hash::PerceptualHash;
use std::ffi::CStr;
/** /**
* Prepare the library for work. * Prepare the library for work.
* *
* Not performing this step may cause parts to fail. * Not performing this step may cause parts to fail.
*/ */
pub fn init() {
#[no_mangle]
pub extern fn init() {
match cache::prep_cache() { match cache::prep_cache() {
Ok(_) => {} Ok(_) => {}
Err(e) => println!("Error: {}", e), Err(e) => println!("Error: {}", e),
@ -24,7 +26,8 @@ pub fn init() {
/** /**
* Teardown for the library * Teardown for the library
*/ */
pub fn teardown() {
#[no_mangle]
pub extern fn teardown() {
match cache::clear_cache() { match cache::clear_cache() {
Ok(_) => {} Ok(_) => {}
Err(e) => println!("Error: {}", e), Err(e) => println!("Error: {}", e),
@ -35,14 +38,44 @@ pub fn get_phashes(path: &Path) -> hash::PerceptualHashes {
hash::get_perceptual_hashes(path, &hash::Precision::Medium) hash::get_perceptual_hashes(path, &hash::Precision::Medium)
} }
#[no_mangle]
pub extern fn ext_get_ahash(path_str: &CStr) -> u64 {
let image_path = match path_str.to_str() {
Ok(result) => result,
Err(e) => "",
};
let path = Path::new(&image_path);
get_ahash(&path)
}
pub fn get_ahash(path: &Path) -> u64 { pub fn get_ahash(path: &Path) -> u64 {
hash::AHash::new(&path, &hash::Precision::Medium).get_hash() hash::AHash::new(&path, &hash::Precision::Medium).get_hash()
} }
#[no_mangle]
pub extern fn ext_get_dhash(path_str: &CStr) -> u64 {
let image_path = match path_str.to_str() {
Ok(result) => result,
Err(e) => "",
};
let path = Path::new(&image_path);
get_dhash(&path)
}
pub fn get_dhash(path: &Path) -> u64 { pub fn get_dhash(path: &Path) -> u64 {
hash::DHash::new(&path, &hash::Precision::Medium).get_hash() hash::DHash::new(&path, &hash::Precision::Medium).get_hash()
} }
#[no_mangle]
pub extern fn ext_get_phash(path_str: &CStr) -> u64 {
let image_path = match path_str.to_str() {
Ok(result) => result,
Err(e) => "",
};
let path = Path::new(&image_path);
get_phash(&path)
}
pub fn get_phash(path: &Path) -> u64 { pub fn get_phash(path: &Path) -> u64 {
hash::PHash::new(&path, &hash::Precision::Medium).get_hash() hash::PHash::new(&path, &hash::Precision::Medium).get_hash()
} }

Loading…
Cancel
Save