Struct ril::Image

source · []
pub struct Image<P: Pixel = Dynamic> {
    pub data: Vec<P>,
    /* private fields */
}
Expand description

A high-level image representation.

This represents a static, single-frame image. See [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

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.

Panics
  • width or height is zero.

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 a pixel - and returns a pixel.

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.

Decodes an image with the explicitly given image encoding from the raw byte stream.

Errors
  • DecodingError: The image could not be decoded, maybe it is corrupt.

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.

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 [decode_from_bytes] method.

Errors

todo!()

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.

Saves the image with the given encoding to the given path. You can try saving to a memory buffer by using the [encode] method.

Errors
  • An error occured during encoding.
Panics
  • No encoder implementation for the given encoding format.

Saves the image to the given path, inferring the encoding from the path/filename extension.

This is obviously slower than [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 [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.

Returns the width of the image.

Returns the height of the image.

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

Returns a Vec 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]>.

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.

Returns the overlay mode of the image.

Returns the same image with its overlay mode set to the given value.

Returns the dimensions of the image.

Returns the amount of pixels in the image.

Returns true if the image contains no pixels.

Returns a reference of the pixel at the given coordinates.

Returns a reference of the pixel at the given coordinates, but only if it exists.

Returns a mutable reference to the pixel at the given coordinates.

Sets the pixel at the given coordinates to the given pixel.

Overlays the pixel at the given coordinates with the given pixel according to the overlay mode.

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. This is expected, use [set_pixel] if you want this to panic, or to use a custom overlay mode use [pixel_mut].

Inverts this image in place.

Takes this image and inverts it. Useful for method chaining.

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 resets the background color back to the default.

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

Returns the image with each pixel in the image mapped to the given function.

The function should take the pixel and return another pixel.

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.

Similar to [map_pixels_with_coords], but this maps the pixels in place.

This means that the output pixel type must be the same.

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.

Iterates over each row of pixels in the image.

Converts the image into an image with the given pixel type.

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.

Crops this image in place to the given bounding box.

Panics
  • The width or height of the bounding box is less than 1.

Takes this image and crops it to the given box. Useful for method chaining.

Mirrors, or flips this image horizontally (about the y-axis) in place.

Takes this image and flips it horizontally (about the y-axis). Useful for method chaining.

Flips this image vertically (about the x-axis) in place.

Takes this image and flips it vertically, or about the x-axis. Useful for method chaining.

Resizes this image in place to the given dimensions using the given resizing algorithm in place.

Panics
  • width or height is zero.

Takes this image and resizes this image to the given dimensions using the given resizing algorithm. Useful for method chaining.

Panics
  • width or height is zero.

Draws an object or shape onto this image.

Takes this image, draws the given object or shape onto it, and returns it. Useful for method chaining and drawing multiple objects at once.

Pastes the given image onto this image at the given x and y coordinates.

This is a shorthand for using the [draw] method with [Paste].

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 [draw] method with [Paste].

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.

Trait Implementations

Takes this image and returns its bands.

Creates a new image from the given bands.

Takes this image and returns its bands.

Creates a new image from the given bands.

Returns a copy of the value. Read more

Performs copy-assignment from source. Read more

Converts to this type from the input type.

Converts to this type from the input type.

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

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.

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

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.