|
|
@ -20,17 +20,21 @@ 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};
|
|
|
|
let cache = Cache {
|
|
|
|
cache_dir: path,
|
|
|
|
use_cache: true,
|
|
|
|
};
|
|
|
|
match cache.init() {
|
|
|
|
Ok(_) => PIHash { cache: Some(cache) },
|
|
|
|
Err(e) => {
|
|
|
@ -38,12 +42,16 @@ impl<'a> PIHash<'a> { |
|
|
|
PIHash { cache: None }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
None => PIHash { cache: None },
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get_perceptual_hash(&self, path: &Path, precision: &hash::Precision, hash_type: &hash::HashType) -> u64 {
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
@ -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],
|
|
|
@ -238,7 +244,8 @@ mod tests { |
|
|
|
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 [{}]",
|
|
|
|
println!("Hashes [{}] and [{}] have a hamming distance of [{}] of a max \
|
|
|
|
allowed distance of [{}]",
|
|
|
|
hashes[index],
|
|
|
|
hashes[index2],
|
|
|
|
distance,
|
|
|
@ -448,11 +455,15 @@ mod tests { |
|
|
|
|
|
|
|
// 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);
|
|
|
|
lib.get_perceptual_hash(&Path::new("./test_images/sample_01_large.jpg"),
|
|
|
|
&hash::Precision::Medium,
|
|
|
|
&hash::HashType::PHash);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
@ -464,7 +475,9 @@ mod tests { |
|
|
|
|
|
|
|
bench.iter(|| {
|
|
|
|
// Sample_01 Bench
|
|
|
|
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);
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|