Increased speed of CZ3 and 4 decoding, feature gate for image

This commit is contained in:
G2-Games 2024-07-01 14:40:52 -05:00
parent d31917ae12
commit 3911c73761
6 changed files with 71 additions and 43 deletions

View file

@ -6,8 +6,11 @@ description="""
A encoder/decoder for CZ# image files used in the LUCA System Engine. A encoder/decoder for CZ# image files used in the LUCA System Engine.
""" """
[features]
png = ["dep:image"]
[dependencies] [dependencies]
byteorder = "1.5.0" byteorder = "1.5.0"
thiserror = "1.0.59" thiserror = "1.0.59"
image = { version = "0.25.1", default-features = false, features = ["png"] }
imagequant = "4.3.1" imagequant = "4.3.1"
image = { version = "0.25", default-features = false, features = ["png"], optional = true }

View file

@ -1,4 +1,9 @@
pub struct BitIO { pub enum BitError {
InputLength
}
pub struct BitIo {
data: Vec<u8>, data: Vec<u8>,
byte_offset: usize, byte_offset: usize,
bit_offset: usize, bit_offset: usize,
@ -6,7 +11,8 @@ pub struct BitIO {
byte_size: usize, byte_size: usize,
} }
impl BitIO { impl BitIo {
/// Create a new BitIO reader and writer over some data
pub fn new(data: Vec<u8>) -> Self { pub fn new(data: Vec<u8>) -> Self {
Self { Self {
data, data,
@ -16,20 +22,23 @@ impl BitIO {
} }
} }
/// Get the byte offset of the reader
pub fn byte_offset(&self) -> usize { pub fn byte_offset(&self) -> usize {
self.byte_offset self.byte_offset
} }
/// Get the byte size of the reader
pub fn byte_size(&self) -> usize { pub fn byte_size(&self) -> usize {
self.byte_size self.byte_size
} }
/// Get the current bytes up to `byte_size` in the reader
pub fn bytes(&self) -> Vec<u8> { pub fn bytes(&self) -> Vec<u8> {
self.data[..self.byte_size].to_vec() self.data[..self.byte_size].to_vec()
} }
/// Read some bits from the buffer
pub fn read_bit(&mut self, bit_len: usize) -> u64 { pub fn read_bit(&mut self, bit_len: usize) -> u64 {
//print!("{}: ", bit_len);
if bit_len > 8 * 8 { if bit_len > 8 * 8 {
panic!() panic!()
} }
@ -54,6 +63,7 @@ impl BitIO {
result result
} }
/// Read some bytes from the buffer
pub fn read(&mut self, byte_len: usize) -> u64 { pub fn read(&mut self, byte_len: usize) -> u64 {
if byte_len > 8 { if byte_len > 8 {
panic!() panic!()
@ -66,6 +76,7 @@ impl BitIO {
u64::from_le_bytes(padded_slice) u64::from_le_bytes(padded_slice)
} }
/// Write some bits to the buffer
pub fn write_bit(&mut self, data: u64, bit_len: usize) { pub fn write_bit(&mut self, data: u64, bit_len: usize) {
if bit_len > 8 * 8 { if bit_len > 8 * 8 {
panic!(); panic!();
@ -93,9 +104,9 @@ impl BitIO {
self.byte_size = self.byte_offset + (self.bit_offset + 7) / 8; self.byte_size = self.byte_offset + (self.bit_offset + 7) / 8;
} }
pub fn write(&mut self, data: u64, byte_len: usize) { pub fn write(&mut self, data: u64, byte_len: usize) -> Result<(), BitError> {
if byte_len > 8 { if byte_len > 8 {
panic!() return Err(BitError::InputLength);
} }
let mut padded_slice = [0u8; 8]; let mut padded_slice = [0u8; 8];
@ -106,5 +117,7 @@ impl BitIO {
self.byte_offset += byte_len; self.byte_offset += byte_len;
self.byte_size = self.byte_offset + (self.bit_offset + 7) / 8; self.byte_size = self.byte_offset + (self.bit_offset + 7) / 8;
Ok(())
} }
} }

View file

@ -4,7 +4,7 @@ use std::{
io::{Read, Seek, Write}, io::{Read, Seek, Write},
}; };
use crate::binio::BitIO; use crate::binio::BitIo;
use crate::common::CzError; use crate::common::CzError;
/// The size of compressed data in each chunk /// The size of compressed data in each chunk
@ -189,7 +189,7 @@ fn decompress_lzw2(input_data: &[u8], size: usize) -> Vec<u8> {
let data_size = input_data.len(); let data_size = input_data.len();
data.extend_from_slice(&[0, 0]); data.extend_from_slice(&[0, 0]);
let mut bit_io = BitIO::new(data); let mut bit_io = BitIo::new(data);
let mut w = dictionary.get(&0).unwrap().clone(); let mut w = dictionary.get(&0).unwrap().clone();
let mut element; let mut element;
@ -391,8 +391,8 @@ fn compress_lzw2(data: &[u8], size: usize, last: Vec<u8>) -> (usize, Vec<u8>, Ve
element = last element = last
} }
let mut bit_io = BitIO::new(vec![0u8; size + 2]); let mut bit_io = BitIo::new(vec![0u8; size + 2]);
let write_bit = |bit_io: &mut BitIO, code: u64| { let write_bit = |bit_io: &mut BitIo, code: u64| {
if code > 0x7FFF { if code > 0x7FFF {
bit_io.write_bit(1, 1); bit_io.write_bit(1, 1);
bit_io.write_bit(code, 18); bit_io.write_bit(code, 18);

View file

@ -195,6 +195,7 @@ impl DynamicCz {
/// Internally, the [`DynamicCz`] struct operates on 32-bit RGBA values, /// Internally, the [`DynamicCz`] struct operates on 32-bit RGBA values,
/// which is the highest encountered in CZ# files, therefore saving them /// which is the highest encountered in CZ# files, therefore saving them
/// as a PNG of the same or better quality is lossless. /// as a PNG of the same or better quality is lossless.
#[cfg(feature = "png")]
pub fn save_as_png<P: ?Sized + AsRef<Path>>( pub fn save_as_png<P: ?Sized + AsRef<Path>>(
&self, &self,
path: &P, path: &P,

View file

@ -1,5 +1,6 @@
use byteorder::{ReadBytesExt, WriteBytesExt}; use byteorder::{ReadBytesExt, WriteBytesExt};
use std::io::{Read, Seek, SeekFrom, Write}; use std::io::{Read, Seek, SeekFrom, Write};
use std::time::Instant;
use crate::common::{CommonHeader, CzError}; use crate::common::{CommonHeader, CzError};
use crate::compression::{compress, decompress, get_chunk_info}; use crate::compression::{compress, decompress, get_chunk_info};
@ -11,8 +12,13 @@ pub fn decode<T: Seek + ReadBytesExt + Read>(
let block_info = get_chunk_info(bytes)?; let block_info = get_chunk_info(bytes)?;
bytes.seek(SeekFrom::Start(block_info.length as u64))?; bytes.seek(SeekFrom::Start(block_info.length as u64))?;
let timer = Instant::now();
let bitmap = decompress(bytes, &block_info)?; let bitmap = decompress(bytes, &block_info)?;
dbg!(timer.elapsed());
let timer = Instant::now();
let bitmap = line_diff(header, &bitmap); let bitmap = line_diff(header, &bitmap);
dbg!(timer.elapsed());
Ok(bitmap) Ok(bitmap)
} }
@ -46,17 +52,17 @@ fn line_diff(header: &CommonHeader, data: &[u8]) -> Vec<u8> {
let pixel_byte_count = header.depth() >> 3; let pixel_byte_count = header.depth() >> 3;
let line_byte_count = (width * pixel_byte_count as u32) as usize; let line_byte_count = (width * pixel_byte_count as u32) as usize;
let mut curr_line: Vec<u8>; let mut curr_line;
let mut prev_line: Vec<u8> = Vec::with_capacity(line_byte_count); let mut prev_line = Vec::with_capacity(line_byte_count);
let mut i = 0; let mut i = 0;
for y in 0..height { for y in 0..height {
curr_line = data[i..i + line_byte_count].to_vec(); curr_line = data[i..i + line_byte_count].to_vec();
if y % block_height as u32 != 0 { if y % block_height as u32 != 0 {
for x in 0..line_byte_count { curr_line.iter_mut().zip(&prev_line).for_each(|(curr_p, prev_p)| {
curr_line[x] = u8::wrapping_add(curr_line[x], prev_line[x]) *curr_p = curr_p.wrapping_add(*prev_p)
} });
} }
prev_line.clone_from(&curr_line); prev_line.clone_from(&curr_line);

View file

@ -1,6 +1,6 @@
use byteorder::{ReadBytesExt, WriteBytesExt}; use byteorder::{ReadBytesExt, WriteBytesExt};
use image::RgbaImage;
use std::io::{Read, Seek, SeekFrom, Write}; use std::io::{Read, Seek, SeekFrom, Write};
use std::time::Instant;
use crate::common::{CommonHeader, CzError}; use crate::common::{CommonHeader, CzError};
use crate::compression::{compress, decompress, get_chunk_info}; use crate::compression::{compress, decompress, get_chunk_info};
@ -12,14 +12,15 @@ pub fn decode<T: Seek + ReadBytesExt + Read>(
let block_info = get_chunk_info(bytes)?; let block_info = get_chunk_info(bytes)?;
bytes.seek(SeekFrom::Start(block_info.length as u64))?; bytes.seek(SeekFrom::Start(block_info.length as u64))?;
let timer = Instant::now();
let data = decompress(bytes, &block_info)?; let data = decompress(bytes, &block_info)?;
dbg!(timer.elapsed());
let timer = Instant::now();
let output = line_diff(header, &data);
dbg!(timer.elapsed());
let mut picture = image::RgbaImage::new(header.width() as u32, header.height() as u32); Ok(output)
line_diff(&mut picture, &data);
Ok(picture.into_raw())
} }
pub fn encode<T: WriteBytesExt + Write>( pub fn encode<T: WriteBytesExt + Write>(
@ -38,9 +39,12 @@ pub fn encode<T: WriteBytesExt + Write>(
Ok(()) Ok(())
} }
fn line_diff(picture: &mut RgbaImage, data: &[u8]) { fn line_diff(header: &CommonHeader, data: &[u8]) -> Vec<u8> {
let width = picture.width(); let width = header.width() as u32;
let height = picture.height(); let height = header.height() as u32;
let mut output_buf = Vec::with_capacity((width * height * 4) as usize);
let block_height = (f32::ceil(height as f32 / 3.0) as u16) as u32; let block_height = (f32::ceil(height as f32 / 3.0) as u16) as u32;
let mut curr_line; let mut curr_line;
@ -49,37 +53,38 @@ fn line_diff(picture: &mut RgbaImage, data: &[u8]) {
let mut curr_alpha; let mut curr_alpha;
let mut prev_alpha = Vec::with_capacity(width as usize); let mut prev_alpha = Vec::with_capacity(width as usize);
let pcount = (width * height * 3) as usize; let mut rgb_index = 0;
let mut alpha_index = (width * height * 3) as usize;
let mut i = 0;
let mut z = 0;
for y in 0..height { for y in 0..height {
curr_line = data[i..i + width as usize * 3].to_vec(); curr_line = data[rgb_index..rgb_index + width as usize * 3].to_vec();
curr_alpha = data[pcount + z..pcount + z + width as usize].to_vec(); curr_alpha = data[alpha_index..alpha_index + width as usize].to_vec();
if y % block_height != 0 { if y % block_height != 0 {
for x in 0..(width as usize * 3) { curr_line.iter_mut().zip(&prev_line).for_each(|(curr_p, prev_p)| {
curr_line[x] = curr_line[x].wrapping_add(prev_line[x]) *curr_p = curr_p.wrapping_add(*prev_p);
} });
for x in 0..width as usize { curr_alpha.iter_mut().zip(&prev_alpha).for_each(|(curr_a, prev_a)| {
curr_alpha[x] = curr_alpha[x].wrapping_add(prev_alpha[x]) *curr_a = curr_a.wrapping_add(*prev_a);
} });
} }
for x in 0..width as usize { for x in 0..width as usize {
picture.get_pixel_mut(x as u32, y).0 = [ let pos = x * 3;
curr_line[x * 3], output_buf.extend_from_slice(&[
curr_line[x * 3 + 1], curr_line[pos],
curr_line[x * 3 + 2], curr_line[pos + 1],
curr_line[pos + 2],
curr_alpha[x], curr_alpha[x],
]; ]);
} }
prev_line.clone_from(&curr_line); prev_line.clone_from(&curr_line);
prev_alpha.clone_from(&curr_alpha); prev_alpha.clone_from(&curr_alpha);
i += width as usize * 3; rgb_index += width as usize * 3;
z += width as usize; alpha_index += width as usize;
} }
output_buf
} }
fn diff_line(header: &CommonHeader, input: &[u8]) -> Vec<u8> { fn diff_line(header: &CommonHeader, input: &[u8]) -> Vec<u8> {