Fixed various issues with saving

This commit is contained in:
G2 2024-05-06 12:26:53 -05:00
parent a1fbb3368a
commit b28a8d0b88
12 changed files with 89 additions and 75 deletions

View file

@ -10,3 +10,6 @@ authors = ["G2"]
[workspace.lints.rust]
unsafe_code = "forbid"
[build]
rustflags = ["-C", "target-cpu=native"]

View file

@ -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"

View file

@ -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 {

View file

@ -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 {

View file

@ -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<T: Seek + ReadBytesExt + Read>(bytes: &mut T) -> Result<CompressionInfo, CzError> {
pub fn parse_chunk_info<T: Seek + ReadBytesExt + Read>(
bytes: &mut T,
) -> Result<CompressionInfo, CzError> {
let parts_count = bytes.read_u32::<LittleEndian>()?;
let mut part_sizes = vec![];
@ -125,7 +132,7 @@ pub fn decompress_lzw2(input_data: &[u8], size: usize) -> Vec<u8> {
}
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<T: CzHeader>(header_info: &T, data: &[u8]) -> Vec<u8> {
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<T: CzHeader>(header: &T, data: &[u8]) -> Vec<u8> {
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<u8>;
@ -207,7 +215,6 @@ pub fn line_diff<T: CzHeader>(header_info: &T, data: &[u8]) -> Vec<u8> {
let mut i = 0;
for y in 0..height {
curr_line = data[i..i + line_byte_count].to_vec();
dbg!(curr_line.len());
if y % block_height as u32 != 0 {
for x in 0..line_byte_count {
@ -216,34 +223,38 @@ pub fn line_diff<T: CzHeader>(header_info: &T, data: &[u8]) -> Vec<u8> {
}
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<T: CzHeader>(header: &T, data: &[u8]) -> Vec<u8> {
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
}

View file

@ -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};

View file

@ -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(())
}

View file

@ -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<T: Seek + ReadBytesExt + Read>(bytes: &mut T) -> Result<Self, CzError>
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(())
}

View file

@ -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 {

View file

@ -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)?;

View file

@ -1,5 +1,5 @@
pub mod common;
mod binio;
pub mod common;
mod compression;
pub mod formats {
pub mod cz0;

View file

@ -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")
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();