pytauri_core/ext_mod_impl/
image.rs

1use pyo3::{prelude::*, types::PyBytes};
2use tauri::image;
3
4/// See also: [tauri::image::Image]
5#[pyclass(frozen, subclass)] // subclass for `pillow`
6#[non_exhaustive]
7pub struct Image {
8    // PERF: maybe we can use `memoryview` or `buffer protocol`.
9    rgba: Py<PyBytes>,
10    width: u32,
11    height: u32,
12}
13
14impl Image {
15    pub(crate) fn to_tauri<'a>(&'a self, py: Python<'_>) -> image::Image<'a> {
16        image::Image::new(self.rgba.as_bytes(py), self.width, self.height)
17    }
18
19    pub(crate) fn from_tauri(py: Python<'_>, image: &image::Image) -> Self {
20        Self {
21            rgba: PyBytes::new(py, image.rgba()).unbind(),
22            width: image.width(),
23            height: image.height(),
24        }
25    }
26}
27
28#[pymethods]
29impl Image {
30    #[new]
31    const fn __new__(rgba: Py<PyBytes>, width: u32, height: u32) -> Self {
32        Self {
33            rgba,
34            width,
35            height,
36        }
37    }
38
39    const fn rgba(&self) -> &Py<PyBytes> {
40        &self.rgba
41    }
42
43    const fn width(&self) -> u32 {
44        self.width
45    }
46
47    const fn height(&self) -> u32 {
48        self.height
49    }
50}