Skip to content

Commit 951cc8c

Browse files
committed
feat: use texture2ddecoder
1 parent fdc33e7 commit 951cc8c

File tree

2 files changed

+32
-23
lines changed

2 files changed

+32
-23
lines changed

Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "unity-js-tools"
3-
version = "1.0.0"
3+
version = "1.0.1"
44
edition = "2021"
55
authors = ["神代綺凛 <i@loli.best>"]
66
license = "MIT"
@@ -16,6 +16,6 @@ codegen-units = 1
1616
crate-type = ["cdylib"]
1717

1818
[dependencies]
19-
astc-decode = "0.3.1"
2019
lz4_flex = "0.11.1"
20+
texture2ddecoder = "0.0.5"
2121
wasm-bindgen = "0.2.87"

src/lib.rs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,48 @@
1-
use std::error::Error;
2-
3-
use astc_decode::{astc_decode, Footprint};
41
use lz4_flex::decompress;
2+
use texture2ddecoder::decode_astc;
53
use wasm_bindgen::prelude::*;
64

7-
fn to_js_err(e: impl Error) -> JsError {
5+
fn to_js_err(e: impl ToString) -> JsError {
86
JsError::new(&e.to_string())
97
}
108

9+
fn image_to_rgba(image: Vec<u32>, width: usize, height: usize) -> Vec<u8> {
10+
let mut lines: Vec<&[u32]> = Vec::with_capacity(height);
11+
for i in 0..height {
12+
let start = i * width;
13+
lines.push(&image[start..start + width]);
14+
}
15+
lines
16+
.iter()
17+
.copied()
18+
.rev()
19+
.flatten()
20+
.flat_map(|x| {
21+
let v = x.to_le_bytes();
22+
[v[2], v[1], v[0], v[3]]
23+
})
24+
.collect::<Vec<u8>>()
25+
}
26+
1127
#[wasm_bindgen(js_name = decodeAstc)]
12-
pub fn decode_astc(
28+
pub fn export_decode_astc(
1329
data: &[u8],
14-
width: u32,
15-
height: u32,
16-
block_width: u32,
17-
block_height: u32,
30+
width: usize,
31+
height: usize,
32+
block_width: usize,
33+
block_height: usize,
1834
) -> Result<Vec<u8>, JsError> {
19-
let footprint = Footprint::new(block_width, block_height);
20-
let mut result: Vec<u8> = vec![0; (width * height * 4) as usize];
35+
let mut image: Vec<u32> = vec![0; (width * height) as usize];
2136

22-
let astc_result = astc_decode(data, width, height, footprint, |x, y, v4| {
23-
let y = height - y - 1;
24-
let ri = ((y * width + x) * 4) as usize;
25-
for (i, v) in v4.iter().enumerate() {
26-
result[ri + i] = *v;
27-
}
28-
});
37+
let result = decode_astc(data, width, height, block_width, block_height, &mut image);
2938

30-
match astc_result {
31-
Ok(()) => Ok(result),
39+
match result {
40+
Ok(()) => Ok(image_to_rgba(image, width, height)),
3241
Err(e) => Err(to_js_err(e)),
3342
}
3443
}
3544

3645
#[wasm_bindgen(js_name = decompressLz4)]
37-
pub fn decompress_lz4(data: &[u8], size: usize) -> Result<Vec<u8>, JsError> {
46+
pub fn export_decompress_lz4(data: &[u8], size: usize) -> Result<Vec<u8>, JsError> {
3847
decompress(data, size).map_err(to_js_err)
3948
}

0 commit comments

Comments
 (0)