Trait DynamicChops

Source
pub trait DynamicChops {
    // Required methods
    fn blend(
        &mut self,
        other: &Self,
        op: fn(f64, f64) -> f64,
        apply_to_color: bool,
        apply_to_alpha: bool,
    ) -> Result<(), Error>;
    fn get_alpha(&self) -> Option<Self>
       where Self: Sized;
    fn transplant_alpha(&mut self, other: &Self) -> Result<(), Error>;
    fn set_alpha(&mut self, other: &Self) -> Result<(), Error>
       where Self: Sized;
    fn strip_alpha(&mut self) -> Result<(), Error>
       where Self: Sized;
}

Required Methods§

Source

fn blend( &mut self, other: &Self, op: fn(f64, f64) -> f64, apply_to_color: bool, apply_to_alpha: bool, ) -> Result<(), Error>

Blend other into self using the function op, where arg 0 is self and 1 is other.

Handles type conversion and alpha channel detection and placement automatically.

You may blend a luma image into an rgb image (in which case the luma image will be treated as a grayscale rgb image), but you cannot blend an rgba image into a luma image.

If other has an alpha channel, the output is weighted by this alpha channel (so if alpha for other for this pixel is 0.5, the blend effect will be 0.5 as strong)

§Arguments

Use apply_to_color and apply_to_alpha to control which channels are affected.

If apply_to_alpha is true but self or other does not have an alpha channel, this option has no effect.

op is a function that takes two f64 values and returns a f64 value. (e.g. |self, other| self + other)

Standard blend modes such as those found in photoshop are provided as functions (e.g. pixel_add, pixel_mult, etc.).

The values are normalized to the range 0.0..1.0 before blending, and then scaled back to the input type’s range.

The output from op is automatically clamped from 0.0..1.0 before being converted back to the input type so you don’t need to worry about overflow/underflow.

§Errors

DimensionMismatch: self and other have different dimensions

UnsupportedBlend: self is a luma image and other is an rgb image

§Examples
§Example 1:

Using the pixel_mult function to blend two images together:

use image::open;
use image_blend::DynamicChops;
use image_blend::pixelops::pixel_mult;

// Load an image
let mut img1_dynamic = open("test_data/1.png").unwrap();

// Load another image
let img2_dynamic = open("test_data/2.png").unwrap();

// Blend the images using the pixel_mult function
img1_dynamic.blend(&img2_dynamic, pixel_mult, true, false).unwrap();
img1_dynamic.save("tests_out/doctest_dynamic_blend_result.png").unwrap();
§Example 2:

Using a custom function to blend two images together:

use image::open;
use image_blend::DynamicChops;

let closest_to_gray = |a: f64, b: f64| {
    let a_diff = (a - 0.5).abs();
    let b_diff = (b - 0.5).abs();
    if a_diff < b_diff {
        a
    } else {
        b
    }
};

// Load an image
let mut img1_dynamic = open("test_data/1.png").unwrap();

// Load another image
let img2_dynamic = open("test_data/2.png").unwrap();

// Blend the images using our custom function
img1_dynamic.blend(&img2_dynamic, closest_to_gray, true, false).unwrap();
img1_dynamic.save("tests_out/doctest_dynamic_custom_result.png").unwrap();
Source

fn get_alpha(&self) -> Option<Self>
where Self: Sized,

Get the alpha channel of this image as a grayscale with the same number of channels as the input image. (i.e a 4 channel rgba image will return a 4 channel rgba grayscale image with the alpha channel set to the maximum value of the input type)

The alpha channel of the returned image is set to the maximum value of the input type.

If the image does not have an alpha channel, return None.

§Examples
use image::open;
use image_blend::DynamicChops;

// Load an image and get its alpha channel
let img1_dynamic = open("test_data/1.png").unwrap();
let img1_alpha = img1_dynamic.get_alpha().unwrap();
img1_alpha.clone().save("tests_out/doctest_dynamic_getalpha_alpha.png").unwrap();

// Load another image and set its alpha channel to the first image's alpha channel, using the copied alpha channel
let mut img2_dynamic = open("test_data/2.png").unwrap();
img2_dynamic.set_alpha(&img1_alpha).unwrap();
img2_dynamic.save("tests_out/doctest_dynamic_getalpha_result.png").unwrap();
Source

fn transplant_alpha(&mut self, other: &Self) -> Result<(), Error>

Set an image’s alpha channel from another images alpha channel.

Handles type conversion and alpha channel placement automatically.

§Errors

NoAlphaChannel: self or other does not have an alpha channel

DimensionMismatch: self and other have different dimensions

§Examples
use image::open;
use image_blend::DynamicChops;

// Load an image that has an alpha channel
let img1_dynamic = open("test_data/1.png").unwrap();

// Load another image and set its alpha channel to a copy of the first image's alpha channel.
let mut img2_dynamic = open("test_data/2.png").unwrap();
img2_dynamic.transplant_alpha(&img1_dynamic).unwrap();
img2_dynamic.save("tests_out/doctest_dynamic_transplantalpha_result.png").unwrap();
Source

fn set_alpha(&mut self, other: &Self) -> Result<(), Error>
where Self: Sized,

Set an image’s alpha channel using the grascale color of another image.

Handles type conversion and alpha channel detection and placement automatically.

WARNING: other can be of any type, but only the first channel will be used to set the alpha channel. In a grayscale image this will be the luma channel, in an rgb image the red channel. Consider converting to grayscale if this matters.

§Errors

NoAlphaChannel: self does not have an alpha channel

DimensionMismatch: self and other have different dimensions

§Examples
use image::open;
use image_blend::DynamicChops;

// Load an image and get its alpha channel
let img1_dynamic = open("test_data/1.png").unwrap();
let img1_alpha = img1_dynamic.get_alpha().unwrap();
img1_alpha.clone().save("tests_out/doctest_dynamic_setalpha_alpha.png").unwrap();

// Load another image and set its alpha channel to the first image's alpha channel, using the copied alpha channel
let mut img2_dynamic = open("test_data/2.png").unwrap();
img2_dynamic.set_alpha(&img1_alpha).unwrap();
img2_dynamic.save("tests_out/doctest_dynamic_setalpha_result.png").unwrap();
Source

fn strip_alpha(&mut self) -> Result<(), Error>
where Self: Sized,

Remove this images alpha channel by setting it to the maximum value for every pixel.

Does not modify the underlying type.

§Errors

NoAlphaChannel: self does not have an alpha channel

§Examples
use image::open;
use image_blend::{DynamicChops};

// Load an image and remove its alpha channel
let mut img2_dynamic = open("test_data/2.png").unwrap();
img2_dynamic.strip_alpha().unwrap();
img2_dynamic.save("tests_out/doctest_dynamic_stripalpha_result.png").unwrap();

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementations on Foreign Types§

Source§

impl DynamicChops for DynamicImage

Source§

fn blend( &mut self, other: &Self, op: fn(f64, f64) -> f64, apply_to_color: bool, apply_to_alpha: bool, ) -> Result<(), Error>

Source§

fn get_alpha(&self) -> Option<DynamicImage>

Source§

fn transplant_alpha(&mut self, other: &Self) -> Result<(), Error>

Source§

fn set_alpha(&mut self, other: &Self) -> Result<(), Error>

Source§

fn strip_alpha(&mut self) -> Result<(), Error>
where Self: Sized,

Implementors§