Begin unifying CZ formats

This commit is contained in:
G2-Games 2024-05-08 09:57:24 -05:00
parent 252f382099
commit 3c01617a5c
4 changed files with 50 additions and 19 deletions

View file

@ -26,6 +26,14 @@ pub enum CzError {
DecodeError,
}
pub enum CzVersion {
CZ0,
CZ1,
CZ2,
CZ3,
CZ4,
}
pub trait CzHeader {
fn new<T: Seek + ReadBytesExt + Read>(bytes: &mut T) -> Result<Self, CzError>
where
@ -37,21 +45,34 @@ pub trait CzHeader {
/// Turn the header into bytes equivalent to the original header from the file
fn to_bytes(&self) -> Result<Vec<u8>, io::Error>;
/// The version of the [CzImage] file
/// The version of the image
fn version(&self) -> u8;
/// Set the version of the image
fn set_version(&mut self);
/// The length of the header in bytes
fn length(&self) -> usize;
/// The width of the image
fn width(&self) -> u16;
/// Set the width of the image
fn set_width(&mut self);
/// The height of the image
fn height(&self) -> u16;
/// Set the height of the image
fn set_height(&mut self);
/// The bit depth of the image (BPP)
fn depth(&self) -> u16;
fn set_depth(&mut self) {
unimplemented!()
}
/// An unknown value?
fn color_block(&self) -> u8;
}
@ -157,6 +178,8 @@ pub trait CzImage {
/// Get the header for metadata
fn header(&self) -> &Self::Header;
fn header_mut(&mut self) -> &mut Self::Header;
/// Set the header with its metadata
fn set_header(&mut self, header: &Self::Header);

View file

@ -1,8 +1,11 @@
use std::{io::{Read, Seek}, path::{Path, PathBuf}};
use std::{
io::{BufReader, Read, Seek},
path::Path
};
use byteorder::ReadBytesExt;
use crate::{
common::{CommonHeader, CzError, CzHeader},
common::{CommonHeader, CzError, CzVersion, CzHeader},
Cz0Image,
Cz1Image,
Cz2Image,
@ -11,17 +14,15 @@ use crate::{
CzImage
};
pub enum DynamicCz {
CZ0(Cz0Image),
CZ1(Cz1Image),
CZ2(Cz2Image),
CZ3(Cz3Image),
CZ4(Cz4Image),
struct DynamicCz {
cz_type: CzVersion,
bitmap: Vec<u8>,
header_common: CommonHeader,
}
impl DynamicCz {
pub fn open<P: ?Sized + AsRef<Path>>(path: &P) -> Result<Self, CzError> {
let mut img_file = std::fs::File::open(path)?;
let mut img_file = BufReader::new(std::fs::File::open(path)?);
Self::decode(&mut img_file)
}
@ -84,6 +85,10 @@ impl CzImage for DynamicCz {
}
}
fn header_mut(&mut self) -> &mut Self::Header {
todo!()
}
fn set_header(&mut self, header: &Self::Header) {
todo!()
}

View file

@ -11,9 +11,8 @@ use crate::compression::{decompress, parse_chunk_info};
#[derive(Debug, Clone)]
pub struct Cz1Image {
header: CommonHeader,
raw_bitmap: Option<Vec<u8>>,
bitmap: Vec<u8>,
palette: Vec<[u8; 4]>,
palette: Option<Vec<[u8; 4]>>,
}
impl CzImage for Cz1Image {
@ -40,25 +39,21 @@ impl CzImage for Cz1Image {
palette = Some(parse_colormap(bytes, 1 << header.depth())?);
}
// Get the information about the compressed chunks
let chunk_info = parse_chunk_info(bytes)?;
// Get the bitmap
let mut bitmap = decompress(bytes, &chunk_info).unwrap();
let mut raw_bitmap = None;
// Apply the palette if it exists
if let Some(pal) = &palette {
if let Some(raw) = &mut raw_bitmap {
bitmap.clone_into(raw);
}
bitmap = apply_palette(&mut bitmap.as_slice(), pal);
}
let image = Self {
header,
bitmap,
raw_bitmap,
palette: palette.unwrap(),
palette,
};
Ok(image)
@ -68,6 +63,10 @@ impl CzImage for Cz1Image {
&self.header
}
fn header_mut(&mut self) -> &mut Self::Header {
&mut self.header
}
fn set_header(&mut self, header:& Self::Header) {
self.header = *header
}

View file

@ -98,6 +98,10 @@ impl CzImage for Cz4Image {
&self.header
}
fn header_mut(&mut self) -> &mut Self::Header {
&mut self.header
}
fn set_header(&mut self, header: &Self::Header) {
self.header = *header
}