fix(libtlg-rs): Fix tags parse bug

feature(tlg): Add support to export tags to files
This commit is contained in:
2025-07-27 16:43:18 +08:00
parent 937f8b07b4
commit e06acfbcee
5 changed files with 34 additions and 10 deletions

4
Cargo.lock generated
View File

@@ -158,7 +158,7 @@ checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
[[package]]
name = "libtlg-rs"
version = "0.1.0"
version = "0.1.1"
dependencies = [
"overf",
]
@@ -246,7 +246,7 @@ dependencies = [
[[package]]
name = "tlg"
version = "0.1.0"
version = "0.1.1"
dependencies = [
"clap",
"libtlg-rs",

View File

@@ -1,9 +1,10 @@
[package]
name = "libtlg-rs"
version = "0.1.0"
version = "0.1.1"
description = "Rust version of libtlg"
edition = "2024"
license = "MIT"
repository = "https://github.com/lifegpc/libtlg-rs"
[dependencies]
overf = "0.1"

View File

@@ -205,6 +205,12 @@ pub fn load_tlg<T: Read + Seek>(mut src: T) -> Result<Tlg> {
if i >= len {
break;
}
c = tag[i];
if c != b'=' {
check = false;
break;
}
i += 1;
let mut valuelen = 0usize;
c = tag[i];
ok = true;

View File

@@ -1,9 +1,10 @@
[package]
name = "tlg"
version = "0.1.0"
version = "0.1.1"
description = "Tools to process TLG image file."
edition = "2024"
license = "MIT"
repository = "https://github.com/lifegpc/libtlg-rs"
[dependencies]
clap = { version = "4.5", features = ["derive"] }

View File

@@ -1,5 +1,5 @@
mod arg;
use std::io::Seek;
use std::io::{Seek, Write};
fn convert_bgr_to_rgb(data: &mut libtlg_rs::Tlg) {
match data.color {
@@ -21,6 +21,12 @@ fn convert_bgr_to_rgb(data: &mut libtlg_rs::Tlg) {
}
}
fn get_relative_path(input: &str, ext: &str) -> String {
let mut pb = std::path::PathBuf::from(input);
pb.set_extension(ext);
pb.to_string_lossy().to_string()
}
fn main() {
let args = arg::Arg::parse();
let file = std::fs::File::open(&args.input).expect("Failed to open input file");
@@ -29,11 +35,7 @@ fn main() {
let mut tlg = libtlg_rs::load_tlg(&mut file).expect("Failed to load TLG file");
let output = match &args.output {
Some(output) => output.clone(),
None => {
let mut pb = std::path::PathBuf::from(&args.input);
pb.set_extension("png");
pb.to_string_lossy().to_string()
}
None => get_relative_path(&args.input, "png"),
};
convert_bgr_to_rgb(&mut tlg);
let mut output_file = std::fs::File::create(&output).expect("Failed to create output file");
@@ -48,6 +50,20 @@ fn main() {
writer
.write_image_data(&tlg.data)
.expect("Failed to write PNG image data");
if !tlg.tags.is_empty() {
let mut tags_file = std::fs::File::create(get_relative_path(&output, "tags"))
.expect("Failed to create tags file");
for (key, value) in &tlg.tags {
tags_file.write_all(&key).expect("Failed to write tag key");
tags_file
.write_all(b"=")
.expect("Failed to write tag separator");
tags_file
.write_all(&value)
.expect("Failed to write tag value");
tags_file.write_all(b"\n").expect("Failed to write newline");
}
}
} else {
file.rewind().expect("Failed to rewind file");
}