image_blp/types/
image.rs

1use super::direct::*;
2use super::header::*;
3use super::jpeg::*;
4pub use super::version::BlpVersion;
5
6/// Maximum width that BLP image can have due limitation
7/// of mipmaping storage.
8pub const BLP_MAX_WIDTH: u32 = 65535;
9/// Maximum height that BLP image can have due limitation
10/// of mipmaping storage.
11pub const BLP_MAX_HEIGHT: u32 = 65535;
12
13/// Parsed information from BLP file. The structure of the type
14/// strictly follows how the file is stored on the disk for
15/// easy encoding/decoding and further transformations.
16#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
17pub struct BlpImage {
18    pub header: BlpHeader,
19    pub content: BlpContent,
20}
21
22impl BlpImage {
23    /// Get total amount of images encoded in the content
24    pub fn image_count(&self) -> usize {
25        match &self.content {
26            BlpContent::Dxt1(v) => v.images.len(),
27            BlpContent::Dxt3(v) => v.images.len(),
28            BlpContent::Dxt5(v) => v.images.len(),
29            BlpContent::Raw1(v) => v.images.len(),
30            BlpContent::Raw3(v) => v.images.len(),
31            BlpContent::Jpeg(v) => v.images.len(),
32        }
33    }
34
35    /// If the image is encoded jpeg, return the content
36    pub fn content_jpeg(&self) -> Option<&BlpJpeg> {
37        self.content.jpeg()
38    }
39
40    /// If the image is direct encoded with BLP1 format, return the content
41    pub fn content_raw1(&self) -> Option<&BlpRaw1> {
42        self.content.raw1()
43    }
44
45    /// If the image is direct encoded with raw3 BLP2 format, return the content
46    pub fn content_raw3(&self) -> Option<&BlpRaw3> {
47        self.content.raw3()
48    }
49
50    /// If the image is DXT1 encoded, return the content
51    pub fn content_dxt1(&self) -> Option<&BlpDxtn> {
52        self.content.dxt1()
53    }
54
55    /// If the image is DXT3 encoded, return the content
56    pub fn ontent_dxt3(&self) -> Option<&BlpDxtn> {
57        self.content.dxt3()
58    }
59
60    /// If the image is DXT5 encoded, return the content
61    pub fn content_dxt5(&self) -> Option<&BlpDxtn> {
62        self.content.dxt5()
63    }
64}
65
66/// Collects all possible content types with actual data
67#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
68pub enum BlpContent {
69    Jpeg(BlpJpeg),
70    /// Used with direct type for BLP0/BLP1 and raw compression in BLP2
71    Raw1(BlpRaw1),
72    /// Used with direct type for BLP2, encodes RGBA bitmap.
73    Raw3(BlpRaw3),
74    /// BLP2 DXT1 compression (no alpha)
75    Dxt1(BlpDxtn),
76    /// BLP2 DXT3 compression (with alpha)
77    Dxt3(BlpDxtn),
78    /// BLP2 DXT5 compression (with alpha)
79    Dxt5(BlpDxtn),
80}
81
82impl BlpContent {
83    pub fn tag(&self) -> BlpContentTag {
84        match self {
85            BlpContent::Jpeg { .. } => BlpContentTag::Jpeg,
86            BlpContent::Raw1 { .. } => BlpContentTag::Direct,
87            BlpContent::Raw3 { .. } => BlpContentTag::Direct,
88            BlpContent::Dxt1 { .. } => BlpContentTag::Direct,
89            BlpContent::Dxt3 { .. } => BlpContentTag::Direct,
90            BlpContent::Dxt5 { .. } => BlpContentTag::Direct,
91        }
92    }
93
94    pub fn jpeg(&self) -> Option<&BlpJpeg> {
95        match self {
96            BlpContent::Jpeg(v) => Some(v),
97            _ => None,
98        }
99    }
100
101    pub fn raw1(&self) -> Option<&BlpRaw1> {
102        match self {
103            BlpContent::Raw1(v) => Some(v),
104            _ => None,
105        }
106    }
107
108    pub fn raw3(&self) -> Option<&BlpRaw3> {
109        match self {
110            BlpContent::Raw3(v) => Some(v),
111            _ => None,
112        }
113    }
114
115    pub fn dxt1(&self) -> Option<&BlpDxtn> {
116        match self {
117            BlpContent::Dxt1(v) => Some(v),
118            _ => None,
119        }
120    }
121
122    pub fn dxt3(&self) -> Option<&BlpDxtn> {
123        match self {
124            BlpContent::Dxt3(v) => Some(v),
125            _ => None,
126        }
127    }
128
129    pub fn dxt5(&self) -> Option<&BlpDxtn> {
130        match self {
131            BlpContent::Dxt5(v) => Some(v),
132            _ => None,
133        }
134    }
135}