Skip to main content

unbanished_formats/types/
image.rs

1//! Image buffers: the pixel formats, the faces and the mip levels of a texture.
2
3use super::*;
4
5/// `Video::ImageBuffer::Format`, mapped to DXGI by the VideoDX11 texture-creation table.
6#[derive(Debug, Clone, Copy, PartialEq, Eq)]
7pub enum ImageFormat {
8    /// BC1 (DXT1) blocks.
9    Bc1,
10    /// BC3 (DXT5) blocks.
11    Bc3,
12    /// One byte of alpha per pixel.
13    A8,
14    /// One byte of red per pixel.
15    R8,
16    /// Four bytes per pixel: blue, green, red, alpha.
17    Bgra8,
18    /// One 32-bit float per pixel.
19    R32F,
20    /// 24-bit depth and 8-bit stencil per pixel (*inferred*).
21    R24G8,
22}
23
24/// A texture: its size, format and levels (mip maps, and the six faces of a cube map).
25#[derive(Debug, Clone)]
26pub struct ImageBuffer {
27    /// The width in pixels.
28    pub width: u32,
29    /// The height in pixels.
30    pub height: u32,
31    /// 0 = 2D; otherwise a cube map (levels are 6 faces x mips).
32    pub image_type: u32,
33    /// How the engine uses the texture.
34    pub usage: u32,
35    /// The pixel format.
36    pub format: ImageFormat,
37    /// Whether the engine makes the mip levels itself.
38    pub generate_mips: bool,
39    /// The address (wrap) mode on each axis.
40    pub address: [u32; 3],
41    /// The filter mode.
42    pub filter: u32,
43    /// The original image file the texture was made from.
44    pub source: String,
45    /// The data of each level: faces × mips, face by face.
46    pub levels: Vec<Vec<u8>>,
47}
48
49impl ImageFormat {
50    pub(super) fn from_raw(v: u32) -> Result<Self> {
51        Ok(match v {
52            0 => Self::Bc1,
53            1 | 2 => Self::Bc3,
54            3 => Self::A8,
55            4 => Self::R8,
56            5 => Self::Bgra8,
57            6 => Self::R32F,
58            7 => Self::R24G8,
59            _ => return Err(Error::Unsupported(format!("image format {v}"))),
60        })
61    }
62
63    /// Byte size of one level.
64    pub fn level_size(self, w: u32, h: u32) -> usize {
65        let (w, h) = (w.max(1) as usize, h.max(1) as usize);
66        let blocks = w.div_ceil(4) * h.div_ceil(4);
67        match self {
68            Self::Bc1 => blocks * 8,
69            Self::Bc3 => blocks * 16,
70            Self::A8 | Self::R8 => w * h,
71            Self::Bgra8 | Self::R32F | Self::R24G8 => w * h * 4,
72        }
73    }
74}
75
76impl ImageBuffer {
77    /// The resource class name.
78    pub const CLASS: &'static str = "ImageBuffer";
79
80    /// `Video::ImageBuffer::Serialize` + `PlatformSerialize`.
81    ///
82    /// Original: Runtime `0x1800f4f40`, Runtime `0x1800f5980`.
83    pub fn read(r: &mut Reader) -> Result<Self> {
84        r.block(|r, _| {
85            let width = r.u32()?;
86            let height = r.u32()?;
87            let image_type = r.u32()?;
88            let usage = r.u32()?;
89            let format = ImageFormat::from_raw(r.u32()?)?;
90            let generate_mips = r.u32()? != 0;
91            let address = [r.u32()?, r.u32()?, r.u32()?];
92            let filter = r.u32()?;
93            let source = r.string()?;
94            let mut levels = Vec::new();
95            if !matches!(usage, 4 | 5) {
96                for _ in 0..r.count()? {
97                    let n = r.count()?;
98                    levels.push(r.bytes(n)?.to_vec());
99                }
100            }
101            Ok(ImageBuffer {
102                width,
103                height,
104                image_type,
105                usage,
106                format,
107                generate_mips,
108                address,
109                filter,
110                source,
111                levels,
112            })
113        })
114    }
115
116    /// The number of faces: 6 for a cube map, else 1.
117    pub fn faces(&self) -> usize {
118        if self.image_type == 0 {
119            1
120        } else {
121            6
122        }
123    }
124
125    /// The number of mip levels of each face.
126    pub fn mip_count(&self) -> usize {
127        self.levels.len() / self.faces()
128    }
129}