Struct image::ImageBuffer [−][src]
pub struct ImageBuffer<P: Pixel, Container> { /* fields omitted */ }
Generic image buffer
This is an image parameterised by its Pixel types, represented by a width and height and a
container of channel data. It provides direct access to its pixels and implements the
GenericImageView
and GenericImage
traits. In many ways, this is the standard buffer
implementing those traits. Using this concrete type instead of a generic type parameter has
been shown to improve performance.
The crate defines a few type aliases with regularly used pixel types for your convenience, such
as RgbImage
, GrayImage
etc.
To convert between images of different Pixel types use DynamicImage
.
You can retrieve a complete description of the buffer’s layout and contents through
as_flat_samples
and as_flat_samples_mut
. This can be handy to also use the contents in
a foreign language, map it as a GPU host buffer or other similar tasks.
Examples
Create a simple canvas and paint a small cross.
use image::{RgbImage, Rgb}; let mut img = RgbImage::new(32, 32); for x in 15..=17 { for y in 8..24 { img.put_pixel(x, y, Rgb([255, 0, 0])); img.put_pixel(y, x, Rgb([255, 0, 0])); } }
Overlays an image on top of a larger background raster.
use image::{GenericImage, GenericImageView, ImageBuffer, open}; let on_top = open("path/to/some.png").unwrap().into_rgb(); let mut img = ImageBuffer::from_fn(512, 512, |x, y| { if (x + y) % 2 == 0 { image::Rgb([0, 0, 0]) } else { image::Rgb([255, 255, 255]) } }); image::imageops::overlay(&mut img, &on_top, 128, 128);
Convert an RgbaImage to a GrayImage.
use image::{open, DynamicImage}; let rgba = open("path/to/some.png").unwrap().into_rgba(); let gray = DynamicImage::ImageRgba8(rgba).into_luma();
Implementations
impl<P, Container> ImageBuffer<P, Container> where
P: Pixel + 'static,
P::Subpixel: 'static,
Container: Deref<Target = [P::Subpixel]>,
[src]
impl<P, Container> ImageBuffer<P, Container> where
P: Pixel + 'static,
P::Subpixel: 'static,
Container: Deref<Target = [P::Subpixel]>,
[src]pub fn from_raw(
width: u32,
height: u32,
buf: Container
) -> Option<ImageBuffer<P, Container>>
[src]
width: u32,
height: u32,
buf: Container
) -> Option<ImageBuffer<P, Container>>
Contructs a buffer from a generic container
(for example a Vec
or a slice)
Returns None
if the container is not big enough (including when the image dimensions
necessitate an allocation of more bytes than supported by the container).
pub fn into_raw(self) -> Container
[src]
Returns the underlying raw buffer
pub fn as_raw(&self) -> &Container
[src]
Returns the underlying raw buffer
pub fn dimensions(&self) -> (u32, u32)
[src]
The width and height of this image.
pub fn width(&self) -> u32
[src]
The width of this image.
pub fn height(&self) -> u32
[src]
The height of this image.
pub fn pixels(&self) -> Pixels<'_, P>ⓘ
[src]
Returns an iterator over the pixels of this image. The iteration order is x = 0 to width then y = 0 to height
pub fn rows(&self) -> Rows<'_, P>ⓘ
[src]
Returns an iterator over the rows of this image.
Only non-empty rows can be iterated in this manner. In particular the iterator will not
yield any item when the width of the image is 0
or a pixel type without any channels is
used. This ensures that its length can always be represented by usize
.
pub fn enumerate_pixels(&self) -> EnumeratePixels<'_, P>ⓘ
[src]
Enumerates over the pixels of the image. The iterator yields the coordinates of each pixel along with a reference to them. The iteration order is x = 0 to width then y = 0 to height
pub fn enumerate_rows(&self) -> EnumerateRows<'_, P>ⓘNotable traits for EnumerateRows<'a, P>
impl<'a, P: Pixel + 'a> Iterator for EnumerateRows<'a, P> where
P::Subpixel: 'a, type Item = (u32, EnumeratePixels<'a, P>);
[src]
Notable traits for EnumerateRows<'a, P>
impl<'a, P: Pixel + 'a> Iterator for EnumerateRows<'a, P> where
P::Subpixel: 'a, type Item = (u32, EnumeratePixels<'a, P>);
Enumerates over the rows of the image. The iterator yields the y-coordinate of each row along with a reference to them.
pub fn get_pixel(&self, x: u32, y: u32) -> &P
[src]
Gets a reference to the pixel at location (x, y)
Panics
Panics if (x, y)
is out of the bounds (width, height)
.
pub fn sample_layout(&self) -> SampleLayout
[src]
Get the format of the buffer when viewed as a matrix of samples.
pub fn into_flat_samples(self) -> FlatSamples<Container> where
Container: AsRef<[P::Subpixel]>,
[src]
Container: AsRef<[P::Subpixel]>,
Return the raw sample buffer with its stride an dimension information.
The returned buffer is guaranteed to be well formed in all cases. It is layed out by
colors, width then height, meaning channel_stride <= width_stride <= height_stride
. All
strides are in numbers of elements but those are mostly u8
in which case the strides are
also byte strides.
pub fn as_flat_samples(&self) -> FlatSamples<&[P::Subpixel]> where
Container: AsRef<[P::Subpixel]>,
[src]
Container: AsRef<[P::Subpixel]>,
Return a view on the raw sample buffer.
See into_flat_samples
for more details.
pub fn as_flat_samples_mut(&mut self) -> FlatSamples<&mut [P::Subpixel]> where
Container: AsMut<[P::Subpixel]>,
[src]
Container: AsMut<[P::Subpixel]>,
Return a mutable view on the raw sample buffer.
See into_flat_samples
for more details.
impl<P, Container> ImageBuffer<P, Container> where
P: Pixel + 'static,
P::Subpixel: 'static,
Container: Deref<Target = [P::Subpixel]> + DerefMut,
[src]
impl<P, Container> ImageBuffer<P, Container> where
P: Pixel + 'static,
P::Subpixel: 'static,
Container: Deref<Target = [P::Subpixel]> + DerefMut,
[src]pub fn pixels_mut(&mut self) -> PixelsMut<'_, P>ⓘ
[src]
Returns an iterator over the mutable pixels of this image.
pub fn rows_mut(&mut self) -> RowsMut<'_, P>ⓘ
[src]
Returns an iterator over the mutable rows of this image.
Only non-empty rows can be iterated in this manner. In particular the iterator will not
yield any item when the width of the image is 0
or a pixel type without any channels is
used. This ensures that its length can always be represented by usize
.
pub fn enumerate_pixels_mut(&mut self) -> EnumeratePixelsMut<'_, P>ⓘ
[src]
Enumerates over the pixels of the image. The iterator yields the coordinates of each pixel along with a mutable reference to them.
pub fn enumerate_rows_mut(&mut self) -> EnumerateRowsMut<'_, P>ⓘNotable traits for EnumerateRowsMut<'a, P>
impl<'a, P: Pixel + 'a> Iterator for EnumerateRowsMut<'a, P> where
P::Subpixel: 'a, type Item = (u32, EnumeratePixelsMut<'a, P>);
[src]
Notable traits for EnumerateRowsMut<'a, P>
impl<'a, P: Pixel + 'a> Iterator for EnumerateRowsMut<'a, P> where
P::Subpixel: 'a, type Item = (u32, EnumeratePixelsMut<'a, P>);
Enumerates over the rows of the image. The iterator yields the y-coordinate of each row along with a mutable reference to them.
pub fn get_pixel_mut(&mut self, x: u32, y: u32) -> &mut P
[src]
Gets a reference to the mutable pixel at location (x, y)
Panics
Panics if (x, y)
is out of the bounds (width, height)
.
pub fn put_pixel(&mut self, x: u32, y: u32, pixel: P)
[src]
impl<P, Container> ImageBuffer<P, Container> where
P: Pixel + 'static,
[P::Subpixel]: EncodableLayout,
Container: Deref<Target = [P::Subpixel]>,
[src]
impl<P, Container> ImageBuffer<P, Container> where
P: Pixel + 'static,
[P::Subpixel]: EncodableLayout,
Container: Deref<Target = [P::Subpixel]>,
[src]impl<P, Container> ImageBuffer<P, Container> where
P: Pixel + 'static,
[P::Subpixel]: EncodableLayout,
Container: Deref<Target = [P::Subpixel]>,
[src]
impl<P, Container> ImageBuffer<P, Container> where
P: Pixel + 'static,
[P::Subpixel]: EncodableLayout,
Container: Deref<Target = [P::Subpixel]>,
[src]pub fn save_with_format<Q>(
&self,
path: Q,
format: ImageFormat
) -> ImageResult<()> where
Q: AsRef<Path>,
[src]
&self,
path: Q,
format: ImageFormat
) -> ImageResult<()> where
Q: AsRef<Path>,
Saves the buffer to a file at the specified path in the specified format.
See save_buffer_with_format
for
supported types.
impl<P: Pixel + 'static> ImageBuffer<P, Vec<P::Subpixel>> where
P::Subpixel: 'static,
[src]
impl<P: Pixel + 'static> ImageBuffer<P, Vec<P::Subpixel>> where
P::Subpixel: 'static,
[src]pub fn new(width: u32, height: u32) -> ImageBuffer<P, Vec<P::Subpixel>>
[src]
Creates a new image buffer based on a Vec<P::Subpixel>
.
Panics
Panics when the resulting image is larger the the maximum size of a vector.
pub fn from_pixel(
width: u32,
height: u32,
pixel: P
) -> ImageBuffer<P, Vec<P::Subpixel>>
[src]
width: u32,
height: u32,
pixel: P
) -> ImageBuffer<P, Vec<P::Subpixel>>
Constructs a new ImageBuffer by copying a pixel
Panics
Panics when the resulting image is larger the the maximum size of a vector.
pub fn from_fn<F>(
width: u32,
height: u32,
f: F
) -> ImageBuffer<P, Vec<P::Subpixel>> where
F: FnMut(u32, u32) -> P,
[src]
width: u32,
height: u32,
f: F
) -> ImageBuffer<P, Vec<P::Subpixel>> where
F: FnMut(u32, u32) -> P,
Constructs a new ImageBuffer by repeated application of the supplied function.
The arguments to the function are the pixel’s x and y coordinates.
Panics
Panics when the resulting image is larger the the maximum size of a vector.
pub fn from_vec(
width: u32,
height: u32,
buf: Vec<P::Subpixel>
) -> Option<ImageBuffer<P, Vec<P::Subpixel>>>
[src]
width: u32,
height: u32,
buf: Vec<P::Subpixel>
) -> Option<ImageBuffer<P, Vec<P::Subpixel>>>
Creates an image buffer out of an existing buffer. Returns None if the buffer is not big enough.
pub fn into_vec(self) -> Vec<P::Subpixel>
[src]
Consumes the image buffer and returns the underlying data as an owned buffer
impl ImageBuffer<Luma<u8>, Vec<u8, Global>>
[src]
impl ImageBuffer<Luma<u8>, Vec<u8, Global>>
[src]Trait Implementations
impl<P, Container> Clone for ImageBuffer<P, Container> where
P: Pixel,
Container: Deref<Target = [P::Subpixel]> + Clone,
[src]
impl<P, Container> Clone for ImageBuffer<P, Container> where
P: Pixel,
Container: Deref<Target = [P::Subpixel]> + Clone,
[src]fn clone(&self) -> ImageBuffer<P, Container>
[src]
pub fn clone_from(&mut self, source: &Self)
1.0.0[src]
impl<'a, 'b, Container, FromType: Pixel + 'static, ToType: Pixel + 'static> ConvertBuffer<ImageBuffer<ToType, Vec<<ToType as Pixel>::Subpixel, Global>>> for ImageBuffer<FromType, Container> where
Container: Deref<Target = [FromType::Subpixel]>,
ToType: FromColor<FromType>,
FromType::Subpixel: 'static,
ToType::Subpixel: 'static,
[src]
impl<'a, 'b, Container, FromType: Pixel + 'static, ToType: Pixel + 'static> ConvertBuffer<ImageBuffer<ToType, Vec<<ToType as Pixel>::Subpixel, Global>>> for ImageBuffer<FromType, Container> where
Container: Deref<Target = [FromType::Subpixel]>,
ToType: FromColor<FromType>,
FromType::Subpixel: 'static,
ToType::Subpixel: 'static,
[src]fn convert(&self) -> ImageBuffer<ToType, Vec<ToType::Subpixel>>
[src]
Examples
Convert RGB image to gray image.
use image::buffer::ConvertBuffer; use image::GrayImage; let image_path = "examples/fractal.png"; let image = image::open(&image_path) .expect("Open file failed") .to_rgba(); let gray_image: GrayImage = image.convert();
impl<P, Container> Deref for ImageBuffer<P, Container> where
P: Pixel + 'static,
P::Subpixel: 'static,
Container: Deref<Target = [P::Subpixel]>,
[src]
impl<P, Container> Deref for ImageBuffer<P, Container> where
P: Pixel + 'static,
P::Subpixel: 'static,
Container: Deref<Target = [P::Subpixel]>,
[src]impl<P, Container> DerefMut for ImageBuffer<P, Container> where
P: Pixel + 'static,
P::Subpixel: 'static,
Container: Deref<Target = [P::Subpixel]> + DerefMut,
[src]
impl<P, Container> DerefMut for ImageBuffer<P, Container> where
P: Pixel + 'static,
P::Subpixel: 'static,
Container: Deref<Target = [P::Subpixel]> + DerefMut,
[src]impl<P, Container> GenericImage for ImageBuffer<P, Container> where
P: Pixel + 'static,
Container: Deref<Target = [P::Subpixel]> + DerefMut,
P::Subpixel: 'static,
[src]
impl<P, Container> GenericImage for ImageBuffer<P, Container> where
P: Pixel + 'static,
Container: Deref<Target = [P::Subpixel]> + DerefMut,
P::Subpixel: 'static,
[src]type InnerImage = Self
Underlying image type. This is mainly used by SubImages in order to always have a reference to the original image. This allows for less indirections and it eases the use of nested SubImages. Read more
fn get_pixel_mut(&mut self, x: u32, y: u32) -> &mut P
[src]
fn put_pixel(&mut self, x: u32, y: u32, pixel: P)
[src]
unsafe fn unsafe_put_pixel(&mut self, x: u32, y: u32, pixel: P)
[src]
Puts a pixel at location (x, y), ignoring bounds checking.
fn blend_pixel(&mut self, x: u32, y: u32, p: P)
[src]
Put a pixel at location (x, y), taking into account alpha channels
DEPRECATED: This method will be removed. Blend the pixel directly instead.
fn copy_within(&mut self, source: Rect, x: u32, y: u32) -> bool
[src]
fn inner_mut(&mut self) -> &mut Self::InnerImage
[src]
fn copy_from<O>(&mut self, other: &O, x: u32, y: u32) -> ImageResult<()> where
O: GenericImageView<Pixel = Self::Pixel>,
[src]
O: GenericImageView<Pixel = Self::Pixel>,
fn sub_image(
&mut self,
x: u32,
y: u32,
width: u32,
height: u32
) -> SubImage<&mut Self::InnerImage>
[src]
&mut self,
x: u32,
y: u32,
width: u32,
height: u32
) -> SubImage<&mut Self::InnerImage>
impl<P, Container> GenericImageView for ImageBuffer<P, Container> where
P: Pixel + 'static,
Container: Deref<Target = [P::Subpixel]> + Deref,
P::Subpixel: 'static,
[src]
impl<P, Container> GenericImageView for ImageBuffer<P, Container> where
P: Pixel + 'static,
Container: Deref<Target = [P::Subpixel]> + Deref,
P::Subpixel: 'static,
[src]type Pixel = P
The type of pixel.
type InnerImageView = Self
Underlying image type. This is mainly used by SubImages in order to always have a reference to the original image. This allows for less indirections and it eases the use of nested SubImages. Read more
fn dimensions(&self) -> (u32, u32)
[src]
fn bounds(&self) -> (u32, u32, u32, u32)
[src]
fn get_pixel(&self, x: u32, y: u32) -> P
[src]
unsafe fn unsafe_get_pixel(&self, x: u32, y: u32) -> P
[src]
Returns the pixel located at (x, y), ignoring bounds checking.
fn inner(&self) -> &Self::InnerImageView
[src]
fn width(&self) -> u32
[src]
fn height(&self) -> u32
[src]
fn in_bounds(&self, x: u32, y: u32) -> bool
[src]
fn pixels(&self) -> Pixels<'_, Self>ⓘ
[src]
fn view(
&self,
x: u32,
y: u32,
width: u32,
height: u32
) -> SubImage<&Self::InnerImageView>
[src]
&self,
x: u32,
y: u32,
width: u32,
height: u32
) -> SubImage<&Self::InnerImageView>
impl<P, Container> Index<(u32, u32)> for ImageBuffer<P, Container> where
P: Pixel + 'static,
P::Subpixel: 'static,
Container: Deref<Target = [P::Subpixel]>,
[src]
impl<P, Container> Index<(u32, u32)> for ImageBuffer<P, Container> where
P: Pixel + 'static,
P::Subpixel: 'static,
Container: Deref<Target = [P::Subpixel]>,
[src]impl<P, Container> IndexMut<(u32, u32)> for ImageBuffer<P, Container> where
P: Pixel + 'static,
P::Subpixel: 'static,
Container: Deref<Target = [P::Subpixel]> + DerefMut,
[src]
impl<P, Container> IndexMut<(u32, u32)> for ImageBuffer<P, Container> where
P: Pixel + 'static,
P::Subpixel: 'static,
Container: Deref<Target = [P::Subpixel]> + DerefMut,
[src]impl<P: PartialEq + Pixel, Container: PartialEq> PartialEq<ImageBuffer<P, Container>> for ImageBuffer<P, Container>
[src]
impl<P: PartialEq + Pixel, Container: PartialEq> PartialEq<ImageBuffer<P, Container>> for ImageBuffer<P, Container>
[src]fn eq(&self, other: &ImageBuffer<P, Container>) -> bool
[src]
fn ne(&self, other: &ImageBuffer<P, Container>) -> bool
[src]
impl<P: Eq + Pixel, Container: Eq> Eq for ImageBuffer<P, Container>
[src]
impl<P: Pixel, Container> StructuralEq for ImageBuffer<P, Container>
[src]
impl<P: Pixel, Container> StructuralPartialEq for ImageBuffer<P, Container>
[src]
Auto Trait Implementations
impl<P, Container> RefUnwindSafe for ImageBuffer<P, Container> where
Container: RefUnwindSafe,
P: RefUnwindSafe,
Container: RefUnwindSafe,
P: RefUnwindSafe,
impl<P, Container> Send for ImageBuffer<P, Container> where
Container: Send,
P: Send,
Container: Send,
P: Send,
impl<P, Container> Sync for ImageBuffer<P, Container> where
Container: Sync,
P: Sync,
Container: Sync,
P: Sync,
impl<P, Container> Unpin for ImageBuffer<P, Container> where
Container: Unpin,
P: Unpin,
Container: Unpin,
P: Unpin,
impl<P, Container> UnwindSafe for ImageBuffer<P, Container> where
Container: UnwindSafe,
P: UnwindSafe,
Container: UnwindSafe,
P: UnwindSafe,