Renamed DynamicCz to CzFile, added Encode option for czutil

This commit is contained in:
G2-Games 2024-11-14 12:43:27 -06:00
parent a7a486999d
commit e68ca53ab5
5 changed files with 100 additions and 26 deletions

View file

@ -13,7 +13,7 @@ use crate::{
/// A CZ# interface which can open and save any CZ file type. /// A CZ# interface which can open and save any CZ file type.
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct DynamicCz { pub struct CzFile {
header_common: CommonHeader, header_common: CommonHeader,
header_extended: Option<ExtendedHeader>, header_extended: Option<ExtendedHeader>,
@ -24,7 +24,7 @@ pub struct DynamicCz {
bitmap: Vec<u8>, bitmap: Vec<u8>,
} }
impl DynamicCz { impl CzFile {
/// Decode a CZ# file from anything that implements [`Read`] and [`Seek`] /// Decode a CZ# file from anything that implements [`Read`] and [`Seek`]
/// ///
/// The input must begin with the /// The input must begin with the

View file

@ -17,14 +17,14 @@ use common::CzError;
use std::{io::BufReader, path::Path}; use std::{io::BufReader, path::Path};
/// Open a CZ# file from a path /// Open a CZ# file from a path
pub fn open<P: ?Sized + AsRef<Path>>(path: &P) -> Result<DynamicCz, CzError> { pub fn open<P: ?Sized + AsRef<Path>>(path: &P) -> Result<CzFile, CzError> {
let mut img_file = BufReader::new(std::fs::File::open(path)?); let mut img_file = BufReader::new(std::fs::File::open(path)?);
DynamicCz::decode(&mut img_file) CzFile::decode(&mut img_file)
} }
#[doc(inline)] #[doc(inline)]
pub use dynamic::DynamicCz; pub use dynamic::CzFile;
/* /*
#[doc(inline)] #[doc(inline)]

View file

@ -1,6 +1,6 @@
use std::io::Cursor; use std::io::Cursor;
use cz::{common::CzVersion, DynamicCz}; use cz::{common::CzVersion, CzFile};
const KODIM03: (u16, u16, &[u8]) = (128, 128, include_bytes!("test_images/kodim03.rgba")); const KODIM03: (u16, u16, &[u8]) = (128, 128, include_bytes!("test_images/kodim03.rgba"));
const KODIM23: (u16, u16, &[u8]) = (225, 225, include_bytes!("test_images/kodim23.rgba")); const KODIM23: (u16, u16, &[u8]) = (225, 225, include_bytes!("test_images/kodim23.rgba"));
@ -13,13 +13,13 @@ const TEST_IMAGES: &[TestImage] = &[KODIM03, KODIM23, SQPTEXT, DPFLOGO];
#[test] #[test]
fn cz0_round_trip() { fn cz0_round_trip() {
for image in TEST_IMAGES { for image in TEST_IMAGES {
let original_cz = DynamicCz::from_raw(CzVersion::CZ0, image.0, image.1, image.2.to_vec()); let original_cz = CzFile::from_raw(CzVersion::CZ0, image.0, image.1, image.2.to_vec());
let mut cz_bytes = Vec::new(); let mut cz_bytes = Vec::new();
original_cz.encode(&mut cz_bytes).unwrap(); original_cz.encode(&mut cz_bytes).unwrap();
let mut cz_bytes = Cursor::new(cz_bytes); let mut cz_bytes = Cursor::new(cz_bytes);
let decoded_cz = DynamicCz::decode(&mut cz_bytes).unwrap(); let decoded_cz = CzFile::decode(&mut cz_bytes).unwrap();
assert_eq!(original_cz.as_raw(), decoded_cz.as_raw()); assert_eq!(original_cz.as_raw(), decoded_cz.as_raw());
} }
@ -28,13 +28,13 @@ fn cz0_round_trip() {
#[test] #[test]
fn cz1_round_trip() { fn cz1_round_trip() {
for image in TEST_IMAGES { for image in TEST_IMAGES {
let original_cz = DynamicCz::from_raw(CzVersion::CZ1, image.0, image.1, image.2.to_vec()); let original_cz = CzFile::from_raw(CzVersion::CZ1, image.0, image.1, image.2.to_vec());
let mut cz_bytes = Vec::new(); let mut cz_bytes = Vec::new();
original_cz.encode(&mut cz_bytes).unwrap(); original_cz.encode(&mut cz_bytes).unwrap();
let mut cz_bytes = Cursor::new(cz_bytes); let mut cz_bytes = Cursor::new(cz_bytes);
let decoded_cz = DynamicCz::decode(&mut cz_bytes).unwrap(); let decoded_cz = CzFile::decode(&mut cz_bytes).unwrap();
assert_eq!(original_cz.as_raw(), decoded_cz.as_raw()); assert_eq!(original_cz.as_raw(), decoded_cz.as_raw());
} }
@ -42,32 +42,29 @@ fn cz1_round_trip() {
#[test] #[test]
fn cz2_round_trip() { fn cz2_round_trip() {
let mut i = 0;
for image in TEST_IMAGES { for image in TEST_IMAGES {
let original_cz = DynamicCz::from_raw(CzVersion::CZ2, image.0, image.1, image.2.to_vec()); let original_cz = CzFile::from_raw(CzVersion::CZ2, image.0, image.1, image.2.to_vec());
let mut cz_bytes = Vec::new(); let mut cz_bytes = Vec::new();
original_cz.encode(&mut cz_bytes).unwrap(); original_cz.encode(&mut cz_bytes).unwrap();
let mut cz_bytes = Cursor::new(cz_bytes); let mut cz_bytes = Cursor::new(cz_bytes);
let decoded_cz = DynamicCz::decode(&mut cz_bytes).unwrap(); let decoded_cz = CzFile::decode(&mut cz_bytes).unwrap();
assert_eq!(original_cz.as_raw(), decoded_cz.as_raw()); assert_eq!(original_cz.as_raw(), decoded_cz.as_raw());
i += 1;
} }
} }
#[test] #[test]
fn cz3_round_trip() { fn cz3_round_trip() {
for image in TEST_IMAGES { for image in TEST_IMAGES {
let original_cz = DynamicCz::from_raw(CzVersion::CZ3, image.0, image.1, image.2.to_vec()); let original_cz = CzFile::from_raw(CzVersion::CZ3, image.0, image.1, image.2.to_vec());
let mut cz_bytes = Vec::new(); let mut cz_bytes = Vec::new();
original_cz.encode(&mut cz_bytes).unwrap(); original_cz.encode(&mut cz_bytes).unwrap();
let mut cz_bytes = Cursor::new(cz_bytes); let mut cz_bytes = Cursor::new(cz_bytes);
let decoded_cz = DynamicCz::decode(&mut cz_bytes).unwrap(); let decoded_cz = CzFile::decode(&mut cz_bytes).unwrap();
assert_eq!(original_cz.as_raw(), decoded_cz.as_raw()); assert_eq!(original_cz.as_raw(), decoded_cz.as_raw());
} }
@ -76,13 +73,13 @@ fn cz3_round_trip() {
#[test] #[test]
fn cz4_round_trip() { fn cz4_round_trip() {
for image in TEST_IMAGES { for image in TEST_IMAGES {
let original_cz = DynamicCz::from_raw(CzVersion::CZ4, image.0, image.1, image.2.to_vec()); let original_cz = CzFile::from_raw(CzVersion::CZ4, image.0, image.1, image.2.to_vec());
let mut cz_bytes = Vec::new(); let mut cz_bytes = Vec::new();
original_cz.encode(&mut cz_bytes).unwrap(); original_cz.encode(&mut cz_bytes).unwrap();
let mut cz_bytes = Cursor::new(cz_bytes); let mut cz_bytes = Cursor::new(cz_bytes);
let decoded_cz = DynamicCz::decode(&mut cz_bytes).unwrap(); let decoded_cz = CzFile::decode(&mut cz_bytes).unwrap();
assert_eq!(original_cz.as_raw(), decoded_cz.as_raw()); assert_eq!(original_cz.as_raw(), decoded_cz.as_raw());
} }

View file

@ -156,7 +156,7 @@ impl eframe::App for PakExplorer {
.set_file_name(display_name) .set_file_name(display_name)
.save_file() .save_file()
{ {
let cz = cz::DynamicCz::decode(&mut std::io::Cursor::new( let cz = cz::CzFile::decode(&mut std::io::Cursor::new(
entry.as_bytes(), entry.as_bytes(),
)) ))
.unwrap(); .unwrap();
@ -175,7 +175,7 @@ impl eframe::App for PakExplorer {
let texture: &TextureHandle = self.image_texture.get_or_insert_with(|| { let texture: &TextureHandle = self.image_texture.get_or_insert_with(|| {
let cz = let cz =
cz::DynamicCz::decode(&mut std::io::Cursor::new(entry.as_bytes())) cz::CzFile::decode(&mut std::io::Cursor::new(entry.as_bytes()))
.unwrap(); .unwrap();
let image = ColorImage::from_rgba_unmultiplied( let image = ColorImage::from_rgba_unmultiplied(
[cz.header().width() as usize, cz.header().height() as usize], [cz.header().width() as usize, cz.header().height() as usize],

View file

@ -1,10 +1,10 @@
use clap::{error::ErrorKind, Error, Parser, Subcommand}; use clap::{error::ErrorKind, Error, Parser, Subcommand};
use cz::{common::CzVersion, CzFile};
use image::ColorType; use image::ColorType;
use lbee_utils::version; use lbee_utils::version;
use owo_colors::OwoColorize;
use std::{ use std::{
fs, ascii::AsciiExt, fs, path::{Path, PathBuf}, process::exit
path::{Path, PathBuf},
process::exit,
}; };
/// Utility to maniuplate CZ image files from the LUCA System game engine by /// Utility to maniuplate CZ image files from the LUCA System game engine by
@ -24,7 +24,7 @@ struct Cli {
#[derive(Subcommand)] #[derive(Subcommand)]
enum Commands { enum Commands {
/// Converts a CZ file to a PNG /// Decode a CZ file to a PNG
Decode { Decode {
/// Decode a whole folder, and output to another folder /// Decode a whole folder, and output to another folder
#[arg(short, long)] #[arg(short, long)]
@ -39,7 +39,26 @@ enum Commands {
output: Option<PathBuf>, output: Option<PathBuf>,
}, },
/// Replace a CZ file's image data /// Encode a PNG file to a CZ
Encode {
/// Input image to encode
#[arg(value_name = "INPUT")]
input: PathBuf,
/// Output CZ file location
#[arg(value_name = "OUTPUT")]
output: PathBuf,
/// Output CZ file version
#[arg(short, long, value_name = "CZ VERSION")]
version: Option<u8>,
/// Output CZ file bit depth
#[arg(short, long, value_name = "CZ BIT DEPTH")]
depth: Option<u16>,
},
/// Replace an existing CZ file's image data
Replace { Replace {
/// Replace a whole folder, and output to another folder, /// Replace a whole folder, and output to another folder,
/// using a folder of replacements /// using a folder of replacements
@ -265,6 +284,60 @@ fn main() {
replace_cz(&input, &output, &replacement, version, depth).unwrap(); replace_cz(&input, &output, &replacement, version, depth).unwrap();
} }
} }
Commands::Encode {
input,
output,
version,
depth
} => {
if !input.exists() {
pretty_error("The original file provided does not exist");
exit(1);
}
if output.exists() {
pretty_error("The output path already exists; not overwriting");
exit(1);
}
let version = if let Some(v) = version {
match CzVersion::try_from(*v) {
Ok(v) => v,
Err(_) => {
pretty_error(&format!("Invalid CZ version {}; must be 0, 1, 2, 3, or 4", v));
exit(1);
},
}
} else if output.extension().is_some_and(|e| e.to_ascii_lowercase().to_string_lossy().starts_with("cz")) {
let ext_string = output.extension().unwrap().to_string_lossy();
let last_char = ext_string.chars().last().unwrap();
match CzVersion::try_from(last_char) {
Ok(v) => v,
Err(e) => {
pretty_error(&format!("Invalid CZ type {}", e));
exit(1);
},
}
} else {
pretty_error("CZ version not specified or not parseable from file path");
exit(1);
};
let image = match image::open(input) {
Ok(i) => i.to_rgba8(),
Err(e) => {
pretty_error(&format!("Could not open input file: {e}"));
exit(1);
},
};
let cz = CzFile::from_raw(
version,
image.width() as u16,
image.height() as u16,
image.into_vec()
);
cz.save_as_cz(output).expect("Saving CZ file failed");
}
} }
} }
@ -310,3 +383,7 @@ fn replace_cz<P: ?Sized + AsRef<Path>>(
Ok(()) Ok(())
} }
fn pretty_error(message: &str) {
eprintln!("{}: {}", "Error".red().italic(), message);
}