mirror of
https://github.com/G2-Games/lbee-utils.git
synced 2025-04-19 23:32:55 -05:00
Ran cargo fmt
to auto format the files in a standard way, cleaned up cz_utils.rs
This commit is contained in:
parent
cb3343c933
commit
f9542a1540
3 changed files with 45 additions and 35 deletions
|
@ -1,12 +1,12 @@
|
||||||
use image::{RgbaImage, ImageFormat};
|
use image::{ImageFormat, RgbaImage};
|
||||||
|
|
||||||
/// The header of a CZ* file
|
/// The header of a CZ* file
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
struct HeaderCZ {
|
struct HeaderCZ {
|
||||||
magic: [u8; 3], // The magic bytes, can be CZ0, (CZ1, CZ2?)
|
magic: [u8; 3], // The magic bytes, can be CZ0, (CZ1, CZ2?)
|
||||||
length: u8,
|
length: u8,
|
||||||
res: (i16, i16), // The width in the header
|
res: (i16, i16), // The width in the header
|
||||||
depth: u8, // Bit depth
|
depth: u8, // Bit depth
|
||||||
mystery: Vec<u8>,
|
mystery: Vec<u8>,
|
||||||
crop: (i16, i16), // Crop dimensions
|
crop: (i16, i16), // Crop dimensions
|
||||||
bounds: (i16, i16), // Bounding box dimensions
|
bounds: (i16, i16), // Bounding box dimensions
|
||||||
|
@ -23,21 +23,17 @@ pub struct CZFile {
|
||||||
|
|
||||||
/// Create and save a PNG of the image data
|
/// Create and save a PNG of the image data
|
||||||
/// This errors if the image data is too short
|
/// This errors if the image data is too short
|
||||||
pub fn create_png(image:&CZFile, out_name:&str) {
|
pub fn create_png(image: &CZFile, out_name: &str) {
|
||||||
let process_bitmap = image.bitmap.clone();
|
let process_bitmap = image.bitmap.clone();
|
||||||
|
|
||||||
let tmp = match RgbaImage::from_raw(
|
let image_data = RgbaImage::from_raw(
|
||||||
image.header.res.0 as u32,
|
image.header.res.0 as u32,
|
||||||
image.header.res.1 as u32,
|
image.header.res.1 as u32,
|
||||||
process_bitmap,
|
process_bitmap,
|
||||||
) {
|
)
|
||||||
Some(img) => img,
|
.expect("Error encoding the image");
|
||||||
None => {
|
|
||||||
RgbaImage::new(0, 0)
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
match tmp.save_with_format(out_name, ImageFormat::Png) {
|
match image_data.save_with_format(out_name, ImageFormat::Png) {
|
||||||
Ok(()) => {}
|
Ok(()) => {}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
eprintln!("ERROR SAVING IMAGE: {}", e);
|
eprintln!("ERROR SAVING IMAGE: {}", e);
|
||||||
|
@ -48,17 +44,17 @@ pub fn create_png(image:&CZFile, out_name:&str) {
|
||||||
|
|
||||||
/// Utilities for decoding CZ0 images
|
/// Utilities for decoding CZ0 images
|
||||||
pub mod cz0 {
|
pub mod cz0 {
|
||||||
use std::io;
|
|
||||||
use std::io::Write;
|
|
||||||
use std::fs;
|
use std::fs;
|
||||||
use std::fs::File;
|
use std::fs::File;
|
||||||
|
use std::io;
|
||||||
|
use std::io::Write;
|
||||||
|
|
||||||
use image::DynamicImage;
|
|
||||||
use crate::utils::*;
|
|
||||||
use crate::cz_utils::{CZFile, HeaderCZ};
|
use crate::cz_utils::{CZFile, HeaderCZ};
|
||||||
|
use crate::utils::*;
|
||||||
|
use image::DynamicImage;
|
||||||
|
|
||||||
/// Extract all the header information from a CZ0 file
|
/// Extract all the header information from a CZ0 file
|
||||||
fn extract_header_cz0(header_vec:&Vec<u8>) -> (HeaderCZ, usize) {
|
fn extract_header_cz0(header_vec: &Vec<u8>) -> (HeaderCZ, usize) {
|
||||||
// Get the magic bytes
|
// Get the magic bytes
|
||||||
let magic: [u8; 3] = header_vec[0..3].try_into().unwrap();
|
let magic: [u8; 3] = header_vec[0..3].try_into().unwrap();
|
||||||
|
|
||||||
|
@ -104,7 +100,7 @@ pub mod cz0 {
|
||||||
/// Provided a bitstream, extract the header information and
|
/// Provided a bitstream, extract the header information and
|
||||||
/// the rest of the metadata about a CZ0 file, returning a
|
/// the rest of the metadata about a CZ0 file, returning a
|
||||||
/// struct containing the header information and bitmap
|
/// struct containing the header information and bitmap
|
||||||
pub fn decode_cz0(input_filename:&str) -> CZFile {
|
pub fn decode_cz0(input_filename: &str) -> CZFile {
|
||||||
println!("");
|
println!("");
|
||||||
println!("--Beginning CZ0 Decode--");
|
println!("--Beginning CZ0 Decode--");
|
||||||
let mut input = fs::read(input_filename).unwrap();
|
let mut input = fs::read(input_filename).unwrap();
|
||||||
|
@ -132,7 +128,11 @@ pub mod cz0 {
|
||||||
/// Provided an image, extract the bitstream and create the
|
/// Provided an image, extract the bitstream and create the
|
||||||
/// header information from a previous CZ0 file in order to
|
/// header information from a previous CZ0 file in order to
|
||||||
/// replace it
|
/// replace it
|
||||||
pub fn encode_cz0(original_file:CZFile, input_image:DynamicImage, out_name:&str) -> io::Result<()> {
|
pub fn encode_cz0(
|
||||||
|
original_file: CZFile,
|
||||||
|
input_image: DynamicImage,
|
||||||
|
out_name: &str,
|
||||||
|
) -> io::Result<()> {
|
||||||
println!("");
|
println!("");
|
||||||
println!("--Beginning CZ0 Encode--");
|
println!("--Beginning CZ0 Encode--");
|
||||||
|
|
||||||
|
@ -161,22 +161,19 @@ pub mod cz0 {
|
||||||
&[0],
|
&[0],
|
||||||
&[header.length],
|
&[header.length],
|
||||||
&vec![0u8; 3],
|
&vec![0u8; 3],
|
||||||
|
|
||||||
&word_to_bytes(header.res.0),
|
&word_to_bytes(header.res.0),
|
||||||
&word_to_bytes(header.res.1),
|
&word_to_bytes(header.res.1),
|
||||||
&[header.depth],
|
&[header.depth],
|
||||||
|
|
||||||
&header.mystery,
|
&header.mystery,
|
||||||
&word_to_bytes(header.crop.0),
|
&word_to_bytes(header.crop.0),
|
||||||
&word_to_bytes(header.crop.1),
|
&word_to_bytes(header.crop.1),
|
||||||
|
|
||||||
&word_to_bytes(header.bounds.0),
|
&word_to_bytes(header.bounds.0),
|
||||||
&word_to_bytes(header.bounds.1),
|
&word_to_bytes(header.bounds.1),
|
||||||
|
|
||||||
&word_to_bytes(header.offset.0),
|
&word_to_bytes(header.offset.0),
|
||||||
&word_to_bytes(header.offset.1),
|
&word_to_bytes(header.offset.1),
|
||||||
&vec![0u8; 4],
|
&vec![0u8; 4],
|
||||||
].concat();
|
]
|
||||||
|
.concat();
|
||||||
|
|
||||||
println!("Trimming Header...");
|
println!("Trimming Header...");
|
||||||
// Cut off unnecessary information from the header
|
// Cut off unnecessary information from the header
|
||||||
|
@ -206,7 +203,7 @@ pub mod cz0 {
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn display_info(image:&CZFile) {
|
pub fn display_info(image: &CZFile) {
|
||||||
let mut image_size = image.bitmap.len() as f32 + image.header.length as f32;
|
let mut image_size = image.bitmap.len() as f32 + image.header.length as f32;
|
||||||
image_size /= 1024.0;
|
image_size /= 1024.0;
|
||||||
|
|
||||||
|
@ -214,11 +211,23 @@ pub mod cz0 {
|
||||||
println!("Image size : {:.2} KB", image_size);
|
println!("Image size : {:.2} KB", image_size);
|
||||||
println!("Magic Bytes : {:?}", image.header.magic);
|
println!("Magic Bytes : {:?}", image.header.magic);
|
||||||
println!("Header Length : {:?} bytes", image.header.length);
|
println!("Header Length : {:?} bytes", image.header.length);
|
||||||
println!("Resolution : {}x{}", image.header.res.0, image.header.res.1);
|
println!(
|
||||||
|
"Resolution : {}x{}",
|
||||||
|
image.header.res.0, image.header.res.1
|
||||||
|
);
|
||||||
println!("Bit Depth : {} bits", image.header.depth);
|
println!("Bit Depth : {} bits", image.header.depth);
|
||||||
println!("Mystery Bytes : {:?}", image.header.mystery);
|
println!("Mystery Bytes : {:?}", image.header.mystery);
|
||||||
println!("Crop Coords : {}x{}", image.header.crop.0, image.header.crop.1);
|
println!(
|
||||||
println!("Bound Coords : {}x{}", image.header.bounds.0, image.header.bounds.1);
|
"Crop Coords : {}x{}",
|
||||||
println!("Offset Coords : {}x{}", image.header.offset.0, image.header.offset.1);
|
image.header.crop.0, image.header.crop.1
|
||||||
|
);
|
||||||
|
println!(
|
||||||
|
"Bound Coords : {}x{}",
|
||||||
|
image.header.bounds.0, image.header.bounds.1
|
||||||
|
);
|
||||||
|
println!(
|
||||||
|
"Offset Coords : {}x{}",
|
||||||
|
image.header.offset.0, image.header.offset.1
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,11 +1,12 @@
|
||||||
pub mod utils;
|
|
||||||
pub mod cz_utils;
|
pub mod cz_utils;
|
||||||
use cz_utils::cz0::*;
|
pub mod utils;
|
||||||
use cz_utils::create_png;
|
use cz_utils::create_png;
|
||||||
|
use cz_utils::cz0::*;
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
let original_image = decode_cz0("../test_files/775.cz0");
|
let original_image = decode_cz0("../test_files/775.cz0");
|
||||||
let replacement_image = image::open("../test_files/blue.png").expect("Failed to open image file");
|
let replacement_image =
|
||||||
|
image::open("../test_files/blue.png").expect("Failed to open image file");
|
||||||
|
|
||||||
encode_cz0(original_image, replacement_image, "test.cz0").expect("Error encoding the image");
|
encode_cz0(original_image, replacement_image, "test.cz0").expect("Error encoding the image");
|
||||||
|
|
||||||
|
|
|
@ -1,12 +1,12 @@
|
||||||
/// Converts 8 bit bytes to a 16 bit little endian word or
|
/// Converts 8 bit bytes to a 16 bit little endian word or
|
||||||
pub fn bytes_to_word(first:u8, second:u8) -> i16 {
|
pub fn bytes_to_word(first: u8, second: u8) -> i16 {
|
||||||
let final_value = ((second as i16) << 8) | (first as i16);
|
let final_value = ((second as i16) << 8) | (first as i16);
|
||||||
|
|
||||||
return final_value;
|
return final_value;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts a 16 bit little endian word to 8 bit bytes
|
/// Converts a 16 bit little endian word to 8 bit bytes
|
||||||
pub fn word_to_bytes(word:i16) -> [u8; 2] {
|
pub fn word_to_bytes(word: i16) -> [u8; 2] {
|
||||||
let first: u8 = (word & 0xFF) as u8; // Extract the first byte
|
let first: u8 = (word & 0xFF) as u8; // Extract the first byte
|
||||||
let second: u8 = ((word >> 8) & 0xFF) as u8; // Extract the second byte
|
let second: u8 = ((word >> 8) & 0xFF) as u8; // Extract the second byte
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue