From 092acaa5d070fbbda27ce41aa06204673eff42ca Mon Sep 17 00:00:00 2001 From: 5ohue <86558263+5ohue@users.noreply.github.com> Date: Sat, 11 May 2024 13:37:55 +0300 Subject: [PATCH] Add `AlphaChannelOption` type --- src/types/alpha_channel_option.rs | 52 +++++++++++++++++++++++++++++++ src/types/mod.rs | 2 ++ src/wand/macros.rs | 2 +- src/wand/magick.rs | 4 +-- tests/lib.rs | 2 +- 5 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 src/types/alpha_channel_option.rs diff --git a/src/types/alpha_channel_option.rs b/src/types/alpha_channel_option.rs new file mode 100644 index 0000000..51d8551 --- /dev/null +++ b/src/types/alpha_channel_option.rs @@ -0,0 +1,52 @@ +use crate::bindings; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +#[repr(u32)] +pub enum AlphaChannelOption { + Undefined = bindings::AlphaChannelOption_UndefinedAlphaChannel, + Activate = bindings::AlphaChannelOption_ActivateAlphaChannel, + Associate = bindings::AlphaChannelOption_AssociateAlphaChannel, + Background = bindings::AlphaChannelOption_BackgroundAlphaChannel, + Copy = bindings::AlphaChannelOption_CopyAlphaChannel, + Deactivate = bindings::AlphaChannelOption_DeactivateAlphaChannel, + Discrete = bindings::AlphaChannelOption_DiscreteAlphaChannel, + Disassociate = bindings::AlphaChannelOption_DisassociateAlphaChannel, + Extract = bindings::AlphaChannelOption_ExtractAlphaChannel, + Off = bindings::AlphaChannelOption_OffAlphaChannel, + On = bindings::AlphaChannelOption_OnAlphaChannel, + Opaque = bindings::AlphaChannelOption_OpaqueAlphaChannel, + Remove = bindings::AlphaChannelOption_RemoveAlphaChannel, + Set = bindings::AlphaChannelOption_SetAlphaChannel, + Shape = bindings::AlphaChannelOption_ShapeAlphaChannel, + Transparent = bindings::AlphaChannelOption_TransparentAlphaChannel, + OffIfOpaque = bindings::AlphaChannelOption_OffIfOpaqueAlphaChannel, +} + +impl Default for AlphaChannelOption { + fn default() -> Self { + return AlphaChannelOption::Undefined; + } +} + +impl From for bindings::AlphaChannelOption { + fn from(value: AlphaChannelOption) -> Self { + return value as bindings::AlphaChannelOption; + } +} + +impl From for AlphaChannelOption { + fn from(value: bindings::AlphaChannelOption) -> Self { + /* + * SAFETY: + * + * `AlphaChannelOption` has the same repr as `bindings::AlphaChannelOption` - u32 + * + * If `value` is less than OffIfOpaque than it is in the vaild range and can be safely + * reinterpreted as `AlphaChannelOption` + */ + if value <= bindings::AlphaChannelOption_OffIfOpaqueAlphaChannel { + return unsafe { std::mem::transmute(value) }; + } + return AlphaChannelOption::default(); + } +} diff --git a/src/types/mod.rs b/src/types/mod.rs index 476bcba..7a652b0 100644 --- a/src/types/mod.rs +++ b/src/types/mod.rs @@ -1,3 +1,4 @@ +mod alpha_channel_option; mod colorspace_type; mod composite_operator; mod dither_method; @@ -7,6 +8,7 @@ mod pixel_interpolate_method; mod metric_type; mod resource_type; +pub use self::alpha_channel_option::AlphaChannelOption; pub use self::colorspace_type::ColorspaceType; pub use self::composite_operator::CompositeOperator; pub use self::dither_method::DitherMethod; diff --git a/src/wand/macros.rs b/src/wand/macros.rs index 8a1e2bf..f7006f5 100644 --- a/src/wand/macros.rs +++ b/src/wand/macros.rs @@ -258,7 +258,7 @@ macro_rules! mutations { $( $(#[$attr])* pub fn $fun(&self $(, $arg: $ty)*) -> Result<()> { - match unsafe { bindings::$c_fun(self.wand $(, $arg)*) } { + match unsafe { bindings::$c_fun(self.wand $(, $arg.into())*) } { bindings::MagickBooleanType_MagickTrue => Ok(()), _ => Err(MagickError(concat!(stringify!($c_fun), " invocation failed"))) } diff --git a/src/wand/magick.rs b/src/wand/magick.rs index 35af147..56b8567 100644 --- a/src/wand/magick.rs +++ b/src/wand/magick.rs @@ -30,6 +30,7 @@ use crate::result::Result; use super::{DrawingWand, PixelWand}; use crate::{ + AlphaChannelOption, ColorspaceType, CompositeOperator, DitherMethod, @@ -1142,8 +1143,7 @@ impl MagickWand { MagickBrightnessContrastImage => brightness_contrast_image(brightness: f64, contrast: f64) /// Set the image alpha channel mode. - MagickSetImageAlphaChannel => set_image_alpha_channel( - alpha_channel: bindings::AlphaChannelOption) + MagickSetImageAlphaChannel => set_image_alpha_channel(alpha_channel: AlphaChannelOption) /// Discard all but one of any pixel color. MagickUniqueImageColors => unique_image_colors() diff --git a/tests/lib.rs b/tests/lib.rs index 6d1708f..2e2440d 100644 --- a/tests/lib.rs +++ b/tests/lib.rs @@ -317,7 +317,7 @@ fn test_set_image_background_color() { let mut pw = PixelWand::new(); pw.set_color("#0000FF").unwrap(); wand.set_image_background_color(&pw).unwrap(); - wand.set_image_alpha_channel(bindings::AlphaChannelOption_RemoveAlphaChannel) + wand.set_image_alpha_channel(magick_rust::AlphaChannelOption::Remove) .unwrap(); let blob = wand.write_image_blob("rgb").unwrap(); assert_eq!(0u8, blob[0]);