diff --git a/cz/src/common.rs b/cz/src/common.rs index 540d130..23280dc 100644 --- a/cz/src/common.rs +++ b/cz/src/common.rs @@ -26,6 +26,14 @@ pub enum CzError { DecodeError, } +pub enum CzVersion { + CZ0, + CZ1, + CZ2, + CZ3, + CZ4, +} + pub trait CzHeader { fn new(bytes: &mut T) -> Result 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, 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); diff --git a/cz/src/dynamic.rs b/cz/src/dynamic.rs index 42cd705..46c85de 100644 --- a/cz/src/dynamic.rs +++ b/cz/src/dynamic.rs @@ -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, + header_common: CommonHeader, } impl DynamicCz { pub fn open>(path: &P) -> Result { - 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!() } diff --git a/cz/src/formats/cz1.rs b/cz/src/formats/cz1.rs index 6364bb5..d41a3a0 100644 --- a/cz/src/formats/cz1.rs +++ b/cz/src/formats/cz1.rs @@ -11,9 +11,8 @@ use crate::compression::{decompress, parse_chunk_info}; #[derive(Debug, Clone)] pub struct Cz1Image { header: CommonHeader, - raw_bitmap: Option>, bitmap: Vec, - palette: Vec<[u8; 4]>, + palette: Option>, } 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 } diff --git a/cz/src/formats/cz4.rs b/cz/src/formats/cz4.rs index 9d9b72e..56d46ee 100644 --- a/cz/src/formats/cz4.rs +++ b/cz/src/formats/cz4.rs @@ -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 }