From aea6ad00b67395bd5782f2b92eeb5e2376b1fe36 Mon Sep 17 00:00:00 2001 From: G2 Date: Thu, 20 Apr 2023 10:15:57 -0500 Subject: [PATCH] moved `create_png` to implementation of `CZFile` --- src/cz_utils.rs | 34 ++++++++++++++++++---------------- src/main.rs | 8 ++++---- src/utils.rs | 2 +- 3 files changed, 23 insertions(+), 21 deletions(-) diff --git a/src/cz_utils.rs b/src/cz_utils.rs index b28fe1b..89571c4 100644 --- a/src/cz_utils.rs +++ b/src/cz_utils.rs @@ -21,23 +21,25 @@ pub struct CZFile { bitmap: Vec, } -/// Create and save a PNG of the image data -/// This errors if the image data is too short -pub fn create_png(image: &CZFile, out_name: &str) { - let process_bitmap = image.bitmap.clone(); +impl CZFile { + /// Create and save a PNG of the image data + /// This errors if the image data is too short + pub fn create_png(&self, out_name: &str) { + let process_bitmap = self.bitmap.clone(); - let image_data = RgbaImage::from_raw( - image.header.res.0 as u32, - image.header.res.1 as u32, - process_bitmap, - ) - .expect("Error encoding the image"); + let image_data = RgbaImage::from_raw( + self.header.res.0 as u32, + self.header.res.1 as u32, + process_bitmap, + ) + .expect("Error encoding the image"); - match image_data.save_with_format(out_name, ImageFormat::Png) { - Ok(()) => {} - Err(e) => { - eprintln!("ERROR SAVING IMAGE: {}", e); - eprintln!("You probably have an image with the CZ0 offset bug!") + match image_data.save_with_format(out_name, ImageFormat::Png) { + Ok(()) => {} + Err(e) => { + eprintln!("ERROR SAVING IMAGE: {}", e); + eprintln!("You probably have an image with the CZ0 offset bug!") + } } } } @@ -103,7 +105,7 @@ pub mod cz0 { pub fn decode_cz0(input_filename: &str) -> CZFile { println!(""); println!("--Beginning CZ0 Decode--"); - let mut input = fs::read(input_filename).unwrap(); + let mut input = fs::read(input_filename).expect("Error, could not open image"); println!("Extracting Header..."); // TODO Research the header more! diff --git a/src/main.rs b/src/main.rs index 8c95701..85912cb 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,6 @@ -pub mod cz_utils; -pub mod utils; -use cz_utils::create_png; +// Create the modules +pub mod cz_utils; // CZ file tools +pub mod utils; // Generic tools use cz_utils::cz0::*; fn main() { @@ -12,5 +12,5 @@ fn main() { let image = decode_cz0("test.cz0"); display_info(&image); - create_png(&image, "tmp.png"); + image.create_png("tmp.png"); } diff --git a/src/utils.rs b/src/utils.rs index ef6066b..d22058b 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,4 +1,4 @@ -/// Converts 8 bit bytes to a 16 bit little endian word or +/// Converts 8 bit bytes to a 16 bit little endian word pub fn bytes_to_word(first: u8, second: u8) -> i16 { let final_value = ((second as i16) << 8) | (first as i16);