Merge pull request #25 from gentoo90/compare
Add `compare_images` method
This commit is contained in:
@ -37,6 +37,7 @@ mod conversions;
|
|||||||
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
include!(concat!(env!("OUT_DIR"), "/bindings.rs"));
|
||||||
|
|
||||||
pub use wand::*;
|
pub use wand::*;
|
||||||
|
pub use bindings::MetricType;
|
||||||
|
|
||||||
use libc::size_t;
|
use libc::size_t;
|
||||||
#[cfg(not(target_os = "freebsd"))]
|
#[cfg(not(target_os = "freebsd"))]
|
||||||
|
|||||||
@ -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.
|
/// Retrieve the width of the image.
|
||||||
pub fn get_image_width(&self) -> usize {
|
pub fn get_image_width(&self) -> usize {
|
||||||
unsafe {
|
unsafe {
|
||||||
|
|||||||
19
tests/lib.rs
19
tests/lib.rs
@ -16,7 +16,7 @@
|
|||||||
|
|
||||||
extern crate magick_rust;
|
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::error::Error;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
@ -177,6 +177,23 @@ fn test_auto_orient() {
|
|||||||
assert_eq!(false, wand.requires_orientation());
|
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]
|
#[test]
|
||||||
fn test_set_option() {
|
fn test_set_option() {
|
||||||
START.call_once(|| {
|
START.call_once(|| {
|
||||||
|
|||||||
Reference in New Issue
Block a user