Browse Source

Working on resolvoing development repository vs published repository differences

develop
Drew Short 8 years ago
parent
commit
78bbb520f9
  1. 16
      Cargo.toml
  2. 20
      src/cache.rs
  3. 12
      src/hash/phash.rs
  4. 25
      src/lib.rs
  5. 6
      src/main.rs

16
Cargo.toml

@ -1,6 +1,6 @@
[package]
name = "pihash"
version = "0.3.4"
version = "0.3.5"
authors = ["Drew Short <warrick@sothr.com>"]
description = "A simple library for generating perceptual hashes for images and comparing images based on their perceptual hashes."
repository = "https://github.com/warricksothr/Perceptual-Image-Hashing/"
@ -20,13 +20,11 @@ default = []
bench = []
[dependencies]
libc = "0.2.17"
rustc-serialize = "0.3.19"
dft = "0.5.2"
image = "0.10.3"
libc = "0.2.20"
rustc-serialize = "0.3.22"
dft = "0.5.4"
image = "0.13.0"
num = "0.1.36"
docopt = "0.6.86"
flate2 = "0.2.14"
docopt = "0.7.0"
flate2 = "0.2.19"
sha1 = "0.2.0"
#criterion = { git = "https://github.com/japaric/criterion.rs" }
log = "0.3.6"

20
src/cache.rs

@ -139,8 +139,9 @@ impl<'a> Cache<'a> {
try!(source.read_to_end(&mut buf));
let mut sha1 = Sha1::new();
sha1.update(&buf);
let digest = sha1.digest();
// Return the hex result of the hash
Ok(sha1.digest().to_string())
Ok(format!("{}", digest))
}
/**
@ -252,11 +253,10 @@ impl<'a> Cache<'a> {
let mut compressor = ZlibEncoder::new(Vec::new(),
Compression::Default);
for row in file_contents {
let mut row_str = row.iter().fold(String::new(),
|acc, &item| {
acc +
&format!("{},", item)
});
let mut row_str =
row.iter()
.fold(String::new(),
|acc, &item| acc + &format!("{},", item));
// remove the last comma
let desire_len = row_str.len() - 1;
row_str.truncate(desire_len);
@ -318,14 +318,12 @@ impl<'a> Cache<'a> {
}
};
// convert the matrix
let matrix: Vec<Vec<f64>> = matrix_data_str.trim()
let matrix: Vec<Vec<f64>> = matrix_data_str
.trim()
.split("\n")
.map(|line| {
line.split(",")
.map(|f| {
f64::from_str(f)
.unwrap()
})
.map(|f| f64::from_str(f).unwrap())
.collect()
})
.collect();

12
src/hash/phash.rs

@ -50,8 +50,12 @@ impl<'a> PerceptualHash for PHash<'a> {
None => {
let matrix = create_data_matrix(width, height, &image);
// Store this DFT in the cache
<<<<<<< HEAD
match c.put_matrix_in_cache(&Path::new(self.prepared_image
.orig_path),
=======
match c.put_matrix_in_cache(&Path::new(self.prepared_image.orig_path),
>>>>>>> master
width as u32,
&matrix) {
Ok(_) => {}
@ -82,19 +86,15 @@ impl<'a> PerceptualHash for PHash<'a> {
// Calculating a hash based on the mean
let mut hash = 0u64;
for x in 0..target_width {
// println!("Mean: {} Values: {:?}",mean,data_matrix[x]);
for y in 0..target_height {
if data_matrix[x][y] >= mean {
hash |= 1;
// println!("Pixel {} is >= {} therefore {:b}", pixel_sum, mean, hash);
} else {
hash |= 0;
// println!("Pixel {} is < {} therefore {:b}", pixel_sum, mean, hash);
}
hash <<= 1;
}
}
// println!("Hash for {} is {}", prepared_image.orig_path, hash);
hash
}
None => 0u64,
@ -113,8 +113,12 @@ fn create_data_matrix(width: usize,
for y in 0..height {
let pos_x = x as u32;
let pos_y = y as u32;
<<<<<<< HEAD
data_matrix[x].push(image.get_pixel(pos_x, pos_y)
.channels()[0] as f64);
=======
data_matrix[x].push(image.get_pixel(pos_x, pos_y).channels()[0] as f64);
>>>>>>> master
}
}

25
src/lib.rs

@ -25,9 +25,15 @@ pub struct PIHash<'a> {
impl<'a> PIHash<'a> {
/**
<<<<<<< HEAD
* 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
*/
>>>>>>> master
pub fn new(cache_path: Option<&'a str>) -> PIHash<'a> {
match cache_path {
Some(path) => {
@ -212,8 +218,8 @@ mod tests {
}
// println!("Name: {}", path.unwrap().path().display())
}
// Currently 12 images in the test imaages directory
assert!(num_paths == 12);
// Currently 12 images in the test images directory
assert_eq!(num_paths, 12);
}
/**
@ -234,7 +240,7 @@ mod tests {
image_path.to_str().unwrap(),
image_hashes[index],
calculated_hash);
assert!(calculated_hash == image_hashes[index]);
assert_eq!(calculated_hash, image_hashes[index]);
hashes[index] = calculated_hash;
}
@ -244,8 +250,7 @@ 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,
@ -291,9 +296,8 @@ mod tests {
let sample_03_images: [&Path; 3] = [&Path::new("./test_images/sample_03_large.jpg"),
&Path::new("./test_images/sample_03_medium.jpg"),
&Path::new("./test_images/sample_03_small.jpg")];
let sample_03_hashes: [u64; 3] = [135670932300497406,
135670932300497406,
135670932300497406];
let sample_03_hashes: [u64; 3] =
[135670932300497406, 135670932300497406, 135670932300497406];
test_imageset_hash(hash::HashType::AHash,
hash::Precision::Medium,
1u64,
@ -356,9 +360,8 @@ mod tests {
let sample_03_images: [&Path; 3] = [&Path::new("./test_images/sample_03_large.jpg"),
&Path::new("./test_images/sample_03_medium.jpg"),
&Path::new("./test_images/sample_03_small.jpg")];
let sample_03_hashes: [u64; 3] = [262683193365159876,
225528496439353284,
225528496435158982];
let sample_03_hashes: [u64; 3] =
[262683193365159876, 225528496439353284, 225528496435158982];
test_imageset_hash(hash::HashType::DHash,
hash::Precision::Medium,
4u64,

6
src/main.rs

@ -63,8 +63,10 @@ fn main() {
let mut comparison_hashes: Vec<pihash::hash::PerceptualHashes> = Vec::new();
for index in 0..args.arg_comparison.len() {
comparison_hashes.push(get_requested_perceptual_hashes(&lib,
&Path::new(&args.arg_comparison[index]), &args));
comparison_hashes
.push(get_requested_perceptual_hashes(&lib,
&Path::new(&args.arg_comparison[index]),
&args));
}
let mut similar_images: Vec<&str> = Vec::new();

Loading…
Cancel
Save