use std::path::Path;
use image::GenericImageView;
use crate::widgets::image::ImageSource;
pub fn get_intrinsic_size(source: &ImageSource) -> Option<(u32, u32)> {
match source {
ImageSource::Path(path) => image::image_dimensions(path).ok(),
ImageSource::Bytes(bytes) => image::load_from_memory(bytes)
.ok()
.map(|img| img.dimensions()),
ImageSource::Rgba { width, height, .. } => Some((*width, *height)),
ImageSource::SvgPath(path) => get_svg_size_from_file(path),
ImageSource::SvgBytes(bytes) => get_svg_size_from_bytes(bytes),
}
}
#[cfg(feature = "svg")]
fn get_svg_size_from_file(path: &Path) -> Option<(u32, u32)> {
let data = std::fs::read(path).ok()?;
get_svg_size_from_bytes(&data)
}
#[cfg(feature = "svg")]
fn get_svg_size_from_bytes(bytes: &[u8]) -> Option<(u32, u32)> {
let tree = resvg::usvg::Tree::from_data(bytes, &resvg::usvg::Options::default()).ok()?;
let size = tree.size();
Some((size.width() as u32, size.height() as u32))
}
#[cfg(not(feature = "svg"))]
fn get_svg_size_from_file(_path: &Path) -> Option<(u32, u32)> {
log::warn!("SVG image used but the `svg` feature is disabled");
None
}
#[cfg(not(feature = "svg"))]
fn get_svg_size_from_bytes(_bytes: &[u8]) -> Option<(u32, u32)> {
log::warn!("SVG image used but the `svg` feature is disabled");
None
}