Formatting and doc fixes

This commit is contained in:
G2-Games 2024-07-05 00:10:01 -05:00
parent 976fffed1f
commit a1b4b04208
8 changed files with 41 additions and 37 deletions

View file

@ -200,11 +200,10 @@ impl CommonHeader {
self.unknown self.unknown
} }
pub fn write_into<T: Seek + WriteBytesExt + Write>( pub fn write_into<T: WriteBytesExt + Write>(
&self, &self,
output: &mut T, output: &mut T,
) -> Result<usize, io::Error> { ) -> Result<(), io::Error> {
let pos = output.stream_position()?;
let magic_bytes = [b'C', b'Z', b'0' + self.version as u8, b'\0']; let magic_bytes = [b'C', b'Z', b'0' + self.version as u8, b'\0'];
output.write_all(&magic_bytes)?; output.write_all(&magic_bytes)?;
@ -214,7 +213,7 @@ impl CommonHeader {
output.write_u16::<LittleEndian>(self.depth())?; output.write_u16::<LittleEndian>(self.depth())?;
output.write_u8(self.color_block())?; output.write_u8(self.color_block())?;
Ok((output.stream_position()? - pos) as usize) Ok(())
} }
} }
@ -325,12 +324,10 @@ impl ExtendedHeader {
}) })
} }
pub fn write_into<T: Seek + WriteBytesExt + Write>( pub fn write_into<T: WriteBytesExt + Write>(
&self, &self,
output: &mut T, output: &mut T,
) -> Result<usize, io::Error> { ) -> Result<(), io::Error> {
let pos = output.stream_position()?;
output.write_all(&self.unknown_1)?; output.write_all(&self.unknown_1)?;
output.write_u16::<LittleEndian>(self.crop_width)?; output.write_u16::<LittleEndian>(self.crop_width)?;
output.write_u16::<LittleEndian>(self.crop_height)?; output.write_u16::<LittleEndian>(self.crop_height)?;
@ -343,6 +340,6 @@ impl ExtendedHeader {
output.write_u32::<LittleEndian>(self.unknown_2.unwrap())?; output.write_u32::<LittleEndian>(self.unknown_2.unwrap())?;
} }
Ok((output.stream_position()? - pos) as usize) Ok(())
} }
} }

View file

