From 1a252c05c2b892a016e8ee5025f45a40f9dcedc2 Mon Sep 17 00:00:00 2001 From: lifegpc Date: Tue, 29 Jul 2025 09:29:54 +0800 Subject: [PATCH] features(libtlg-rs): Add tlg5 grayscale decode support --- Cargo.lock | 4 ++-- libtlg-rs/Cargo.toml | 2 +- libtlg-rs/src/load_tlg.rs | 20 ++++++++++++++++++-- libtlg-rs/src/tvpgl.rs | 13 +++++++++++++ tlg/Cargo.toml | 2 +- 5 files changed, 35 insertions(+), 6 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 63fc84a..c0467a2 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -164,7 +164,7 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe" [[package]] name = "libtlg-rs" -version = "0.1.2" +version = "0.1.3" dependencies = [ "lazy_static", "overf", @@ -253,7 +253,7 @@ dependencies = [ [[package]] name = "tlg" -version = "0.1.2" +version = "0.1.3" dependencies = [ "clap", "libtlg-rs", diff --git a/libtlg-rs/Cargo.toml b/libtlg-rs/Cargo.toml index 5c166b0..d65abb7 100644 --- a/libtlg-rs/Cargo.toml +++ b/libtlg-rs/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "libtlg-rs" -version = "0.1.2" +version = "0.1.3" description = "Rust version of libtlg" edition = "2024" license = "MIT" diff --git a/libtlg-rs/src/load_tlg.rs b/libtlg-rs/src/load_tlg.rs index a2be21d..0b4bf5c 100644 --- a/libtlg-rs/src/load_tlg.rs +++ b/libtlg-rs/src/load_tlg.rs @@ -12,6 +12,7 @@ fn load_tlg5(src: &mut T) -> Result { let color = match colors { 3 => TlgColorType::Bgr24, 4 => TlgColorType::Bgra32, + 1 => TlgColorType::Grayscale8, _ => return Err(TlgError::UnsupportedColorType(colors)), }; let blockcount = ((height - 1) / blockheight) + 1; @@ -62,7 +63,10 @@ fn load_tlg5(src: &mut T) -> Result { outbufp[2] = &outbufp[2][width as usize..]; outbufp[3] = &outbufp[3][width as usize..]; } - _ => {} + TlgColorType::Grayscale8 => { + tlg5_compose_colors1(current, &prev, &outbufp, width); + outbufp[0] = &outbufp[0][width as usize..]; + } }, None => match color { TlgColorType::Bgra32 => { @@ -125,7 +129,19 @@ fn load_tlg5(src: &mut T) -> Result { outbufp[1] = &outbufp[1][width as usize..]; outbufp[2] = &outbufp[2][width as usize..]; } - _ => {} + TlgColorType::Grayscale8 => { + let mut current_pos = 0usize; + let mut pb = 0u8; + for x in 0..width as usize { + let b = outbufp[0][x]; + wrapping! { + pb += b; + } + current[current_pos] = pb; + current_pos += 1; + } + outbufp[0] = &outbufp[0][width as usize..]; + } }, } prevline = Some(current.to_vec()); diff --git a/libtlg-rs/src/tvpgl.rs b/libtlg-rs/src/tvpgl.rs index 083f457..d83e0b6 100644 --- a/libtlg-rs/src/tvpgl.rs +++ b/libtlg-rs/src/tvpgl.rs @@ -54,6 +54,19 @@ fn tlg6_init_golomb_table() -> [[i8; TLG6_GOLOMB_N_COUNT]; TLG6_GLOBMB_TABLE_SIZ table } +pub fn tlg5_compose_colors1(outp: &mut [u8], upper: &[u8], buf: &[&[u8]], width: u32) { + let mut outpos = 0usize; + let mut upper_pos = 0usize; + let mut pb = 0u8; + for x in 0..width as usize { + let b = buf[0][x]; + wrapping! { pb += b }; + outp[outpos] = wrapping! { pb + upper[upper_pos]}; + outpos += 1; + upper_pos += 1; + } +} + pub fn tlg5_compose_colors3(outp: &mut [u8], upper: &[u8], buf: &[&[u8]], width: u32) { let mut outpos = 0usize; let mut upper_pos = 0usize; diff --git a/tlg/Cargo.toml b/tlg/Cargo.toml index 2ff0413..905f94c 100644 --- a/tlg/Cargo.toml +++ b/tlg/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "tlg" -version = "0.1.2" +version = "0.1.3" description = "Tools to process TLG image file." edition = "2024" license = "MIT"