diff --git a/src/lib.rs b/src/lib.rs index a14913a..cbe2dbf 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -36,7 +36,7 @@ use libc::size_t; #[cfg(not(target_os = "freebsd"))] use libc::ssize_t; -pub use bindings::{ColorspaceType, DitherMethod, FilterType, GravityType, MetricType}; +pub use bindings::{ColorspaceType, DitherMethod, FilterType, GravityType}; pub use conversions::ToMagick; pub use result::MagickError; use result::Result; diff --git a/src/types/metric_type.rs b/src/types/metric_type.rs new file mode 100644 index 0000000..9d49154 --- /dev/null +++ b/src/types/metric_type.rs @@ -0,0 +1,29 @@ +use crate::bindings; + +pub enum MetricType { + Undefined = bindings::MetricType_UndefinedErrorMetric as isize, + Absolute = bindings::MetricType_AbsoluteErrorMetric as isize, + Fuzz = bindings::MetricType_FuzzErrorMetric as isize, + MeanAbsolute = bindings::MetricType_MeanAbsoluteErrorMetric as isize, + MeanErrorPerPixel = bindings::MetricType_MeanErrorPerPixelErrorMetric as isize, + MeanSquared = bindings::MetricType_MeanSquaredErrorMetric as isize, + NormalizedCrossCorrelation = bindings::MetricType_NormalizedCrossCorrelationErrorMetric as isize, + PeakAbsolute = bindings::MetricType_PeakAbsoluteErrorMetric as isize, + PeakSignalToNoiseRatio = bindings::MetricType_PeakSignalToNoiseRatioErrorMetric as isize, + PerceptualHash = bindings::MetricType_PerceptualHashErrorMetric as isize, + RootMeanSquared = bindings::MetricType_RootMeanSquaredErrorMetric as isize, + StructuralSimilarity = bindings::MetricType_StructuralSimilarityErrorMetric as isize, + StructuralDissimilarity = bindings::MetricType_StructuralDissimilarityErrorMetric as isize, +} + +impl Default for MetricType { + fn default() -> Self { + return MetricType::Absolute; + } +} + +impl From for bindings::MetricType { + fn from(value: MetricType) -> Self { + return value as bindings::MetricType; + } +} diff --git a/src/wand/magick.rs b/src/wand/magick.rs index 2e5991e..007fbe9 100644 --- a/src/wand/magick.rs +++ b/src/wand/magick.rs @@ -29,7 +29,7 @@ use {size_t, ssize_t}; use crate::result::Result; use super::{DrawingWand, PixelWand}; -use crate::{CompositeOperator, ResourceType}; +use crate::{CompositeOperator, MetricType, ResourceType}; wand_common!( MagickWand, @@ -202,11 +202,11 @@ impl MagickWand { pub fn compare_images( &self, reference: &MagickWand, - metric: bindings::MetricType, + metric: MetricType, ) -> (f64, Option) { let mut distortion: f64 = 0.0; let result = unsafe { - bindings::MagickCompareImages(self.wand, reference.wand, metric, &mut distortion) + bindings::MagickCompareImages(self.wand, reference.wand, metric.into(), &mut distortion) }; let wand = if result.is_null() { None diff --git a/tests/lib.rs b/tests/lib.rs index 7747526..1e12294 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -22,7 +22,7 @@ use std::io::Read; use std::path::Path; use std::sync::Once; -use magick_rust::{bindings, magick_wand_genesis, MagickWand, PixelWand}; +use magick_rust::{bindings, magick_wand_genesis, CompositeOperator, MagickWand, PixelWand}; use magick_rust::{MagickError, ToMagick}; // Used to make sure MagickWand is initialized exactly once. Note that we @@ -209,7 +209,7 @@ fn test_compare_images() { wand2.auto_orient(); let (distortion, diff) = - wand1.compare_images(&wand2, bindings::MetricType_RootMeanSquaredErrorMetric); + wand1.compare_images(&wand2, magick_rust::MetricType::RootMeanSquared); assert!(distortion < 0.01); assert!(diff.is_some()); } @@ -417,6 +417,48 @@ fn test_auto_gamma() { assert!(wand.auto_gamma().is_ok()); } +#[test] +fn test_image_compose() { + START.call_once(|| { + magick_wand_genesis(); + }); + let wand = MagickWand::new(); + wand.new_image(4, 4, &PixelWand::new()).unwrap(); + + let operators = [ + CompositeOperator::Alpha, + CompositeOperator::MinusDst, + CompositeOperator::Over, + CompositeOperator::Xor, + CompositeOperator::Bumpmap, + CompositeOperator::ChangeMask, + CompositeOperator::Clear, + CompositeOperator::ColorBurn, + CompositeOperator::ColorDodge, + CompositeOperator::Colorize, + CompositeOperator::CopyBlack, + CompositeOperator::CopyBlue, + CompositeOperator::Copy, + CompositeOperator::CopyCyan, + CompositeOperator::CopyGreen, + CompositeOperator::CopyMagenta, + CompositeOperator::CopyAlpha, + CompositeOperator::CopyRed, + CompositeOperator::CopyYellow, + CompositeOperator::Darken, + CompositeOperator::DarkenIntensity, + CompositeOperator::Difference, + CompositeOperator::Displace, + CompositeOperator::Dissolve, + CompositeOperator::Distort, + CompositeOperator::DivideDst, + ]; + for op in operators.iter() { + wand.set_image_compose(*op).unwrap(); + assert_eq!(*op, wand.get_image_compose()); + } +} + #[test] fn test_import_export_pixels_roundtrip() { START.call_once(|| {