1use crate::{extension::image_extension, mime::image_mime_type, size::ImageSize};
2
3#[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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
21pub enum ImageKind {
22 Raster,
23 Vector,
24 #[default]
25 Unknown,
26}
27
28#[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 #[must_use]
41 pub fn new(format: ImageFormat) -> Self {
42 Self::with_size(format, None)
43 }
44
45 #[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#[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#[must_use]
77pub const fn is_raster_image(format: ImageFormat) -> bool {
78 matches!(image_kind(format), ImageKind::Raster)
79}
80
81#[must_use]
83pub const fn is_vector_image(format: ImageFormat) -> bool {
84 matches!(image_kind(format), ImageKind::Vector)
85}
86
87#[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#[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#[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}