use super::{format::*, source::*, stream::*};
use {
cursive::*,
std::{cell::*, io, path::*, sync::*},
};
pub struct Image {
pub source: ImageSource,
pub format: ImageFormat,
pub size: Vec2,
pub(crate) id: Mutex<RefCell<Option<usize>>>,
pub(crate) placement: Mutex<RefCell<Option<usize>>>,
}
impl Image {
pub fn new_owned<SizeT>(data: Vec<u8>, compressed: bool, format: ImageFormat, size: SizeT) -> Self
where
SizeT: Into<Vec2>,
{
Self {
source: ImageSource::Owned(data, compressed),
format,
size: size.into(),
id: Default::default(),
placement: Default::default(),
}
}
pub fn new_file<PathT, SizeT>(path: PathT, compressed: bool, format: ImageFormat, size: SizeT) -> Self
where
PathT: Into<PathBuf>,
SizeT: Into<Vec2>,
{
Self {
source: ImageSource::LocalFile(path.into(), compressed),
format,
size: size.into(),
id: Default::default(),
placement: Default::default(),
}
}
pub fn new_stream<ImageStreamT, SizeT>(stream: ImageStreamT, format: ImageFormat, size: SizeT) -> Self
where
ImageStreamT: 'static + ImageStream + Send + Sync,
SizeT: Into<Vec2>,
{
Self {
source: ImageSource::Stream(Box::new(stream)),
format,
size: size.into(),
id: Default::default(),
placement: Default::default(),
}
}
pub fn into_owned(&mut self) -> io::Result<()> {
self.source.into_owned()
}
pub(crate) fn get_id(&self) -> Option<usize> {
*self.id.lock().unwrap().borrow()
}
pub(crate) fn set_id(&self, id: Option<usize>) {
*self.id.lock().unwrap().borrow_mut() = id;
}
pub(crate) fn get_placement(&self) -> Option<usize> {
*self.placement.lock().unwrap().borrow()
}
pub(crate) fn set_placement(&self, placement: Option<usize>) {
*self.placement.lock().unwrap().borrow_mut() = placement;
}
}