use error::RasterResult;
use Image;
use editor::{self, ResizeMode};
pub fn similar(image1: &Image, image2: &Image) -> RasterResult<u8> {
let bin1 = try!(diff_hash(image1));
let bin2 = try!(diff_hash(image2));
let mut distance = 0;
for (index, value) in bin1.iter().enumerate() {
if value != &bin2[index] {
distance += 1;
}
}
Ok(distance)
}
pub fn equal(image1: &Image, image2: &Image)-> RasterResult<bool> {
if image1.width != image2.width || image1.height != image2.height {
Ok(false)
} else {
for y in 0..image1.height {
for x in 0..image1.width {
let pixel1 = try!(image1.get_pixel(x, y));
let pixel2 = try!(image2.get_pixel(x, y));
if
pixel1.r != pixel2.r ||
pixel1.g != pixel2.g ||
pixel1.b != pixel2.b
{
return Ok(false);
}
}
}
Ok(true)
}
}
fn diff_hash(image: &Image) -> RasterResult<Vec<u8>> {
let width = 9;
let height = 8;
let mut image = image.clone(); try!(editor::resize(&mut image, width, height, ResizeMode::Exact));
let mut hash = Vec::new();
for y in 0..height {
let pixel = try!(image.get_pixel(0, y));
let mut left = ((pixel.r as f32 + pixel.g as f32 + pixel.b as f32) / 3.0).floor();
for x in 1..width {
let pixel = try!(image.get_pixel(x, y));
let right = ((pixel.r as f32 + pixel.g as f32 + pixel.b as f32) / 3.0).floor();
if left > right {
hash.push(1);
} else {
hash.push(0);
}
left = right;
}
}
Ok(hash)
}