Skip to main content

image_blp/types/direct/
raw3.rs

1use super::super::{
2    header::{BlpHeader, BlpVersion},
3    locator::MipmapLocator,
4};
5
6#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
7pub struct BlpRaw3 {
8    /// The cmap field array is the colour look up table used for an indexed
9    /// colour model. Each element represents 24 bit RGB colour component values
10    /// in the order of 0xBBGGRR. The final byte is alignment padding and will
11    /// not alter the decoded image in any way. One might be able to improve the
12    /// file compressibility by carefully choosing padding values.
13    pub cmap: Vec<u32>,
14    /// Image itself and all mipmaps levels. If there are no mipmaps,
15    /// the length of the vector is 1.
16    pub images: Vec<Raw3Image>,
17}
18
19impl BlpRaw3 {
20    /// Predict internal locator to write down mipmaps
21    pub fn mipmap_locator(&self, version: BlpVersion) -> MipmapLocator {
22        let mut offsets = [0; 16];
23        let mut sizes = [0; 16];
24        let mut cur_offset = BlpHeader::size(version) + self.cmap.len() * 4;
25        for (i, image) in self.images.iter().take(16).enumerate() {
26            offsets[i] = cur_offset as u32;
27            sizes[i] = image.len() as u32;
28            cur_offset += image.len();
29        }
30
31        MipmapLocator::Internal { offsets, sizes }
32    }
33}
34
35/// Each mipmap contains what appears to be
36/// 32 bit BGRA data. `alpha_bits` seems to represent a set of bit flags
37/// rather than depth, as all images of this type seem to have 4 bytes per
38/// pixel regardless of depth, and it has been seen to exceed 8. Their
39/// meaning is unknown.
40#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
41pub struct Raw3Image {
42    pub pixels: Vec<u32>,
43}
44
45impl Raw3Image {
46    /// Get size in bytes of serialized image
47    pub fn len(&self) -> usize {
48        self.pixels.len() * 4
49    }
50
51    pub fn is_empty(&self) -> bool {
52        self.pixels.is_empty()
53    }
54}