Added create_png to cz_utils.rs, cleaned up main.rs

This commit is contained in:
G2-Games 2023-04-20 02:36:27 -05:00
parent c32c0bfef9
commit cb3343c933
2 changed files with 45 additions and 41 deletions

View file

@ -1,3 +1,6 @@
use image::{RgbaImage, ImageFormat};
/// 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?)
@ -10,19 +13,47 @@
offset: (i16, i16), // Offset coordinates offset: (i16, i16), // Offset coordinates
} }
/// Defines a file that has a header and a
/// bitmap body
#[derive(Debug)] #[derive(Debug)]
pub struct CZFile { pub struct CZFile {
header: HeaderCZ, header: HeaderCZ,
bitmap: Vec<u8>, bitmap: Vec<u8>,
} }
/// 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();
let tmp = match 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)
}
};
match tmp.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!")
}
}
}
/// Utilities for decoding CZ0 images
pub mod cz0 { pub mod cz0 {
use std::io; use std::io;
use std::io::Write; use std::io::Write;
use std::fs; use std::fs;
use std::fs::File; use std::fs::File;
use image::{RgbaImage, DynamicImage, ImageFormat}; use image::DynamicImage;
use crate::utils::*; use crate::utils::*;
use crate::cz_utils::{CZFile, HeaderCZ}; use crate::cz_utils::{CZFile, HeaderCZ};
@ -175,31 +206,6 @@ pub mod cz0 {
return Ok(()); return Ok(());
} }
/// 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();
let tmp = match 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)
}
};
match tmp.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!")
}
}
}
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;

View file

@ -1,15 +1,13 @@
pub mod utils; pub mod utils;
pub mod cz_utils; pub mod cz_utils;
use cz_utils::cz0::*; use cz_utils::cz0::*;
use cz_utils::create_png;
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");
match encode_cz0(original_image, replacement_image, "test.cz0") { encode_cz0(original_image, replacement_image, "test.cz0").expect("Error encoding the image");
Ok(file) => file,
Err(error) => panic!("Problem opening the file: {:?}", error),
};
let image = decode_cz0("test.cz0"); let image = decode_cz0("test.cz0");
display_info(&image); display_info(&image);