Browse Source

Code cleanup

* Whitespace removal
* Followed IDE recommended simplifications
develop
Drew Short 7 years ago
parent
commit
bc90d6a7bf
  1. 12
      src/cache.rs
  2. 10
      src/hash/mod.rs
  3. 42
      src/hash/phash.rs
  4. 4
      src/lib.rs
  5. 7
      src/main.rs

12
src/cache.rs

@ -134,9 +134,9 @@ impl<'a> Cache<'a> {
* Get the hash of the desired file and return it as a hex string
*/
pub fn get_file_hash(&self, path: &Path) -> Result<String, Error> {
let mut source = try!(File::open(&path));
let mut source = File::open(&path)?;
let mut buf: Vec<u8> = Vec::new();
try!(source.read_to_end(&mut buf));
source.read_to_end(&mut buf)?;
let mut sha1 = Sha1::new();
sha1.update(&buf);
let digest = sha1.digest();
@ -261,7 +261,7 @@ impl<'a> Cache<'a> {
let desire_len = row_str.len() - 1;
row_str.truncate(desire_len);
row_str.push_str("\n");
try!(compressor.write(&row_str.into_bytes()));
compressor.write(&row_str.into_bytes())?;
}
let compressed_matrix = match compressor.finish() {
Ok(data) => data,
@ -270,8 +270,8 @@ impl<'a> Cache<'a> {
return Err(e);
}
};
try!(file.write(&compressed_matrix));
try!(file.flush());
file.write(&compressed_matrix)?;
file.flush()?;
}
Err(e) => {
return Err(e);
@ -355,7 +355,7 @@ fn test_get_file_hash() {
match hash {
Ok(v) => {
println!("Hash: {}", v);
assert!(v == "4beb6f2d852b75a313863916a1803ebad13a3196");
assert_eq!(v, "4beb6f2d852b75a313863916a1803ebad13a3196");
}
Err(e) => {
println!("Error: {:?}", e);

10
src/hash/mod.rs

@ -116,7 +116,7 @@ pub trait PerceptualHash {
// Functions //
/**
* Resonsible for parsing a path, converting an image and package it to be
* Responsible for parsing a path, converting an image and package it to be
* hashed.
*
* # Arguments
@ -187,7 +187,7 @@ fn process_image<'a>(image_path: &'a str, size: u32) -> PreparedImage<'a> {
};
PreparedImage {
orig_path: &*image_path,
image: image,
image,
}
}
@ -219,9 +219,9 @@ pub fn get_perceptual_hashes<'a>(path: &'a Path,
let phash = phash::PHash::new(&path, &precision, &cache).get_hash(&cache);
PerceptualHashes {
orig_path: &*image_path,
ahash: ahash,
dhash: dhash,
phash: phash,
ahash,
dhash,
phash,
}
}

42
src/hash/phash.rs

@ -116,11 +116,11 @@ fn create_data_matrix(width: usize,
data_matrix
}
// Use a 1D DFT to cacluate the 2D DFT.
// Use a 1D DFT to calculate the 2D DFT.
//
// This is achieved by calculating the DFT for each row, then calculating the
// DFT for each column of DFT row data. This means that a 32x32 image with have
// 1024 1D DFT operations performed on it. (Slightly caclulation intensive)
// 1024 1D DFT operations performed on it. (Slightly calculation intensive)
//
// This operation is in place on the data in the provided vector
//
@ -204,23 +204,23 @@ fn test_2d_dft() {
println!("{:?}", test_matrix[2]);
println!("{:?}", test_matrix[3]);
assert!(test_matrix[0][0] == 24_f64);
assert!(test_matrix[0][1] == 0_f64);
assert!(test_matrix[0][2] == 0_f64);
assert!(test_matrix[0][3] == 0_f64);
assert!(test_matrix[1][0] == 0_f64);
assert!(test_matrix[1][1] == 0_f64);
assert!(test_matrix[1][2] == -2_f64);
assert!(test_matrix[1][3] == 2_f64);
assert!(test_matrix[2][0] == 0_f64);
assert!(test_matrix[2][1] == -2_f64);
assert!(test_matrix[2][2] == -4_f64);
assert!(test_matrix[2][3] == -2_f64);
assert!(test_matrix[3][0] == 0_f64);
assert!(test_matrix[3][1] == 2_f64);
assert!(test_matrix[3][2] == -2_f64);
assert!(test_matrix[3][3] == 0_f64);
assert_eq!(test_matrix[0][0], 24_f64);
assert_eq!(test_matrix[0][1], 0_f64);
assert_eq!(test_matrix[0][2], 0_f64);
assert_eq!(test_matrix[0][3], 0_f64);
assert_eq!(test_matrix[1][0], 0_f64);
assert_eq!(test_matrix[1][1], 0_f64);
assert_eq!(test_matrix[1][2], -2_f64);
assert_eq!(test_matrix[1][3], 2_f64);
assert_eq!(test_matrix[2][0], 0_f64);
assert_eq!(test_matrix[2][1], -2_f64);
assert_eq!(test_matrix[2][2], -4_f64);
assert_eq!(test_matrix[2][3], -2_f64);
assert_eq!(test_matrix[3][0], 0_f64);
assert_eq!(test_matrix[3][1], 2_f64);
assert_eq!(test_matrix[3][2], -2_f64);
assert_eq!(test_matrix[3][3], 0_f64);
}

4
src/lib.rs

@ -108,7 +108,6 @@ impl<'a> PIHash<'a> {
second: &Path,
sensitivity: Option<u64>)
-> bool {
let threshold = match sensitivity {
Some(value) => value,
None => 4,
@ -229,7 +228,6 @@ fn to_hex_string(bytes: &[u8]) -> String {
//
#[cfg(test)]
mod tests {
use std::fs;
use std::path::Path;
use hash;
@ -530,7 +528,7 @@ mod tests {
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
// All phashes so that the matrices are pulled from cache as well
lib.get_perceptual_hash(&Path::new("./test_images/sample_01_large.jpg"),
&hash::Precision::Medium,
&hash::HashType::PHash);

7
src/main.rs

@ -82,7 +82,6 @@ fn main() {
for similar_image in similar_images {
println!("{}", similar_image);
}
} else {
let image_path = Path::new(&args.arg_path);
let hashes = get_requested_perceptual_hashes(&lib, &image_path, &args);
@ -129,8 +128,8 @@ fn get_requested_perceptual_hashes<'a>(lib: &pihash::PIHash,
pihash::hash::PerceptualHashes {
orig_path: image_path.to_str().unwrap(),
ahash: ahash,
dhash: dhash,
phash: phash,
ahash,
dhash,
phash,
}
}
Loading…
Cancel
Save