Skip to content

Commit

Permalink
Replace libsquish-wrapper with texpresso, equivalent Rust crate (#38
Browse files Browse the repository at this point in the history
)

Replace `libsquish-wrapper`, a wrapper around a C++ dependency, with
`texpresso`, a Rust crate providing the same compression and
decompression algorithm, but without support for Gamecube format.
Conversion to Gamecube format was added manually.
  • Loading branch information
marsolk authored May 20, 2024
1 parent 3e4eadd commit 012743e
Show file tree
Hide file tree
Showing 10 changed files with 57 additions and 107 deletions.
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

20 changes: 14 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ dol_linker = { path = "dol_linker" }
dol_symbol_table = { path = "generated/dol_symbol_table" }
json_data = { path = "generated/json_data" }
json_strip = { path = "generated/json_strip" }
libsquish_wrapper = { path = "libsquish_wrapper" }
ppcasm = { path = "ppcasm" }
reader_writer.workspace = true
rel_files = { path = "generated/rel_files" }
resource_info_table.workspace = true
structs = { path = "structs" }
texpresso = "2.0.1"

[profile.release]
lto = "thin"
Expand All @@ -51,7 +51,6 @@ members = [
"generated/dol_symbol_table/dol_symbol_table_macro",
"generated/json_data",
"generated/json_strip",
"libsquish_wrapper",
"ppcasm",
"ppcasm/ppcasm_macro",
"reader_writer",
Expand Down
8 changes: 0 additions & 8 deletions libsquish_wrapper/Cargo.toml

This file was deleted.

17 changes: 0 additions & 17 deletions libsquish_wrapper/build.rs

This file was deleted.

1 change: 0 additions & 1 deletion libsquish_wrapper/libSquish
Submodule libSquish deleted from fd5e6f
32 changes: 0 additions & 32 deletions libsquish_wrapper/src/lib.rs

This file was deleted.

26 changes: 0 additions & 26 deletions libsquish_wrapper/src/wrapper.cpp

This file was deleted.

11 changes: 3 additions & 8 deletions src/bin/txtr_converter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use image::{
codecs::png::{PngDecoder, PngEncoder},
ColorType, ImageDecoder,
};
use libsquish_wrapper::{compress_dxt1gcn_block, decompress_dxt1gcn_block};
use randomprime::txtr_conversions::{compress_dxt1gcn_block, decompress_dxt1gcn_block};
use reader_writer::{Readable, Reader, Writable};
use structs::{Txtr, TxtrFormat, TxtrPaletteFormat};

Expand Down Expand Up @@ -445,13 +445,8 @@ impl TxtrFormatExt for TxtrFormat {
}
TxtrFormat::Rgba8 => pixels.copy_from_slice(block),
TxtrFormat::Cmpr => {
let mut decoded_dxt1_block = [[0u8; 4]; 16];
for i in 0..4 {
decompress_dxt1gcn_block(
&mut decoded_dxt1_block,
block[i * 8..(i + 1) * 8].try_into().unwrap(),
);

let decoded_dxt1_block = decompress_dxt1gcn_block(&block[i * 8..(i + 1) * 8]);
let outer_x = i % 2 * 4;
let outer_y = i / 2 * 4;
for (k, decoded_pixel) in decoded_dxt1_block.iter().enumerate() {
Expand Down Expand Up @@ -542,7 +537,7 @@ impl TxtrFormatExt for TxtrFormat {
sub_block_pixel[..].copy_from_slice(&pixels[start..start + 4]);
}

compress_dxt1gcn_block(&sub_block_pixels, sub_block.try_into().unwrap());
compress_dxt1gcn_block(sub_block_pixels, sub_block);
}
}
}
Expand Down
43 changes: 39 additions & 4 deletions src/txtr_conversions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::convert::TryInto;

use libsquish_wrapper::{compress_dxt1gcn_block, decompress_dxt1gcn_block};
use resource_info_table::{resource_info, ResourceInfo};
use texpresso::Format;

// 0 - Power
// 1 - Gravity
Expand Down Expand Up @@ -186,8 +186,7 @@ impl Iterator for CmprPixelIter {
pub fn cmpr_decompress(compressed: &[u8], width: usize, height: usize, decompressed: &mut [u8]) {
let cmpr_iter = CmprPixelIter::new(width, height);
for (chunk, (first_pixel_x, first_pixel_y)) in compressed.chunks_exact(8).zip(cmpr_iter) {
let mut decompressed_pixels = [[0u8; 4]; 16];
decompress_dxt1gcn_block(&mut decompressed_pixels, chunk.try_into().unwrap());
let decompressed_pixels = decompress_dxt1gcn_block(chunk);
for y in 0..4 {
for x in 0..4 {
let pixel_x = first_pixel_x + x;
Expand Down Expand Up @@ -216,7 +215,7 @@ pub fn cmpr_compress(uncompressed: &[u8], width: usize, height: usize, compresse
}
}

compress_dxt1gcn_block(&uncompressed_pixels, chunk.try_into().unwrap());
compress_dxt1gcn_block(uncompressed_pixels, chunk);
}
}

Expand Down Expand Up @@ -266,3 +265,39 @@ pub fn huerotate_in_place(image: &mut [u8], width: usize, height: usize, matrix:
}
}
}

pub fn compress_dxt1gcn_block(rgba: [[u8; 4]; 16], block: &mut [u8]) {
Format::Bc1.compress_block_masked(
rgba,
0xFFFF,
texpresso::Params {
algorithm: texpresso::Algorithm::IterativeClusterFit,
..Default::default()
},
block,
);
block.swap(0, 1);
block.swap(2, 3);
for byte in block[4..8].iter_mut() {
*byte = reverse_byte(*byte);
}
}

pub fn decompress_dxt1gcn_block(block: &[u8]) -> [[u8; 4]; 16] {
let mut compressed = [0u8; 8];
compressed[0] = block[1];
compressed[1] = block[0];
compressed[2] = block[3];
compressed[3] = block[2];
for (byte, val) in compressed[4..8].iter_mut().zip(block[4..8].iter()) {
*byte = reverse_byte(*val);
}
Format::Bc1.decompress_block(&compressed)
}

fn reverse_byte(byte: u8) -> u8 {
(byte & 0b00000011) << 6
| (byte & 0b00001100) << 2
| (byte & 0b00110000) >> 2
| (byte & 0b11000000) >> 6
}

0 comments on commit 012743e

Please sign in to comment.