|
1 |
| -use std::error::Error; |
2 |
| - |
3 |
| -use astc_decode::{astc_decode, Footprint}; |
4 | 1 | use lz4_flex::decompress;
|
| 2 | +use texture2ddecoder::decode_astc; |
5 | 3 | use wasm_bindgen::prelude::*;
|
6 | 4 |
|
7 |
| -fn to_js_err(e: impl Error) -> JsError { |
| 5 | +fn to_js_err(e: impl ToString) -> JsError { |
8 | 6 | JsError::new(&e.to_string())
|
9 | 7 | }
|
10 | 8 |
|
| 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 | + |
11 | 27 | #[wasm_bindgen(js_name = decodeAstc)]
|
12 |
| -pub fn decode_astc( |
| 28 | +pub fn export_decode_astc( |
13 | 29 | 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, |
18 | 34 | ) -> 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]; |
21 | 36 |
|
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); |
29 | 38 |
|
30 |
| - match astc_result { |
31 |
| - Ok(()) => Ok(result), |
| 39 | + match result { |
| 40 | + Ok(()) => Ok(image_to_rgba(image, width, height)), |
32 | 41 | Err(e) => Err(to_js_err(e)),
|
33 | 42 | }
|
34 | 43 | }
|
35 | 44 |
|
36 | 45 | #[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> { |
38 | 47 | decompress(data, size).map_err(to_js_err)
|
39 | 48 | }
|
0 commit comments