From b28a8d0b888bae14b9df6081fddee19629cb272d Mon Sep 17 00:00:00 2001 From: G2 Date: Mon, 6 May 2024 12:26:53 -0500 Subject: [PATCH] Fixed various issues with saving --- Cargo.toml | 3 ++ cz/Cargo.toml | 2 + cz/src/binio.rs | 7 ++-- cz/src/common.rs | 5 ++- cz/src/compression.rs | 86 +++++++++++++++++++++++++------------------ cz/src/formats/cz0.rs | 6 ++- cz/src/formats/cz1.rs | 8 ++-- cz/src/formats/cz2.rs | 11 +++--- cz/src/formats/cz3.rs | 4 +- cz/src/formats/cz4.rs | 21 ++++------- cz/src/lib.rs | 2 +- utils/src/main.rs | 9 ++--- 12 files changed, 89 insertions(+), 75 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 31eb562..a70ccc2 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,6 @@ authors = ["G2"] [workspace.lints.rust] unsafe_code = "forbid" + +[build] +rustflags = ["-C", "target-cpu=native"] diff --git a/cz/Cargo.toml b/cz/Cargo.toml index 0b239ef..54259ab 100644 --- a/cz/Cargo.toml +++ b/cz/Cargo.toml @@ -12,3 +12,5 @@ image = "0.25" byteorder = "1.5.0" thiserror = "1.0.59" bitstream-io = "2.2.0" +png = "0.17.13" +rayon = "1.10.0" diff --git a/cz/src/binio.rs b/cz/src/binio.rs index 10db2f3..5bf99e6 100644 --- a/cz/src/binio.rs +++ b/cz/src/binio.rs @@ -32,12 +32,13 @@ impl BitIO { } if bit_len % 8 == 0 && self.bit_offset == 0 { - return self.read(bit_len / 8) + return self.read(bit_len / 8); } let mut result = 0; for i in 0..bit_len { - let bit_value = ((self.data[self.byte_offset] as usize >> self.bit_offset as usize) & 1) as u64; + let bit_value = + ((self.data[self.byte_offset] as usize >> self.bit_offset as usize) & 1) as u64; self.bit_offset += 1; if self.bit_offset == 8 { @@ -48,7 +49,7 @@ impl BitIO { result |= bit_value << i; } - return result + return result; } pub fn read(&mut self, byte_len: usize) -> u64 { diff --git a/cz/src/common.rs b/cz/src/common.rs index 3fadc46..cef2fb1 100644 --- a/cz/src/common.rs +++ b/cz/src/common.rs @@ -1,7 +1,8 @@ //! Shared types and traits between CZ# files use std::{ - io::{self, Read, Seek, Write}, path::PathBuf + io::{self, Read, Seek, Write}, + path::PathBuf, }; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; @@ -78,7 +79,7 @@ impl CzHeader for CommonHeader { bytes.read_exact(&mut magic)?; if magic[0..2] != [b'C', b'Z'] { - return Err(CzError::InvalidFormat) + return Err(CzError::InvalidFormat); } Ok(Self { diff --git a/cz/src/compression.rs b/cz/src/compression.rs index 08d26f8..ac0b328 100644 --- a/cz/src/compression.rs +++ b/cz/src/compression.rs @@ -1,9 +1,14 @@ -use std::{collections::BTreeMap, io::{Read, Seek, Write}}; use byteorder::{LittleEndian, ReadBytesExt}; -use image::{buffer, ColorType, DynamicImage, GenericImage, GenericImageView, RgbImage, Rgba, RgbaImage}; +use image::{ + buffer, ColorType, DynamicImage, GenericImage, GenericImageView, RgbImage, Rgba, RgbaImage, +}; +use std::{ + collections::BTreeMap, + io::{Read, Seek, Write}, +}; -use crate::common::{CzError, CzHeader}; use crate::binio::BitIO; +use crate::common::{CzError, CzHeader}; #[derive(Debug, Clone, Copy)] pub struct ChunkInfo { @@ -23,7 +28,9 @@ pub struct CompressionInfo { } /// Get info about the compression chunks -pub fn parse_chunk_info(bytes: &mut T) -> Result { +pub fn parse_chunk_info( + bytes: &mut T, +) -> Result { let parts_count = bytes.read_u32::()?; let mut part_sizes = vec![]; @@ -125,7 +132,7 @@ pub fn decompress_lzw2(input_data: &[u8], size: usize) -> Vec { } if bit_io.byte_offset() > data_size { - break + break; } let mut entry; @@ -192,13 +199,14 @@ fn copy_one(input: &[u8], src: usize) -> u8 { } } -pub fn line_diff(header_info: &T, data: &[u8]) -> Vec { - let width = header_info.width() as u32; - let height = header_info.height() as u32; - let mut pic = image::RgbaImage::new(width, height); +pub fn line_diff(header: &T, data: &[u8]) -> Vec { + let width = header.width() as u32; + let height = header.height() as u32; + let mut output_buf = data.to_vec(); - let block_height = (f32::ceil(height as f32 / 4 as f32) as u16) as usize; - let pixel_byte_count = header_info.depth() >> 3; + let block_height = + (f32::ceil(height as f32 / header.color_block() as f32) as u16) as usize; + let pixel_byte_count = header.depth() >> 3; let line_byte_count = (width * pixel_byte_count as u32) as usize; let mut curr_line: Vec; @@ -206,8 +214,7 @@ pub fn line_diff(header_info: &T, data: &[u8]) -> Vec { let mut i = 0; for y in 0..height { - curr_line = data[i..i+line_byte_count].to_vec(); - dbg!(curr_line.len()); + curr_line = data[i..i + line_byte_count].to_vec(); if y % block_height as u32 != 0 { for x in 0..line_byte_count { @@ -216,34 +223,38 @@ pub fn line_diff(header_info: &T, data: &[u8]) -> Vec { } prev_line = curr_line.clone(); - if header_info.version() == 4 { - for x in 0..width { - pic.get_pixel_mut(x as u32, y).0[3] = curr_line[x as usize]; - } - } else if pixel_byte_count == 4 { - let mut raw = pic.into_raw(); - raw[i..i+line_byte_count].copy_from_slice(&curr_line); - - pic = RgbaImage::from_raw(width, height, raw).unwrap(); + if pixel_byte_count == 4 { + output_buf[i..i + line_byte_count].copy_from_slice(&curr_line); } else if pixel_byte_count == 3 { for x in 0..line_byte_count { - pic.get_pixel_mut((x/3) as u32, y).0 = [curr_line[x], curr_line[x + 1], curr_line[x + 2], 0xFF]; + let loc = ((y * width) as usize + x) * 4; + + output_buf[loc..loc + 4].copy_from_slice(&[ + curr_line[x + 0], + curr_line[x + 1], + curr_line[x + 2], + 0xFF + ]) } } i += line_byte_count; } - pic.into_vec() + output_buf } -pub fn line_diff_cz4(picture: &mut RgbaImage, color_block: u8, pixel_byte_count: usize, data: &[u8]) { - let width = picture.width() as u32; - let height = picture.height() as u32; - let block_height = (f32::ceil(height as f32 / color_block as f32) as u16) as u32; +pub fn line_diff_cz4(header: &T, data: &[u8]) -> Vec { + let width = header.width() as u32; + let height = header.height() as u32; + let block_height = (f32::ceil(height as f32 / 3.0) as u16) as u32; + //let pixel_byte_count = (header.depth() >> 3) as usize; + let pixel_byte_count = 3; - let mut curr_line = vec![0u8; width as usize * pixel_byte_count]; + let mut output_buf = data.to_vec(); + + let mut curr_line; let mut prev_line = vec![0u8; width as usize * pixel_byte_count]; let mut i = 0; @@ -252,27 +263,28 @@ pub fn line_diff_cz4(picture: &mut RgbaImage, color_block: u8, pixel_byte_count: if y % block_height != 0 { for x in 0..(width as usize * pixel_byte_count) { - curr_line[x] += prev_line[x] + curr_line[x] = u8::wrapping_add(curr_line[x], prev_line[x]) } } - for x in 0..width as usize { + let loc = ((y * width) as usize + x) * 4; + if pixel_byte_count == 1 { - picture.get_pixel_mut(x as u32, y).0[3] = curr_line[x]; + output_buf[loc + 4] = curr_line[x]; } else if pixel_byte_count == 4 { - *picture.get_pixel_mut(x as u32, y) = Rgba([ + output_buf[loc..loc + 4].copy_from_slice(&[ curr_line[x * pixel_byte_count + 0], curr_line[x * pixel_byte_count + 1], curr_line[x * pixel_byte_count + 2], - curr_line[x * pixel_byte_count + 3] + curr_line[x * pixel_byte_count + 3], ]); } else if pixel_byte_count == 3 { - *picture.get_pixel_mut(x as u32, y) = Rgba([ + output_buf[loc..loc + 4].copy_from_slice(&[ curr_line[x * pixel_byte_count + 0], curr_line[x * pixel_byte_count + 1], curr_line[x * pixel_byte_count + 2], - 0xFF + 0xFF, ]); } } @@ -280,4 +292,6 @@ pub fn line_diff_cz4(picture: &mut RgbaImage, color_block: u8, pixel_byte_count: prev_line = curr_line.clone(); i += width as usize * pixel_byte_count; } + + output_buf } diff --git a/cz/src/formats/cz0.rs b/cz/src/formats/cz0.rs index a72c03d..c7f3e90 100644 --- a/cz/src/formats/cz0.rs +++ b/cz/src/formats/cz0.rs @@ -1,4 +1,8 @@ -use std::{fs::File, io::{self, BufWriter, Cursor, Read, Seek, Write}, path::PathBuf}; +use std::{ + fs::File, + io::{self, BufWriter, Cursor, Read, Seek, Write}, + path::PathBuf, +}; use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; diff --git a/cz/src/formats/cz1.rs b/cz/src/formats/cz1.rs index 0410dc9..9e0a3e2 100644 --- a/cz/src/formats/cz1.rs +++ b/cz/src/formats/cz1.rs @@ -3,11 +3,11 @@ use image::{ImageFormat, Rgba}; use std::{ fs::File, io::{BufWriter, Read, Seek, SeekFrom, Write}, - path::PathBuf + path::PathBuf, }; -use crate::compression::{decompress, parse_chunk_info}; use crate::common::{apply_palette, parse_colormap, CommonHeader, CzError, CzHeader, CzImage}; +use crate::compression::{decompress, parse_chunk_info}; #[derive(Debug, Clone)] pub struct Cz1Image { @@ -66,7 +66,7 @@ impl CzImage for Cz1Image { self.header.width() as u32, self.header.height() as u32, image::ExtendedColorType::Rgba8, - ImageFormat::Png + ImageFormat::Png, ) } @@ -87,8 +87,6 @@ impl CzImage for Cz1Image { output_file.write_all(&self.header.to_bytes()?)?; - - output_file.flush()?; Ok(()) } diff --git a/cz/src/formats/cz2.rs b/cz/src/formats/cz2.rs index 0f84b50..119880b 100644 --- a/cz/src/formats/cz2.rs +++ b/cz/src/formats/cz2.rs @@ -3,11 +3,11 @@ use image::{ImageFormat, Rgba}; use std::{ fs::File, io::{BufWriter, Read, Seek, SeekFrom, Write}, - path::PathBuf + path::PathBuf, }; -use crate::compression::{decompress, decompress_2, parse_chunk_info}; use crate::common::{apply_palette, parse_colormap, CommonHeader, CzError, CzHeader, CzImage}; +use crate::compression::{decompress, decompress_2, parse_chunk_info}; #[derive(Debug, Clone, Copy)] pub struct Cz2Header { @@ -20,7 +20,7 @@ pub struct Cz2Header { impl CzHeader for Cz2Header { fn new(bytes: &mut T) -> Result where - Self: Sized + Self: Sized, { let common = CommonHeader::new(bytes)?; @@ -122,7 +122,8 @@ impl CzImage for Cz2Image { self.header.width() as u32, self.header.height() as u32, self.bitmap.clone(), - ).unwrap(); + ) + .unwrap(); img.save(name)?; @@ -146,8 +147,6 @@ impl CzImage for Cz2Image { output_file.write_all(&self.header.to_bytes()?)?; - - output_file.flush()?; Ok(()) } diff --git a/cz/src/formats/cz3.rs b/cz/src/formats/cz3.rs index e2cf66d..a31b53f 100644 --- a/cz/src/formats/cz3.rs +++ b/cz/src/formats/cz3.rs @@ -1,12 +1,12 @@ use std::{ io::{self, Read, Seek, SeekFrom}, - path::PathBuf + path::PathBuf, }; use byteorder::{LittleEndian, ReadBytesExt}; -use crate::compression::{decompress, line_diff, parse_chunk_info}; use crate::common::{CommonHeader, CzError, CzHeader, CzImage}; +use crate::compression::{decompress, line_diff, parse_chunk_info}; #[derive(Debug, Clone, Copy)] pub struct Cz3Header { diff --git a/cz/src/formats/cz4.rs b/cz/src/formats/cz4.rs index 562d9c9..6e5193d 100644 --- a/cz/src/formats/cz4.rs +++ b/cz/src/formats/cz4.rs @@ -1,13 +1,13 @@ use std::{ io::{self, Read, Seek, SeekFrom}, - path::PathBuf + path::PathBuf, }; use byteorder::{LittleEndian, ReadBytesExt}; use image::DynamicImage; -use crate::compression::{decompress, line_diff, line_diff_cz4, parse_chunk_info}; use crate::common::{CommonHeader, CzError, CzHeader, CzImage}; +use crate::compression::{decompress, line_diff, line_diff_cz4, parse_chunk_info}; #[derive(Debug, Clone, Copy)] pub struct Cz4Header { @@ -26,9 +26,7 @@ impl CzHeader for Cz4Header { return Err(CzError::VersionMismatch(common.version(), 3)); } - Ok(Self { - common, - }) + Ok(Self { common }) } fn version(&self) -> u8 { @@ -78,15 +76,9 @@ impl CzImage for Cz4Image { let bitmap = decompress(bytes, &block_info)?; - let mut picture = image::RgbaImage::new(header.width() as u32, header.height() as u32); + let bitmap = line_diff_cz4(&header, &bitmap); - let pixel_byte_count = 3; - line_diff_cz4(&mut picture, 3, pixel_byte_count, &bitmap); - - Ok(Self { - header, - bitmap: picture.into_vec() - }) + Ok(Self { header, bitmap }) } fn save_as_png(&self, name: &str) -> Result<(), image::error::ImageError> { @@ -94,7 +86,8 @@ impl CzImage for Cz4Image { self.header.width() as u32, self.header.height() as u32, self.bitmap.clone(), - ).unwrap(); + ) + .unwrap(); img.save(name)?; diff --git a/cz/src/lib.rs b/cz/src/lib.rs index 6ae7428..f6edaa3 100644 --- a/cz/src/lib.rs +++ b/cz/src/lib.rs @@ -1,5 +1,5 @@ -pub mod common; mod binio; +pub mod common; mod compression; pub mod formats { pub mod cz0; diff --git a/utils/src/main.rs b/utils/src/main.rs index 9dbb296..e0f251f 100644 --- a/utils/src/main.rs +++ b/utils/src/main.rs @@ -1,13 +1,12 @@ +use cz::{Cz3Image, CzImage}; use std::fs; -use cz::{Cz4Image, CzImage}; fn main() { - let mut input = fs::File::open("../../test_files/BAD_BG_011_10.cz4") - .expect("Failed to open file"); + let mut input = fs::File::open("../../test_files/GOOD_00009.cz3") + .expect("Failed to open file"); let timer = std::time::Instant::now(); - let img_file = Cz4Image::decode(&mut input) - .expect("Failed to decode image"); + let img_file = Cz3Image::decode(&mut input).expect("Failed to decode image"); println!("{:?}", timer.elapsed()); img_file.save_as_png("test1.png").unwrap();