pub struct Image<P: Pixel> {
pub data: Vec<P>,
/* private fields */
}
Expand description
A high-level image representation.
This represents a static, single-frame image.
See crate::ImageSequence
for information on opening animated or multi-frame images.
Fields§
§data: Vec<P>
A 1-dimensional vector of pixels representing all pixels in the image. This is shaped according to the image’s width and height to form the image.
This data is a low-level, raw representation of the image. You can see the various pixel
mapping functions, or use the pixels
method directly for higher level
representations of the data.
Implementations§
Source§impl<P: Pixel> Image<P>
impl<P: Pixel> Image<P>
Sourcepub fn new(width: u32, height: u32, fill: P) -> Self
pub fn new(width: u32, height: u32, fill: P) -> Self
Creates a new image with the given width and height, with all pixels being set
intially to fill
.
Both the width and height must be non-zero, or else this will panic. You should validate the width and height before calling this function.
§Panics
width
orheight
is zero.
§Example
// 16x16 RGB image with all pixels set to white
let image = Image::new(16, 16, Rgb::white());
assert_eq!(image.width(), 16);
assert_eq!(image.height(), 16);
assert_eq!(image.pixel(0, 0), &Rgb::white());
Sourcepub fn from_fn(width: u32, height: u32, f: impl Fn(u32, u32) -> P) -> Self
pub fn from_fn(width: u32, height: u32, f: impl Fn(u32, u32) -> P) -> Self
Creates a new image with the given width and height. The pixels are then resolved through then given callback function which takes two parameters (the x and y coordinates of each pixel) and returns a pixel.
§Example
let gradient = Image::from_fn(256, 256, |x, _y| L(x as u8));
assert_eq!(gradient.pixel(0, 0), &L(0));
assert_eq!(gradient.pixel(255, 0), &L(255));
Sourcepub fn from_pixels(width: u32, pixels: impl AsRef<[P]>) -> Self
pub fn from_pixels(width: u32, pixels: impl AsRef<[P]>) -> Self
Creates a new image shaped with the given width and a 1-dimensional sequence of pixels which will be shaped according to the width.
§Panics
- The length of the pixels is not a multiple of the width.
§Example
let image = Image::from_pixels(2, &[L(0), L(1), L(2), L(3)]);
assert_eq!(image.width(), 2);
assert_eq!(image.height(), 2);
assert_eq!(image.pixel(1, 1), &L(3));
Sourcepub fn from_paletted_pixels<'p>(
width: u32,
palette: impl ToOwned<Owned = Vec<P::Color>> + 'p,
pixels: impl AsRef<[P::Subpixel]>,
) -> Selfwhere
P: Paletted<'p>,
pub fn from_paletted_pixels<'p>(
width: u32,
palette: impl ToOwned<Owned = Vec<P::Color>> + 'p,
pixels: impl AsRef<[P::Subpixel]>,
) -> Selfwhere
P: Paletted<'p>,
Creates a new image shaped with the given width and a 1-dimensional sequence of paletted pixels which will be shaped according to the width.
§Panics
- The length of the pixels is not a multiple of the width.
- The palette is empty.
- The a pixel index is out of bounds with regards to the palette.
§Example
let image = Image::<PalettedRgb>::from_paletted_pixels(
2,
vec![Rgb::white(), Rgb::black()],
&[0, 1, 0, 1],
);
assert_eq!(image.pixel(1, 1).color(), Rgb::black());
Sourcepub fn from_reader(format: ImageFormat, bytes: impl Read) -> Result<Self>
pub fn from_reader(format: ImageFormat, bytes: impl Read) -> Result<Self>
Sourcepub fn from_reader_inferred(bytes: impl Read) -> Result<Self>
pub fn from_reader_inferred(bytes: impl Read) -> Result<Self>
Decodes an image from the given read stream of bytes, inferring its encoding.
§Errors
DecodingError
: The image could not be decoded, maybe it is corrupt.UnknownEncodingFormat
: Could not infer the encoding from the image. Try explicitly specifying it.
§Panics
- No decoder implementation for the given encoding format.
§Example
let file = std::fs::File::open("image.png")?;
let image = Image::<Rgb>::from_reader_inferred(file)?;
Sourcepub fn from_bytes(format: ImageFormat, bytes: impl AsRef<[u8]>) -> Result<Self>
pub fn from_bytes(format: ImageFormat, bytes: impl AsRef<[u8]>) -> Result<Self>
Decodes an image with the explicitly given image encoding from the given bytes.
Could be useful in conjunction with the include_bytes!
macro.
Currently, this is not any different from [from_reader
].
§Errors
DecodingError
: The image could not be decoded, maybe it is corrupt.
§Panics
- No decoder implementation for the given encoding format.
§Examples
let bytes = include_bytes!("sample.png") as &[u8];
let image = Image::<Rgb>::from_bytes(ImageFormat::Png, bytes)?;
Sourcepub fn from_bytes_inferred(bytes: impl AsRef<[u8]>) -> Result<Self>
pub fn from_bytes_inferred(bytes: impl AsRef<[u8]>) -> Result<Self>
Decodes an image from the given bytes, inferring its encoding.
Could be useful in conjunction with the include_bytes!
macro.
This is more efficient than [from_reader_inferred
].
§Errors
DecodingError
: The image could not be decoded, maybe it is corrupt.UnknownEncodingFormat
: Could not infer the encoding from the image. Try explicitly specifying it.
§Panics
- No decoder implementation for the given encoding format.
§Examples
let bytes = include_bytes!("sample.png") as &[u8];
let image = Image::<Rgb>::from_bytes_inferred(bytes)?;
Sourcepub fn open(path: impl AsRef<Path>) -> Result<Self>
pub fn open(path: impl AsRef<Path>) -> Result<Self>
Opens a file from the given path and decodes it into an image.
The encoding of the image is automatically inferred. You can explicitly pass in an encoding
by using the [from_reader
] method.
§Errors
DecodingError
: The image could not be decoded, maybe it is corrupt.UnknownEncodingFormat
: Could not infer the encoding from the image. Try explicitly specifying it.IoError
: The file could not be opened.
§Panics
- No decoder implementation for the given encoding format.
§Example
let image = Image::<Rgb>::open("sample.png")?;
println!("Image dimensions: {}x{}", image.width(), image.height());
Sourcepub fn encode(&self, encoding: ImageFormat, dest: &mut impl Write) -> Result<()>
pub fn encode(&self, encoding: ImageFormat, dest: &mut impl Write) -> Result<()>
Encodes the image with the given encoding and writes it to the given write buffer.
§Errors
- An error occured during encoding.
§Panics
- No encoder implementation for the given encoding format.
§Example
let image = Image::new(100, 100, Rgb::new(255, 0, 0));
let mut out = Vec::new();
image.encode(ImageFormat::Png, &mut out)?;
Sourcepub fn save(&self, encoding: ImageFormat, path: impl AsRef<Path>) -> Result<()>
pub fn save(&self, encoding: ImageFormat, path: impl AsRef<Path>) -> Result<()>
Saves the image with the given encoding to the given path.
You can try saving to a memory buffer by using the Self::encode
method.
§Errors
- An error occured during encoding.
§Panics
- No encoder implementation for the given encoding format.
§Example
let image = Image::new(100, 100, Rgb::new(255, 0, 0));
image.save(ImageFormat::Png, "out.png")?;
Sourcepub fn save_inferred(&self, path: impl AsRef<Path>) -> Result<()>
pub fn save_inferred(&self, path: impl AsRef<Path>) -> Result<()>
Saves the image to the given path, inferring the encoding from the path/filename extension.
This is obviously slower than Self::save
since this method itself uses it. You should
only use this method if the filename is dynamic, or if you do not know the desired encoding
before runtime.
§See Also
Self::save
for more information on how saving works.
§Errors
- Could not infer encoding format.
- An error occured during encoding.
§Panics
- No encoder implementation for the given encoding format.
§Example
let image = Image::new(100, 100, Rgb::new(255, 0, 0));
image.save_inferred("out.png")?;
Sourcepub const fn center(&self) -> (u32, u32)
pub const fn center(&self) -> (u32, u32)
Returns the nearest pixel coordinates to the center of the image.
This uses integer division which means if an image dimension is not even, then the value is
rounded down - e.g. a 5x5 image returns (2, 2)
, rounded down from (2.5, 2.5)
.
Sourcepub fn pixels(&self) -> impl Iterator<Item = &[P]>
pub fn pixels(&self) -> impl Iterator<Item = &[P]>
Returns an iterator of slices representing the pixels of the image.
Each slice in the Vec is a row. The returned slice should be of Vec<&[P; width]>
.
Sourcepub const fn format(&self) -> ImageFormat
pub const fn format(&self) -> ImageFormat
Returns the encoding format of the image. This is nothing more but metadata about the image. When saving the image, you will still have to explicitly specify the encoding format.
Sourcepub const fn overlay_mode(&self) -> OverlayMode
pub const fn overlay_mode(&self) -> OverlayMode
Returns the overlay mode of the image.
Sourcepub const fn with_overlay_mode(self, mode: OverlayMode) -> Self
pub const fn with_overlay_mode(self, mode: OverlayMode) -> Self
Returns the same image with its overlay mode set to the given value.
Sourcepub const fn dimensions(&self) -> (u32, u32)
pub const fn dimensions(&self) -> (u32, u32)
Returns the dimensions of the image.
Sourcepub fn pixel(&self, x: u32, y: u32) -> &P
pub fn pixel(&self, x: u32, y: u32) -> &P
Returns a reference of the pixel at the given coordinates.
Sourcepub fn get_pixel(&self, x: u32, y: u32) -> Option<&P>
pub fn get_pixel(&self, x: u32, y: u32) -> Option<&P>
Returns a reference of the pixel at the given coordinates, but only if it exists.
Sourcepub fn pixel_mut(&mut self, x: u32, y: u32) -> &mut P
pub fn pixel_mut(&mut self, x: u32, y: u32) -> &mut P
Returns a mutable reference to the pixel at the given coordinates.
Sourcepub fn set_pixel(&mut self, x: u32, y: u32, pixel: P)
pub fn set_pixel(&mut self, x: u32, y: u32, pixel: P)
Sets the pixel at the given coordinates to the given pixel.
Sourcepub fn overlay_pixel(&mut self, x: u32, y: u32, pixel: P)
pub fn overlay_pixel(&mut self, x: u32, y: u32, pixel: P)
Overlays the pixel at the given coordinates with the given pixel according to the overlay mode.
Sourcepub fn overlay_pixel_with_mode(
&mut self,
x: u32,
y: u32,
pixel: P,
mode: OverlayMode,
)
pub fn overlay_pixel_with_mode( &mut self, x: u32, y: u32, pixel: P, mode: OverlayMode, )
Overlays the pixel at the given coordinates with the given pixel according to the specified overlay mode.
If the pixel is out of bounds, nothing occurs: the method will fail silently.
This is expected, use Self::set_pixel
if you want this to panic, or to use a custom
overlay mode use Self::pixel_mut
.
Sourcepub fn overlay_pixel_with_alpha(
&mut self,
x: u32,
y: u32,
pixel: P,
mode: OverlayMode,
alpha: u8,
)
pub fn overlay_pixel_with_alpha( &mut self, x: u32, y: u32, pixel: P, mode: OverlayMode, alpha: u8, )
Overlays the pixel at the given coordinates with the given alpha intensity. This does not regard the overlay mode, since this is usually used for anti-aliasing.
If the pixel is out of bounds, nothing occurs: this method will fail silently.
This is expected, use Self::set_pixel
if you want this to panic, or to use a custom
overlay mode use Self::pixel_mut
.
Sourcepub fn invert(&mut self)
pub fn invert(&mut self)
Inverts this image in place.
Equivalent to:
image.map_in_place(|_x, _y, pixel| *pixel = !*pixel);
Sourcepub fn inverted(self) -> Self
👎Deprecated: use the Not
trait instead (e.g. !image
)
pub fn inverted(self) -> Self
Not
trait instead (e.g. !image
)Takes this image and inverts it. Useful for method chaining.
Sourcepub fn brighten(&mut self, amount: P::Subpixel)
pub fn brighten(&mut self, amount: P::Subpixel)
Brightens the image by increasing all pixels by the specified amount of subpixels in place.
See Self::darken
to darken the image, since this usually does not take any negative
values.
A subpixel is a value of a pixel’s component, for example in RGB, each subpixel is a value of either R, G, or B.
For anything with alpha, alpha is not brightened.
Sourcepub fn darken(&mut self, amount: P::Subpixel)
pub fn darken(&mut self, amount: P::Subpixel)
Darkens the image by decreasing all pixels by the specified amount of subpixels in place.
See Self::brighten
to brighten the image, since this usually does not take any negative
values.
A subpixel is a value of a pixel’s component, for example in RGB, each subpixel is a value of either R, G, or B.
For anything with alpha, alpha is not brightened.
Sourcepub fn brightened(self, amount: P::Subpixel) -> Self
pub fn brightened(self, amount: P::Subpixel) -> Self
Takes this image and brightens it by increasing all pixels by the specified amount of subpixels. Negative values will darken the image. Useful for method chaining.
See Self::darkened
to darken the image, since this usually does not take any negative
values.
A subpixel is a value of a pixel’s component, for example in RGB, each subpixel is a value.
For anything with alpha, alpha is not brightened.
Sourcepub fn darkened(self, amount: P::Subpixel) -> Self
pub fn darkened(self, amount: P::Subpixel) -> Self
Takes this image and darkens it by decreasing all pixels by the specified amount of subpixels. Negative values will brighten the image. Useful for method chaining.
See Self::brightened
to brighten the image, since this usually does not take any
negative values.
A subpixel is a value of a pixel’s component, for example in RGB, each subpixel is a value.
For anything with alpha, alpha is not brightened.
Sourcepub fn hue_rotate(&mut self, degrees: i32)where
P: TrueColor,
pub fn hue_rotate(&mut self, degrees: i32)where
P: TrueColor,
Hue rotates the image by the specified amount of degrees in place.
The hue is a standard angle degree, that is a value between 0 and 360, although values below and above will be wrapped using the modulo operator.
For anything with alpha, alpha is not rotated.
Sourcepub fn hue_rotated(self, degrees: i32) -> Selfwhere
P: TrueColor,
pub fn hue_rotated(self, degrees: i32) -> Selfwhere
P: TrueColor,
Takes this image and hue rotates it by the specified amount of degrees. Useful for method chaining.
See Self::hue_rotate
for more information.
Sourcepub fn map_data<T: Pixel>(self, f: impl FnOnce(Vec<P>) -> Vec<T>) -> Image<T>
pub fn map_data<T: Pixel>(self, f: impl FnOnce(Vec<P>) -> Vec<T>) -> Image<T>
Returns the image replaced with the given data. It is up to you to make sure the data is the correct size.
The function should take the current image data and return the new data.
§Note
This will not work for paletted images, nor will it work for conversion to paletted
images. For conversion from paletted images, see the [Self::flatten
] method to flatten
the palette fist. For conversion to paletted images, try quantizing the image.
Sourcepub fn set_data(&mut self, data: Vec<P>)
pub fn set_data(&mut self, data: Vec<P>)
Sets the data of this image to the new data. This is used a lot internally, but should rarely be used by you.
§Panics
- Panics if the data is malformed.
Sourcepub fn map_pixels<T: Pixel>(self, f: impl FnMut(P) -> T) -> Image<T>
pub fn map_pixels<T: Pixel>(self, f: impl FnMut(P) -> T) -> Image<T>
Returns the image with each pixel in the image mapped to the given function.
The function should take the pixel and return another pixel.
Sourcepub fn map_pixels_with_coords<T: Pixel>(
self,
f: impl Fn(u32, u32, P) -> T,
) -> Image<T>
pub fn map_pixels_with_coords<T: Pixel>( self, f: impl Fn(u32, u32, P) -> T, ) -> Image<T>
Returns the image with the each pixel in the image mapped to the given function, with the function taking additional data of the pixel.
The function should take the x and y coordinates followed by the pixel and return the new pixel.
Sourcepub fn map_in_place(&mut self, f: impl Fn(u32, u32, &mut P))
pub fn map_in_place(&mut self, f: impl Fn(u32, u32, &mut P))
Similar to Self::map_pixels_with_coords
, but this maps the pixels in place.
This means that the output pixel type must be the same.
Sourcepub fn map_rows<I, T: Pixel>(self, f: impl Fn(u32, &[P]) -> I) -> Image<T>where
I: IntoIterator<Item = T>,
pub fn map_rows<I, T: Pixel>(self, f: impl Fn(u32, &[P]) -> I) -> Image<T>where
I: IntoIterator<Item = T>,
Returns the image with each row of pixels represented as a slice mapped to the given function.
The function should take the y coordinate followed by the row of pixels (represented as a slice) and return an Iterator of pixels.
Sourcepub fn rows(&self) -> impl Iterator<Item = &[P]>
pub fn rows(&self) -> impl Iterator<Item = &[P]>
Iterates over each row of pixels in the image.
Sourcepub fn convert<T: Pixel + From<P>>(self) -> Image<T>
pub fn convert<T: Pixel + From<P>>(self) -> Image<T>
Converts the image into an image with the given pixel type.
§Note
Currently there is a slight inconsistency with paletted images - if you would like to
convert from a paletted image to a paletted image with a different pixel type, you cannot
use this method and must instead use the From
/Into
trait instead.
That said, you can also use the From
/Into
trait regardless of the pixel type.
Sourcepub fn set_format(&mut self, format: ImageFormat)
pub fn set_format(&mut self, format: ImageFormat)
Sets the encoding format of this image. Note that when saving the file, an encoding format will still have to be explicitly specified. This is more or less image metadata.
Sourcepub fn crop(&mut self, x1: u32, y1: u32, x2: u32, y2: u32)
pub fn crop(&mut self, x1: u32, y1: u32, x2: u32, y2: u32)
Crops this image in place to the given bounding box.
§Panics
- The width or height of the bounding box is less than 1.
Sourcepub fn cropped(self, x1: u32, y1: u32, x2: u32, y2: u32) -> Self
pub fn cropped(self, x1: u32, y1: u32, x2: u32, y2: u32) -> Self
Takes this image and crops it to the given box. Useful for method chaining.
Sourcepub fn mirror(&mut self)
pub fn mirror(&mut self)
Mirrors, or flips this image horizontally (about the y-axis) in place.
Sourcepub fn mirrored(self) -> Self
pub fn mirrored(self) -> Self
Takes this image and flips it horizontally (about the y-axis). Useful for method chaining.
Sourcepub fn flipped(self) -> Self
pub fn flipped(self) -> Self
Takes this image and flips it vertically, or about the x-axis. Useful for method chaining.
Sourcepub fn rotate_90(&mut self)
pub fn rotate_90(&mut self)
Rotates this image by 90 degrees clockwise, or 270 degrees counterclockwise, in place.
§See Also
Self::rotate
for a version that can take any arbitrary amount of degreesSelf::rotated
for the above method which does operate in-place - useful for method chaining
Sourcepub fn rotate_180(&mut self)
pub fn rotate_180(&mut self)
Rotates this image by 180 degrees in place.
§See Also
Self::rotate
for a version that can take any arbitrary amount of degreesSelf::rotated
for the above method which does operate in-place - useful for method chaining
Sourcepub fn rotate_270(&mut self)
pub fn rotate_270(&mut self)
Rotates this image by 270 degrees clockwise, or 90 degrees counterclockwise, in place.
§See Also
Self::rotate
for a version that can take any arbitrary amount of degreesSelf::rotated
for the above method which does operate in-place - useful for method chaining
Sourcepub fn rotate(&mut self, degrees: i32)
pub fn rotate(&mut self, degrees: i32)
Rotates this image in place about its center. There are optimized rotating algorithms for 90, 180, and 270 degree rotations (clockwise).
As mentioned, the argument is specified in degrees.
§See Also
Self::rotated
for this method which does operate in-place - useful for method chaining
Sourcepub fn rotated(self, degrees: i32) -> Self
pub fn rotated(self, degrees: i32) -> Self
Takes the image and rotates it by the specified amount of degrees about its center. Useful for method chaining. There are optimized rotating algorithms for 90, 180, and 270 degree rotations.
Sourcepub fn resize(&mut self, width: u32, height: u32, algorithm: ResizeAlgorithm)
pub fn resize(&mut self, width: u32, height: u32, algorithm: ResizeAlgorithm)
Resizes this image in place to the given dimensions using the given resizing algorithm in place.
width
and height
must be greater than 0, otherwise this method will panic. You should
validate user input before calling this method.
§Panics
width
orheight
is zero.
§Example
let mut image = Image::new(256, 256, Rgb::white());
assert_eq!(image.dimensions(), (256, 256));
image.resize(64, 64, ResizeAlgorithm::Lanczos3);
assert_eq!(image.dimensions(), (64, 64));
Sourcepub fn resized(
self,
width: u32,
height: u32,
algorithm: ResizeAlgorithm,
) -> Self
pub fn resized( self, width: u32, height: u32, algorithm: ResizeAlgorithm, ) -> Self
Takes this image and resizes this image to the given dimensions using the given resizing algorithm. Useful for method chaining.
width
and height
must be greater than 0, otherwise this method will panic. You should
validate user input before calling this method.
§Panics
width
orheight
is zero.
§See Also
Self::resize
for a version that operates in-place
Sourcepub fn draw(&mut self, entity: &impl Draw<P>)
pub fn draw(&mut self, entity: &impl Draw<P>)
Draws an object or shape onto this image.
§Example
let mut image = Image::new(256, 256, Rgb::white());
let rectangle = Rectangle::at(64, 64)
.with_size(128, 128)
.with_fill(Rgb::black());
image.draw(&rectangle);
Sourcepub fn with(self, entity: &impl Draw<P>) -> Self
pub fn with(self, entity: &impl Draw<P>) -> Self
Takes this image, draws the given object or shape onto it, and returns it. Useful for method chaining and drawing multiple objects at once.
§See Also
Self::draw
for a version that operates in-place
Sourcepub fn paste(&mut self, x: u32, y: u32, image: &Self)
pub fn paste(&mut self, x: u32, y: u32, image: &Self)
Pastes the given image onto this image at the given x and y coordinates.
This is a shorthand for using the Self::draw
method with crate::Paste
.
§Example
let mut image = Image::new(256, 256, Rgb::white());
let overlay_image = Image::open("overlay.png")?;
image.paste(64, 64, &overlay_image);
Sourcepub fn paste_with_mask(
&mut self,
x: u32,
y: u32,
image: &Self,
mask: &Image<BitPixel>,
)
pub fn paste_with_mask( &mut self, x: u32, y: u32, image: &Self, mask: &Image<BitPixel>, )
Pastes the given image onto this image at the given x and y coordinates, masked with the given masking image.
Currently, only BitPixel
images are supported for the masking image.
This is a shorthand for using the Self::draw
method with crate::Paste
.
§Example
let mut image = Image::new(256, 256, Rgb::white());
let overlay_image = Image::open("overlay.png")?;
let (w, h) = overlay_image.dimensions();
let mut mask = Image::new(w, h, BitPixel::off());
mask.draw(&Ellipse::from_bounding_box(0, 0, w, h).with_fill(BitPixel::on()));
image.paste_with_mask(64, 64, &overlay_image, &mask);
Sourcepub fn mask_alpha(&mut self, mask: &Image<L>)where
P: Alpha,
pub fn mask_alpha(&mut self, mask: &Image<L>)where
P: Alpha,
Masks the alpha values of this image with the luminance values of the given single-channel
L
image.
If you want to mask using the alpha values of the image instead of providing an L
image,
you can split the bands of the image and extract the alpha band.
This masking image must have the same dimensions as this image. If it doesn’t, you will receive a panic.
§Panics
- The masking image has different dimensions from this image.
Sourcepub fn palette(&self) -> Option<&[P::Color]>
pub fn palette(&self) -> Option<&[P::Color]>
Returns the palette associated with this image as a slice.
If there is no palette, this returns None
.
Sourcepub fn palette_mut(&mut self) -> Option<&mut [P::Color]>
pub fn palette_mut(&mut self) -> Option<&mut [P::Color]>
Returns the palette associated with this image as a mutable slice.
If there is no palette, this returns None
.
Sourcepub unsafe fn palette_unchecked(&self) -> &[P::Color]
pub unsafe fn palette_unchecked(&self) -> &[P::Color]
Returns the palette associated with this image as a slice. You must uphold the guarantee that the image is paletted, otherwise this will result in undefined behaviour.
§Safety
- The image must always be paletted.
§See Also
Self::palette
- A safe, checked alternative to this method.
Sourcepub unsafe fn palette_mut_unchecked(&mut self) -> &mut [P::Color]
pub unsafe fn palette_mut_unchecked(&mut self) -> &mut [P::Color]
Returns the palette associated with this image as a mutable slice. You must uphold the guarantee that the image is paletted, otherwise this will result in undefined behaviour.
§Safety
- The image must always be paletted.
§See Also
Self::palette_mut
- A safe, checked alternative to this method.
Sourcepub fn map_palette<'a, U, F, C: TrueColor>(self, f: F) -> Image<U>
pub fn map_palette<'a, U, F, C: TrueColor>(self, f: F) -> Image<U>
Maps the palette of this image using the given function. If this image has no palette, this will do nothing.
§Panics
- Safe conversion of palette references failed.
Sourcepub fn flatten_palette<'a>(self) -> Image<P::Color>where
Self: 'a,
P: Paletted<'a>,
pub fn flatten_palette<'a>(self) -> Image<P::Color>where
Self: 'a,
P: Paletted<'a>,
Takes this image and flattens this paletted image into an unpaletted image. This is similar
to Self::convert
but the output type is automatically resolved.
Sourcepub fn quantize<'p, T>(self, palette_size: u8) -> Image<T>
pub fn quantize<'p, T>(self, palette_size: u8) -> Image<T>
Quantizes this image using its colors and turns it into its paletted counterpart. This currently only works with 8-bit palettes.
This is similar to Self::convert
but the output type is automatically resolved.
This is also the inverse conversion of Self::flatten_palette
.
§Errors
- The palette could not be created.
§Panics
- Unable to quantize the image.
§See Also
- [
Quantizer
] - Implementation of the core quantizer. Use this for more fine-grained control over the quantization process, such as adjusting the quantization speed.
Source§impl Image<Rgba>
impl Image<Rgba>
Sourcepub fn split_rgb_and_alpha(self) -> (Image<Rgb>, Image<L>)
pub fn split_rgb_and_alpha(self) -> (Image<Rgb>, Image<L>)
Splits this image into an Rgb
image and an L
image, where the Rgb
image contains the
red, green, and blue color channels and the L
image contains the alpha channel.
There is a more optimized method available, Self::map_rgb_pixels
, if you only need to perform
operations on individual RGB pixels. If you can, you should use that instead.
§Example
Rotating the image by 90 degrees but keeping the alpha channel untouched:
use ril::prelude::*;
let image = Image::<Rgba>::open("image.png")?;
let (rgb, alpha) = image.split_rgb_and_alpha();
let inverted = Image::from_rgb_and_alpha(rgb.rotated(90), alpha);
§See Also
Self::from_rgb_and_alpha
- The inverse of this method.Self::map_rgb_pixels
- A more optimized method for performing operations on individual RGB pixels.
Sourcepub fn from_rgb_and_alpha(rgb: Image<Rgb>, alpha: Image<L>) -> Self
pub fn from_rgb_and_alpha(rgb: Image<Rgb>, alpha: Image<L>) -> Self
Creates an Rgba
image from an Rgb
image and an L
image, where the Rgb
image contains
the red, green, and blue color channels and the L
image contains the alpha channel.
§Panics
- The dimensions of the two images do not match.
§See Also
Self::split_rgb_and_alpha
- The inverse of this method.
Sourcepub fn map_rgb_pixels(self, f: impl FnMut(Rgb) -> Rgb) -> Self
pub fn map_rgb_pixels(self, f: impl FnMut(Rgb) -> Rgb) -> Self
Performs the given operation f
on every pixel in this image, ignoring the alpha channel.
The alpha channel is left untouched.
§Example
Inverting the image but keeping the alpha channel untouched:
use ril::prelude::*;
let image = Image::<Rgba>::open("image.png")?;
let inverted = image.map_rgb_pixels(|rgb| !rgb);
§See Also
Self::map_alpha_pixels
- Performs the given operation on every pixel in the alpha channel.Self::split_rgb_and_alpha
- If you need to operate on the entireImage<Rgb>
(andImage<L>
).
Sourcepub fn map_alpha_pixels(self, f: impl FnMut(L) -> L) -> Self
pub fn map_alpha_pixels(self, f: impl FnMut(L) -> L) -> Self
Performs the given operation f
on every pixel in the alpha channel of this image.
The RGB channels are left untouched.
§See Also
Self::map_rgb_pixels
- Performs the given operation on every pixel in the RGB channels.Self::split_rgb_and_alpha
- If you need to operate on the entireImage<L>
(andImage<Rgb>
).
Trait Implementations§
Source§impl From<Image<PalettedRgb<'_>>> for Image<BitPixel>
impl From<Image<PalettedRgb<'_>>> for Image<BitPixel>
Source§fn from(f: Image<PalettedRgb<'_>>) -> Self
fn from(f: Image<PalettedRgb<'_>>) -> Self
Source§impl From<Image<PalettedRgb<'_>>> for Image<Dynamic>
impl From<Image<PalettedRgb<'_>>> for Image<Dynamic>
Source§fn from(f: Image<PalettedRgb<'_>>) -> Self
fn from(f: Image<PalettedRgb<'_>>) -> Self
Source§impl From<Image<PalettedRgb<'_>>> for Image<L>
impl From<Image<PalettedRgb<'_>>> for Image<L>
Source§fn from(f: Image<PalettedRgb<'_>>) -> Self
fn from(f: Image<PalettedRgb<'_>>) -> Self
Source§impl From<Image<PalettedRgb<'_>>> for Image<Rgb>
impl From<Image<PalettedRgb<'_>>> for Image<Rgb>
Source§fn from(f: Image<PalettedRgb<'_>>) -> Self
fn from(f: Image<PalettedRgb<'_>>) -> Self
Source§impl From<Image<PalettedRgb<'_>>> for Image<Rgba>
impl From<Image<PalettedRgb<'_>>> for Image<Rgba>
Source§fn from(f: Image<PalettedRgb<'_>>) -> Self
fn from(f: Image<PalettedRgb<'_>>) -> Self
Source§impl<'a> From<Image<PalettedRgb<'a>>> for Image<PalettedRgba<'a>>
impl<'a> From<Image<PalettedRgb<'a>>> for Image<PalettedRgba<'a>>
Source§fn from(image: Image<PalettedRgb<'a>>) -> Self
fn from(image: Image<PalettedRgb<'a>>) -> Self
Source§impl From<Image<PalettedRgba<'_>>> for Image<BitPixel>
impl From<Image<PalettedRgba<'_>>> for Image<BitPixel>
Source§fn from(f: Image<PalettedRgba<'_>>) -> Self
fn from(f: Image<PalettedRgba<'_>>) -> Self
Source§impl From<Image<PalettedRgba<'_>>> for Image<Dynamic>
impl From<Image<PalettedRgba<'_>>> for Image<Dynamic>
Source§fn from(f: Image<PalettedRgba<'_>>) -> Self
fn from(f: Image<PalettedRgba<'_>>) -> Self
Source§impl From<Image<PalettedRgba<'_>>> for Image<L>
impl From<Image<PalettedRgba<'_>>> for Image<L>
Source§fn from(f: Image<PalettedRgba<'_>>) -> Self
fn from(f: Image<PalettedRgba<'_>>) -> Self
Source§impl From<Image<PalettedRgba<'_>>> for Image<Rgb>
impl From<Image<PalettedRgba<'_>>> for Image<Rgb>
Source§fn from(f: Image<PalettedRgba<'_>>) -> Self
fn from(f: Image<PalettedRgba<'_>>) -> Self
Source§impl From<Image<PalettedRgba<'_>>> for Image<Rgba>
impl From<Image<PalettedRgba<'_>>> for Image<Rgba>
Source§fn from(f: Image<PalettedRgba<'_>>) -> Self
fn from(f: Image<PalettedRgba<'_>>) -> Self
Source§impl<'a> From<Image<PalettedRgba<'a>>> for Image<PalettedRgb<'a>>
impl<'a> From<Image<PalettedRgba<'a>>> for Image<PalettedRgb<'a>>
Source§fn from(image: Image<PalettedRgba<'a>>) -> Self
fn from(image: Image<PalettedRgba<'a>>) -> Self
Auto Trait Implementations§
impl<P> Freeze for Image<P>
impl<P> RefUnwindSafe for Image<P>
impl<P> Send for Image<P>
impl<P> Sync for Image<P>
impl<P> Unpin for Image<P>where
P: Unpin,
impl<P> UnwindSafe for Image<P>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more