image_blp/types/direct/
dxtn.rs

1use super::super::{
2    header::{BlpHeader, BlpVersion},
3    locator::MipmapLocator,
4};
5
6/// Which compression algorithm is used to compress the image
7#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
8pub enum DxtnFormat {
9    Dxt1,
10    Dxt3,
11    Dxt5,
12}
13
14impl From<DxtnFormat> for texpresso::Format {
15    fn from(v: DxtnFormat) -> texpresso::Format {
16        match v {
17            DxtnFormat::Dxt1 => texpresso::Format::Bc1,
18            DxtnFormat::Dxt3 => texpresso::Format::Bc2,
19            DxtnFormat::Dxt5 => texpresso::Format::Bc3,
20        }
21    }
22}
23
24impl DxtnFormat {
25    pub fn block_size(&self) -> usize {
26        match self {
27            DxtnFormat::Dxt1 => 8,
28            DxtnFormat::Dxt3 => 16,
29            DxtnFormat::Dxt5 => 16,
30        }
31    }
32}
33
34#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
35pub struct BlpDxtn {
36    pub format: DxtnFormat,
37    pub cmap: Vec<u32>,
38    pub images: Vec<DxtnImage>,
39}
40
41impl BlpDxtn {
42    /// Predict internal locator to write down mipmaps
43    pub fn mipmap_locator(&self, version: BlpVersion) -> MipmapLocator {
44        let mut offsets = [0; 16];
45        let mut sizes = [0; 16];
46        let mut cur_offset = BlpHeader::size(version) + self.cmap.len() * 4;
47        for (i, image) in self.images.iter().take(16).enumerate() {
48            offsets[i] = cur_offset as u32;
49            sizes[i] = image.len() as u32;
50            cur_offset += image.len();
51        }
52
53        MipmapLocator::Internal { offsets, sizes }
54    }
55}
56
57#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
58pub struct DxtnImage {
59    pub content: Vec<u8>,
60}
61
62impl DxtnImage {
63    /// Get size in bytes of serialized image
64    pub fn len(&self) -> usize {
65        self.content.len()
66    }
67
68    pub fn is_empty(&self) -> bool {
69        self.content.is_empty()
70    }
71}