Skip to main content

cursive_image/image/
image.rs

1use super::{format::*, source::*, stream::*};
2
3use {
4    cursive::*,
5    std::{cell::*, io, path::*, sync::*},
6};
7
8//
9// Image
10//
11
12/// Image.
13pub struct Image {
14    /// Source.
15    pub source: ImageSource,
16
17    /// Format.
18    pub format: ImageFormat,
19
20    /// Size (in pixels).
21    pub size: Vec2,
22
23    pub(crate) id: Mutex<RefCell<Option<usize>>>,
24    pub(crate) placement: Mutex<RefCell<Option<usize>>>,
25}
26
27impl Image {
28    /// Constructor.
29    pub fn new_owned<SizeT>(data: Vec<u8>, compressed: bool, format: ImageFormat, size: SizeT) -> Self
30    where
31        SizeT: Into<Vec2>,
32    {
33        Self {
34            source: ImageSource::Owned(data, compressed),
35            format,
36            size: size.into(),
37            id: Default::default(),
38            placement: Default::default(),
39        }
40    }
41
42    /// Constructor.
43    pub fn new_file<PathT, SizeT>(path: PathT, compressed: bool, format: ImageFormat, size: SizeT) -> Self
44    where
45        PathT: Into<PathBuf>,
46        SizeT: Into<Vec2>,
47    {
48        Self {
49            source: ImageSource::LocalFile(path.into(), compressed),
50            format,
51            size: size.into(),
52            id: Default::default(),
53            placement: Default::default(),
54        }
55    }
56
57    /// Constructor.
58    pub fn new_stream<ImageStreamT, SizeT>(stream: ImageStreamT, format: ImageFormat, size: SizeT) -> Self
59    where
60        ImageStreamT: 'static + ImageStream + Send + Sync,
61        SizeT: Into<Vec2>,
62    {
63        Self {
64            source: ImageSource::Stream(Box::new(stream)),
65            format,
66            size: size.into(),
67            id: Default::default(),
68            placement: Default::default(),
69        }
70    }
71
72    /// Into owned.
73    pub fn into_owned(&mut self) -> io::Result<()> {
74        self.source.into_owned()
75    }
76
77    pub(crate) fn get_id(&self) -> Option<usize> {
78        *self.id.lock().unwrap().borrow()
79    }
80
81    pub(crate) fn set_id(&self, id: Option<usize>) {
82        *self.id.lock().unwrap().borrow_mut() = id;
83    }
84
85    pub(crate) fn get_placement(&self) -> Option<usize> {
86        *self.placement.lock().unwrap().borrow()
87    }
88
89    pub(crate) fn set_placement(&self, placement: Option<usize>) {
90        *self.placement.lock().unwrap().borrow_mut() = placement;
91    }
92}