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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
//! BC1 (DXT1) block texture compression.
//!
//! BC1 compresses RGB texels into 8-byte blocks. Each block stores two [`Rgb565`]
//! endpoint colors and a 4×4 grid of 2-bit indices into a 4-entry interpolated palette.
use std::{convert::Infallible, mem::swap};
use crate::{
cluster_fit::cluster_fit,
math::{Rgb32F, Rgb565, Rgba32F, Vec3, Yiq32F},
};
/// A block of 4x4 texels compressed with BC1.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
#[repr(C)]
pub struct Block {
pub color0: Rgb565,
pub color1: Rgb565,
pub texels: [u8; 4],
}
impl_fixedcode_struct!(
Block {
color0: Rgb565,
color1: Rgb565,
texels: [u8; 4],
} | Infallible
);
impl Block {
pub const BLACK: Block = Block {
color0: Rgb565::BLACK,
color1: Rgb565::BLACK,
texels: [0x00; 4],
};
pub const WHITE: Block = Block {
color0: Rgb565::WHITE,
color1: Rgb565::WHITE,
texels: [0x00; 4],
};
pub const TRANSPARENT: Block = Block {
color0: Rgb565::BLACK,
color1: Rgb565::BLACK,
texels: [0xFF; 4],
};
/// Returns the raw 8-byte representation of this block.
pub fn bytes(&self) -> [u8; 8] {
let color0 = self.color0.bytes();
let color1 = self.color1.bytes();
let texels = self.texels;
[
color0[0], color0[1], color1[0], color1[1], texels[0], texels[1], texels[2], texels[3],
]
}
/// Constructs a `Block` from its raw 8-byte representation.
pub fn from_bytes(bytes: [u8; 8]) -> Block {
let color0 = Rgb565::from_bytes([bytes[0], bytes[1]]);
let color1 = Rgb565::from_bytes([bytes[2], bytes[3]]);
let texels = [bytes[4], bytes[5], bytes[6], bytes[7]];
Block {
color0,
color1,
texels,
}
}
/// Decodes single BC1 block.
pub fn decode(self) -> [[Rgb32F; 4]; 4] {
// Decode endpoints.
let color0 = self.color0.into_f32();
let color1 = self.color1.into_f32();
// Prepare local variables.
let mut colors = [[Rgb32F::BLACK; 4]; 4];
let texels = self.texels;
// Check mode and build palette.
let palette = if self.color0.bits() > self.color1.bits() {
// Interpolate two intermediate colors.
[
color0,
color1,
Rgb32F::lerp(color0, color1, 1.0 / 3.0),
Rgb32F::lerp(color0, color1, 2.0 / 3.0),
]
} else {
// Interpolate one intermediate color.
[
color0,
color1,
Rgb32F::lerp(color0, color1, 1.0 / 2.0),
Rgb32F::BLACK,
]
};
// Decode texels.
for y in 0..4 {
for x in 0..4 {
let index = (texels[y] >> (2 * x)) & 0b11;
colors[y][x] = palette[index as usize];
}
}
colors
}
/// Decodes single BC1 block.
pub fn decode_with_alpha(self) -> [[Rgba32F; 4]; 4] {
// Decode endpoints.
let color0 = self.color0.into_f32();
let color1 = self.color1.into_f32();
// Prepare local variables.
let mut colors = [[Rgba32F::TRANSPARENT; 4]; 4];
let texels = self.texels;
// Check mode and build palette.
let palette = if self.color0.bits() > self.color1.bits() {
// Interpolate two intermediate colors.
[
color0.with_alpha(1.0),
color1.with_alpha(1.0),
Rgb32F::lerp(color0, color1, 1.0 / 3.0).with_alpha(1.0),
Rgb32F::lerp(color0, color1, 2.0 / 3.0).with_alpha(1.0),
]
} else {
// Interpolate one intermediate color.
[
color0.with_alpha(1.0),
color1.with_alpha(1.0),
Rgb32F::lerp(color0, color1, 1.0 / 2.0).with_alpha(1.0),
Rgba32F::TRANSPARENT,
]
};
// Decode texels.
for y in 0..4 {
for x in 0..4 {
let index = (texels[y] >> (2 * x)) & 0b11;
colors[y][x] = palette[index as usize];
}
}
colors
}
/// Encodes a 4×4 grid of RGB colors into a BC1 block.
pub fn encode(colors: [[Rgb32F; 4]; 4]) -> Self {
let mut samples = [Vec3::ZERO; 16];
for y in 0..4 {
for x in 0..4 {
samples[y * 4 + x] = colors[y][x].into();
}
}
let mut cf = cluster_fit::<Vec3, 4, 16>(
&samples,
|a: Vec3, b: Vec3| {
let a = Rgb565::from_f32(a.into());
let b = Rgb565::from_f32(b.into());
(a.into_f32().into(), b.into_f32().into())
},
|a: Vec3, b: Vec3| {
let a = Rgb32F::from(a);
let b = Rgb32F::from(b);
let a = Yiq32F::from_rgb(a);
let b = Yiq32F::from_rgb(b);
Yiq32F::perceptual_distance(a, b)
},
);
let (color0, color1) = cf.endpoints;
let mut color0 = Rgb565::from_f32(Rgb32F::from(color0));
let mut color1 = Rgb565::from_f32(Rgb32F::from(color1));
if color0 == color1 {
return Block {
color0,
color1: Rgb565::BLACK,
texels: [0x00; 4],
};
} else if color0.bits() < color1.bits() {
swap(&mut color0, &mut color1);
for index in &mut cf.indices {
*index = 3 - *index;
}
}
let mut texels = [0; 4];
for y in 0..4 {
for x in 0..4 {
let idx = match cf.indices[y * 4 + x] {
0 => 0,
1 => 2,
2 => 3,
3 => 1,
_ => unreachable!(),
};
texels[y] |= idx << (x * 2);
}
}
Block {
color0,
color1,
texels,
}
}
/// Encode block into BC1 setting texels to TRANSPARENT if alpha <= threshold.
pub fn encode_with_alpha(colors: [[Rgba32F; 4]; 4], threshold: f32) -> Self {
#![allow(clippy::needless_range_loop)]
let mut samples = [Vec3::ZERO; 16];
let mut num_samples = 0;
for row in &colors {
for &c in row {
if c.a() <= threshold {
continue;
}
samples[num_samples] = c.rgb().into();
num_samples += 1;
}
}
match num_samples {
0 => Self::TRANSPARENT,
1..16 => {
// Some samples are transparent.
let mut cf = cluster_fit::<Vec3, 3, 16>(
&samples[..num_samples],
|a: Vec3, b: Vec3| {
let a = Rgb565::from_f32(a.into());
let b = Rgb565::from_f32(b.into());
(a.into_f32().into(), b.into_f32().into())
},
|a: Vec3, b: Vec3| {
let a = Rgb32F::from(a);
let b = Rgb32F::from(b);
let a = Yiq32F::from_rgb(a);
let b = Yiq32F::from_rgb(b);
Yiq32F::perceptual_distance(a, b)
},
);
let (color0, color1) = cf.endpoints;
let mut color0 = Rgb565::from_f32(Rgb32F::from(color0));
let mut color1 = Rgb565::from_f32(Rgb32F::from(color1));
if color0.bits() > color1.bits() {
swap(&mut color0, &mut color1);
for index in &mut cf.indices {
*index = 2 - *index;
}
}
let mut texels = [0; 4];
for y in 0..4 {
for x in 0..4 {
let c = colors[y][x];
if c.a() < threshold {
texels[y] |= 0b11 << (x * 2);
} else {
let idx = match cf.indices[y * 4 + x] {
0 => 0,
1 => 2,
2 => 1,
_ => unreachable!(),
};
texels[y] |= idx << (x * 2);
}
}
}
Block {
color0,
color1,
texels,
}
}
16 => {
// Solid case.
let mut cf = cluster_fit::<Vec3, 4, 16>(
&samples[..num_samples],
|a: Vec3, b: Vec3| {
let a = Rgb565::from_f32(a.into());
let b = Rgb565::from_f32(b.into());
(a.into_f32().into(), b.into_f32().into())
},
|a: Vec3, b: Vec3| {
let a = Rgb32F::from(a);
let b = Rgb32F::from(b);
let a = Yiq32F::from_rgb(a);
let b = Yiq32F::from_rgb(b);
Yiq32F::perceptual_distance(a, b)
},
);
let (color0, color1) = cf.endpoints;
let mut color0 = Rgb565::from_f32(Rgb32F::from(color0));
let mut color1 = Rgb565::from_f32(Rgb32F::from(color1));
if color0 == color1 {
return Block {
color0,
color1: Rgb565::BLACK,
texels: [0x00; 4],
};
} else if color0.bits() < color1.bits() {
swap(&mut color0, &mut color1);
for index in &mut cf.indices {
*index = 3 - *index;
}
}
let mut texels = [0; 4];
for y in 0..4 {
for x in 0..4 {
let idx = match cf.indices[y * 4 + x] {
0 => 0,
1 => 2,
2 => 3,
3 => 1,
_ => unreachable!(),
};
texels[y] |= idx << (x * 2);
}
}
Block {
color0,
color1,
texels,
}
}
_ => unreachable!(),
}
}
}