Skip to main content

source_vtf/image/
format.rs

1use num_enum::TryFromPrimitive;
2
3use crate::{Error, Result, Version};
4
5/// Image formats, numbered as in VTF 7.5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, TryFromPrimitive)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[repr(i32)]
9pub enum ImageFormat {
10    Rgba8888 = 0,
11    Abgr8888 = 1,
12    Rgb888 = 2,
13    Bgr888 = 3,
14    Rgb565 = 4,
15    I8 = 5,
16    Ia88 = 6,
17    P8 = 7,
18    A8 = 8,
19    Rgb888Bluescreen = 9,
20    Bgr888Bluescreen = 10,
21    Argb8888 = 11,
22    Bgra8888 = 12,
23    Dxt1 = 13,
24    Dxt3 = 14,
25    Dxt5 = 15,
26    Bgrx8888 = 16,
27    Bgr565 = 17,
28    Bgrx5551 = 18,
29    Bgra4444 = 19,
30    Dxt1OneBitAlpha = 20,
31    Bgra5551 = 21,
32    Uv88 = 22,
33    Uvwq8888 = 23,
34    Rgba16161616F = 24,
35    Rgba16161616 = 25,
36    Uvlx8888 = 26,
37    R32F = 27,
38    Rgb323232F = 28,
39    Rgba32323232F = 29,
40    Rg1616F = 30,
41    Rg3232F = 31,
42    Rgbx8888 = 32,
43    Empty = 33,
44    Ati2n = 34,
45    Ati1n = 35,
46    Rgba1010102 = 36,
47    Bgra1010102 = 37,
48    R16F = 38,
49}
50
51impl ImageFormat {
52    pub const fn is_block_compressed(self) -> bool {
53        self.block().0 == 4
54    }
55
56    /// Bytes of one `width` x `height` image, block-compressed formats rounded up to whole 4x4 blocks
57    pub const fn byte_len(self, width: u16, height: u16) -> usize {
58        let (block, bytes) = self.block();
59        (width as usize)
60            .div_ceil(block)
61            .saturating_mul((height as usize).div_ceil(block))
62            .saturating_mul(bytes)
63    }
64
65    pub const fn has_alpha(self) -> bool {
66        matches!(
67            self,
68            Self::Rgba8888
69                | Self::Abgr8888
70                | Self::Argb8888
71                | Self::Bgra8888
72                | Self::Dxt1OneBitAlpha
73                | Self::Dxt3
74                | Self::Dxt5
75                | Self::Bgra4444
76                | Self::Bgra5551
77                | Self::Ia88
78                | Self::A8
79                | Self::Rgba16161616F
80                | Self::Rgba16161616
81                | Self::Rgba32323232F
82                | Self::Rgba1010102
83                | Self::Bgra1010102
84        )
85    }
86
87    /// Side of a block in pixels and its size in bytes, uncompressed formats are 1x1 blocks
88    const fn block(self) -> (usize, usize) {
89        match self {
90            Self::Dxt1 | Self::Dxt1OneBitAlpha | Self::Ati1n => (4, 8),
91            Self::Dxt3 | Self::Dxt5 | Self::Ati2n => (4, 16),
92            Self::Rgba32323232F => (1, 16),
93            Self::Rgb323232F => (1, 12),
94            Self::Rgba16161616F | Self::Rgba16161616 | Self::Rg3232F => (1, 8),
95            Self::Rgba8888
96            | Self::Abgr8888
97            | Self::Argb8888
98            | Self::Bgra8888
99            | Self::Bgrx8888
100            | Self::Uvwq8888
101            | Self::Uvlx8888
102            | Self::R32F
103            | Self::Rg1616F
104            | Self::Rgbx8888
105            | Self::Rgba1010102
106            | Self::Bgra1010102 => (1, 4),
107            Self::Rgb888 | Self::Bgr888 | Self::Rgb888Bluescreen | Self::Bgr888Bluescreen => (1, 3),
108            Self::Rgb565
109            | Self::Ia88
110            | Self::Bgr565
111            | Self::Bgrx5551
112            | Self::Bgra4444
113            | Self::Bgra5551
114            | Self::Uv88
115            | Self::R16F => (1, 2),
116            Self::I8 | Self::P8 | Self::A8 => (1, 1),
117            Self::Empty => (1, 0),
118        }
119    }
120
121    /// Reads a format ID the way a file of `version` numbers them
122    pub(crate) fn from_wire(id: i32, version: Version, field: &'static str) -> Result<Self> {
123        let id = if version < Version::V7_5 {
124            // Before 7.5 it's SDK 2013's imageformat.h: 30..35 are NV/ATI depth formats, NV_NULL/ATI2N/ATI1N sit at 36..38
125            // 7.5 put RG1616F, RG3232F, RGBX8888 at 30..32 and moved those three down!
126            // todo: hmmmm, this keys the enum on the FILE version like 'SourcePP', but prob it's the engine branch that decides? check find and fix
127            match id {
128                30..=35 => return Err(Error::UnsupportedFormat { field, id }),
129                36..=38 => id - 3,
130                _ => id,
131            }
132        } else {
133            id
134        };
135
136        Self::try_from(id).map_err(|_| Error::InvalidField {
137            field,
138            value: id.into(),
139            reason: "unknown image format",
140        })
141    }
142}