mirror of
https://github.com/G2-Games/lbee-utils.git
synced 2025-06-23 07:02:53 -05:00
Compare commits
No commits in common. "9e6ba57deed2ec4f93b3039e491c6523d9ddb784" and "3911c73761b7ef25b4bd9918cbcdc9f332460b21" have entirely different histories.
9e6ba57dee
...
3911c73761
14 changed files with 234 additions and 207 deletions
|
@ -1,7 +1,7 @@
|
|||
[workspace]
|
||||
resolver = "2"
|
||||
members = [
|
||||
"cz", "experimental",
|
||||
"cz",
|
||||
"luca_pak",
|
||||
"utils",
|
||||
]
|
||||
|
|
|
@ -1,3 +1,8 @@
|
|||
pub enum BitError {
|
||||
InputLength
|
||||
}
|
||||
|
||||
|
||||
pub struct BitIo {
|
||||
data: Vec<u8>,
|
||||
byte_offset: usize,
|
||||
|
@ -35,7 +40,7 @@ impl BitIo {
|
|||
/// Read some bits from the buffer
|
||||
pub fn read_bit(&mut self, bit_len: usize) -> u64 {
|
||||
if bit_len > 8 * 8 {
|
||||
panic!("Cannot read more than 64 bits")
|
||||
panic!()
|
||||
}
|
||||
|
||||
if bit_len % 8 == 0 && self.bit_offset == 0 {
|
||||
|
@ -61,7 +66,7 @@ impl BitIo {
|
|||
/// Read some bytes from the buffer
|
||||
pub fn read(&mut self, byte_len: usize) -> u64 {
|
||||
if byte_len > 8 {
|
||||
panic!("Cannot read more than 8 bytes")
|
||||
panic!()
|
||||
}
|
||||
|
||||
let mut padded_slice = [0u8; 8];
|
||||
|
@ -74,7 +79,7 @@ impl BitIo {
|
|||
/// Write some bits to the buffer
|
||||
pub fn write_bit(&mut self, data: u64, bit_len: usize) {
|
||||
if bit_len > 8 * 8 {
|
||||
panic!("Cannot write more than 64 bits");
|
||||
panic!();
|
||||
}
|
||||
|
||||
if bit_len % 8 == 0 && self.bit_offset == 0 {
|
||||
|
@ -99,9 +104,9 @@ impl BitIo {
|
|||
self.byte_size = self.byte_offset + (self.bit_offset + 7) / 8;
|
||||
}
|
||||
|
||||
pub fn write(&mut self, data: u64, byte_len: usize) {
|
||||
pub fn write(&mut self, data: u64, byte_len: usize) -> Result<(), BitError> {
|
||||
if byte_len > 8 {
|
||||
panic!("Cannot write more than 8 bytes")
|
||||
return Err(BitError::InputLength);
|
||||
}
|
||||
|
||||
let mut padded_slice = [0u8; 8];
|
||||
|
@ -112,5 +117,7 @@ impl BitIo {
|
|||
self.byte_offset += byte_len;
|
||||
|
||||
self.byte_size = self.byte_offset + (self.bit_offset + 7) / 8;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -107,9 +107,13 @@ pub fn indexed_gen_palette(
|
|||
.map(|c| Rgba([c.r, c.g, c.b, c.a]))
|
||||
.collect();
|
||||
|
||||
dbg!(gen_palette.len());
|
||||
|
||||
let mut output_palette = vec![Rgba([0, 0, 0, 0]); 256];
|
||||
output_palette[0..gen_palette.len()].copy_from_slice(&gen_palette);
|
||||
|
||||
dbg!(output_palette.len());
|
||||
|
||||
Ok((indicies, output_palette))
|
||||
}
|
||||
|
||||
|
|
|
@ -16,9 +16,6 @@ pub enum CzError {
|
|||
#[error("Bitmap size does not match image size")]
|
||||
BitmapFormat,
|
||||
|
||||
#[error("CZ version is invalid: {}", 0)]
|
||||
InvalidVersion(u32),
|
||||
|
||||
#[error("File data is incorrect, it might be corrupt: {0}")]
|
||||
Corrupt(String),
|
||||
|
||||
|
@ -159,10 +156,10 @@ impl CommonHeader {
|
|||
self.version
|
||||
}
|
||||
|
||||
pub fn set_version<I: TryInto<CzVersion> + Into<u32> + Clone>(&mut self, version: I) -> Result<(), CzError> {
|
||||
self.version = match version.clone().try_into() {
|
||||
pub fn set_version<I: TryInto<CzVersion>>(&mut self, version: I) -> Result<(), ()> {
|
||||
self.version = match version.try_into() {
|
||||
Ok(val) => val,
|
||||
Err(_) => return Err(CzError::InvalidVersion(version.into())),
|
||||
Err(_) => return Err(()),
|
||||
};
|
||||
|
||||
Ok(())
|
||||
|
|
|
@ -27,7 +27,7 @@ pub struct CompressionInfo {
|
|||
pub total_size_compressed: usize,
|
||||
|
||||
/// Total size of the original uncompressed data
|
||||
pub _total_size_raw: usize,
|
||||
pub total_size_raw: usize,
|
||||
|
||||
/// The compression chunk information
|
||||
pub chunks: Vec<ChunkInfo>,
|
||||
|
@ -82,7 +82,7 @@ pub fn get_chunk_info<T: Seek + ReadBytesExt + Read>(
|
|||
Ok(CompressionInfo {
|
||||
chunk_count: parts_count as usize,
|
||||
total_size_compressed: total_size as usize,
|
||||
_total_size_raw: total_size_raw as usize,
|
||||
total_size_raw: total_size_raw as usize,
|
||||
chunks: part_sizes,
|
||||
length: bytes.stream_position()? as usize,
|
||||
})
|
||||
|
@ -93,59 +93,70 @@ pub fn decompress<T: Seek + ReadBytesExt + Read>(
|
|||
input: &mut T,
|
||||
chunk_info: &CompressionInfo,
|
||||
) -> Result<Vec<u8>, CzError> {
|
||||
let mut output_buf: Vec<u8> = vec![];
|
||||
let mut m_dst = 0;
|
||||
let mut bitmap = vec![0; chunk_info.total_size_raw];
|
||||
for chunk in &chunk_info.chunks {
|
||||
let mut part = vec![0u8; chunk.size_compressed * 2];
|
||||
input.read_exact(&mut part)?;
|
||||
|
||||
for block in &chunk_info.chunks {
|
||||
let mut buffer = vec![0u16; block.size_compressed];
|
||||
for j in (0..part.len()).step_by(2) {
|
||||
let ctl = part[j + 1];
|
||||
|
||||
for word in buffer.iter_mut() {
|
||||
*word = input.read_u16::<LittleEndian>().unwrap();
|
||||
}
|
||||
|
||||
let raw_buf = decompress_lzw(&buffer, block.size_raw);
|
||||
|
||||
output_buf.write_all(&raw_buf)?;
|
||||
}
|
||||
|
||||
Ok(output_buf)
|
||||
}
|
||||
|
||||
fn decompress_lzw(
|
||||
input_data: &[u16],
|
||||
size: usize
|
||||
) -> Vec<u8> {
|
||||
let mut dictionary: HashMap<u16, Vec<u8>> = HashMap::new();
|
||||
for i in 0..256 {
|
||||
dictionary.insert(i as u16, vec![i as u8]);
|
||||
}
|
||||
let mut dictionary_count = dictionary.len() as u16;
|
||||
|
||||
let mut w = vec![0];
|
||||
let mut result = Vec::with_capacity(size);
|
||||
|
||||
input_data.iter().for_each(|element| {
|
||||
let mut entry;
|
||||
if let Some(x) = dictionary.get(element) {
|
||||
entry = x.clone();
|
||||
} else if *element == dictionary_count {
|
||||
entry = w.clone();
|
||||
entry.push(w[0]);
|
||||
if ctl == 0 {
|
||||
bitmap[m_dst] = part[j];
|
||||
m_dst += 1;
|
||||
} else {
|
||||
panic!("Bad compressed element: {}", element)
|
||||
m_dst += copy_range(&mut bitmap, &part, get_offset(&part, j), m_dst);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
result.write_all(&entry).unwrap();
|
||||
w.push(entry[0]);
|
||||
bitmap.truncate(chunk_info.total_size_raw);
|
||||
|
||||
dictionary.insert(dictionary_count, w.clone());
|
||||
dictionary_count += 1;
|
||||
|
||||
w = entry;
|
||||
});
|
||||
|
||||
result
|
||||
Ok(bitmap)
|
||||
}
|
||||
|
||||
fn get_offset(input: &[u8], src: usize) -> usize {
|
||||
(((input[src] as usize) | (input[src + 1] as usize) << 8) - 0x101) * 2
|
||||
}
|
||||
|
||||
fn copy_range(bitmap: &mut Vec<u8>, input: &[u8], src: usize, dst: usize) -> usize {
|
||||
let mut dst = dst;
|
||||
let start_pos = dst;
|
||||
|
||||
if input[src + 1] == 0 {
|
||||
bitmap[dst] = input[src];
|
||||
dst += 1;
|
||||
} else if get_offset(input, src) == src {
|
||||
bitmap[dst] = 0;
|
||||
dst += 1;
|
||||
} else {
|
||||
dst += copy_range(bitmap, input, get_offset(input, src), dst);
|
||||
}
|
||||
|
||||
if input[src + 3] == 0 {
|
||||
bitmap[dst] = input[src + 2];
|
||||
dst += 1;
|
||||
} else if get_offset(input, src + 2) == src {
|
||||
bitmap[dst] = bitmap[start_pos];
|
||||
dst += 1;
|
||||
} else {
|
||||
bitmap[dst] = copy_one(input, get_offset(input, src + 2));
|
||||
dst += 1;
|
||||
}
|
||||
|
||||
dst - start_pos
|
||||
}
|
||||
|
||||
fn copy_one(input: &[u8], src: usize) -> u8 {
|
||||
if input[src + 1] == 0 {
|
||||
input[src]
|
||||
} else if get_offset(input, src) == src {
|
||||
0
|
||||
} else {
|
||||
copy_one(input, get_offset(input, src))
|
||||
}
|
||||
}
|
||||
|
||||
/// Decompress an LZW compressed stream like CZ2
|
||||
pub fn decompress2<T: Seek + ReadBytesExt + Read>(
|
||||
|
@ -166,10 +177,7 @@ pub fn decompress2<T: Seek + ReadBytesExt + Read>(
|
|||
Ok(output_buf)
|
||||
}
|
||||
|
||||
fn decompress_lzw2(
|
||||
input_data: &[u8],
|
||||
size: usize
|
||||
) -> Vec<u8> {
|
||||
fn decompress_lzw2(input_data: &[u8], size: usize) -> Vec<u8> {
|
||||
let mut data = input_data.to_vec();
|
||||
data[0] = 0;
|
||||
let mut dictionary = HashMap::new();
|
||||
|
@ -236,7 +244,7 @@ pub fn compress(
|
|||
|
||||
let mut output_buf: Vec<u8> = vec![];
|
||||
let mut output_info = CompressionInfo {
|
||||
_total_size_raw: data.len(),
|
||||
total_size_raw: data.len(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
|
@ -271,11 +279,7 @@ pub fn compress(
|
|||
(output_buf, output_info)
|
||||
}
|
||||
|
||||
fn compress_lzw(
|
||||
data: &[u8],
|
||||
size: usize,
|
||||
last: Vec<u8>
|
||||
) -> (usize, Vec<u16>, Vec<u8>) {
|
||||
fn compress_lzw(data: &[u8], size: usize, last: Vec<u8>) -> (usize, Vec<u16>, Vec<u8>) {
|
||||
let mut count = 0;
|
||||
let mut dictionary = HashMap::new();
|
||||
for i in 0..=255 {
|
||||
|
@ -327,10 +331,7 @@ fn compress_lzw(
|
|||
(count, compressed, last_element)
|
||||
}
|
||||
|
||||
pub fn compress2(
|
||||
data: &[u8],
|
||||
size: usize
|
||||
) -> (Vec<u8>, CompressionInfo) {
|
||||
pub fn compress2(data: &[u8], size: usize) -> (Vec<u8>, CompressionInfo) {
|
||||
let size = if size == 0 { 0x87BDF } else { size };
|
||||
|
||||
let mut part_data;
|
||||
|
@ -341,7 +342,7 @@ pub fn compress2(
|
|||
|
||||
let mut output_buf: Vec<u8> = Vec::new();
|
||||
let mut output_info = CompressionInfo {
|
||||
_total_size_raw: data.len(),
|
||||
total_size_raw: data.len(),
|
||||
..Default::default()
|
||||
};
|
||||
|
||||
|
@ -373,11 +374,7 @@ pub fn compress2(
|
|||
(output_buf, output_info)
|
||||
}
|
||||
|
||||
fn compress_lzw2(
|
||||
data: &[u8],
|
||||
size: usize,
|
||||
last: Vec<u8>
|
||||
) -> (usize, Vec<u8>, Vec<u8>) {
|
||||
fn compress_lzw2(data: &[u8], size: usize, last: Vec<u8>) -> (usize, Vec<u8>, Vec<u8>) {
|
||||
let mut data = data.to_vec();
|
||||
if !data.is_empty() {
|
||||
data[0] = 0;
|
||||
|
|
|
@ -113,7 +113,7 @@ impl DynamicCz {
|
|||
/// to change the CZ# version.
|
||||
pub fn save_as_cz<T: Into<std::path::PathBuf>>(&self, path: T) -> Result<(), CzError> {
|
||||
let mut out_file = BufWriter::new(File::create(path.into())?);
|
||||
let mut header = *self.header();
|
||||
let mut header = self.header().clone();
|
||||
|
||||
if header.version() == CzVersion::CZ2 {
|
||||
header.set_length(0x12)
|
||||
|
|
|
@ -55,9 +55,9 @@ fn line_diff(header: &CommonHeader, data: &[u8]) -> Vec<u8> {
|
|||
let mut curr_line;
|
||||
let mut prev_line = Vec::with_capacity(line_byte_count);
|
||||
|
||||
let mut index = 0;
|
||||
let mut i = 0;
|
||||
for y in 0..height {
|
||||
curr_line = data[index..index + line_byte_count].to_vec();
|
||||
curr_line = data[i..i + line_byte_count].to_vec();
|
||||
|
||||
if y % block_height as u32 != 0 {
|
||||
curr_line.iter_mut().zip(&prev_line).for_each(|(curr_p, prev_p)| {
|
||||
|
@ -67,7 +67,7 @@ fn line_diff(header: &CommonHeader, data: &[u8]) -> Vec<u8> {
|
|||
|
||||
prev_line.clone_from(&curr_line);
|
||||
if pixel_byte_count == 4 {
|
||||
output_buf[index..index + line_byte_count].copy_from_slice(&curr_line);
|
||||
output_buf[i..i + line_byte_count].copy_from_slice(&curr_line);
|
||||
} else if pixel_byte_count == 3 {
|
||||
for x in (0..line_byte_count).step_by(3) {
|
||||
let loc = (y * 3 * width) as usize + x;
|
||||
|
@ -86,7 +86,7 @@ fn line_diff(header: &CommonHeader, data: &[u8]) -> Vec<u8> {
|
|||
}
|
||||
}
|
||||
|
||||
index += line_byte_count;
|
||||
i += line_byte_count;
|
||||
}
|
||||
|
||||
output_buf
|
||||
|
|
|
@ -68,23 +68,18 @@ fn line_diff(header: &CommonHeader, data: &[u8]) -> Vec<u8> {
|
|||
});
|
||||
}
|
||||
|
||||
// Write the decoded RGBA data to the final buffer
|
||||
curr_line
|
||||
.windows(3)
|
||||
.step_by(3)
|
||||
.zip(&curr_alpha)
|
||||
.for_each(|(curr_p, alpha_p)| {
|
||||
for x in 0..width as usize {
|
||||
let pos = x * 3;
|
||||
output_buf.extend_from_slice(&[
|
||||
curr_p[0],
|
||||
curr_p[1],
|
||||
curr_p[2],
|
||||
*alpha_p,
|
||||
curr_line[pos],
|
||||
curr_line[pos + 1],
|
||||
curr_line[pos + 2],
|
||||
curr_alpha[x],
|
||||
]);
|
||||
});
|
||||
}
|
||||
|
||||
prev_line.clone_from(&curr_line);
|
||||
prev_alpha.clone_from(&curr_alpha);
|
||||
|
||||
rgb_index += width as usize * 3;
|
||||
alpha_index += width as usize;
|
||||
}
|
||||
|
|
|
@ -1,15 +0,0 @@
|
|||
[package]
|
||||
name = "experimental"
|
||||
description = """
|
||||
A package for experimentation, not for publishing
|
||||
"""
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
authors.workspace = true
|
||||
publish = false
|
||||
|
||||
[dependencies]
|
||||
cz = { path = "../cz/", features = ["png"] }
|
||||
|
||||
[lints]
|
||||
workspace = true
|
|
@ -1,3 +0,0 @@
|
|||
fn main() {
|
||||
println!("Hello, world!");
|
||||
}
|
|
@ -134,7 +134,6 @@ impl Pak {
|
|||
length: offsets[i].1,
|
||||
data,
|
||||
name: Some(file_names[i].clone()),
|
||||
unknown1: todo!(),
|
||||
id: header.id_start + i as u32,
|
||||
replace: false,
|
||||
};
|
||||
|
|
|
@ -6,5 +6,6 @@ fn main() {
|
|||
|
||||
for entry in pak.entries() {
|
||||
println!("{}", entry.name().as_ref().unwrap());
|
||||
println!("{}", entry);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -8,6 +8,6 @@ edition = "2021"
|
|||
[dependencies]
|
||||
byteorder = "1.5.0"
|
||||
clap = { version = "4.5.4", features = ["derive", "unicode"] }
|
||||
cz = { path = "../cz", features = ["png"] }
|
||||
image = { version = "0.25", default-features = false, features = ["png"] }
|
||||
cz = { path = "../cz" }
|
||||
image = { version = "0.25.1", default-features = false, features = ["png"] }
|
||||
walkdir = "2.5.0"
|
||||
|
|
|
@ -1,5 +1,7 @@
|
|||
use cz::DynamicCz;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use std::path::PathBuf;
|
||||
|
||||
use clap::{error::ErrorKind, Error, Parser, Subcommand};
|
||||
|
||||
#[derive(Parser)]
|
||||
|
@ -52,7 +54,7 @@ enum Commands {
|
|||
|
||||
/// Output CZ file bit depth
|
||||
#[arg(short, long, value_name = "BIT DEPTH")]
|
||||
depth: Option<u16>,
|
||||
depth: Option<u8>,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -63,16 +65,25 @@ fn main() {
|
|||
match &cli.command {
|
||||
Commands::Decode { input, output, batch } => {
|
||||
if !input.exists() {
|
||||
Error::raw(ErrorKind::ValueValidation, "The input file/folder provided does not exist\n").exit()
|
||||
Error::raw(
|
||||
ErrorKind::ValueValidation,
|
||||
format!("The input file/folder provided does not exist\n")
|
||||
).exit()
|
||||
}
|
||||
|
||||
if *batch {
|
||||
if input.is_file() {
|
||||
Error::raw(ErrorKind::ValueValidation, "Batch input must be a directory\n").exit()
|
||||
Error::raw(
|
||||
ErrorKind::ValueValidation,
|
||||
format!("Batch input must be a directory\n")
|
||||
).exit()
|
||||
}
|
||||
|
||||
if output.is_none() || output.as_ref().unwrap().is_file() {
|
||||
Error::raw(ErrorKind::ValueValidation, "Batch output must be a directory\n").exit()
|
||||
Error::raw(
|
||||
ErrorKind::ValueValidation,
|
||||
format!("Batch output must be a directory\n")
|
||||
).exit()
|
||||
}
|
||||
|
||||
for entry in walkdir::WalkDir::new(input).max_depth(1) {
|
||||
|
@ -113,120 +124,154 @@ fn main() {
|
|||
}
|
||||
Commands::Replace { batch, input, replacement, output, version, depth } => {
|
||||
if !input.exists() {
|
||||
Error::raw(ErrorKind::ValueValidation, "The original file provided does not exist\n").exit()
|
||||
Error::raw(
|
||||
ErrorKind::ValueValidation,
|
||||
format!("The original file provided does not exist\n")
|
||||
).exit()
|
||||
}
|
||||
|
||||
if !replacement.exists() {
|
||||
Error::raw(ErrorKind::ValueValidation, "The replacement file provided does not exist\n").exit()
|
||||
Error::raw(
|
||||
ErrorKind::ValueValidation,
|
||||
format!("The replacement file provided does not exist\n")
|
||||
).exit()
|
||||
}
|
||||
|
||||
// If it's a batch replacement, we want directories to search
|
||||
if *batch {
|
||||
if !input.is_dir() {
|
||||
Error::raw(ErrorKind::ValueValidation, "Batch input location must be a directory\n").exit()
|
||||
if input.is_file() {
|
||||
Error::raw(
|
||||
ErrorKind::ValueValidation,
|
||||
format!("Batch input must be a directory\n")
|
||||
).exit()
|
||||
}
|
||||
|
||||
if !replacement.is_dir() {
|
||||
Error::raw(ErrorKind::ValueValidation, "Batch replacement location must be a directory\n").exit()
|
||||
if replacement.is_file() {
|
||||
Error::raw(
|
||||
ErrorKind::ValueValidation,
|
||||
format!("Batch replacement location must be a directory\n")
|
||||
).exit()
|
||||
}
|
||||
|
||||
if !output.is_dir() {
|
||||
Error::raw(ErrorKind::ValueValidation, "Batch output location must be a directory\n").exit()
|
||||
if output.is_file() {
|
||||
Error::raw(
|
||||
ErrorKind::ValueValidation,
|
||||
format!("Batch output must be a directory\n")
|
||||
).exit()
|
||||
}
|
||||
|
||||
// Replace all the files within the directory and print errors for them
|
||||
for entry in walkdir::WalkDir::new(input)
|
||||
.max_depth(1)
|
||||
.same_file_system(true)
|
||||
{
|
||||
for entry in walkdir::WalkDir::new(input).max_depth(1) {
|
||||
let path = entry.unwrap().into_path();
|
||||
if !path.is_file() {
|
||||
continue
|
||||
continue;
|
||||
}
|
||||
|
||||
// Set the replacement image to the same name as the original file
|
||||
let mut final_replacement = replacement.to_path_buf();
|
||||
final_replacement.push(PathBuf::from(path.file_name().unwrap()).with_extension("png"));
|
||||
let filename = PathBuf::from(path.file_name().unwrap());
|
||||
|
||||
// Set the replacement image to the same name as the original file
|
||||
let mut final_output = output.to_path_buf();
|
||||
final_output.push(path.file_name().unwrap());
|
||||
let mut final_path = output.clone();
|
||||
final_path.push(&filename);
|
||||
|
||||
if let Err(error) = replace_cz(
|
||||
&path,
|
||||
&final_output,
|
||||
&final_replacement,
|
||||
version,
|
||||
depth
|
||||
) {
|
||||
Error::raw(ErrorKind::ValueValidation, format!("{:?} - {}\n", path, error)).print().unwrap();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
if !input.is_file() {
|
||||
Error::raw(ErrorKind::ValueValidation, "Input must be a file\n").exit()
|
||||
}
|
||||
let mut final_replacement = replacement.clone();
|
||||
final_replacement.push(filename.with_extension("png"));
|
||||
|
||||
if !replacement.is_file() {
|
||||
Error::raw(ErrorKind::ValueValidation, "Replacement must be a file\n").exit()
|
||||
}
|
||||
|
||||
if !output.is_file() {
|
||||
Error::raw(ErrorKind::ValueValidation, "Replacement output must be a file\n").exit()
|
||||
}
|
||||
|
||||
// Replace the input file with the new image
|
||||
replace_cz(
|
||||
&input,
|
||||
&output,
|
||||
&replacement,
|
||||
version,
|
||||
depth
|
||||
).unwrap();
|
||||
}
|
||||
let repl_img = match image::open(&final_replacement) {
|
||||
Ok(img) => img,
|
||||
Err(_) => {
|
||||
Error::raw(
|
||||
ErrorKind::ValueValidation,
|
||||
format!("Could not open replacement file as an image: {}\n", final_replacement.into_os_string().to_str().unwrap())
|
||||
).print().unwrap();
|
||||
continue;
|
||||
},
|
||||
}
|
||||
}
|
||||
};
|
||||
let repl_img = repl_img.to_rgba8();
|
||||
|
||||
/// Replace a CZ file with the bitmap of a PNG file
|
||||
fn replace_cz<P: ?Sized + AsRef<Path>>(
|
||||
input_path: &P,
|
||||
output_path: &P,
|
||||
replacement_path: &P,
|
||||
version: &Option<u8>,
|
||||
depth: &Option<u16>,
|
||||
) -> Result<(), Box<dyn std::error::Error>> {
|
||||
let path = input_path.as_ref();
|
||||
if !path.is_file() {
|
||||
return Err("Input path is not a file".into())
|
||||
}
|
||||
let mut cz = match DynamicCz::open(&path) {
|
||||
Ok(cz) => cz,
|
||||
Err(_) => {
|
||||
Error::raw(
|
||||
ErrorKind::ValueValidation,
|
||||
format!("Could not open input as a CZ file: {}\n", path.into_os_string().to_str().unwrap())
|
||||
).print().unwrap();
|
||||
continue;
|
||||
},
|
||||
};
|
||||
|
||||
if !replacement_path.as_ref().exists() || !replacement_path.as_ref().is_file() {
|
||||
return Err("Replacement path does not exist or is not a file".into())
|
||||
}
|
||||
|
||||
// Open the replacement image and convert it to RGBA8
|
||||
let repl_img = image::open(&replacement_path)?.to_rgba8();
|
||||
|
||||
// Open the original CZ file
|
||||
let mut cz = DynamicCz::open(&path)?;
|
||||
|
||||
// Set CZ header parameters and the new bitmap
|
||||
cz.header_mut().set_width(repl_img.width() as u16);
|
||||
cz.header_mut().set_height(repl_img.height() as u16);
|
||||
cz.set_bitmap(repl_img.into_raw());
|
||||
cz.remove_palette();
|
||||
|
||||
if let Some(depth) = depth {
|
||||
cz.header_mut().set_depth(*depth)
|
||||
cz.header_mut().set_depth(*depth as u16)
|
||||
}
|
||||
|
||||
if let Some(ver) = version {
|
||||
cz.header_mut().set_version(*ver)?;
|
||||
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()
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
// Save the file to the proper output location
|
||||
cz.save_as_cz(&output_path.as_ref()).unwrap();
|
||||
|
||||
Ok(())
|
||||
cz.save_as_cz(&final_path).unwrap();
|
||||
}
|
||||
} else {
|
||||
let mut 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 repl_img = match image::open(replacement) {
|
||||
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();
|
||||
|
||||
cz.header_mut().set_width(repl_img.width() as u16);
|
||||
cz.header_mut().set_height(repl_img.height() as u16);
|
||||
cz.set_bitmap(repl_img.into_raw());
|
||||
cz.remove_palette();
|
||||
|
||||
if let Some(depth) = depth {
|
||||
cz.header_mut().set_depth(*depth as u16)
|
||||
}
|
||||
|
||||
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