Skip to content

Commit

Permalink
feat: no longer adjusting pixel format
Browse files Browse the repository at this point in the history
  • Loading branch information
Tsuk1ko committed May 25, 2024
1 parent 4375dae commit 685b42d
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 18 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "unity-js-tools"
version = "1.2.0"
version = "2.0.0"
edition = "2021"
authors = ["神代綺凛 <[email protected]>"]
license = "MIT"
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,7 @@

- [PSeitz/lz4_flex](https://github.com/PSeitz/lz4_flex)
- [UniversalGameExtraction/texture2ddecoder](https://github.com/UniversalGameExtraction/texture2ddecoder)

## 注意

wasm-bindgen 貌似有某种缺陷,rust 中极少数位运算场合编译成 wasm 后运算结果不正确,这导致 `decodeEtc2Rgba8` 解码后 alpha 通道数值错误,需要手动实现来修正,不排除还有其他情况有问题
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub fn export_decode_astc(
let result = decode_astc(data, width, height, block_width, block_height, &mut image);

match result {
Ok(()) => Ok(image_to_rgba(image, width, height)),
Ok(()) => Ok(split_channel(image)),
Err(e) => Err(to_js_err(e)),
}
}
Expand Down
21 changes: 5 additions & 16 deletions src/tools.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,10 @@ pub fn to_js_err(e: impl ToString) -> JsError {
JsError::new(&e.to_string())
}

pub fn image_to_rgba(image: Vec<u32>, width: usize, height: usize) -> Vec<u8> {
let mut lines: Vec<&[u32]> = Vec::with_capacity(height);
for i in 0..height {
let start = i * width;
lines.push(&image[start..start + width]);
}
lines
.iter()
.copied()
.rev()
.flatten()
.flat_map(|x| {
let v = x.to_le_bytes();
[v[2], v[1], v[0], v[3]]
})
pub fn split_channel(image: Vec<u32>) -> Vec<u8> {
image
.into_iter()
.flat_map(|x| x.to_le_bytes())
.collect::<Vec<u8>>()
}

Expand All @@ -35,7 +24,7 @@ pub fn decode_texture(
let result = decode_func(data, width, height, &mut image);

match result {
Ok(()) => Ok(image_to_rgba(image, width, height)),
Ok(()) => Ok(split_channel(image)),
Err(e) => Err(to_js_err(e)),
}
}
Expand Down

0 comments on commit 685b42d

Please sign in to comment.