Ran cargo fmt to auto format the files in a standard way, cleaned up cz_utils.rs

This commit is contained in:
G2-Games 2023-04-20 03:07:39 -05:00
parent cb3343c933
commit f9542a1540
3 changed files with 45 additions and 35 deletions

View file

@ -1,4 +1,4 @@
use image::{RgbaImage, ImageFormat};
use image::{ImageFormat, RgbaImage};
/// The header of a CZ* file
#[derive(Debug)]
@ -26,18 +26,14 @@ pub struct CZFile {
pub fn create_png(image: &CZFile, out_name: &str) {
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.1 as u32,
process_bitmap,
) {
Some(img) => img,
None => {
RgbaImage::new(0, 0)
}
};
)
.expect("Error encoding the image");
match tmp.save_with_format(out_name, ImageFormat::Png) {
match image_data.save_with_format(out_name, ImageFormat::Png) {
Ok(()) => {}
Err(e) => {
eprintln!("ERROR SAVING IMAGE: {}", e);
@ -48,14 +44,14 @@ pub fn create_png(image:&CZFile, out_name:&str) {
/// Utilities for decoding CZ0 images
pub mod cz0 {
use std::io;
use std::io::Write;
use std::fs;
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::utils::*;
use image::DynamicImage;
/// Extract all the header information from a CZ0 file
fn extract_header_cz0(header_vec: &Vec<u8>) -> (HeaderCZ, usize) {
@ -132,7 +128,11 @@ pub mod cz0 {
/// Provided an image, extract the bitstream and create the
/// header information from a previous CZ0 file in order to
/// 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!("--Beginning CZ0 Encode--");
@ -161,22 +161,19 @@ pub mod cz0 {
&[0],
&[header.length],
&vec![0u8; 3],
&word_to_bytes(header.res.0),
&word_to_bytes(header.res.1),
&[header.depth],
&header.mystery,
&word_to_bytes(header.crop.0),
&word_to_bytes(header.crop.1),
&word_to_bytes(header.bounds.0),
&word_to_bytes(header.bounds.1),
&word_to_bytes(header.offset.0),
&word_to_bytes(header.offset.1),
&vec![0u8; 4],
].concat();
]
.concat();
println!("Trimming Header...");
// Cut off unnecessary information from the header
@ -214,11 +211,23 @@ pub mod cz0 {
println!("Image size : {:.2} KB", image_size);
println!("Magic Bytes : {:?}", image.header.magic);
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!("Mystery Bytes : {:?}", image.header.mystery);
println!("Crop Coords : {}x{}", 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);
println!(
"Crop Coords : {}x{}",
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
);
}
}

View file

@ -1,11 +1,12 @@
pub mod utils;
pub mod cz_utils;
use cz_utils::cz0::*;
pub mod utils;
use cz_utils::create_png;
use cz_utils::cz0::*;
fn main() {
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");