pub struct ImageSource { /* private fields */ }Expand description
A decode-once, cheaply-clonable handle around a decoded RGBA8 image.
Identity is the inner Arc pointer: ImageSource::same compares by
pointer, not pixel content, in O(1) regardless of image size — the signal
ImageView’s rebuild relies on to know “same image, nothing to redo”
without ever touching the decoder again.
Implementations§
Source§impl ImageSource
impl ImageSource
Sourcepub fn decode(bytes: &[u8]) -> Result<Self, ImageError>
pub fn decode(bytes: &[u8]) -> Result<Self, ImageError>
Decode PNG/JPEG bytes into straight-alpha RGBA8 once, wrapping the
result in a shareable handle. Callers own the resulting ImageSource
(typically stashing it in app state) and re-pass it into Image
every frame — this never re-decodes.
Sourcepub fn from_rgba8(pixels: Vec<u8>, width: u32, height: u32) -> Self
pub fn from_rgba8(pixels: Vec<u8>, width: u32, height: u32) -> Self
Wrap already-decoded straight-alpha RGBA8 pixel data directly (e.g. a
procedurally generated image, or a test fixture) without going through
the image crate’s decoder.
pixels.len() must equal width * height * 4; a mismatched length is
a caller bug and is not checked here (mirrors peniko::ImageData’s own
contract — the renderer will simply read width * height * 4 bytes).
Sourcepub fn same(&self, other: &ImageSource) -> bool
pub fn same(&self, other: &ImageSource) -> bool
Whether self and other share the same decoded data (Arc pointer
identity, not content equality) — never true for two independently
decoded sources even if their bytes matched, since each decode
allocates a fresh Arc.
Sourcepub fn natural_size(&self) -> Size
pub fn natural_size(&self) -> Size
The natural (decoded pixel) size.
Sourcepub fn image_data(&self) -> &ImageData
pub fn image_data(&self) -> &ImageData
Borrow the decoded peniko::ImageData this source wraps, for painting.
This returns a borrow of already-decoded pixels and triggers no decode.
The source caches pixel data once at construction via Self::decode;
this accessor hands back a reference to that cache. Calling this
repeatedly with the same ImageSource is zero-cost and does not
re-trigger the image decoder.