Add function_image, polynomial_image functions

This commit is contained in:
5ohue
2024-05-11 23:30:41 +03:00
parent 24c2a3d2de
commit 5eb991ba69
3 changed files with 92 additions and 1 deletions

View File

@ -0,0 +1,23 @@
use crate::bindings;
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum MagickFunction {
Undefined = bindings::MagickFunction_UndefinedFunction,
Arcsin = bindings::MagickFunction_ArcsinFunction,
Arctan = bindings::MagickFunction_ArctanFunction,
Polynomial = bindings::MagickFunction_PolynomialFunction,
Sinusoid = bindings::MagickFunction_SinusoidFunction,
}
impl Default for MagickFunction {
fn default() -> Self {
return MagickFunction::Undefined;
}
}
impl From<MagickFunction> for bindings::MagickFunction {
fn from(value: MagickFunction) -> Self {
return value as bindings::MagickFunction;
}
}

View File

@ -16,6 +16,7 @@ mod image_type;
mod interlace_type; mod interlace_type;
mod line_cap; mod line_cap;
mod line_join; mod line_join;
mod magick_function;
mod metric_type; mod metric_type;
mod orientation_type; mod orientation_type;
mod pixel_interpolate_method; mod pixel_interpolate_method;
@ -44,6 +45,7 @@ pub use self::image_type::ImageType;
pub use self::interlace_type::InterlaceType; pub use self::interlace_type::InterlaceType;
pub use self::line_cap::LineCap; pub use self::line_cap::LineCap;
pub use self::line_join::LineJoin; pub use self::line_join::LineJoin;
pub use self::magick_function::MagickFunction;
pub use self::metric_type::MetricType; pub use self::metric_type::MetricType;
pub use self::orientation_type::OrientationType; pub use self::orientation_type::OrientationType;
pub use self::pixel_interpolate_method::PixelInterpolateMethod; pub use self::pixel_interpolate_method::PixelInterpolateMethod;

View File

@ -42,6 +42,7 @@ use crate::{
GravityType, GravityType,
ImageType, ImageType,
InterlaceType, InterlaceType,
MagickFunction,
MetricType, MetricType,
OrientationType, OrientationType,
PixelInterpolateMethod, PixelInterpolateMethod,
@ -698,7 +699,7 @@ impl MagickWand {
/// ///
/// fn main() -> Result<(), magick_rust::MagickError> { /// fn main() -> Result<(), magick_rust::MagickError> {
/// let mut wand1 = MagickWand::new(); /// let mut wand1 = MagickWand::new();
/// wand1.new_image(4, 4, &PixelWand::new())?; /// wand1.new_image(4, 4, &PixelWand::new())?; // Replace with `read_image` to open your image file
/// let wand2 = wand1.clone(); /// let wand2 = wand1.clone();
/// ///
/// wand1.median_blur_image(10, 10)?; /// wand1.median_blur_image(10, 10)?;
@ -1341,6 +1342,71 @@ impl MagickWand {
} }
} }
/// Applies an arithmetic, relational, or logical expression to an image. Use these operators
/// to lighten or darken an image, to increase or decrease contrast in an image, or to produce
/// the "negative" of an image.
///
/// * `function`: the image function.
/// * `args`: the function arguments.
///
/// # Example
///
/// This example show how you can apply smoothstep function (a polynomial `-2x^3 + 3x^2`) to
/// every image pixel.
///
/// ```
/// use magick_rust::{MagickWand, PixelWand, MagickFunction};
///
/// fn main() -> Result<(), magick_rust::MagickError> {
/// let mut wand1 = MagickWand::new();
/// wand1.new_image(4, 4, &PixelWand::new())?; // Replace with `read_image` to open your image file
///
/// // Apply smoothstep polynomial
/// wand1.function_image(MagickFunction::Polynomial, &[-2.0, 3.0, 0.0, 0.0])?;
///
/// Ok(())
/// }
/// ```
pub fn function_image(
&self,
function: MagickFunction,
args: &[f64]
) -> Result<()> {
let num_of_args: size_t = args.len().into();
match unsafe {
bindings::MagickFunctionImage(
self.wand,
function.into(),
num_of_args,
args.as_ptr()
)
} {
MagickTrue => Ok(()),
_ => Err(MagickError("failed to apply function to image")),
}
}
/// Returns an image where each pixel is the sum of the pixels in the image sequence after
/// applying its corresponding terms (coefficient and degree pairs).
///
/// * `terms`: the list of polynomial coefficients and degree pairs and a constant.
pub fn polynomial_image(&self, terms: &[f64]) -> Result<()> {
if terms.len() & 1 != 1 {
return Err(MagickError("no constant coefficient given"));
}
let num_of_terms: size_t = (terms.len() >> 1).into();
match unsafe {
bindings::MagickPolynomialImage(
self.wand,
num_of_terms,
terms.as_ptr()
)
} {
MagickTrue => Ok(()),
_ => Err(MagickError("failed to apply polynomial to image")),
}
}
mutations!( mutations!(
/// Sets the image to the specified alpha level. /// Sets the image to the specified alpha level.
MagickSetImageAlpha => set_image_alpha(alpha: f64) MagickSetImageAlpha => set_image_alpha(alpha: f64)