Trait image_blend::dynamic_blend::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;
}

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.

If other has an alpha channel, it will be used to weight the blending of the color channels. If there is no alpha channel, the blending will be unweighted.

You may blend a luma image into an rgba 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.

§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, nothing will happen.

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_grey = |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_grey, 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 3 channel rgb image will return a 3 channel rgb grayscale image)

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 and get its 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 colour 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.

§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();

Object Safety§

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>

Implementors§