mirror of
https://github.com/G2-Games/lbee-utils.git
synced 2025-04-19 15:22:53 -05:00
Added create_png to cz_utils.rs, cleaned up main.rs
This commit is contained in:
parent
c32c0bfef9
commit
cb3343c933
2 changed files with 45 additions and 41 deletions
|
@ -1,28 +1,59 @@
|
||||||
#[derive(Debug)]
|
use image::{RgbaImage, ImageFormat};
|
||||||
struct HeaderCZ {
|
|
||||||
magic: [u8; 3], // The magic bytes, can be CZ0, (CZ1, CZ2?)
|
|
||||||
length: u8,
|
|
||||||
res: (i16, i16), // The width in the header
|
|
||||||
depth: u8, // Bit depth
|
|
||||||
mystery: Vec<u8>,
|
|
||||||
crop: (i16, i16), // Crop dimensions
|
|
||||||
bounds: (i16, i16), // Bounding box dimensions
|
|
||||||
offset: (i16, i16), // Offset coordinates
|
|
||||||
}
|
|
||||||
|
|
||||||
|
/// The header of a CZ* file
|
||||||
|
#[derive(Debug)]
|
||||||
|
struct HeaderCZ {
|
||||||
|
magic: [u8; 3], // The magic bytes, can be CZ0, (CZ1, CZ2?)
|
||||||
|
length: u8,
|
||||||
|
res: (i16, i16), // The width in the header
|
||||||
|
depth: u8, // Bit depth
|
||||||
|
mystery: Vec<u8>,
|
||||||
|
crop: (i16, i16), // Crop dimensions
|
||||||
|
bounds: (i16, i16), // Bounding box dimensions
|
||||||
|
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;
|
||||||
|
|
|
@ -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);
|
||||||
|
|
Loading…
Reference in a new issue