Browse Source

Project warnings cleanup

Cache filling methods follow standard and return a Result<bool,Error>
The methods to place the data in the cache from the hash source are wrapped in a match statement.
develop
Drew Short 9 years ago
parent
commit
7fe8fa06e7
  1. 62
      src/cache.rs
  2. 25
      src/hash.rs

62
src/cache.rs

@ -47,7 +47,10 @@ fn get_file_hash(path: &Path) -> Result<String, Error> {
/**
* Put an image buffer in the cache
*/
pub fn put_image_in_cache(path: &Path, size: u32, image: &ImageBuffer<image::Luma<u8>, Vec<u8>>) {
pub fn put_image_in_cache(path: &Path,
size: u32,
image: &ImageBuffer<image::Luma<u8>, Vec<u8>>)
-> Result<bool, Error> {
let hash = get_file_hash(&path);
match hash {
Ok(sha1) => {
@ -61,11 +64,18 @@ pub fn put_image_in_cache(path: &Path, size: u32, image: &ImageBuffer<image::Lum
// Save the file into the cache
match image.save(cached_path) {
Ok(_) => {}
Err(e) => println!("Error: {}", e),
Err(e) => {
println!("Error: {}", e);
return Err(e);
}
}
Err(e) => println!("Error: {}", e),
}
Err(e) => {
println!("Error: {}", e);
return Err(e);
}
}
Ok(true)
}
/**
@ -74,7 +84,8 @@ pub fn put_image_in_cache(path: &Path, size: u32, image: &ImageBuffer<image::Lum
pub fn put_matrix_in_cache(path: &Path,
size: u32,
extension: &str,
file_contents: &Vec<Vec<f64>>) {
file_contents: &Vec<Vec<f64>>)
-> Result<bool, Error> {
let hash = get_file_hash(&path);
match hash {
Ok(sha1) => {
@ -91,20 +102,29 @@ pub fn put_matrix_in_cache(path: &Path,
let desire_len = row_str.len() - 1;
row_str.truncate(desire_len);
row_str.push_str("\n");
compressor.write(&row_str.into_bytes());
try!(compressor.write(&row_str.into_bytes()));
}
let compressed_matrix = match compressor.finish() {
Ok(data) => data,
Err(e) => { println!("Unable to compress matrix data: {}", e); return },
Err(e) => {
println!("Unable to compress matrix data: {}", e);
return Err(e);
}
};
file.write(&compressed_matrix);
file.flush();
try!(file.write(&compressed_matrix));
try!(file.flush());
}
Err(e) => {
return Err(e);
}
}
Err(_) => {}
}
Err(e) => {
println!("Error: {}", e);
return Err(e);
}
Err(e) => println!("Error: {}", e),
}
Ok(true)
}
/**
@ -154,18 +174,26 @@ pub fn get_matrix_from_cache(path: &Path, size: u32, extension: &str) -> Option<
let cached_path = Path::new(&cache_path_str);
// Try to open, if it does, then we can read the image in
match File::open(&cached_path) {
Ok(mut file) => {
let mut compressed_matrix_data: Vec<u8> = Vec::new();
Ok(file) => {
let mut decoder = ZlibDecoder::new(&file);
let mut matrix_data_str = String::new();
match decoder.read_to_string(&mut matrix_data_str) {
Ok(_) => {},
Err(e) => { println!("Unable to decompress matrix: {}",e); return None }
Ok(_) => {}
Err(e) => {
println!("Unable to decompress matrix: {}", e);
return None;
}
};
// convert the matrix
let matrix: Vec<Vec<f64>> = matrix_data_str.trim().split("\n")
.map(|line| line.split(",")
.map(|f| f64::from_str(f).unwrap()).collect())
let matrix: Vec<Vec<f64>> = matrix_data_str.trim()
.split("\n")
.map(|line| {
line.split(",")
.map(|f| {
f64::from_str(f).unwrap()
})
.collect()
})
.collect();
Some(matrix)

25
src/hash.rs

@ -119,7 +119,10 @@ pub fn prepare_image<'a>(path: &'a Path,
let image = image::open(path).unwrap();
let small_image = image.resize_exact(size, size, FilterType::Lanczos3);
let grey_image = small_image.to_luma();
cache::put_image_in_cache(&path, size, &grey_image);
match cache::put_image_in_cache(&path, size, &grey_image) {
Ok(_) => {}
Err(e) => println!("Unable to store image in cache. {}", e),
};
PreparedImage {
orig_path: &*image_path,
image: grey_image,
@ -298,28 +301,36 @@ impl<'a> PerceptualHash for PHash<'a> {
// Pretty fast already, so caching doesn't make a huge difference
// Atleast compared to opening and processing the images
let mut data_matrix: Vec<Vec<f64>> = Vec::new();
match cache::get_matrix_from_cache(&Path::new(self.prepared_image.orig_path), width as u32, &"dft") {
match cache::get_matrix_from_cache(&Path::new(self.prepared_image.orig_path),
width as u32,
&"dft") {
Some(matrix) => data_matrix = matrix,
None => {
//Preparing the results
// Preparing the results
for x in 0..width {
data_matrix.push(Vec::new());
for y in 0..height {
let pos_x = x as u32;
let pos_y = y as u32;
data_matrix[x]
.push(self.prepared_image.image.get_pixel(pos_x, pos_y).channels()[0] as f64);
.push(self.prepared_image
.image
.get_pixel(pos_x, pos_y)
.channels()[0] as f64);
}
}
// Perform the 2D DFT operation on our matrix
calculate_2d_dft(&mut data_matrix);
// Store this DFT in the cache
cache::put_matrix_in_cache(&Path::new(self.prepared_image.orig_path),
match cache::put_matrix_in_cache(&Path::new(self.prepared_image.orig_path),
width as u32,
&"dft",
&data_matrix);
},
&data_matrix) {
Ok(_) => {}
Err(e) => println!("Unable to store matrix in cache. {}", e),
};
}
}
// Only need the top left quadrant

Loading…
Cancel
Save