image_blp/types/direct/
dxtn.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
use super::super::{
    header::{BlpHeader, BlpVersion},
    locator::MipmapLocator,
};

/// Which compression algorithm is used to compress the image
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum DxtnFormat {
    Dxt1,
    Dxt3,
    Dxt5,
}

impl From<DxtnFormat> for texpresso::Format {
    fn from(v: DxtnFormat) -> texpresso::Format {
        match v {
            DxtnFormat::Dxt1 => texpresso::Format::Bc1,
            DxtnFormat::Dxt3 => texpresso::Format::Bc2,
            DxtnFormat::Dxt5 => texpresso::Format::Bc3,
        }
    }
}

impl DxtnFormat {
    pub fn block_size(&self) -> usize {
        match self {
            DxtnFormat::Dxt1 => 8,
            DxtnFormat::Dxt3 => 16,
            DxtnFormat::Dxt5 => 16,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BlpDxtn {
    pub format: DxtnFormat,
    pub cmap: Vec<u32>,
    pub images: Vec<DxtnImage>,
}

impl BlpDxtn {
    /// Predict internal locator to write down mipmaps
    pub fn mipmap_locator(&self, version: BlpVersion) -> MipmapLocator {
        let mut offsets = [0; 16];
        let mut sizes = [0; 16];
        let mut cur_offset = BlpHeader::size(version) + self.cmap.len() * 4;
        for (i, image) in self.images.iter().take(16).enumerate() {
            offsets[i] = cur_offset as u32;
            sizes[i] = image.len() as u32;
            cur_offset += image.len();
        }

        MipmapLocator::Internal { offsets, sizes }
    }
}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct DxtnImage {
    pub content: Vec<u8>,
}

impl DxtnImage {
    /// Get size in bytes of serialized image
    pub fn len(&self) -> usize {
        self.content.len()
    }

    pub fn is_empty(&self) -> bool {
        self.content.is_empty()
    }
}