Struct Canvas

Source
pub struct Canvas { /* private fields */ }
Expand description

A byte buffer with dynamic color contents.

Implementations§

Source§

impl Canvas

Source

pub fn new(layout: CanvasLayout) -> Self

Create a frame by its layout.

§Usage
use image_canvas::Canvas;
use image_canvas::layout::{CanvasLayout, SampleParts, Texel};

// Define what type of color we want to store...
let texel = Texel::new_u8(SampleParts::RgbA);
// and which dimensions to use, chooses a stride for us.
let layout = CanvasLayout::with_texel(&texel, 32, 32)?;

let frame = Canvas::new(layout);
Source

pub fn layout(&self) -> &CanvasLayout

Get a reference to the layout of this frame.

Source

pub fn set_layout(&mut self, layout: CanvasLayout)

Overwrite the layout, allocate if necessary, and clear the image.

Source

pub fn set_layout_conservative(&mut self, layout: CanvasLayout)

Overwrite the layout, allocate if necessary, do not clear the image.

Source

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

Return this image’s pixels as a native endian byte slice.

See also Self::as_bytes_mut.

Source

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

Return this image’s pixels as a mutable native endian byte slice.

The exact interpretation of each byte depends on the layout, which also contains a descriptor of the color types used. This method is for purposes of foreign-function interfaces and as a fallback to process bytes regardless of any restrictions placed by the limits of what other methods are offered.

Mind that the library guarantees that the byte slice is actually aligned with an alignment larger than 1. The exact details depend on the underlying platform but are at least 8.

Still, do not write uninitialized bytes to the buffer. This is UB by Rust’s semantics. (Writing into the buffer by syscalls or assembly most likely never counts as writing uninitialized bytes but take this with a grain of salt).

Source

pub fn as_texels<T>(&self, texel: Texel<T>) -> &[T]

Return the bytes making up this image as a slice of arbitrary elements.

Source

pub fn as_texels_mut<T>(&mut self, texel: Texel<T>) -> &mut [T]

Return the bytes making up this image as a slice of arbitrary elements.

Source

pub fn channels_u8(&self) -> Option<ChannelsRef<'_, u8>>

Get the matrix-like channel descriptor if the channels are u8.

Returns Some only when texel is some multiple of u8.

Source

pub fn channels_u16(&self) -> Option<ChannelsRef<'_, u16>>

Get the matrix-like channel descriptor if the channels are u16.

Returns Some only when texel is some multiple of u16.

Source

pub fn channels_f32(&self) -> Option<ChannelsRef<'_, f32>>

Get the matrix-like channel descriptor if the channels are f32.

Returns Some only when texel is some multiple of f32.

Source

pub fn channels_u8_mut(&mut self) -> Option<ChannelsMut<'_, u8>>

Get the matrix-like channel descriptor if the channels are u8.

Returns Some only when texel is some multiple of u8.

Source

pub fn channels_u16_mut(&mut self) -> Option<ChannelsMut<'_, u16>>

Get the matrix-like channel descriptor if the channels are u16.

Returns Some only when texel is some multiple of u16.

Source

pub fn channels_f32_mut(&mut self) -> Option<ChannelsMut<'_, f32>>

Get the matrix-like channel descriptor if the channels are f32.

Returns Some only when texel is some multiple of f32.

Source

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

Return this image’s pixels as a byte vector.

Source

pub fn plane(&self, idx: u8) -> Option<BytePlaneRef<'_>>

Get the untyped descriptor of the texel matrix.

Returns None if the image contains data that can not be described as a single texel plane, e.g. multiple planes or if the plane is not a matrix.

Source

pub fn planes<const N: usize>(&self) -> Option<[BytePlaneRef<'_>; N]>

Get references to multiple planes at the same time.

Source

pub fn plane_mut(&mut self, idx: u8) -> Option<BytePlaneMut<'_>>

Get the untyped, mutable reference to the texel matrix.

Returns None if the image contains data that can not be described as a single texel plane, e.g. multiple planes or if the plane is not a matrix.

Source

pub fn planes_mut<const N: usize>(&mut self) -> Option<[BytePlaneMut<'_>; N]>

Get mutable references to multiple planes at the same time.

This works because planes never overlap. Note that all planes are aligned to the same byte boundary as the complete canvas bytes.

Source

pub fn set_color(&mut self, color: Color) -> Result<(), LayoutError>

Set the color model.

This never changes the physical layout of data and preserves all its bits. It returns an error if the new color is not compatible with the texel’s color channels.

Source§

impl Canvas

Conversion related methods.

Source

pub fn convert(&self, into: &mut Self) -> Result<(), ConversionError>

Write into another frame, converting color representation between.

§Design notes

The intention is that we can convert between any canvases through some common connection color space. In debug builds we assert that the conversion ran to completion. However, consider this a bit finicky. We want to represent canvases that have not yet had their conversion implemented, for instance certain planar combinations, or certain ICC profile combinations if we get around to it.

Trait Implementations§

Source§

impl Clone for Canvas

Source§

fn clone(&self) -> Canvas

Returns a duplicate of the value. Read more
1.0.0 · Source§

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

Performs copy-assignment from source. Read more
Source§

impl From<ArcCanvas> for Canvas

Source§

fn from(canvas: ArcCanvas) -> Self

Converts to this type from the input type.
Source§

impl From<Canvas> for ArcCanvas

Source§

fn from(canvas: Canvas) -> Self

Converts to this type from the input type.
Source§

impl From<Canvas> for RcCanvas

Source§

fn from(canvas: Canvas) -> Self

Converts to this type from the input type.
Source§

impl From<Plane> for Canvas

Source§

fn from(plane: Plane) -> Self

Converts to this type from the input type.
Source§

impl From<RcCanvas> for Canvas

Source§

fn from(canvas: RcCanvas) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl Freeze for Canvas

§

impl RefUnwindSafe for Canvas

§

impl Send for Canvas

§

impl Sync for Canvas

§

impl Unpin for Canvas

§

impl UnwindSafe for Canvas

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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<P, L> PlaneOf<&L> for P
where P: PlaneOf<L>,

Source§

type Plane = <P as PlaneOf<L>>::Plane

Source§

fn get_plane(self, layout: &&L) -> Option<<P as PlaneOf<&L>>::Plane>

Get the layout describing the plane.
Source§

impl<P, L> PlaneOf<&mut L> for P
where P: PlaneOf<L>,

Source§

type Plane = <P as PlaneOf<L>>::Plane

Source§

fn get_plane(self, layout: &&mut L) -> Option<<P as PlaneOf<&mut L>>::Plane>

Get the layout describing the plane.
Source§

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

Source§

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>,

Source§

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>,

Source§

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.