From 23f0eec0f47d9f64ea1331f2a5b3b15d3054cd00 Mon Sep 17 00:00:00 2001 From: Drew Short Date: Mon, 4 Jan 2016 15:43:36 -0600 Subject: [PATCH] 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. --- Cargo.toml | 3 +++ src/lib.rs | 37 +++++++++++++++++++++++++++++++++++-- 2 files changed, 38 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 509c205..d792e6b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,6 +9,9 @@ keywords = ["phash", "perceptual", "image", "comparison"] license-file = "LICENSE" exclude = ["test_images/*"] +[lib] +crate-type = ["dylib", "rlib"] + [dependencies] docopt = "0.6.78" rustc-serialize = "0.3.16" diff --git a/src/lib.rs b/src/lib.rs index f935301..bd2fdb0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -8,13 +8,15 @@ mod cache; use std::path::Path; use hash::PerceptualHash; +use std::ffi::CStr; /** * Prepare the library for work. * * Not performing this step may cause parts to fail. */ -pub fn init() { +#[no_mangle] +pub extern fn init() { match cache::prep_cache() { Ok(_) => {} Err(e) => println!("Error: {}", e), @@ -24,7 +26,8 @@ pub fn init() { /** * Teardown for the library */ -pub fn teardown() { +#[no_mangle] +pub extern fn teardown() { match cache::clear_cache() { Ok(_) => {} 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) } +#[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 { 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 { 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 { hash::PHash::new(&path, &hash::Precision::Medium).get_hash() }