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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
mod blp0;
mod blp1;
mod blp2;
use super::error::Error;
use super::types::Parser;
use crate::types::*;
use blp0::parse_blp0;
use blp1::parse_raw1;
use blp2::{parse_dxtn, parse_raw3};
use log::*;
use nom::{error::context, multi::count, number::complete::le_u32, Err};
pub fn parse_direct_content<'a, F>(
blp_header: &BlpHeader,
external_mipmaps: F,
original_input: &'a [u8],
input: &'a [u8],
) -> Parser<'a, BlpContent>
where
F: FnMut(usize) -> Result<Option<&'a [u8]>, Box<dyn std::error::Error>>,
{
let (input, cmap) = context("color palette", count(le_u32, 256))(input)?;
match blp_header.flags {
BlpFlags::Blp2 {
compression,
alpha_type,
..
} => match blp_header.mipmap_locator {
MipmapLocator::External => {
error!("BLP2 doesn't support external mipmaps!");
Err(Err::Failure(Error::<&[u8]>::Blp2NoExternalMips))
}
MipmapLocator::Internal { offsets, sizes } => match compression {
Compression::Jpeg => {
// Should not go there as we process JPEG on content tag branching
// Although, BLP2 never uses JPEG, that branch is for totality of the code.
Err(Err::Failure(Error::Blp2UnexpectedJpegCompression))
}
Compression::Raw1 => {
let mut images = vec![];
let (input, _) = context("raw1 format", |input| {
parse_raw1(
blp_header,
original_input,
&offsets,
&sizes,
&mut images,
input,
)
})(input)?;
Ok((input, BlpContent::Raw1(BlpRaw1 { cmap, images })))
}
Compression::Raw3 => {
let mut images = vec![];
let (input, _) = context("raw3 format", |input| {
parse_raw3(
// Actually the same at parsing level
blp_header,
original_input,
&offsets,
&sizes,
&mut images,
input,
)
})(input)?;
Ok((input, BlpContent::Raw3(BlpRaw3 { cmap, images })))
}
Compression::Dxtc if alpha_type == 0 => {
let format = DxtnFormat::Dxt1;
let mut images = vec![];
let (input, _) = context("dxt1 format", |input| {
parse_dxtn(
blp_header,
format,
original_input,
&offsets,
&sizes,
&mut images,
input,
)
})(input)?;
Ok((
input,
BlpContent::Dxt1(BlpDxtn {
format,
cmap,
images,
}),
))
}
Compression::Dxtc if alpha_type == 1 => {
let format = DxtnFormat::Dxt3;
let mut images = vec![];
let (input, _) = context("dxt3 format", |input| {
parse_dxtn(
blp_header,
format,
original_input,
&offsets,
&sizes,
&mut images,
input,
)
})(input)?;
Ok((
input,
BlpContent::Dxt3(BlpDxtn {
format,
cmap,
images,
}),
))
}
Compression::Dxtc if alpha_type == 7 => {
let format = DxtnFormat::Dxt5;
let mut images = vec![];
let (input, _) = context("dxt5 format", |input| {
parse_dxtn(
blp_header,
format,
original_input,
&offsets,
&sizes,
&mut images,
input,
)
})(input)?;
Ok((
input,
BlpContent::Dxt5(BlpDxtn {
format,
cmap,
images,
}),
))
}
Compression::Dxtc => {
error!("Alpha type {} is not supported for BLP2!", alpha_type);
Err(Err::Failure(Error::<&[u8]>::Blp2UnknownAlphaType(
alpha_type,
)))
}
},
},
BlpFlags::Old { .. } => {
let mut images = vec![];
let (input, _) = match blp_header.mipmap_locator {
MipmapLocator::External => {
parse_blp0(blp_header, external_mipmaps, &mut images, input)?
}
MipmapLocator::Internal { offsets, sizes } => parse_raw1(
blp_header,
original_input,
&offsets,
&sizes,
&mut images,
input,
)?,
};
Ok((input, BlpContent::Raw1(BlpRaw1 { cmap, images })))
}
}
}