use crate::{extension::image_extension, mime::image_mime_type, size::ImageSize};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ImageFormat {
Png,
Jpeg,
Webp,
Gif,
Svg,
Ico,
Bmp,
Tiff,
Avif,
#[default]
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
pub enum ImageKind {
Raster,
Vector,
#[default]
Unknown,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ImageMetadata {
pub format: ImageFormat,
pub kind: ImageKind,
pub mime_type: Option<&'static str>,
pub extension: Option<&'static str>,
pub size: Option<ImageSize>,
}
impl ImageMetadata {
#[must_use]
pub fn new(format: ImageFormat) -> Self {
Self::with_size(format, None)
}
#[must_use]
pub fn with_size(format: ImageFormat, size: Option<ImageSize>) -> Self {
Self {
format,
kind: image_kind(format),
mime_type: image_mime_type(format),
extension: image_extension(format),
size,
}
}
}
#[must_use]
pub const fn image_kind(format: ImageFormat) -> ImageKind {
match format {
ImageFormat::Svg => ImageKind::Vector,
ImageFormat::Png
| ImageFormat::Jpeg
| ImageFormat::Webp
| ImageFormat::Gif
| ImageFormat::Ico
| ImageFormat::Bmp
| ImageFormat::Tiff
| ImageFormat::Avif => ImageKind::Raster,
ImageFormat::Unknown => ImageKind::Unknown,
}
}
#[must_use]
pub const fn is_raster_image(format: ImageFormat) -> bool {
matches!(image_kind(format), ImageKind::Raster)
}
#[must_use]
pub const fn is_vector_image(format: ImageFormat) -> bool {
matches!(image_kind(format), ImageKind::Vector)
}
#[must_use]
pub const fn is_web_image_format(format: ImageFormat) -> bool {
matches!(
format,
ImageFormat::Png
| ImageFormat::Jpeg
| ImageFormat::Webp
| ImageFormat::Gif
| ImageFormat::Svg
| ImageFormat::Ico
| ImageFormat::Avif
)
}
#[must_use]
pub const fn supports_transparency(format: ImageFormat) -> bool {
matches!(
format,
ImageFormat::Png
| ImageFormat::Webp
| ImageFormat::Gif
| ImageFormat::Svg
| ImageFormat::Ico
| ImageFormat::Tiff
| ImageFormat::Avif
)
}
#[must_use]
pub const fn supports_animation(format: ImageFormat) -> bool {
matches!(
format,
ImageFormat::Gif | ImageFormat::Webp | ImageFormat::Svg | ImageFormat::Avif
)
}