|
|
@ -20,58 +20,66 @@ use cache::Cache; |
|
|
|
|
|
|
|
#[repr(C)]
|
|
|
|
pub struct PIHash<'a> {
|
|
|
|
cache: Option<Cache<'a>>
|
|
|
|
cache: Option<Cache<'a>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> PIHash<'a> {
|
|
|
|
/**
|
|
|
|
* Create a new pihash library, and initialize a cache of a path is passed. If none is passed then no cache is initialized or used with the library
|
|
|
|
/**
|
|
|
|
* Create a new pihash library, and initialize a cache of a path is passed.
|
|
|
|
* If none is passed then no cache is initialized or used with the library
|
|
|
|
*/
|
|
|
|
pub fn new(cache_path: Option<&'a str>) -> PIHash<'a> {
|
|
|
|
match cache_path {
|
|
|
|
Some(path) => {
|
|
|
|
let cache = Cache { cache_dir: path, use_cache: true};
|
|
|
|
match cache.init() {
|
|
|
|
Ok(_) => PIHash { cache: Some(cache) },
|
|
|
|
Err(e) => {
|
|
|
|
println!("Error creating library with cache: {}", e);
|
|
|
|
PIHash { cache: None }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
None => PIHash { cache: None },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_perceptual_hash(&self, path: &Path, precision: &hash::Precision, hash_type: &hash::HashType) -> u64 {
|
|
|
|
hash::get_perceptual_hash(&path, &precision, &hash_type, &self.cache)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_phashes(&self, path: &'a Path) -> hash::PerceptualHashes {
|
|
|
|
hash::get_perceptual_hashes(path, &hash::Precision::Medium, &self.cache)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get_ahash(&self, path: &Path) -> u64 {
|
|
|
|
hash::get_perceptual_hash(&path,
|
|
|
|
&hash::Precision::Medium,
|
|
|
|
&hash::HashType::AHash,
|
|
|
|
&self.cache)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_dhash(&self, path: &Path) -> u64 {
|
|
|
|
hash::get_perceptual_hash(&path,
|
|
|
|
&hash::Precision::Medium,
|
|
|
|
&hash::HashType::DHash,
|
|
|
|
&self.cache)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_phash(&self, path: &Path) -> u64 {
|
|
|
|
hash::get_perceptual_hash(&path,
|
|
|
|
&hash::Precision::Medium,
|
|
|
|
&hash::HashType::PHash,
|
|
|
|
&self.cache)
|
|
|
|
}
|
|
|
|
pub fn new(cache_path: Option<&'a str>) -> PIHash<'a> {
|
|
|
|
match cache_path {
|
|
|
|
Some(path) => {
|
|
|
|
let cache = Cache {
|
|
|
|
cache_dir: path,
|
|
|
|
use_cache: true,
|
|
|
|
};
|
|
|
|
match cache.init() {
|
|
|
|
Ok(_) => PIHash { cache: Some(cache) },
|
|
|
|
Err(e) => {
|
|
|
|
println!("Error creating library with cache: {}", e);
|
|
|
|
PIHash { cache: None }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None => PIHash { cache: None },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_perceptual_hash(&self,
|
|
|
|
path: &Path,
|
|
|
|
precision: &hash::Precision,
|
|
|
|
hash_type: &hash::HashType)
|
|
|
|
-> u64 {
|
|
|
|
hash::get_perceptual_hash(&path, &precision, &hash_type, &self.cache)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_phashes(&self, path: &'a Path) -> hash::PerceptualHashes {
|
|
|
|
hash::get_perceptual_hashes(path, &hash::Precision::Medium, &self.cache)
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
pub fn get_ahash(&self, path: &Path) -> u64 {
|
|
|
|
hash::get_perceptual_hash(&path,
|
|
|
|
&hash::Precision::Medium,
|
|
|
|
&hash::HashType::AHash,
|
|
|
|
&self.cache)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_dhash(&self, path: &Path) -> u64 {
|
|
|
|
hash::get_perceptual_hash(&path,
|
|
|
|
&hash::Precision::Medium,
|
|
|
|
&hash::HashType::DHash,
|
|
|
|
&self.cache)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_phash(&self, path: &Path) -> u64 {
|
|
|
|
hash::get_perceptual_hash(&path,
|
|
|
|
&hash::Precision::Medium,
|
|
|
|
&hash::HashType::PHash,
|
|
|
|
&self.cache)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
@ -86,24 +94,24 @@ pub fn get_hamming_distance(hash1: u64, hash2: u64) -> u64 { |
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn ext_init(cache_path_char: *const libc::c_char) -> *const libc::c_void {
|
|
|
|
unsafe {
|
|
|
|
unsafe {
|
|
|
|
let path_cstr = CStr::from_ptr(cache_path_char);
|
|
|
|
let path_str = match path_cstr.to_str() {
|
|
|
|
Ok(path) => Some(path),
|
|
|
|
Err(_) => None,
|
|
|
|
Ok(path) => Some(path),
|
|
|
|
Err(_) => None,
|
|
|
|
};
|
|
|
|
//println!("Created new lib, with cache at {}", path_str.unwrap());
|
|
|
|
// println!("Created new lib, with cache at {}", path_str.unwrap());
|
|
|
|
let lib = Box::new(PIHash::new(path_str));
|
|
|
|
let ptr = Box::into_raw(lib) as *mut libc::c_void;
|
|
|
|
ptr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn ext_free(raw_lib: *const libc::c_void) {
|
|
|
|
unsafe {
|
|
|
|
drop(Box::from_raw(raw_lib as *mut PIHash));
|
|
|
|
}
|
|
|
|
unsafe {
|
|
|
|
drop(Box::from_raw(raw_lib as *mut PIHash));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
@ -221,9 +229,7 @@ mod tests { |
|
|
|
let mut hashes: [u64; 3] = [0; 3];
|
|
|
|
for index in 0..image_paths.len() {
|
|
|
|
let image_path = image_paths[index];
|
|
|
|
let calculated_hash = lib.get_perceptual_hash(&image_path,
|
|
|
|
&hash_precision,
|
|
|
|
&hash_type);
|
|
|
|
let calculated_hash = lib.get_perceptual_hash(&image_path, &hash_precision, &hash_type);
|
|
|
|
println!("Image hashes for [{}] expected: [{}] actual: [{}]",
|
|
|
|
image_path.to_str().unwrap(),
|
|
|
|
image_hashes[index],
|
|
|
@ -233,19 +239,20 @@ mod tests { |
|
|
|
}
|
|
|
|
|
|
|
|
for index in 0..hashes.len() {
|
|
|
|
for index2 in 0..hashes.len() {
|
|
|
|
if index == index2 {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
let distance = hash::calculate_hamming_distance(hashes[index], hashes[index2]);
|
|
|
|
println!("Hashes [{}] and [{}] have a hamming distance of [{}] of a max allowed distance of [{}]",
|
|
|
|
hashes[index],
|
|
|
|
hashes[index2],
|
|
|
|
distance,
|
|
|
|
max_hamming_distance);
|
|
|
|
assert!(distance <= max_hamming_distance);
|
|
|
|
for index2 in 0..hashes.len() {
|
|
|
|
if index == index2 {
|
|
|
|
continue;
|
|
|
|
} else {
|
|
|
|
let distance = hash::calculate_hamming_distance(hashes[index], hashes[index2]);
|
|
|
|
println!("Hashes [{}] and [{}] have a hamming distance of [{}] of a max \
|
|
|
|
allowed distance of [{}]",
|
|
|
|
hashes[index],
|
|
|
|
hashes[index2],
|
|
|
|
distance,
|
|
|
|
max_hamming_distance);
|
|
|
|
assert!(distance <= max_hamming_distance);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
@ -443,28 +450,34 @@ mod tests { |
|
|
|
#[cfg(feature = "bench")]
|
|
|
|
#[bench]
|
|
|
|
fn bench_with_cache(bench: &mut Bencher) -> () {
|
|
|
|
// Prep_library
|
|
|
|
// Prep_library
|
|
|
|
let lib = PIHash::new(Some(cache::DEFAULT_CACHE_DIR));
|
|
|
|
|
|
|
|
// Setup the caches to make sure we're good to properly bench
|
|
|
|
// All phashes so that the matricies are pulled from cache as well
|
|
|
|
lib.get_perceptual_hash(&Path::new("./test_images/sample_01_large.jpg"),&hash::Precision::Medium, &hash::HashType::PHash);
|
|
|
|
lib.get_perceptual_hash(&Path::new("./test_images/sample_01_large.jpg"),
|
|
|
|
&hash::Precision::Medium,
|
|
|
|
&hash::HashType::PHash);
|
|
|
|
|
|
|
|
bench.iter(|| {
|
|
|
|
// Sample_01 Bench
|
|
|
|
lib.get_perceptual_hash(&Path::new("./test_images/sample_01_large.jpg"), &hash::Precision::Medium, &hash::HashType::PHash);
|
|
|
|
// Sample_01 Bench
|
|
|
|
lib.get_perceptual_hash(&Path::new("./test_images/sample_01_large.jpg"),
|
|
|
|
&hash::Precision::Medium,
|
|
|
|
&hash::HashType::PHash);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(feature = "bench")]
|
|
|
|
#[bench]
|
|
|
|
fn bench_without_cache(bench: &mut Bencher) -> () {
|
|
|
|
// Prep_library
|
|
|
|
// Prep_library
|
|
|
|
let lib = PIHash::new(None);
|
|
|
|
|
|
|
|
bench.iter(|| {
|
|
|
|
// Sample_01 Bench
|
|
|
|
lib.get_perceptual_hash(&Path::new("./test_images/sample_01_large.jpg"), &hash::Precision::Medium, &hash::HashType::PHash);
|
|
|
|
// Sample_01 Bench
|
|
|
|
lib.get_perceptual_hash(&Path::new("./test_images/sample_01_large.jpg"),
|
|
|
|
&hash::Precision::Medium,
|
|
|
|
&hash::HashType::PHash);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|