Struct image::ImageBuffer[][src]

pub struct ImageBuffer<P: Pixel, Container> { /* fields omitted */ }
Expand description

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

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).

Returns the underlying raw buffer

Returns the underlying raw buffer

The width and height of this image.

The width of this image.

The height of this image.

Returns an iterator over the pixels of this image. The iteration order is x = 0 to width then y = 0 to height

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.

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 Starting from the top left.

Enumerates over the rows of the image. The iterator yields the y-coordinate of each row along with a reference to them.

Gets a reference to the pixel at location (x, y)

Panics

Panics if (x, y) is out of the bounds (width, height).

Get the format of the buffer when viewed as a matrix of samples.

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.

Return a view on the raw sample buffer.

See into_flat_samples for more details.

Return a mutable view on the raw sample buffer.

See into_flat_samples for more details.

Returns an iterator over the mutable pixels of this image.

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.

Enumerates over the pixels of the image. The iterator yields the coordinates of each pixel along with a mutable reference to them.

Enumerates over the rows of the image. The iterator yields the y-coordinate of each row along with a mutable reference to them.

Gets a reference to the mutable pixel at location (x, y)

Panics

Panics if (x, y) is out of the bounds (width, height).

Puts a pixel at location (x, y)

Panics

Panics if (x, y) is out of the bounds (width, height).

Saves the buffer to a file at the path specified.

The image format is derived from the file extension. Currently only jpeg and png files are supported.

Saves the buffer to a file at the specified path in the specified format.

See save_buffer_with_format for supported types.

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.

Constructs a new ImageBuffer by copying a pixel

Panics

Panics when the resulting image is larger the the maximum size of a vector.

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.

Creates an image buffer out of an existing buffer. Returns None if the buffer is not big enough.

Consumes the image buffer and returns the underlying data as an owned buffer

Expands a color palette by re-using the existing buffer. Assumes 8 bit per pixel. Uses an optionally transparent index to adjust it’s alpha value accordingly.

Trait Implementations

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

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

Formats the value using the given formatter. Read more

Returns the “default value” for a type. Read more

The resulting type after dereferencing.

Dereferences the value.

Mutably dereferences the value.

Puts a pixel at location (x, y), ignoring bounds checking.

Put a pixel at location (x, y), taking into account alpha channels

DEPRECATED: This method will be removed. Blend the pixel directly instead.

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

Gets a reference to the mutable pixel at location (x, y). Indexed from top left. Read more

Put a pixel at location (x, y). Indexed from top left. Read more

Copies all of the pixels from one part of this image to another part of this image. Read more

Returns a mutable reference to the underlying image.

Copies all of the pixels from another image into this image. Read more

Returns a mutable subimage that is a view into this image. If you want an immutable subimage instead, use GenericImageView::view The coordinates set the position of the top left corner of the SubImage. Read more

Returns the pixel located at (x, y), ignoring bounds checking.

The type of pixel.

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

The width and height of this image.

The bounding rectangle of this image.

Returns the pixel located at (x, y). Indexed from top left. Read more

Returns a reference to the underlying image.

The width of this image.

The height of this image.

Returns true if this x, y coordinate is contained inside the image.

Returns an Iterator over the pixels of this image. The iterator yields the coordinates of each pixel along with their value Read more

Returns an subimage that is an immutable view into this image. You can use GenericImage::sub_image if you need a mutable view instead. The coordinates set the position of the top left corner of the view. Read more

Feeds this value into the given Hasher. Read more

Feeds a slice of this type into the given Hasher. Read more

The returned type after indexing.

Performs the indexing (container[index]) operation. Read more

Performs the mutable indexing (container[index]) operation. Read more

This method tests for self and other values to be equal, and is used by ==. Read more

This method tests for !=.

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Performs the conversion.

Performs the conversion.

The alignment of pointer.

The type for initializers.

Initializes a with the given initializer. Read more

Dereferences the given pointer. Read more

Mutably dereferences the given pointer. Read more

Drops the object pointed to by the given pointer. Read more

The resulting type after obtaining ownership.

Creates owned data from borrowed data, usually by cloning. Read more

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.