From c50b3e27de64efe52c549dcf13e0cb4e0143469c Mon Sep 17 00:00:00 2001 From: gentoo90 Date: Tue, 16 May 2017 16:43:08 +0300 Subject: [PATCH] Add `compare_images` method --- src/lib.rs | 1 + src/wand/magick.rs | 16 ++++++++++++++++ tests/lib.rs | 19 ++++++++++++++++++- 3 files changed, 35 insertions(+), 1 deletion(-) diff --git a/src/lib.rs b/src/lib.rs index 49974ff..2f8514d 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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"))] diff --git a/src/wand/magick.rs b/src/wand/magick.rs index aed6612..a7a5a87 100644 --- a/src/wand/magick.rs +++ b/src/wand/magick.rs @@ -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) { + 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 { diff --git a/tests/lib.rs b/tests/lib.rs index 99de8a7..3f37d6f 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -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(|| {