@ -25,7 +25,7 @@ pub struct DynamicCz {
} }
impl DynamicCz { impl DynamicCz {
/// Decode a CZ# file from anything which implements [`Read`] and [`Seek`] /// Decode a CZ# file from anything that implements [`Read`] and [`Seek`]
/// ///
/// The input must begin with the /// The input must begin with the
/// [magic bytes](https://en.wikipedia.org/wiki/File_format#Magic_number) /// [magic bytes](https://en.wikipedia.org/wiki/File_format#Magic_number)
@ -117,11 +117,12 @@ impl DynamicCz {
Ok(()) Ok(())
} }
/// Encode the CZ file into a byte stream. /// Encode a CZ# file into anything that implements [`Write`] and [`Seek`]
///
/// This encodes everything based on options the header which have been /// This encodes everything based on options the header which have been
/// set by the user. For example, to change the version of file to be /// set by the user. For example, to change the version of file to be
/// saved, use [`CommonHeader::set_version()`] /// saved, use [`CommonHeader::set_version()`]
pub fn encode<T: Write + Seek>( pub fn encode<T: Write>(
&self, &self,
mut output: &mut T mut output: &mut T
) -> Result<(), CzError> { ) -> Result<(), CzError> {

View file

@ -3,7 +3,9 @@ use std::io::{Read, Seek, Write};
use crate::common::CzError; use crate::common::CzError;
pub fn decode<T: Seek + ReadBytesExt + Read>(input: &mut T) -> Result<Vec<u8>, CzError> { pub fn decode<T: Seek + ReadBytesExt + Read>(
input: &mut T
) -> Result<Vec<u8>, CzError> {
// Get the rest of the file, which is the bitmap // Get the rest of the file, which is the bitmap
let mut bitmap = vec![]; let mut bitmap = vec![];
input.read_to_end(&mut bitmap)?; input.read_to_end(&mut bitmap)?;
@ -11,7 +13,10 @@ pub fn decode<T: Seek + ReadBytesExt + Read>(input: &mut T) -> Result<Vec<u8>, C
Ok(bitmap) Ok(bitmap)
} }
pub fn encode<T: WriteBytesExt + Write>(output: &mut T, bitmap: &[u8]) -> Result<(), CzError> { pub fn encode<T: WriteBytesExt + Write>(
output: &mut T,
bitmap: &[u8]
) -> Result<(), CzError> {
output.write_all(bitmap)?; output.write_all(bitmap)?;
Ok(()) Ok(())

View file

@ -4,7 +4,9 @@ use std::io::{Read, Seek, SeekFrom, Write};
use crate::common::CzError; use crate::common::CzError;
use crate::compression::{compress, decompress, get_chunk_info}; use crate::compression::{compress, decompress, get_chunk_info};
pub fn decode<T: Seek + ReadBytesExt + Read>(bytes: &mut T) -> Result<Vec<u8>, CzError> { pub fn decode<T: Seek + ReadBytesExt + Read>(
bytes: &mut T
) -> Result<Vec<u8>, CzError> {
// Get information about the compressed chunks // Get information about the compressed chunks
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))?;
@ -15,7 +17,10 @@ pub fn decode<T: Seek + ReadBytesExt + Read>(bytes: &mut T) -> Result<Vec<u8>, C
Ok(bitmap) Ok(bitmap)
} }
pub fn encode<T: WriteBytesExt + Write>(output: &mut T, bitmap: &[u8]) -> Result<(), CzError> { pub fn encode<T: WriteBytesExt + Write>(
output: &mut T,
bitmap: &[u8]
) -> Result<(), CzError> {
let (compressed_data, compressed_info) = compress(bitmap, 0xFEFD); let (compressed_data, compressed_info) = compress(bitmap, 0xFEFD);
compressed_info.write_into(output)?; compressed_info.write_into(output)?;

View file

@ -4,7 +4,9 @@ use std::io::{Read, Seek, SeekFrom, Write};
use crate::common::CzError; use crate::common::CzError;
use crate::compression::{compress2, decompress2, get_chunk_info}; use crate::compression::{compress2, decompress2, get_chunk_info};
pub fn decode<T: Seek + ReadBytesExt + Read>(bytes: &mut T) -> Result<Vec<u8>, CzError> { pub fn decode<T: Seek + ReadBytesExt + Read>(
bytes: &mut T
) -> Result<Vec<u8>, CzError> {
// Get information about the compressed chunks // Get information about the compressed chunks
let block_info = get_chunk_info(bytes).unwrap(); let block_info = get_chunk_info(bytes).unwrap();
bytes.seek(SeekFrom::Start(block_info.length as u64))?; bytes.seek(SeekFrom::Start(block_info.length as u64))?;
@ -15,7 +17,10 @@ pub fn decode<T: Seek + ReadBytesExt + Read>(bytes: &mut T) -> Result<Vec<u8>, C
Ok(bitmap) Ok(bitmap)
} }
pub fn encode<T: WriteBytesExt + Write>(output: &mut T, bitmap: &[u8]) -> Result<(), CzError> { pub fn encode<T: WriteBytesExt + Write>(
output: &mut T,
bitmap: &[u8]
) -> Result<(), CzError> {
let (compressed_data, compressed_info) = compress2(bitmap, 0x87BDF); let (compressed_data, compressed_info) = compress2(bitmap, 0x87BDF);
compressed_info.write_into(output)?; compressed_info.write_into(output)?;

View file

@ -1,8 +1,7 @@
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::{CzError, CommonHeader};
use crate::compression::{compress, decompress, get_chunk_info}; use crate::compression::{compress, decompress, get_chunk_info};
pub fn decode<T: Seek + ReadBytesExt + Read>( pub fn decode<T: Seek + ReadBytesExt + Read>(
@ -12,13 +11,9 @@ 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 bitmap = decompress(bytes, &block_info)?;
dbg!(timer.elapsed());
let timer = Instant::now(); let bitmap = line_diff(header, &data);
let bitmap = line_diff(header, &bitmap);
dbg!(timer.elapsed());
Ok(bitmap) Ok(bitmap)
} }

View file

@ -1,8 +1,7 @@
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::{CzError, CommonHeader};
use crate::compression::{compress, decompress, get_chunk_info}; use crate::compression::{compress, decompress, get_chunk_info};
pub fn decode<T: Seek + ReadBytesExt + Read>( pub fn decode<T: Seek + ReadBytesExt + Read>(
@ -12,15 +11,11 @@ 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 bitmap = line_diff(header, &data);
let output = line_diff(header, &data);
dbg!(timer.elapsed());
Ok(output) Ok(bitmap)
} }
pub fn encode<T: WriteBytesExt + Write>( pub fn encode<T: WriteBytesExt + Write>(

View file

@ -1,7 +1,8 @@
fn main() { fn main() {
let cz_file = cz::open("test_file.cz3").unwrap(); let mut cz_file = cz::open("test_file.cz3").unwrap();
cz_file.save_as_png("test.png").unwrap();
cz_file.save_as_png("test_file.png").unwrap(); cz_file.header_mut().set_version(3).unwrap();
cz_file.save_as_cz("test_file.cz").unwrap(); cz_file.save_as_cz("test_file.cz2").unwrap();
} }