Skip to main content

Canvas

Struct Canvas 

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

An image, and what has been asked of it so far.

A canvas is immutable: apply and draw return a new one that shares the source bytes. Steps run in the order they were added, each on what the one before it left. Nothing is decoded until encode, which decodes, runs every step and writes the result in one pass. The size, format and orientation come from the header.

PHP: Utopia\Image\Canvas.

use utopia_image::{Canvas, Limits, OutputFormat, Transform};

let png = std::fs::read("../../../packages/image/tests/resources/resize/100x100.png")?;
let canvas = Canvas::decode(&png, Limits::default())?;
assert_eq!((canvas.width(), canvas.height()), (100, 100));
let jpeg = canvas.apply(Transform { width: 50, height: 25, ..Transform::default() })?.encode(OutputFormat::Jpeg, Some(80))?;
assert_eq!(&jpeg[..3], &[0xFF, 0xD8, 0xFF]);

Implementations§

Source§

impl Canvas

Source

pub fn decode(data: &[u8], limits: Limits) -> Result<Canvas>

Reads the header of a JPEG, PNG, GIF, WebP, AVIF or HEIC file.

Fails here, before any pixel is decoded, for a format that is not one of accept, a header that cannot be read, or a size past limits. A file whose pixel data is damaged fails later, in encode.

PHP: new Canvas($data, $limits).

Source

pub fn decode_accepting( data: &[u8], limits: Limits, accept: &[Format], ) -> Result<Canvas>

As decode, refusing any format not in accept (every format when empty).

Source

pub fn blank( width: u32, height: u32, background: &str, limits: Limits, ) -> Result<Canvas>

A canvas of one colour.

PHP: new Canvas(new Blank($width, $height, $background), $limits).

Source

pub fn width(&self) -> u32

The width of the source, before any step.

Source

pub fn height(&self) -> u32

The height of the source, before any step.

Source

pub fn orientation(&self) -> u32

The EXIF orientation of the source, 1 to 8 (1 when it has none).

Source

pub fn format(&self) -> Option<Format>

The format of the source; None for a blank canvas.

Source

pub fn apply(&self, transform: Transform) -> Result<Canvas>

A canvas with transform added to its plan. Fails for a colour that cannot be parsed, or a size past the canvas’s limits.

Source

pub fn draw(&self, drawing: Drawing) -> Result<Canvas>

A canvas with drawing added to its plan. Fails for a colour, a path or a font that cannot be parsed.

Source

pub fn encode( &self, format: OutputFormat, quality: Option<u8>, ) -> Result<Vec<u8>>

Decodes the source, runs the plan and writes format.

quality is 0 to 100; None keeps a JPEG source’s own quality and otherwise uses the format’s default. Metadata is not written.

Trait Implementations§

Source§

impl Clone for Canvas

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Canvas

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl Forget for Canvas

§

impl Freeze for Canvas

§

impl Move for Canvas

§

impl RefUnwindSafe for Canvas

§

impl Send for Canvas

§

impl Sync for Canvas

§

impl Unpin for Canvas

§

impl UnsafeUnpin 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> ErasedDestructor for T
where T: 'static,

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts self into a Left variant of Either<Self, Self> if into_left is true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts self into a Left variant of Either<Self, Self> if into_left(&self) returns true. Converts self into a Right variant of Either<Self, Self> otherwise. Read more
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

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

impl<T> Same for T

Source§

type Output = T

Should always be Self
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.