Skip to main content

goud_engine/assets/loaders/texture/
format.rs

1//! [`TextureFormat`] — image file format enumeration.
2
3use image::ImageFormat;
4use std::fmt;
5
6/// Image format of a texture.
7///
8/// This enum represents the file format the texture was loaded from,
9/// not the in-memory pixel format (which is always RGBA8).
10#[repr(u8)]
11#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default)]
12pub enum TextureFormat {
13    /// PNG - Portable Network Graphics (default)
14    #[default]
15    Png = 0,
16    /// JPEG - Joint Photographic Experts Group
17    Jpeg = 1,
18    /// BMP - Windows Bitmap
19    Bmp = 2,
20    /// TGA - Truevision TGA
21    Tga = 3,
22    /// GIF - Graphics Interchange Format
23    Gif = 4,
24    /// WebP - Google WebP format
25    WebP = 5,
26    /// ICO - Windows Icon
27    Ico = 6,
28    /// TIFF - Tagged Image File Format
29    Tiff = 7,
30    /// Unknown or custom format
31    Unknown = 255,
32}
33
34impl TextureFormat {
35    /// Returns the file extension typically associated with this format.
36    pub const fn extension(&self) -> &'static str {
37        match self {
38            Self::Png => "png",
39            Self::Jpeg => "jpg",
40            Self::Bmp => "bmp",
41            Self::Tga => "tga",
42            Self::Gif => "gif",
43            Self::WebP => "webp",
44            Self::Ico => "ico",
45            Self::Tiff => "tiff",
46            Self::Unknown => "",
47        }
48    }
49
50    /// Returns the format name.
51    pub const fn name(&self) -> &'static str {
52        match self {
53            Self::Png => "PNG",
54            Self::Jpeg => "JPEG",
55            Self::Bmp => "BMP",
56            Self::Tga => "TGA",
57            Self::Gif => "GIF",
58            Self::WebP => "WebP",
59            Self::Ico => "ICO",
60            Self::Tiff => "TIFF",
61            Self::Unknown => "Unknown",
62        }
63    }
64
65    /// Detects the format from a file extension.
66    pub fn from_extension(ext: &str) -> Self {
67        match ext.to_lowercase().as_str() {
68            "png" => Self::Png,
69            "jpg" | "jpeg" => Self::Jpeg,
70            "bmp" => Self::Bmp,
71            "tga" => Self::Tga,
72            "gif" => Self::Gif,
73            "webp" => Self::WebP,
74            "ico" => Self::Ico,
75            "tif" | "tiff" => Self::Tiff,
76            _ => Self::Unknown,
77        }
78    }
79
80    /// Converts to the `image` crate's [`ImageFormat`].
81    pub(super) fn to_image_format(self) -> Option<ImageFormat> {
82        match self {
83            Self::Png => Some(ImageFormat::Png),
84            Self::Jpeg => Some(ImageFormat::Jpeg),
85            Self::Bmp => Some(ImageFormat::Bmp),
86            Self::Tga => Some(ImageFormat::Tga),
87            Self::Gif => Some(ImageFormat::Gif),
88            Self::WebP => Some(ImageFormat::WebP),
89            Self::Ico => Some(ImageFormat::Ico),
90            Self::Tiff => Some(ImageFormat::Tiff),
91            Self::Unknown => None,
92        }
93    }
94}
95
96impl fmt::Display for TextureFormat {
97    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
98        write!(f, "{}", self.name())
99    }
100}