mirror of
https://github.com/lifegpc/libtlg-rs.git
synced 2026-07-08 01:33:22 +08:00
fix(libtlg-rs): Fix tags parse bug
feature(tlg): Add support to export tags to files
This commit is contained in:
4
Cargo.lock
generated
4
Cargo.lock
generated
@@ -158,7 +158,7 @@ checksum = "7943c866cc5cd64cbc25b2e01621d07fa8eb2a1a23160ee81ce38704e97b8ecf"
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "libtlg-rs"
|
name = "libtlg-rs"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"overf",
|
"overf",
|
||||||
]
|
]
|
||||||
@@ -246,7 +246,7 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tlg"
|
name = "tlg"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"clap",
|
"clap",
|
||||||
"libtlg-rs",
|
"libtlg-rs",
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "libtlg-rs"
|
name = "libtlg-rs"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
description = "Rust version of libtlg"
|
description = "Rust version of libtlg"
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
repository = "https://github.com/lifegpc/libtlg-rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
overf = "0.1"
|
overf = "0.1"
|
||||||
|
|||||||
@@ -205,6 +205,12 @@ pub fn load_tlg<T: Read + Seek>(mut src: T) -> Result<Tlg> {
|
|||||||
if i >= len {
|
if i >= len {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
c = tag[i];
|
||||||
|
if c != b'=' {
|
||||||
|
check = false;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
i += 1;
|
||||||
let mut valuelen = 0usize;
|
let mut valuelen = 0usize;
|
||||||
c = tag[i];
|
c = tag[i];
|
||||||
ok = true;
|
ok = true;
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
[package]
|
[package]
|
||||||
name = "tlg"
|
name = "tlg"
|
||||||
version = "0.1.0"
|
version = "0.1.1"
|
||||||
description = "Tools to process TLG image file."
|
description = "Tools to process TLG image file."
|
||||||
edition = "2024"
|
edition = "2024"
|
||||||
license = "MIT"
|
license = "MIT"
|
||||||
|
repository = "https://github.com/lifegpc/libtlg-rs"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
clap = { version = "4.5", features = ["derive"] }
|
clap = { version = "4.5", features = ["derive"] }
|
||||||
|
|||||||
@@ -1,5 +1,5 @@
|
|||||||
mod arg;
|
mod arg;
|
||||||
use std::io::Seek;
|
use std::io::{Seek, Write};
|
||||||
|
|
||||||
fn convert_bgr_to_rgb(data: &mut libtlg_rs::Tlg) {
|
fn convert_bgr_to_rgb(data: &mut libtlg_rs::Tlg) {
|
||||||
match data.color {
|
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() {
|
fn main() {
|
||||||
let args = arg::Arg::parse();
|
let args = arg::Arg::parse();
|
||||||
let file = std::fs::File::open(&args.input).expect("Failed to open input file");
|
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 mut tlg = libtlg_rs::load_tlg(&mut file).expect("Failed to load TLG file");
|
||||||
let output = match &args.output {
|
let output = match &args.output {
|
||||||
Some(output) => output.clone(),
|
Some(output) => output.clone(),
|
||||||
None => {
|
None => get_relative_path(&args.input, "png"),
|
||||||
let mut pb = std::path::PathBuf::from(&args.input);
|
|
||||||
pb.set_extension("png");
|
|
||||||
pb.to_string_lossy().to_string()
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
convert_bgr_to_rgb(&mut tlg);
|
convert_bgr_to_rgb(&mut tlg);
|
||||||
let mut output_file = std::fs::File::create(&output).expect("Failed to create output file");
|
let mut output_file = std::fs::File::create(&output).expect("Failed to create output file");
|
||||||
@@ -48,6 +50,20 @@ fn main() {
|
|||||||
writer
|
writer
|
||||||
.write_image_data(&tlg.data)
|
.write_image_data(&tlg.data)
|
||||||
.expect("Failed to write PNG image 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 {
|
} else {
|
||||||
file.rewind().expect("Failed to rewind file");
|
file.rewind().expect("Failed to rewind file");
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user