Skip to main content

Image

Struct Image 

Source
pub struct Image(/* private fields */);
Expand description

Immutable, cheap-to-clone RGBA image data.

Clones share both identity and pixels. That identity lets every window reuse one GPU texture until the renderer’s bounded cache needs the space for a newer visible image.

Implementations§

Source§

impl Image

Source

pub fn from_rgba( width: u32, height: u32, rgba: impl Into<Arc<[u8]>>, ) -> Result<Self, ImageError>

Construct an image from tightly packed, straight-alpha RGBA8 pixels.

Source

pub fn decode(encoded: impl AsRef<[u8]>) -> Result<Self, ImageError>

Decode PNG, JPEG, TIFF, WebP, or the first frame of a GIF from memory.

Decoding is explicit and synchronous. Applications should perform it away from latency- sensitive input handling and then publish the resulting cheap-to-clone Image.

Source

pub fn open(path: impl AsRef<Path>) -> Result<Self, ImageError>

Decode a supported image file synchronously with encoded and decoded size limits.

Prefer passing a path to crate::img for event-driven background loading. This method is useful when an application already owns a background execution context.

Paths whose stem ends in Template, optionally followed by a scale suffix such as @2x, are marked for macOS template rendering. Call Self::template to override that convention.

Source

pub fn width(&self) -> u32

Source

pub fn height(&self) -> u32

Source

pub fn size(&self) -> Size

Source

pub fn byte_len(&self) -> usize

Source

pub fn rgba(&self) -> &[u8] ⓘ

Tightly packed, straight-alpha RGBA8 pixels.

Source

pub fn from_data_url(url: &str) -> Result<Self, ImageError>

Decode a base64 data: URL carrying PNG, JPEG, GIF, or WebP bytes.

Only base64 payloads are accepted; percent-encoded data: URLs are rejected because the binary formats QuickGUI decodes are never sent that way in practice. The URL text is bounded by MAX_IMAGE_DATA_URL_BYTES and the decoded payload by the same limits as Image::decode.

Source

pub fn named_system(name: &str) -> Result<Self, ImageError>

Rasterize a system-provided image at DEFAULT_SYSTEM_IMAGE_POINT_SIZE and DEFAULT_SYSTEM_IMAGE_SCALE.

On macOS this resolves an NSImage name first and then an SF Symbol name. Other platforms report ImageError::SystemImageUnavailable.

Source

pub fn named_system_sized( name: &str, point_size: f32, scale: f32, ) -> Result<Self, ImageError>

Rasterize a system-provided image at a requested point size and backing scale.

The resulting bitmap is round(point_size * scale) pixels on its longest axis.

Source

pub fn template(&self, template: bool) -> Self

Return a copy marked as a macOS template image.

A template image is recolored by AppKit for the menu bar’s light, dark, and highlighted appearances using only its alpha channel. The returned value has a fresh identity so a renderer cache never confuses it with the untemplated original; the pixels are shared.

Source

pub fn is_template(&self) -> bool

Whether this image is marked for macOS template rendering.

Source

pub fn with_representations( &self, representations: impl IntoIterator<Item = (f32, Self)>, ) -> Result<Self, ImageError>

Attach additional backing-scale representations used when building a native image.

The receiver is the 1x representation. At most MAX_IMAGE_REPRESENTATIONS variants are retained and each scale must lie between MIN_IMAGE_REPRESENTATION_SCALE and MAX_IMAGE_REPRESENTATION_SCALE.

Source

pub fn representations(&self) -> &[(f32, Self)]

Additional backing-scale representations attached to this image.

Source

pub fn resize(&self, width: u32, height: u32) -> Result<Self, ImageError>

Resample this image to exact pixel dimensions with a bilinear filter.

Source

pub fn crop(&self, bounds: Rect) -> Result<Self, ImageError>

Copy a pixel rectangle out of this image.

The rectangle is rounded to whole pixels and must lie inside the image with a non-empty area.

Source

pub fn to_png(&self) -> Result<Vec<u8>, ImageError>

Encode this image as PNG with its alpha channel intact.

Source

pub fn to_jpeg(&self, quality: u8) -> Result<Vec<u8>, ImageError>

Encode this image as JPEG at quality (1-100), compositing away the alpha channel.

JPEG has no alpha, so translucent pixels are composited over opaque white.

Trait Implementations§

Source§

impl Clone for Image

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 Image

Source§

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

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

impl Eq for Image

Source§

impl From<&Image> for ImageSource

Source§

fn from(image: &Image) -> Self

Converts to this type from the input type.
Source§

impl From<Image> for ImageSource

Source§

fn from(image: Image) -> Self

Converts to this type from the input type.
Source§

impl PartialEq for Image

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more

Auto Trait Implementations§

§

impl Forget for Image

§

impl Freeze for Image

§

impl Move for Image

§

impl RefUnwindSafe for Image

§

impl Send for Image

§

impl Sync for Image

§

impl Unpin for Image

§

impl UnsafeUnpin for Image

§

impl UnwindSafe for Image

Blanket Implementations§

Source§

impl<T> Action for T
where T: Clone + PartialEq + 'static,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

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> Downcast for T
where T: Any,

Source§

fn into_any(self: Box<T>) -> Box<dyn Any>

Convert Box<dyn Trait> (where Trait: Downcast) to Box<dyn Any>. Box<dyn Any> can then be further downcast into Box<ConcreteType> where ConcreteType implements Trait.
Source§

fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>

Convert Rc<Trait> (where Trait: Downcast) to Rc<Any>. Rc<Any> can then be further downcast into Rc<ConcreteType> where ConcreteType implements Trait.
Source§

fn as_any(&self) -> &(dyn Any + 'static)

Convert &Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &Any’s vtable from &Trait’s.
Source§

fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)

Convert &mut Trait (where Trait: Downcast) to &Any. This is needed since Rust cannot generate &mut Any’s vtable from &mut Trait’s.
Source§

impl<T> Downcast<T> for T

Source§

fn downcast(&self) -> &T

Source§

impl<T> DowncastSync for T
where T: Any + Send + Sync,

Source§

fn into_any_arc(self: Arc<T>) -> Arc<dyn Any + Send + Sync> ⓘ

Convert Arc<Trait> (where Trait: Downcast) to Arc<Any>. Arc<Any> can then be further downcast into Arc<ConcreteType> where ConcreteType implements Trait.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self> ⓘ

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self> ⓘ

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

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

Source§

fn try_clone(&self) -> Result<T, Error>

Clones self, possibly returning an error.
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.
Source§

impl<T> Upcast<T> for T

Source§

fn upcast(&self) -> Option<&T>

Source§

impl<T> WasmNotSend for T
where T: Send,

Source§

impl<T> WasmNotSendSync for T

Source§

impl<T> WasmNotSync for T
where T: Sync,

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self> ⓘ
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self> ⓘ

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more