use std::path::PathBuf;
pub struct Tile
{
pub(crate) tile_x: u32,
pub(crate) tile_y: u32,
pub(crate) tile_z: u32,
pub(crate) filename: PathBuf,
pub(crate) bounding_box: geo::Rect,
pub(crate) image: ccutils::sync::ArcMutex<Option<image::DynamicImage>>,
}
impl Tile
{
fn load_image(&self)
{
if !self.filename.exists() || self.image.lock().is_ok_and(|x| x.is_some())
{
return;
}
let image = image::open(&self.filename);
match image
{
Ok(image) =>
{
*self.image.lock().unwrap() = Some(image);
}
Err(e) =>
{
log::error!(
"Failed to read image {} with {:?}",
self.filename.display(),
e
);
}
}
}
pub fn tile_x(&self) -> u32
{
self.tile_x
}
pub fn tile_y(&self) -> u32
{
self.tile_y
}
pub fn tile_z(&self) -> u32
{
self.tile_z
}
pub fn bounding_box(&self) -> geo::Rect
{
self.bounding_box
}
pub fn filename(&self) -> &PathBuf
{
&self.filename
}
pub fn is_cached(&self) -> bool
{
self.filename.exists()
}
pub fn pixel_size(&self) -> (u32, u32)
{
self.load_image();
self.image.lock().map_or(Default::default(), |x| {
x.as_ref()
.map_or(Default::default(), |x| (x.width(), x.height()))
})
}
pub fn rgba_data(&self) -> Vec<u8>
{
self.load_image();
self.image.lock().map_or(Default::default(), |x| {
x.as_ref()
.map_or(Default::default(), |x| x.to_rgba8().into_raw().to_vec())
})
}
}