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
impl Canvas
Sourcepub fn decode(data: &[u8], limits: Limits) -> Result<Canvas>
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).
Sourcepub fn decode_accepting(
data: &[u8],
limits: Limits,
accept: &[Format],
) -> Result<Canvas>
pub fn decode_accepting( data: &[u8], limits: Limits, accept: &[Format], ) -> Result<Canvas>
As decode, refusing any format not in accept (every format when empty).
Sourcepub fn blank(
width: u32,
height: u32,
background: &str,
limits: Limits,
) -> Result<Canvas>
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).
Sourcepub fn orientation(&self) -> u32
pub fn orientation(&self) -> u32
The EXIF orientation of the source, 1 to 8 (1 when it has none).
Sourcepub fn apply(&self, transform: Transform) -> Result<Canvas>
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.
Trait Implementations§
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> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
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 moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
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