Trait BufferSetAlpha

Source
pub trait BufferSetAlpha<P, Container>
where P: Pixel, Container: Deref<Target = [P::Subpixel]> + AsRef<[P::Subpixel]>,
{ // Required methods fn set_alpha( &mut self, other: &ImageBuffer<P, Container>, ) -> Result<(), Error>; fn transplant_alpha( &mut self, other: &ImageBuffer<P, Container>, ) -> Result<(), Error>; }

Required Methods§

Source

fn set_alpha(&mut self, other: &ImageBuffer<P, Container>) -> Result<(), Error>

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. In a grayscale image this will be the luma channel, in an rgb image the red channel.

§Errors

NoAlphaChannel: self does not have an alpha channel

DimensionMismatch: self and other have different dimensions

§Examples
use image::open;
use image_blend::{BufferGetAlpha, BufferSetAlpha};

// Load an image and get its alpha channel
let img1_dynamic = open("test_data/1.png").unwrap();
let img1_buffer = img1_dynamic.as_rgba8().unwrap();
let img1_alpha = img1_buffer.get_alpha().unwrap();
img1_alpha.clone().save("tests_out/doctest_buffer_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();
let mut img2_buffer = img2_dynamic.to_rgba16();
img2_buffer.set_alpha(&img1_alpha).unwrap();
img2_buffer.save("tests_out/doctest_buffer_setalpha_result.png").unwrap();
Source

fn transplant_alpha( &mut self, other: &ImageBuffer<P, Container>, ) -> 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::{BufferGetAlpha, BufferSetAlpha};

// Load an image that has an alpha channel
let img1_dynamic = open("test_data/1.png").unwrap();
let img1_buffer = img1_dynamic.as_rgba8().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();
let mut img2_buffer = img2_dynamic.to_rgba16();
img2_buffer.transplant_alpha(&img1_buffer).unwrap();
img2_buffer.save("tests_out/doctest_buffer_transplantalpha_result.png").unwrap();

Implementations on Foreign Types§

Source§

impl<P, Pmut, Container, ContainerMut> BufferSetAlpha<P, Container> for ImageBuffer<Pmut, ContainerMut>
where Pmut: Pixel, P: Pixel, Container: Deref<Target = [P::Subpixel]> + AsRef<[<P as Pixel>::Subpixel]>, ContainerMut: DerefMut<Target = [Pmut::Subpixel]> + AsMut<[<Pmut as Pixel>::Subpixel]>,

Source§

fn set_alpha(&mut self, other: &ImageBuffer<P, Container>) -> Result<(), Error>

Source§

fn transplant_alpha( &mut self, other: &ImageBuffer<P, Container>, ) -> Result<(), Error>

Implementors§