Struct slint::Image

pub struct Image(_);
Expand description

An image type that can be displayed by the Image element. You can construct Image objects from a path to an image file on disk, using Self::load_from_path.

Another typical use-case is to render the image content with Rust code. For this itโ€™s most efficient to create a new SharedPixelBuffer with the known dimensions and pass the mutable slice to your rendering function. Afterwards you can create an Image.

The following example creates a 320x200 RGB pixel buffer and calls an external low_level_render() function to draw a shape into it. Finally the result is stored in an Image with Self::from_rgb8():


fn low_level_render(width: u32, height: u32, buffer: &mut [u8]) {
    // render beautiful circle or other shapes here
}

let mut pixel_buffer = SharedPixelBuffer::<Rgb8Pixel>::new(320, 200);

low_level_render(pixel_buffer.width(), pixel_buffer.height(),
                 pixel_buffer.make_mut_bytes());

let image = Image::from_rgb8(pixel_buffer);

Another use-case is to import existing image data into Slint, by creating a new Image through cloning of another image type.

The following example uses the popular image crate to load a .png file from disk, apply brightening filter on it and then import it into an Image:

let mut cat_image = image::open("cat.png").expect("Error loading cat image").into_rgba8();

image::imageops::colorops::brighten_in_place(&mut cat_image, 20);

let buffer = SharedPixelBuffer::<Rgba8Pixel>::clone_from_slice(
    cat_image.as_raw(),
    cat_image.width(),
    cat_image.height(),
);
let image = Image::from_rgba8(buffer);

A popular software (CPU) rendering library in Rust is tiny-skia. The following example shows how to use tiny-skia to render into a SharedPixelBuffer:

let mut pixel_buffer = SharedPixelBuffer::<Rgba8Pixel>::new(640, 480);
let width = pixel_buffer.width();
let height = pixel_buffer.height();
let mut pixmap = tiny_skia::PixmapMut::from_bytes(
    pixel_buffer.make_mut_bytes(), width, height
).unwrap();
pixmap.fill(tiny_skia::Color::TRANSPARENT);

let circle = tiny_skia::PathBuilder::from_circle(320., 240., 150.).unwrap();

let mut paint = tiny_skia::Paint::default();
paint.shader = tiny_skia::LinearGradient::new(
    tiny_skia::Point::from_xy(100.0, 100.0),
    tiny_skia::Point::from_xy(400.0, 400.0),
    vec![
        tiny_skia::GradientStop::new(0.0, tiny_skia::Color::from_rgba8(50, 127, 150, 200)),
        tiny_skia::GradientStop::new(1.0, tiny_skia::Color::from_rgba8(220, 140, 75, 180)),
    ],
    tiny_skia::SpreadMode::Pad,
    tiny_skia::Transform::identity(),
).unwrap();

pixmap.fill_path(&circle, &paint, tiny_skia::FillRule::Winding, Default::default(), None);

let image = Image::from_rgba8_premultiplied(pixel_buffer);

Implementationsยง

ยง

impl Image

pub fn load_from_path(path: &Path) -> Result<Image, LoadImageError>

Load an Image from a path to a file containing an image

pub fn from_rgb8(buffer: SharedPixelBuffer<RGB<u8>>) -> Image

Creates a new Image from the specified shared pixel buffer, where each pixel has three color channels (red, green and blue) encoded as u8.

pub fn from_rgba8(buffer: SharedPixelBuffer<RGBA<u8, u8>>) -> Image

Creates a new Image from the specified shared pixel buffer, where each pixel has four color channels (red, green, blue and alpha) encoded as u8.

pub fn from_rgba8_premultiplied( buffer: SharedPixelBuffer<RGBA<u8, u8>> ) -> Image

Creates a new Image from the specified shared pixel buffer, where each pixel has four color channels (red, green, blue and alpha) encoded as u8 and, in contrast to Self::from_rgba8, the alpha channel is also assumed to be multiplied to the red, green and blue channels.

Only construct an Image with this function if you know that your pixels are encoded this way.

pub fn size(&self) -> Size2D<u32, UnknownUnit>

Returns the size of the Image in pixels.

pub fn path(&self) -> Option<&Path>

Returns the path of the image on disk, if it was constructed via Self::load_from_path.

For example:

let path_buf = Path::new(env!("CARGO_MANIFEST_DIR"))
    .join("../../examples/printerdemo/ui/images/cat.jpg");
let image = Image::load_from_path(&path_buf).unwrap();
assert_eq!(image.path(), Some(path_buf.as_path()));

Trait Implementationsยง

ยง

impl Clone for Image

ยง

fn clone(&self) -> Image

Returns a copy of the value. Read more
1.0.0 ยท sourceยง

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

Performs copy-assignment from source. Read more
ยง

impl Debug for Image

ยง

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

Formats the value using the given formatter. Read more
ยง

impl Default for Image

ยง

fn default() -> Image

Returns the โ€œdefault valueโ€ for a type. Read more
ยง

impl<'a> From<&'a Image> for &'a ImageInner

ยง

fn from(other: &'a Image) -> &'a ImageInner

Converts to this type from the input type.
ยง

impl From<ImageInner> for Image

ยง

fn from(original: ImageInner) -> Image

Converts to this type from the input type.
ยง

impl PartialEq<Image> for Image

ยง

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

This method tests for self and other values to be equal, and is used by ==.
1.0.0 ยท sourceยง

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

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
ยง

impl StructuralPartialEq for Image

Auto Trait Implementationsยง

Blanket Implementationsยง

sourceยง

impl<T> Any for Twhere T: 'static + ?Sized,

sourceยง

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
sourceยง

impl<T> Borrow<T> for Twhere T: ?Sized,

const: unstable ยท sourceยง

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
sourceยง

impl<T> BorrowMut<T> for Twhere T: ?Sized,

const: unstable ยท sourceยง

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
ยง

impl<T> Downcast for Twhere T: Any,

ยง

fn into_any(self: Box<T, Global>) -> Box<dyn Any + 'static, Global>

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.
ยง

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

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

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.
ยง

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

const: unstable ยท 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 Twhere U: From<T>,

const: unstable ยท 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.

ยง

impl<T> NoneValue for Twhere T: Default,

ยง

type NoneType = T

ยง

fn null_value() -> T

The none-equivalent value.
ยง

impl<T> Pointable for T

ยง

const ALIGN: usize = mem::align_of::<T>()

The alignment of pointer.
ยง

type Init = T

The type for initializers.
ยง

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

Initializes a with the given initializer. Read more
ยง

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

Dereferences the given pointer. Read more
ยง

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

Mutably dereferences the given pointer. Read more
ยง

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
sourceยง

impl<R, P> ReadPrimitive<R> for Pwhere R: Read + ReadEndian<P>, P: Default,

sourceยง

fn read_from_little_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_little_endian().
sourceยง

fn read_from_big_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_big_endian().
sourceยง

fn read_from_native_endian(read: &mut R) -> Result<Self, Error>

Read this value from the supplied reader. Same as ReadEndian::read_from_native_endian().
sourceยง

impl<T> Same<T> for T

ยง

type Output = T

Should always be Self
sourceยง

impl<T> ToOwned for Twhere T: Clone,

ยง

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 Twhere U: Into<T>,

ยง

type Error = Infallible

The type returned in the event of a conversion error.
const: unstable ยท sourceยง

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
sourceยง

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

ยง

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
const: unstable ยท sourceยง

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
ยง

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

ยง

fn vzip(self) -> V

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