Skip to main content

use_image/
format.rs

1use crate::{extension::image_extension, mime::image_mime_type, size::ImageSize};
2
3/// Common image formats handled by this crate.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
5pub enum ImageFormat {
6    Png,
7    Jpeg,
8    Webp,
9    Gif,
10    Svg,
11    Ico,
12    Bmp,
13    Tiff,
14    Avif,
15    #[default]
16    Unknown,
17}
18
19/// Broad classification for image container types.
20#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
21pub enum ImageKind {
22    Raster,
23    Vector,
24    #[default]
25    Unknown,
26}
27
28/// Small image metadata bundle for callers that already know or discovered a format.
29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
30pub struct ImageMetadata {
31    pub format: ImageFormat,
32    pub kind: ImageKind,
33    pub mime_type: Option<&'static str>,
34    pub extension: Option<&'static str>,
35    pub size: Option<ImageSize>,
36}
37
38impl ImageMetadata {
39    /// Builds metadata from a detected format without embedded size information.
40    #[must_use]
41    pub fn new(format: ImageFormat) -> Self {
42        Self::with_size(format, None)
43    }
44
45    /// Builds metadata from a detected format and optional size information.
46    #[must_use]
47    pub fn with_size(format: ImageFormat, size: Option<ImageSize>) -> Self {
48        Self {
49            format,
50            kind: image_kind(format),
51            mime_type: image_mime_type(format),
52            extension: image_extension(format),
53            size,
54        }
55    }
56}
57
58/// Returns the broad image kind for a format.
59#[must_use]
60pub const fn image_kind(format: ImageFormat) -> ImageKind {
61    match format {
62        ImageFormat::Svg => ImageKind::Vector,
63        ImageFormat::Png
64        | ImageFormat::Jpeg
65        | ImageFormat::Webp
66        | ImageFormat::Gif
67        | ImageFormat::Ico
68        | ImageFormat::Bmp
69        | ImageFormat::Tiff
70        | ImageFormat::Avif => ImageKind::Raster,
71        ImageFormat::Unknown => ImageKind::Unknown,
72    }
73}
74
75/// Returns true when the format is raster-based.
76#[must_use]
77pub const fn is_raster_image(format: ImageFormat) -> bool {
78    matches!(image_kind(format), ImageKind::Raster)
79}
80
81/// Returns true when the format is vector-based.
82#[must_use]
83pub const fn is_vector_image(format: ImageFormat) -> bool {
84    matches!(image_kind(format), ImageKind::Vector)
85}
86
87/// Returns true for formats commonly used in browsers and web asset pipelines.
88#[must_use]
89pub const fn is_web_image_format(format: ImageFormat) -> bool {
90    matches!(
91        format,
92        ImageFormat::Png
93            | ImageFormat::Jpeg
94            | ImageFormat::Webp
95            | ImageFormat::Gif
96            | ImageFormat::Svg
97            | ImageFormat::Ico
98            | ImageFormat::Avif
99    )
100}
101
102/// Returns true when the format can commonly carry transparency.
103#[must_use]
104pub const fn supports_transparency(format: ImageFormat) -> bool {
105    matches!(
106        format,
107        ImageFormat::Png
108            | ImageFormat::Webp
109            | ImageFormat::Gif
110            | ImageFormat::Svg
111            | ImageFormat::Ico
112            | ImageFormat::Tiff
113            | ImageFormat::Avif
114    )
115}
116
117/// Returns true when the format commonly supports animation.
118#[must_use]
119pub const fn supports_animation(format: ImageFormat) -> bool {
120    matches!(
121        format,
122        ImageFormat::Gif | ImageFormat::Webp | ImageFormat::Svg | ImageFormat::Avif
123    )
124}