slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
17.73k stars 614 forks source link

Need function to read the bytes inside `Image` #2571

Closed AllenDang closed 2 months ago

AllenDang commented 1 year ago

Here is my use case:

  1. Load a png image via image crate.
  2. Perform some drawing on image.
  3. Display the image by converting it via SharedPixelBuffer to Image.
  4. Allow use to press a button to copy the image to system clipboard via arboard.

Currently I use a callback which contains a image type parameter like callback request_copy_image(image), at rust side, the pass in parameter's type is Image, which has no function to get inner bytes.

I could store the bytes in global state to work around now, but I think it will be good to have related functions to get inner data of Image.

tronical commented 1 year ago

I think it makes sense to offer API in slint::Image to provide access to a SharedPixelBuffer, if that is easily available. I wonder if the API is perhaps just an implementation of TryFrom<slint::Image> for SharedPixelBuffer<Rgba8Pixel>, that fails if the image is not trivially convertible.

Another option would be a counter-part to from_rgba8 and friends, such as to_rgba8 that returns an Option or a Result.

I'm leaning toward TryFrom.

ogoffart commented 2 months ago

Done: https://docs.rs/slint/latest/slint/struct.Image.html#method.to_rgba8 (and similar)