-
Notifications
You must be signed in to change notification settings - Fork 317
Expand file tree
/
Copy pathgx_texture.rs
More file actions
422 lines (361 loc) · 13.3 KB
/
gx_texture.rs
File metadata and controls
422 lines (361 loc) · 13.3 KB
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
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use wasm_bindgen::prelude::*;
use std::convert::TryInto;
// http://www.mindcontrol.org/~hplus/graphics/expand-bits.html
fn expand_n_to_8(v: u8, n: u8) -> u8 {
match v {
3 => (n << (8 - 3)) | (n << (8 - 6)) | (n >> (9 - 8)),
v if (v >= 4) => (n << (8 - v)) | (n >> ((v*2) - 8)),
_ => unreachable!(),
}
}
fn get_u16_be(src: &[u8], i: usize) -> u16 {
u16::from_be_bytes(src[i..i+2].try_into().unwrap())
}
fn s3tcblend(a_: u8, b_: u8) -> u8 {
// return (a*3 + b*5) / 8;
let a = a_ as u32;
let b = b_ as u32;
let tmp = (((a << 1) + a) + ((b << 2) + b)) >> 3;
return tmp as u8;
}
fn halfblend(a_: u8, b_: u8) -> u8 {
let a = a_ as u32;
let b = b_ as u32;
let tmp = (a + b) >> 1;
return tmp as u8;
}
fn decode_rgb5a3_to_rgba8(dst: &mut[u8], p: u16) {
if (p & 0x8000) != 0 {
// RGB5
dst[0] = expand_n_to_8(5, ((p >> 10) & 0x1F) as u8);
dst[1] = expand_n_to_8(5, ((p >> 5) & 0x1F) as u8);
dst[2] = expand_n_to_8(5, ((p >> 0) & 0x1F) as u8);
dst[3] = 0xFF;
} else {
// A3RGB4
dst[0] = expand_n_to_8(4, ((p >> 8) & 0x0F) as u8);
dst[1] = expand_n_to_8(4, ((p >> 4) & 0x0F) as u8);
dst[2] = expand_n_to_8(4, ((p >> 0) & 0x0F) as u8);
dst[3] = expand_n_to_8(3, ((p >> 12) & 0x07) as u8);
}
}
fn decode_rgb565_to_rgba8(dst: &mut[u8], p: u16) {
dst[0] = expand_n_to_8(5, ((p >> 11) & 0x1F) as u8);
dst[1] = expand_n_to_8(6, ((p >> 5) & 0x3F) as u8);
dst[2] = expand_n_to_8(5, ((p >> 0) & 0x1F) as u8);
dst[3] = 0xFF;
}
trait TiledDecoder {
fn decode_single_pixel(self: &Self, src: &[u8], idx: usize, dst: &mut [u8]);
fn block_width() -> usize;
fn block_height() -> usize;
}
fn decode_tiled<T: TiledDecoder>(t: T, src: &[u8], w: usize, h: usize) -> Vec<u8> {
let mut idx: usize = 0;
let mut dst = vec![0x00; w*h*4];
let bw = T::block_width();
let bh = T::block_height();
for yy in (0..h).step_by(bh) {
for xx in (0..w).step_by(bw) {
for y in 0..bh {
for x in 0..bw {
let dst_px = (yy + y) * w + (xx + x);
let dst_offs = dst_px * 4;
if xx + x < w && yy + y < h {
t.decode_single_pixel(src, idx, &mut dst[dst_offs..dst_offs + 4]);
}
idx += 1;
}
}
}
}
dst
}
struct TiledDecoderI4 {}
impl TiledDecoder for TiledDecoderI4 {
fn decode_single_pixel(self: &Self, src: &[u8], idx: usize, dst: &mut [u8]) {
let ii = src[idx >> 1];
let i4 = ii >> (if (idx & 1) != 0 { 0 } else { 4 }) & 0x0F;
let i = expand_n_to_8(4, i4);
dst[0] = i;
dst[1] = i;
dst[2] = i;
dst[3] = i;
}
fn block_width() -> usize { 8 }
fn block_height() -> usize { 8 }
}
struct TiledDecoderI8 {}
impl TiledDecoder for TiledDecoderI8 {
fn decode_single_pixel(self: &Self, src: &[u8], idx: usize, dst: &mut [u8]) {
let i = src[idx];
dst[0] = i;
dst[1] = i;
dst[2] = i;
dst[3] = i;
}
fn block_width() -> usize { 8 }
fn block_height() -> usize { 4 }
}
struct TiledDecoderIA4 {}
impl TiledDecoder for TiledDecoderIA4 {
fn decode_single_pixel(self: &Self, src: &[u8], idx: usize, dst: &mut [u8]) {
let ia = src[idx];
let a = expand_n_to_8(4, ia >> 4);
let i = expand_n_to_8(4, ia & 0x0F);
dst[0] = i;
dst[1] = i;
dst[2] = i;
dst[3] = a;
}
fn block_width() -> usize { 8 }
fn block_height() -> usize { 4 }
}
struct TiledDecoderIA8 {}
impl TiledDecoder for TiledDecoderIA8 {
fn decode_single_pixel(self: &Self, src: &[u8], idx: usize, dst: &mut [u8]) {
let a = src[idx * 2 + 0];
let i = src[idx * 2 + 1];
dst[0] = i;
dst[1] = i;
dst[2] = i;
dst[3] = a;
}
fn block_width() -> usize { 4 }
fn block_height() -> usize { 4 }
}
struct TiledDecoderRGB565 {}
impl TiledDecoder for TiledDecoderRGB565 {
fn decode_single_pixel(self: &Self, src: &[u8], idx: usize, dst: &mut [u8]) {
let p = get_u16_be(src, idx * 2);
decode_rgb565_to_rgba8(dst, p);
}
fn block_width() -> usize { 4 }
fn block_height() -> usize { 4 }
}
struct TiledDecoderRGB5A3 {}
impl TiledDecoder for TiledDecoderRGB5A3 {
fn decode_single_pixel(self: &Self, src: &[u8], idx: usize, dst: &mut [u8]) {
let p = get_u16_be(src, idx * 2);
decode_rgb5a3_to_rgba8(dst, p);
}
fn block_width() -> usize { 4 }
fn block_height() -> usize { 4 }
}
fn decode_rgba8(src: &[u8], w: usize, h: usize) -> Vec<u8> {
let mut src_offs = 0;
let mut dst = vec![0x00; w*h*4];
// RGBA8 is a bit special, so we hand-code this one.
let bh = 4;
let bw = 4;
for yy in (0..h).step_by(bh) {
for xx in (0..w).step_by(bw) {
for y in 0..bh {
for x in 0..bw {
let write = xx + x < w && yy + y < h;
if write {
let dst_px = (yy + y) * w + (xx + x);
let dst_offs = dst_px * 4;
dst[dst_offs + 3] = src[src_offs + 0x00];
dst[dst_offs + 0] = src[src_offs + 0x01];
}
src_offs += 2;
}
}
for y in 0..bh {
for x in 0..bw {
let write = xx + x < w && yy + y < h;
if write {
let dst_px = (yy + y) * w + (xx + x);
let dst_offs = dst_px * 4;
dst[dst_offs + 1] = src[src_offs + 0x00];
dst[dst_offs + 2] = src[src_offs + 0x01];
}
src_offs += 2;
}
}
}
}
dst
}
fn decode_cmpr(src: &[u8], w: usize, h: usize) -> Vec<u8> {
// CMPR swizzles macroblocks to be in a 2x2 grid of UL, UR, BL, BR.
let mut src_offs = 0;
let mut dst = vec![0x00; w*h*4];
for yy in (0..h).step_by(8) {
for xx in (0..w).step_by(8) {
for yb in (0..8).step_by(4) {
for xb in (0..8).step_by(4) {
let src_offs_idx = src_offs;
src_offs += 0x08;
// CMPR difference: Big-endian color1/2
let color1 = get_u16_be(src, src_offs_idx + 0x00);
let color2 = get_u16_be(src, src_offs_idx + 0x02);
// Fill in first two colors in color table.
let mut color_table = [0x00; 16];
color_table[0] = expand_n_to_8(5, ((color1 >> 11) & 0x1F) as u8);
color_table[1] = expand_n_to_8(6, ((color1 >> 5) & 0x3F) as u8);
color_table[2] = expand_n_to_8(5, (color1 & 0x1F) as u8);
color_table[3] = 0xFF;
color_table[4] = expand_n_to_8(5, ((color2 >> 11) & 0x1F) as u8);
color_table[5] = expand_n_to_8(6, ((color2 >> 5) & 0x3F) as u8);
color_table[6] = expand_n_to_8(5, (color2 & 0x1F) as u8);
color_table[7] = 0xFF;
if color1 > color2 {
// Predict gradients.
color_table[8] = s3tcblend(color_table[4], color_table[0]);
color_table[9] = s3tcblend(color_table[5], color_table[1]);
color_table[10] = s3tcblend(color_table[6], color_table[2]);
color_table[11] = 0xFF;
color_table[12] = s3tcblend(color_table[0], color_table[4]);
color_table[13] = s3tcblend(color_table[1], color_table[5]);
color_table[14] = s3tcblend(color_table[2], color_table[6]);
color_table[15] = 0xFF;
} else {
color_table[8] = halfblend(color_table[0], color_table[4]);
color_table[9] = halfblend(color_table[1], color_table[5]);
color_table[10] = halfblend(color_table[2], color_table[6]);
color_table[11] = 0xFF;
// CMPR difference: GX fills with an alpha 0 midway point here.
color_table[12] = color_table[8];
color_table[13] = color_table[9];
color_table[14] = color_table[10];
color_table[15] = 0x00;
}
for y in 0..4 {
let mut bits = src[src_offs_idx + 0x04 + y];
for x in 0..4 {
if xx + xb + x >= w || yy + yb + y >= h {
continue;
}
let dst_px = (yy + yb + y) * w + (xx + xb + x);
let dst_offs = dst_px * 4;
let color_idx = ((bits >> 6) & 0x03) as usize;
let color_table_offs = color_idx * 4;
dst[dst_offs..dst_offs + 4].copy_from_slice(&color_table[color_table_offs..color_table_offs + 4]);
bits <<= 2;
}
}
}
}
}
}
dst
}
#[wasm_bindgen]
pub enum PaletteFormat {
IA8,
RGB565,
RGB5A3,
}
#[wasm_bindgen]
pub enum PixelFormat {
I4,
I8,
IA4,
IA8,
RGB565,
RGB5A3,
RGBA8,
CMPR,
C4,
C8,
C14X2,
}
fn decode_palette(palette_fmt: PaletteFormat, palette_src: &[u8]) -> Vec<u8> {
let palette_count = palette_src.len() / 2;
let mut dst = vec![0x00; palette_count * 4];
match palette_fmt {
PaletteFormat::IA8 => {
for i in 0..palette_count {
let aa = palette_src[i * 2 + 0];
let ii = palette_src[i * 2 + 1];
dst[i * 4 + 0] = ii;
dst[i * 4 + 1] = ii;
dst[i * 4 + 2] = ii;
dst[i * 4 + 3] = aa;
}
},
PaletteFormat::RGB565 => {
for i in 0..palette_count {
let p = get_u16_be(palette_src, i * 2);
decode_rgb565_to_rgba8(&mut dst[i*4+0 .. i*4+4], p);
}
},
PaletteFormat::RGB5A3 => {
for i in 0..palette_count {
let p = get_u16_be(palette_src, i * 2);
decode_rgb5a3_to_rgba8(&mut dst[i*4+0 .. i*4+4], p);
}
},
}
dst
}
struct TiledDecoderC4<'a> {
palette: &'a [u8],
}
impl TiledDecoder for TiledDecoderC4<'_> {
fn decode_single_pixel(self: &Self, src: &[u8], idx: usize, dst: &mut [u8]) {
let ii = src[idx >> 1];
let idx = (ii >> (if (idx & 1) != 0 { 0 } else { 4 }) & 0x0F) as usize;
dst[0] = self.palette[idx * 4 + 0];
dst[1] = self.palette[idx * 4 + 1];
dst[2] = self.palette[idx * 4 + 2];
dst[3] = self.palette[idx * 4 + 3];
}
fn block_width() -> usize { 8 }
fn block_height() -> usize { 8 }
}
struct TiledDecoderC8<'a> {
palette: &'a [u8],
}
impl TiledDecoder for TiledDecoderC8<'_> {
fn decode_single_pixel(self: &Self, src: &[u8], idx: usize, dst: &mut [u8]) {
let idx = src[idx] as usize;
dst[0] = self.palette[idx * 4 + 0];
dst[1] = self.palette[idx * 4 + 1];
dst[2] = self.palette[idx * 4 + 2];
dst[3] = self.palette[idx * 4 + 3];
}
fn block_width() -> usize { 8 }
fn block_height() -> usize { 4 }
}
struct TiledDecoderC14X2<'a> {
palette: &'a [u8],
}
impl TiledDecoder for TiledDecoderC14X2<'_> {
fn decode_single_pixel(self: &Self, src: &[u8], idx: usize, dst: &mut [u8]) {
let idx = (get_u16_be(src, idx * 2) as usize) & 0x3FFF;
dst[0] = self.palette[idx * 4 + 0];
dst[1] = self.palette[idx * 4 + 1];
dst[2] = self.palette[idx * 4 + 2];
dst[3] = self.palette[idx * 4 + 3];
}
fn block_width() -> usize { 4 }
fn block_height() -> usize { 4 }
}
#[wasm_bindgen]
pub fn decode_texture(fmt: PixelFormat, palette_fmt: Option<PaletteFormat>, src: &[u8], palette_src: Option<Box<[u8]>>, w: usize, h: usize) -> Vec<u8> {
match fmt {
PixelFormat::I4 => decode_tiled(TiledDecoderI4{}, src, w, h),
PixelFormat::I8 => decode_tiled(TiledDecoderI8{}, src, w, h),
PixelFormat::IA4 => decode_tiled(TiledDecoderIA4{}, src, w, h),
PixelFormat::IA8 => decode_tiled(TiledDecoderIA8{}, src, w, h),
PixelFormat::RGB565 => decode_tiled(TiledDecoderRGB565{}, src, w, h),
PixelFormat::RGB5A3 => decode_tiled(TiledDecoderRGB5A3{}, src, w, h),
PixelFormat::RGBA8 => decode_rgba8(src, w, h),
PixelFormat::CMPR => decode_cmpr(src, w, h),
PixelFormat::C4 => {
let palette = decode_palette(palette_fmt.unwrap(), &palette_src.unwrap());
decode_tiled(TiledDecoderC4{ palette: &palette }, src, w, h)
},
PixelFormat::C8 => {
let palette = decode_palette(palette_fmt.unwrap(), &palette_src.unwrap());
decode_tiled(TiledDecoderC8{ palette: &palette }, src, w, h)
},
PixelFormat::C14X2 => {
let palette = decode_palette(palette_fmt.unwrap(), &palette_src.unwrap());
decode_tiled(TiledDecoderC14X2{ palette: &palette }, src, w, h)
},
}
}