From c9126a688d08834eba160b77f1522ae6d39bc941 Mon Sep 17 00:00:00 2001 From: G2-Games Date: Wed, 22 May 2024 02:24:07 -0500 Subject: [PATCH] Cleaned up code, fixed clippy warnings --- Cargo.toml | 2 +- cz/src/common.rs | 82 +++++++++++++++---------------------------- cz/src/compression.rs | 12 +++---- cz/src/dynamic.rs | 44 +++++++++++------------ cz/src/formats/cz3.rs | 14 ++++---- cz/src/formats/cz4.rs | 2 +- utils/src/main.rs | 2 -- 7 files changed, 64 insertions(+), 94 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index e8ef275..31eb562 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,7 +1,7 @@ [workspace] resolver = "2" members = [ - "cz", "font", + "cz", "utils", ] diff --git a/cz/src/common.rs b/cz/src/common.rs index 96c73dd..f5e2ccb 100644 --- a/cz/src/common.rs +++ b/cz/src/common.rs @@ -62,48 +62,22 @@ impl TryFrom for CzVersion { } } -pub trait CzHeader { - fn from_bytes(bytes: &mut T) -> Result - where - Self: Sized; +impl TryFrom for CzVersion { + type Error = &'static str; - /// The [CommonHeader] header from the image - fn common(&self) -> &CommonHeader; + fn try_from(value: char) -> Result { + let value = match value { + '0' => Self::CZ0, + '1' => Self::CZ1, + '2' => Self::CZ2, + '3' => Self::CZ3, + '4' => Self::CZ4, + '5' => Self::CZ5, + _ => return Err("Value is not a valid CZ version"), + }; - /// Turn the header into bytes equivalent to the original header from the file - fn write_into( - &self, - output: &mut T, - ) -> Result; - - /// The version of the image - fn version(&self) -> CzVersion; - - /// Set the version of the image - fn set_version(&mut self, version: CzVersion); - - /// 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, width: u16); - - /// The height of the image - fn height(&self) -> u16; - - /// Set the height of the image - fn set_height(&mut self, height: u16); - - /// The bit depth of the image (BPP) - fn depth(&self) -> u16; - - fn set_depth(&mut self, depth: u16); - - /// An unknown value? - fn color_block(&self) -> u8; + Ok(value) + } } /// The common first part of a header of a CZ# file @@ -145,8 +119,8 @@ impl CommonHeader { } } -impl CzHeader for CommonHeader { - fn from_bytes(bytes: &mut T) -> Result +impl CommonHeader { + pub fn from_bytes(bytes: &mut T) -> Result where Self: Sized, { @@ -181,51 +155,51 @@ impl CzHeader for CommonHeader { Ok(header) } - fn common(&self) -> &CommonHeader { + pub fn common(&self) -> &CommonHeader { self } - fn version(&self) -> CzVersion { + pub fn version(&self) -> CzVersion { self.version } - fn set_version(&mut self, version: CzVersion) { + pub fn set_version(&mut self, version: CzVersion) { self.version = version } - fn length(&self) -> usize { + pub fn length(&self) -> usize { self.length as usize } - fn width(&self) -> u16 { + pub fn width(&self) -> u16 { self.width } - fn set_width(&mut self, width: u16) { + pub fn set_width(&mut self, width: u16) { self.width = width } - fn height(&self) -> u16 { + pub fn height(&self) -> u16 { self.height } - fn set_height(&mut self, height: u16) { + pub fn set_height(&mut self, height: u16) { self.height = height } - fn depth(&self) -> u16 { + pub fn depth(&self) -> u16 { self.depth } - fn set_depth(&mut self, depth: u16) { + pub fn set_depth(&mut self, depth: u16) { self.depth = depth } - fn color_block(&self) -> u8 { + pub fn color_block(&self) -> u8 { self.unknown } - fn write_into( + pub fn write_into( &self, output: &mut T, ) -> Result { diff --git a/cz/src/compression.rs b/cz/src/compression.rs index 83f7e09..5253d3f 100644 --- a/cz/src/compression.rs +++ b/cz/src/compression.rs @@ -256,7 +256,7 @@ pub fn compress( offset += count; for d in &part_data { - output_buf.write(&d.to_le_bytes()).unwrap(); + output_buf.write_all(&d.to_le_bytes()).unwrap(); } output_info.chunks.push(ChunkInfo { @@ -288,7 +288,7 @@ fn compress_lzw(data: &[u8], size: usize, last: Vec) -> (usize, Vec, Ve let mut dictionary_count = (dictionary.len() + 1) as u16; let mut element = Vec::new(); - if last.len() != 0 { + if last.is_empty() { element = last } @@ -297,7 +297,7 @@ fn compress_lzw(data: &[u8], size: usize, last: Vec) -> (usize, Vec, Ve let mut entry = element.clone(); entry.push(*c); - if dictionary.get(&entry).is_some() { + if dictionary.contains_key(&entry){ element = entry } else { compressed.push(*dictionary.get(&element).unwrap()); @@ -314,15 +314,15 @@ fn compress_lzw(data: &[u8], size: usize, last: Vec) -> (usize, Vec, Ve } let last_element = element; - if compressed.len() == 0 { - if last_element.len() != 0 { + if compressed.is_empty() { + if last_element.is_empty() { for c in last_element { compressed.push(*dictionary.get(&vec![c]).unwrap()); } } return (count, compressed, Vec::new()) } else if compressed.len() < size { - if last_element.len() != 0 { + if last_element.is_empty() { compressed.push(*dictionary.get(&last_element).unwrap()); } return (count, compressed, Vec::new()) diff --git a/cz/src/dynamic.rs b/cz/src/dynamic.rs index efcf8b7..bad56b6 100644 --- a/cz/src/dynamic.rs +++ b/cz/src/dynamic.rs @@ -9,7 +9,7 @@ use std::{ use crate::{ common::{ apply_palette, get_palette, indexed_gen_palette, - rgba_to_indexed, CommonHeader, CzError, CzHeader, + rgba_to_indexed, CommonHeader, CzError, CzVersion, ExtendedHeader }, formats::{cz0, cz1, cz2, cz3, cz4}, @@ -121,34 +121,32 @@ impl DynamicCz { let output_bitmap; match self.header_common.depth() { 4 => { + eprintln!("Files with a bit depth of 4 are not yet supported"); todo!() } 8 => { - match &self.palette { - Some(pal) if self.header_common.depth() <= 8 => { - output_bitmap = rgba_to_indexed(self.bitmap(), pal)?; + if let Some(pal) = &self.palette { + // Use the existing palette to palette the image + output_bitmap = rgba_to_indexed(self.bitmap(), pal)?; - for rgba in pal { - out_file.write_all(&rgba.0)?; - } - }, - // Generate a palette if there is none - None if self.header_common.depth() <= 8 => { - let result = indexed_gen_palette( - self.bitmap(), - self.header() - )?; + for rgba in pal { + out_file.write_all(&rgba.0)?; + } + } else { + // Generate a palette and corresponding indexed bitmap if there is none + let result = indexed_gen_palette( + self.bitmap(), + self.header() + )?; - output_bitmap = result.0; - let palette = result.1; + output_bitmap = result.0; + let palette = result.1; - for rgba in palette { - let mut rgba_clone = rgba.0.clone(); - rgba_clone[0..3].reverse(); - out_file.write_all(&rgba_clone)?; - } - }, - _ => output_bitmap = self.bitmap().clone(), + for rgba in palette { + let mut rgba_clone = rgba.0; + rgba_clone[0..3].reverse(); + out_file.write_all(&rgba_clone)?; + } } }, 24 => { diff --git a/cz/src/formats/cz3.rs b/cz/src/formats/cz3.rs index 53203ab..b38fa72 100644 --- a/cz/src/formats/cz3.rs +++ b/cz/src/formats/cz3.rs @@ -1,7 +1,7 @@ use byteorder::{ReadBytesExt, WriteBytesExt}; use std::io::{Read, Seek, SeekFrom, Write}; -use crate::common::{CommonHeader, CzError, CzHeader}; +use crate::common::{CommonHeader, CzError}; use crate::compression::{compress, decompress, get_chunk_info}; pub fn decode( @@ -17,10 +17,10 @@ pub fn decode( Ok(bitmap) } -pub fn encode( +pub fn encode( output: &mut T, bitmap: &[u8], - header: &H, + header: &CommonHeader, ) -> Result<(), CzError> { let bitmap = diff_line(header, bitmap); @@ -37,7 +37,7 @@ pub fn encode( /// /// Uses the previous line to determine the characterisitcs of the /// following lines -fn line_diff(header: &T, data: &[u8]) -> Vec { +fn line_diff(header: &CommonHeader, data: &[u8]) -> Vec { let width = header.width() as u32; let height = header.height() as u32; let mut output_buf = data.to_vec(); @@ -74,10 +74,10 @@ fn line_diff(header: &T, data: &[u8]) -> Vec { ]) } } else if pixel_byte_count == 1 { - for x in 0..line_byte_count { + for (x, rgba) in curr_line.iter().enumerate().take(line_byte_count) { let loc = (y * width) as usize + x; - output_buf[loc] = curr_line[x]; + output_buf[loc] = *rgba; } } @@ -90,7 +90,7 @@ fn line_diff(header: &T, data: &[u8]) -> Vec { /// Function to encode data into the CZ3 format before compression /// /// Read more in [`line_diff`] -fn diff_line(header: &T, input: &[u8]) -> Vec { +fn diff_line(header: &CommonHeader, input: &[u8]) -> Vec { let width = header.width() as u32; let height = header.height() as u32; diff --git a/cz/src/formats/cz4.rs b/cz/src/formats/cz4.rs index fd29a25..2716879 100644 --- a/cz/src/formats/cz4.rs +++ b/cz/src/formats/cz4.rs @@ -2,7 +2,7 @@ use byteorder::ReadBytesExt; use image::RgbaImage; use std::io::{Read, Seek, SeekFrom}; -use crate::common::{CommonHeader, CzError, CzHeader}; +use crate::common::{CommonHeader, CzError}; use crate::compression::{decompress, get_chunk_info}; pub fn decode( diff --git a/utils/src/main.rs b/utils/src/main.rs index bd29ca2..432a803 100644 --- a/utils/src/main.rs +++ b/utils/src/main.rs @@ -1,5 +1,3 @@ -mod font_generation; - use cz::{ common::{CzHeader, CzVersion}, dynamic::DynamicCz