extern crate image;
use std::path::Path;
use cache::Cache;
use super::prepare_image;
use super::{HashType, PerceptualHash, Precision, PreparedImage};
use self::image::{GenericImage, GenericImageView};
pub struct AHash<'a> {
prepared_image: Box<PreparedImage<'a>>,
}
impl<'a> AHash<'a> {
pub fn new(path: &'a Path, precision: &Precision, cache: &Option<Cache>) -> Self {
AHash {
prepared_image: Box::new(prepare_image(&path, &HashType::AHash, &precision, cache)),
}
}
}
impl<'a> PerceptualHash for AHash<'a> {
fn get_hash(&self, _: &Option<Cache>) -> u64 {
match self.prepared_image.image {
Some(ref image) => {
let (width, height) = image.dimensions();
let mut total = 0u64;
for (_, _, pixel) in image.pixels() {
total += pixel.0[0] as u64;
}
let mean = total / (height * width) as u64;
let mut hash = 0u64;
for (_, _, pixel) in image.pixels() {
if pixel.0[0] as u64 >= mean {
hash |= 1;
} else {
hash |= 0;
}
hash <<= 1;
}
hash
}
None => 0u64,
}
}
}