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, DecodeError,
} }
pub enum CzVersion {
CZ0,
CZ1,
CZ2,
CZ3,
CZ4,
}
pub trait CzHeader { pub trait CzHeader {
fn new<T: Seek + ReadBytesExt + Read>(bytes: &mut T) -> Result<Self, CzError> fn new<T: Seek + ReadBytesExt + Read>(bytes: &mut T) -> Result<Self, CzError>
where where
@ -37,21 +45,34 @@ pub trait CzHeader {
/// Turn the header into bytes equivalent to the original header from the file /// Turn the header into bytes equivalent to the original header from the file
fn to_bytes(&self) -> Result<Vec<u8>, io::Error>; fn to_bytes(&self) -> Result<Vec<u8>, io::Error>;
/// The version of the [CzImage] file /// The version of the image
fn version(&self) -> u8; fn version(&self) -> u8;
/// Set the version of the image
fn set_version(&mut self);
/// The length of the header in bytes /// The length of the header in bytes
fn length(&self) -> usize; fn length(&self) -> usize;
/// The width of the image /// The width of the image
fn width(&self) -> u16; fn width(&self) -> u16;
/// Set the width of the image
fn set_width(&mut self);
/// The height of the image /// The height of the image
fn height(&self) -> u16; fn height(&self) -> u16;
/// Set the height of the image
fn set_height(&mut self);
/// The bit depth of the image (BPP) /// The bit depth of the image (BPP)
fn depth(&self) -> u16; fn depth(&self) -> u16;
fn set_depth(&mut self) {
unimplemented!()
}
/// An unknown value? /// An unknown value?
fn color_block(&self) -> u8; fn color_block(&self) -> u8;
} }
@ -157,6 +178,8 @@ pub trait CzImage {
/// Get the header for metadata /// Get the header for metadata
fn header(&self) -> &Self::Header; fn header(&self) -> &Self::Header;
fn header_mut(&mut self) -> &mut Self::Header;
/// Set the header with its metadata /// Set the header with its metadata
fn set_header(&mut self, header: &Self::Header); 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 byteorder::ReadBytesExt;
use crate::{ use crate::{
common::{CommonHeader, CzError, CzHeader}, common::{CommonHeader, CzError, CzVersion, CzHeader},
Cz0Image, Cz0Image,
Cz1Image, Cz1Image,
Cz2Image, Cz2Image,
@ -11,17 +14,15 @@ use crate::{
CzImage CzImage
}; };
pub enum DynamicCz { struct DynamicCz {
CZ0(Cz0Image), cz_type: CzVersion,
CZ1(Cz1Image), bitmap: Vec<u8>,
CZ2(Cz2Image), header_common: CommonHeader,
CZ3(Cz3Image),
CZ4(Cz4Image),
} }
impl DynamicCz { impl DynamicCz {
pub fn open<P: ?Sized + AsRef<Path>>(path: &P) -> Result<Self, CzError> { 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) 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) { fn set_header(&mut self, header: &Self::Header) {
todo!() todo!()
} }

View file

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

View file

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