mirror of
https://github.com/G2-Games/lbee-utils.git
synced 2025-04-19 07:12:55 -05:00
Made utils
into a simple CLI app
This commit is contained in:
parent
ea7f8fb6d2
commit
4ad55170b5
5 changed files with 144 additions and 23 deletions
|
@ -10,3 +10,7 @@ authors = ["G2"]
|
||||||
|
|
||||||
[workspace.lints.rust]
|
[workspace.lints.rust]
|
||||||
unsafe_code = "forbid"
|
unsafe_code = "forbid"
|
||||||
|
|
||||||
|
[profile.production]
|
||||||
|
inherits = "release"
|
||||||
|
lto = true
|
||||||
|
|
|
@ -93,7 +93,7 @@ pub fn indexed_gen_palette(
|
||||||
Ok((indicies, gen_palette))
|
Ok((indicies, gen_palette))
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn default_palette() -> Vec<Rgba<u8>> {
|
pub fn _default_palette() -> Vec<Rgba<u8>> {
|
||||||
let mut colormap = Vec::new();
|
let mut colormap = Vec::new();
|
||||||
|
|
||||||
for i in 0..=0xFF {
|
for i in 0..=0xFF {
|
||||||
|
|
|
@ -156,8 +156,13 @@ impl CommonHeader {
|
||||||
self.version
|
self.version
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_version(&mut self, version: CzVersion) {
|
pub fn set_version<I: TryInto<CzVersion>>(&mut self, version: I) -> Result<(), ()> {
|
||||||
self.version = version
|
self.version = match version.try_into() {
|
||||||
|
Ok(val) => val,
|
||||||
|
Err(_) => return Err(()),
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn length(&self) -> usize {
|
pub fn length(&self) -> usize {
|
||||||
|
|
|
@ -7,6 +7,7 @@ edition = "2021"
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
byteorder = "1.5.0"
|
byteorder = "1.5.0"
|
||||||
|
clap = { version = "4.5.4", features = ["derive", "unicode"] }
|
||||||
cz = { path = "../cz" }
|
cz = { path = "../cz" }
|
||||||
fontdue = { version = "0.8.0", features = ["parallel"] }
|
fontdue = { version = "0.8.0", features = ["parallel"] }
|
||||||
image = "0.25.1"
|
image = "0.25.1"
|
||||||
|
|
|
@ -1,30 +1,141 @@
|
||||||
use cz::{common::CzVersion, dynamic::DynamicCz};
|
use cz::DynamicCz;
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
|
|
||||||
|
use clap::{error::ErrorKind, Error, Parser, Subcommand};
|
||||||
|
|
||||||
|
#[derive(Parser)]
|
||||||
|
#[command(name = "CZ Utils")]
|
||||||
|
#[command(version, about, long_about = None)]
|
||||||
|
struct Cli {
|
||||||
|
#[command(subcommand)]
|
||||||
|
command: Commands,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Subcommand)]
|
||||||
|
enum Commands {
|
||||||
|
/// Converts a CZ file to a PNG
|
||||||
|
Decode {
|
||||||
|
/// Input CZ file of any type
|
||||||
|
#[arg(value_name = "CZ FILE")]
|
||||||
|
input: PathBuf,
|
||||||
|
|
||||||
|
/// Output PNG file location
|
||||||
|
#[arg(value_name = "PATH")]
|
||||||
|
output: Option<PathBuf>,
|
||||||
|
},
|
||||||
|
|
||||||
|
/// Replace a CZ file's image data
|
||||||
|
Replace {
|
||||||
|
/// Original input CZ file of any type
|
||||||
|
#[arg(value_name = "CZ FILE")]
|
||||||
|
input: PathBuf,
|
||||||
|
|
||||||
|
/// Image to use as the replacement
|
||||||
|
#[arg(value_name = "IMAGE")]
|
||||||
|
replacement: PathBuf,
|
||||||
|
|
||||||
|
/// Output CZ file location
|
||||||
|
#[arg(value_name = "PATH")]
|
||||||
|
output: PathBuf,
|
||||||
|
|
||||||
|
/// Output CZ file version
|
||||||
|
#[arg(short, long, value_name = "CZ VERSION")]
|
||||||
|
version: Option<u8>,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Open the desired PNG
|
let cli = Cli::parse();
|
||||||
//let new_bitmap = image::open("mio.png").unwrap().to_rgba8();
|
|
||||||
|
|
||||||
let gallery_cz = DynamicCz::open("test.cz4").unwrap();
|
// Check what subcommand was run
|
||||||
gallery_cz.save_as_png("test.png").unwrap();
|
match &cli.command {
|
||||||
|
Commands::Decode { input, output } => {
|
||||||
|
if !input.exists() {
|
||||||
|
Error::raw(
|
||||||
|
ErrorKind::ValueValidation,
|
||||||
|
format!("The input file provided does not exist\n")
|
||||||
|
).exit()
|
||||||
|
}
|
||||||
|
|
||||||
gallery_cz.save_as_cz("test-reencode.cz4").unwrap();
|
let cz = match DynamicCz::open(input) {
|
||||||
|
Ok(cz) => cz,
|
||||||
|
Err(err) => {
|
||||||
|
Error::raw(
|
||||||
|
ErrorKind::ValueValidation,
|
||||||
|
format!("Could not open input as a CZ file: {}\n", err)
|
||||||
|
).exit()
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
let cz_image_test = DynamicCz::open("test-reencode.cz4").unwrap();
|
if let Some(output) = output {
|
||||||
|
cz.save_as_png(output).unwrap();
|
||||||
|
} else {
|
||||||
|
let file_stem = PathBuf::from(input.file_stem().unwrap());
|
||||||
|
cz.save_as_png(&file_stem.with_extension("png")).unwrap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Commands::Replace { input, replacement, output, version } => {
|
||||||
|
if !input.exists() {
|
||||||
|
Error::raw(
|
||||||
|
ErrorKind::ValueValidation,
|
||||||
|
format!("The original file provided does not exist\n")
|
||||||
|
).exit()
|
||||||
|
}
|
||||||
|
|
||||||
// Save the newly decoded CZ3 as another PNG as a test
|
if !replacement.exists() {
|
||||||
cz_image_test.save_as_png("test-reencode.png").unwrap();
|
Error::raw(
|
||||||
|
ErrorKind::ValueValidation,
|
||||||
|
format!("The replacement file provided does not exist\n")
|
||||||
|
).exit()
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
let mut cz = match DynamicCz::open(input) {
|
||||||
gallery_cz.set_bitmap(new_bitmap.into_vec());
|
Ok(cz) => cz,
|
||||||
gallery_cz.header_mut().set_depth(8);
|
Err(err) => {
|
||||||
gallery_cz.remove_palette();
|
Error::raw(
|
||||||
gallery_cz.header_mut().set_version(CzVersion::CZ2);
|
ErrorKind::ValueValidation,
|
||||||
gallery_cz.save_as_cz("24-modified.cz2").unwrap();
|
format!("Could not open input as a CZ file: {}\n", err)
|
||||||
|
).exit()
|
||||||
|
},
|
||||||
|
};
|
||||||
|
|
||||||
// Open that same CZ3 again to test decoding
|
let repl_img = match image::open(replacement) {
|
||||||
let cz_image_test = DynamicCz::open("24-modified.cz2").unwrap();
|
Ok(img) => img,
|
||||||
|
Err(err) => {
|
||||||
|
Error::raw(
|
||||||
|
ErrorKind::ValueValidation,
|
||||||
|
format!("Could not open replacement file as an image: {}\n", err)
|
||||||
|
).exit()
|
||||||
|
},
|
||||||
|
};
|
||||||
|
let repl_img = repl_img.to_rgba8();
|
||||||
|
|
||||||
// Save the newly decoded CZ3 as another PNG as a test
|
cz.header_mut().set_width(repl_img.width() as u16);
|
||||||
cz_image_test.save_as_png("24-modified.png").unwrap();
|
cz.header_mut().set_height(repl_img.height() as u16);
|
||||||
*/
|
cz.set_bitmap(repl_img.into_raw());
|
||||||
|
|
||||||
|
if let Some(ver) = version {
|
||||||
|
match cz.header_mut().set_version(*ver) {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(_) => {
|
||||||
|
Error::raw(
|
||||||
|
ErrorKind::ValueValidation,
|
||||||
|
format!("Invalid CZ Version {}; expected 0, 1, 2, 3, or 4\n", ver)
|
||||||
|
).exit()
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
match cz.save_as_cz(output) {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(err) => {
|
||||||
|
Error::raw(
|
||||||
|
ErrorKind::ValueValidation,
|
||||||
|
format!("Failed to save CZ file: {}\n", err)
|
||||||
|
).exit()
|
||||||
|
},
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue