Struct pix_engine::image::Image

source ·
pub struct Image { /* private fields */ }
Expand description

An Image representing a buffer of pixel color values.

Implementations§

source§

impl Image

source

pub fn new(width: u32, height: u32) -> Self

Constructs an empty RGBA Image with given width and height.

source

pub fn rgba(width: u32, height: u32) -> Self

Constructs an empty RGBA Image with given width and height.

Alias for Image::new.

source

pub fn rgb(width: u32, height: u32) -> Self

Constructs an empty RGB Image with given width and height.

source

pub fn from_bytes<B: AsRef<[u8]>>( width: u32, height: u32, bytes: B, format: PixelFormat ) -> PixResult<Self>

Constructs an Image from a u8 slice representing RGB/A values.

Errors

If the bytes length doesn’t match the image dimensions and PixelFormat provided, then an error is returned.

source

pub fn from_pixels<P: AsRef<[Color]>>( width: u32, height: u32, pixels: P, format: PixelFormat ) -> PixResult<Self>

Constructs an Image from a Color slice representing RGBA values.

Errors

If the pixels length doesn’t match the image dimensions and PixelFormat provided, then an error is returned.

source

pub fn from_vec( width: u32, height: u32, data: Vec<u8>, format: PixelFormat ) -> Self

Constructs an Image from a Vec<u8> representing RGB/A values.

source

pub fn from_file<P: AsRef<Path>>(path: P) -> PixResult<Self>

Constructs an Image from a png file.

Errors

If the file format is not supported or extension is not .png, then an error is returned.

source

pub fn from_read<R: Read>(read: R) -> PixResult<Self>

Constructs an Image from a png reader.

Errors

If the file format is not supported or there is an io::Error reading the file then an error is returned.

source

pub const fn width(&self) -> u32

Returns the Image width.

source

pub const fn height(&self) -> u32

Returns the Image height.

source

pub const fn dimensions(&self) -> (u32, u32)

Returns the Image dimensions as (width, height).

source

pub const fn pitch(&self) -> usize

Returns the pitch of the image data which is the number of bytes in a row of pixel data, including padding between lines.

source

pub fn bounding_rect(&self) -> Rect<i32>

Returns the Image bounding Rect positioned at (0, 0).

The width and height of the returned rectangle are clamped to ensure that size does not exceed i32::MAX. This could result in unexpected behavior with drawing routines if the image size is larger than this.

source

pub fn bounding_rect_offset<P>(&self, offset: P) -> Rect<i32>
where P: Into<Point<i32>>,

Returns the Image bounding Rect positioned at offset.

source

pub fn center(&self) -> Point<i32>

Returns the center position as Point.

source

pub fn bytes(&self) -> Bytes<'_>

Returns the Image pixel data as an iterator of u8.

source

pub fn as_bytes(&self) -> &[u8]

Returns the Image pixel data as a u8 slice.

source

pub fn as_mut_bytes(&mut self) -> &mut [u8]

Returns the Image pixel data as a mutable u8 slice.

source

pub fn into_bytes(self) -> Vec<u8>

Returns the Image pixel data as a Vec<u8>.

This consumes the Image, so we do not need to copy its contents.

source

pub fn pixels(&self) -> Pixels<'_>

Returns the Image pixel data as an iterator of Colors.

source

pub fn into_pixels(self) -> Vec<Color>

Returns the Image pixel data as a Vec<Color>.

Panics

Panics if the image has an invalid sequence of bytes given it’s PixelFormat.

source

pub fn get_pixel(&self, x: u32, y: u32) -> Color

Returns the color value at the given (x, y) position.

Panics

Panics if the image has an invalid sequence of bytes given it’s PixelFormat, or the (x, y) index is out of range.

source

pub fn set_pixel<C: Into<Color>>(&mut self, x: u32, y: u32, color: C)

Sets the color value at the given (x, y) position.

source

pub fn update_bytes<B: AsRef<[u8]>>(&mut self, bytes: B)

Update the Image with a u8 slice representing RGB/A values.

source

pub const fn format(&self) -> PixelFormat

Returns the Image pixel format.

source

pub fn save<P>(&self, path: P) -> PixResult<()>
where P: AsRef<Path>,

Save the Image to a png file.

Errors

Returns an error for any of the following: - An io::Error occurs attempting to create the png file. - A png::EncodingError occurs attempting to write image bytes.

Example
fn on_key_pressed(&mut self, s: &mut PixState, event: KeyEvent) -> PixResult<bool> {
    if let Key::S = event.key {
        self.image.save("test_image.png")?;
    }
    Ok(false)
}

Trait Implementations§

source§

impl Clone for Image

source§

fn clone(&self) -> Image

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for Image

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Default for Image

source§

fn default() -> Image

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

impl From<Image> for Icon

source§

fn from(img: Image) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl RefUnwindSafe for Image

§

impl Send for Image

§

impl Sync for Image

§

impl Unpin for Image

§

impl UnwindSafe for Image

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

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

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

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

fn clone_into(&self, target: &mut T)

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

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V