Merge pull request #25 from gentoo90/compare

Add `compare_images` method
This commit is contained in:
Nathan Fiedler 2017-05-16 10:08:58 -07:00 committed by GitHub
commit cba0de65fa
3 changed files with 35 additions and 1 deletions

View file

@ -37,6 +37,7 @@ mod conversions;
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
pub use wand::*;
pub use bindings::MetricType;
use libc::size_t;
#[cfg(not(target_os = "freebsd"))]

View file

@ -122,6 +122,22 @@ impl MagickWand {
}
}
/// Compare two images and return tuple `(distortion, diffImage)`
/// `diffImage` is `None` if `distortion == 0`
pub fn compare_images(&self, reference: &MagickWand, metric: bindings::MetricType) -> (f64, Option<MagickWand>) {
let mut distortion: f64 = 0.0;
let result = unsafe {
bindings::MagickCompareImages(self.wand, reference.wand, metric, &mut distortion)
};
let wand = if result.is_null() {
None
}
else {
Some(MagickWand { wand: result })
};
(distortion, wand)
}
/// Retrieve the width of the image.
pub fn get_image_width(&self) -> usize {
unsafe {

View file

@ -16,7 +16,7 @@
extern crate magick_rust;
use magick_rust::{MagickWand, magick_wand_genesis};
use magick_rust::{MagickWand, magick_wand_genesis, MetricType};
use std::error::Error;
use std::fs::File;
@ -177,6 +177,23 @@ fn test_auto_orient() {
assert_eq!(false, wand.requires_orientation());
}
#[test]
fn test_compare_images() {
START.call_once(|| {
magick_wand_genesis();
});
let wand1 = MagickWand::new();
assert!(wand1.read_image("tests/data/IMG_5745.JPG").is_ok());
let wand2 = MagickWand::new();
assert!(wand2.read_image("tests/data/IMG_5745_rotl.JPG").is_ok());
wand2.auto_orient();
let (distortion, diff) = wand1.compare_images(&wand2, MetricType::RootMeanSquaredErrorMetric);
assert!(distortion < 0.01);
assert!(diff.is_some());
}
#[test]
fn test_set_option() {
START.call_once(|| {