Replaced byteorder with byteorder-lite, ran cargo fmt

This commit is contained in:
G2-Games 2024-11-12 19:50:10 -06:00
parent d253d57816
commit ec60308e6a
15 changed files with 72 additions and 106 deletions

View file

@ -13,7 +13,7 @@ authors.workspace = true
png = ["dep:image"] png = ["dep:image"]
[dependencies] [dependencies]
byteorder = "1.5" byteorder-lite = "0.1.0"
thiserror = "1.0" thiserror = "1.0"
imagequant = "4.3" imagequant = "4.3"
rgb = "0.8" rgb = "0.8"

View file

@ -1,4 +1,3 @@
use byteorder::ReadBytesExt;
use imagequant::Attributes; use imagequant::Attributes;
use rgb::{ComponentSlice, RGBA8}; use rgb::{ComponentSlice, RGBA8};
use std::{ use std::{
@ -35,10 +34,7 @@ impl Palette {
} }
/// Get a palette from the input stream, beginning where the palette starts. /// Get a palette from the input stream, beginning where the palette starts.
pub fn get_palette<T: Seek + ReadBytesExt + Read>( pub fn get_palette<T: Seek + Read>(input: &mut T, num_colors: usize) -> Result<Palette, CzError> {
input: &mut T,
num_colors: usize,
) -> Result<Palette, CzError> {
let mut colormap = Vec::with_capacity(num_colors); let mut colormap = Vec::with_capacity(num_colors);
let mut rgba_buf = [0u8; 4]; let mut rgba_buf = [0u8; 4];

View file

@ -2,7 +2,7 @@
use std::io::{self, Read, Seek, Write}; use std::io::{self, Read, Seek, Write};
use byteorder::{LittleEndian, ReadBytesExt, WriteBytesExt}; use byteorder_lite::{ReadBytesExt, WriteBytesExt, LE};
use thiserror::Error; use thiserror::Error;
#[derive(Error, Debug)] #[derive(Error, Debug)]
@ -132,10 +132,10 @@ impl CommonHeader {
let mut header = Self { let mut header = Self {
version, version,
length: bytes.read_u32::<LittleEndian>()?, length: bytes.read_u32::<LE>()?,
width: bytes.read_u16::<LittleEndian>()?, width: bytes.read_u16::<LE>()?,
height: bytes.read_u16::<LittleEndian>()?, height: bytes.read_u16::<LE>()?,
depth: bytes.read_u16::<LittleEndian>()?, depth: bytes.read_u16::<LE>()?,
unknown: bytes.read_u8()?, unknown: bytes.read_u8()?,
}; };
@ -204,10 +204,10 @@ impl CommonHeader {
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)?;
output.write_u32::<LittleEndian>(self.length() as u32)?; output.write_u32::<LE>(self.length() as u32)?;
output.write_u16::<LittleEndian>(self.width())?; output.write_u16::<LE>(self.width())?;
output.write_u16::<LittleEndian>(self.height())?; output.write_u16::<LE>(self.height())?;
output.write_u16::<LittleEndian>(self.depth())?; output.write_u16::<LE>(self.depth())?;
output.write_u8(self.color_block())?; output.write_u8(self.color_block())?;
Ok(()) Ok(())
@ -282,27 +282,27 @@ impl ExtendedHeader {
self self
} }
pub fn from_bytes<T: Seek + ReadBytesExt + Read>( pub fn from_bytes<T: Seek + Read>(
input: &mut T, input: &mut T,
common_header: &CommonHeader, common_header: &CommonHeader,
) -> Result<Self, CzError> { ) -> Result<Self, CzError> {
let mut unknown_1 = [0u8; 5]; let mut unknown_1 = [0u8; 5];
input.read_exact(&mut unknown_1)?; input.read_exact(&mut unknown_1)?;
let crop_width = input.read_u16::<LittleEndian>()?; let crop_width = input.read_u16::<LE>()?;
let crop_height = input.read_u16::<LittleEndian>()?; let crop_height = input.read_u16::<LE>()?;
let bounds_width = input.read_u16::<LittleEndian>()?; let bounds_width = input.read_u16::<LE>()?;
let bounds_height = input.read_u16::<LittleEndian>()?; let bounds_height = input.read_u16::<LE>()?;
let mut offset_width = None; let mut offset_width = None;
let mut offset_height = None; let mut offset_height = None;
let mut unknown_2 = None; let mut unknown_2 = None;
if common_header.length() > 28 { if common_header.length() > 28 {
offset_width = Some(input.read_u16::<LittleEndian>()?); offset_width = Some(input.read_u16::<LE>()?);
offset_height = Some(input.read_u16::<LittleEndian>()?); offset_height = Some(input.read_u16::<LE>()?);
unknown_2 = Some(input.read_u32::<LittleEndian>()?); unknown_2 = Some(input.read_u32::<LE>()?);
} }
Ok(Self { Ok(Self {
@ -321,17 +321,17 @@ impl ExtendedHeader {
}) })
} }
pub fn write_into<T: WriteBytesExt + Write>(&self, output: &mut T) -> Result<(), io::Error> { pub fn write_into<T: Write>(&self, output: &mut T) -> Result<(), io::Error> {
output.write_all(&self.unknown_1)?; output.write_all(&self.unknown_1)?;
output.write_u16::<LittleEndian>(self.crop_width)?; output.write_u16::<LE>(self.crop_width)?;
output.write_u16::<LittleEndian>(self.crop_height)?; output.write_u16::<LE>(self.crop_height)?;
output.write_u16::<LittleEndian>(self.bounds_width)?; output.write_u16::<LE>(self.bounds_width)?;
output.write_u16::<LittleEndian>(self.bounds_height)?; output.write_u16::<LE>(self.bounds_height)?;
if self.offset_width.is_some() { if self.offset_width.is_some() {
output.write_u16::<LittleEndian>(self.offset_width.unwrap())?; output.write_u16::<LE>(self.offset_width.unwrap())?;
output.write_u16::<LittleEndian>(self.offset_height.unwrap())?; output.write_u16::<LE>(self.offset_height.unwrap())?;
output.write_u32::<LittleEndian>(self.unknown_2.unwrap())?; output.write_u32::<LE>(self.unknown_2.unwrap())?;
} }
Ok(()) Ok(())

View file

@ -1,4 +1,3 @@
use byteorder::{ReadBytesExt, WriteBytesExt, LE};
use std::{ use std::{
collections::HashMap, collections::HashMap,
io::{Read, Seek, Write}, io::{Read, Seek, Write},
@ -6,6 +5,7 @@ use std::{
use crate::binio::BitIo; use crate::binio::BitIo;
use crate::common::CzError; use crate::common::CzError;
use byteorder_lite::{ReadBytesExt, WriteBytesExt, LE};
/// The size of compressed data in each chunk /// The size of compressed data in each chunk
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@ -37,10 +37,7 @@ pub struct CompressionInfo {
} }
impl CompressionInfo { impl CompressionInfo {
pub fn write_into<T: WriteBytesExt + Write>( pub fn write_into<T: Write>(&self, output: &mut T) -> Result<(), std::io::Error> {
&self,
output: &mut T,
) -> Result<(), std::io::Error> {
output.write_u32::<LE>(self.chunk_count as u32)?; output.write_u32::<LE>(self.chunk_count as u32)?;
for chunk in &self.chunks { for chunk in &self.chunks {
@ -56,9 +53,7 @@ impl CompressionInfo {
/// ///
/// These are defined by a length value, followed by the number of data chunks /// These are defined by a length value, followed by the number of data chunks
/// that length value says split into compressed and original size u32 values /// that length value says split into compressed and original size u32 values
pub fn get_chunk_info<T: Seek + ReadBytesExt + Read>( pub fn get_chunk_info<T: Seek + Read>(bytes: &mut T) -> Result<CompressionInfo, CzError> {
bytes: &mut T,
) -> Result<CompressionInfo, CzError> {
let parts_count = bytes.read_u32::<LE>()?; let parts_count = bytes.read_u32::<LE>()?;
let mut part_sizes = vec![]; let mut part_sizes = vec![];
@ -89,7 +84,7 @@ pub fn get_chunk_info<T: Seek + ReadBytesExt + Read>(
} }
/// Decompress an LZW compressed stream like CZ1 /// Decompress an LZW compressed stream like CZ1
pub fn decompress<T: Seek + ReadBytesExt + Read>( pub fn decompress<T: Seek + Read>(
input: &mut T, input: &mut T,
chunk_info: &CompressionInfo, chunk_info: &CompressionInfo,
) -> Result<Vec<u8>, CzError> { ) -> Result<Vec<u8>, CzError> {
@ -144,7 +139,7 @@ fn decompress_lzw(input_data: &[u16], size: usize) -> Vec<u8> {
} }
/// Decompress an LZW compressed stream like CZ2 /// Decompress an LZW compressed stream like CZ2
pub fn decompress2<T: Seek + ReadBytesExt + Read>( pub fn decompress2<T: Seek + Read>(
input: &mut T, input: &mut T,
chunk_info: &CompressionInfo, chunk_info: &CompressionInfo,
) -> Result<Vec<u8>, CzError> { ) -> Result<Vec<u8>, CzError> {
@ -196,7 +191,11 @@ fn decompress_lzw2(input_data: &[u8], size: usize) -> Vec<u8> {
entry = w.clone(); entry = w.clone();
entry.push(w[0]) entry.push(w[0])
} else { } else {
panic!("Bad compressed element {} at offset {}", element, bit_io.byte_offset()) panic!(
"Bad compressed element {} at offset {}",
element,
bit_io.byte_offset()
)
} }
//println!("{}", element); //println!("{}", element);

View file

@ -1,4 +1,4 @@
use byteorder::ReadBytesExt; use byteorder_lite::ReadBytesExt;
use rgb::ComponentSlice; use rgb::ComponentSlice;
use std::{ use std::{
fs::File, fs::File,

View file

@ -1,9 +1,8 @@
use byteorder::{ReadBytesExt, WriteBytesExt};
use std::io::{Read, Seek, Write}; 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 + 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 +10,7 @@ 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: Write>(output: &mut T, bitmap: &[u8]) -> Result<(), CzError> {
output.write_all(bitmap)?; output.write_all(bitmap)?;
Ok(()) Ok(())

View file

@ -1,10 +1,9 @@
use byteorder::{ReadBytesExt, WriteBytesExt};
use std::io::{Read, Seek, SeekFrom, Write}; 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 + 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 +14,7 @@ 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: 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

@ -1,10 +1,9 @@
use byteorder::{ReadBytesExt, WriteBytesExt};
use std::io::{Read, Seek, SeekFrom, Write}; 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 + 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 +14,7 @@ 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: Write>(output: &mut T, bitmap: &[u8]) -> Result<(), CzError> {
let (compressed_data, compressed_info) = compress2(&bitmap); let (compressed_data, compressed_info) = compress2(&bitmap);
compressed_info.write_into(output)?; compressed_info.write_into(output)?;

View file

@ -1,13 +1,9 @@
use byteorder::{ReadBytesExt, WriteBytesExt};
use std::io::{Read, Seek, SeekFrom, Write}; use std::io::{Read, Seek, SeekFrom, Write};
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};
pub fn decode<T: Seek + ReadBytesExt + Read>( pub fn decode<T: Seek + Read>(bytes: &mut T, header: &CommonHeader) -> Result<Vec<u8>, CzError> {
bytes: &mut T,
header: &CommonHeader,
) -> Result<Vec<u8>, CzError> {
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))?;
@ -18,7 +14,7 @@ pub fn decode<T: Seek + ReadBytesExt + Read>(
Ok(bitmap) Ok(bitmap)
} }
pub fn encode<T: WriteBytesExt + Write>( pub fn encode<T: Write>(
output: &mut T, output: &mut T,
bitmap: &[u8], bitmap: &[u8],
header: &CommonHeader, header: &CommonHeader,

View file

@ -1,13 +1,9 @@
use byteorder::{ReadBytesExt, WriteBytesExt};
use std::io::{Read, Seek, SeekFrom, Write}; use std::io::{Read, Seek, SeekFrom, Write};
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};
pub fn decode<T: Seek + ReadBytesExt + Read>( pub fn decode<T: Seek + Read>(bytes: &mut T, header: &CommonHeader) -> Result<Vec<u8>, CzError> {
bytes: &mut T,
header: &CommonHeader,
) -> Result<Vec<u8>, CzError> {
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))?;
@ -18,7 +14,7 @@ pub fn decode<T: Seek + ReadBytesExt + Read>(
Ok(bitmap) Ok(bitmap)
} }
pub fn encode<T: WriteBytesExt + Write>( pub fn encode<T: Write>(
output: &mut T, output: &mut T,
bitmap: &[u8], bitmap: &[u8],
header: &CommonHeader, header: &CommonHeader,

View file

@ -13,12 +13,7 @@ const TEST_IMAGES: &[TestImage] = &[KODIM03, KODIM23, SQPTEXT, DPFLOGO];
#[test] #[test]
fn cz0_round_trip() { fn cz0_round_trip() {
for image in TEST_IMAGES { for image in TEST_IMAGES {
let original_cz = DynamicCz::from_raw( let original_cz = DynamicCz::from_raw(CzVersion::CZ0, image.0, image.1, image.2.to_vec());
CzVersion::CZ0,
image.0,
image.1,
image.2.to_vec()
);
let mut cz_bytes = Vec::new(); let mut cz_bytes = Vec::new();
original_cz.encode(&mut cz_bytes).unwrap(); original_cz.encode(&mut cz_bytes).unwrap();
@ -33,12 +28,7 @@ fn cz0_round_trip() {
#[test] #[test]
fn cz1_round_trip() { fn cz1_round_trip() {
for image in TEST_IMAGES { for image in TEST_IMAGES {
let original_cz = DynamicCz::from_raw( let original_cz = DynamicCz::from_raw(CzVersion::CZ1, image.0, image.1, image.2.to_vec());
CzVersion::CZ1,
image.0,
image.1,
image.2.to_vec()
);
let mut cz_bytes = Vec::new(); let mut cz_bytes = Vec::new();
original_cz.encode(&mut cz_bytes).unwrap(); original_cz.encode(&mut cz_bytes).unwrap();
@ -54,12 +44,7 @@ fn cz1_round_trip() {
fn cz2_round_trip() { fn cz2_round_trip() {
let mut i = 0; let mut i = 0;
for image in TEST_IMAGES { for image in TEST_IMAGES {
let original_cz = DynamicCz::from_raw( let original_cz = DynamicCz::from_raw(CzVersion::CZ2, image.0, image.1, image.2.to_vec());
CzVersion::CZ2,
image.0,
image.1,
image.2.to_vec()
);
let mut cz_bytes = Vec::new(); let mut cz_bytes = Vec::new();
original_cz.encode(&mut cz_bytes).unwrap(); original_cz.encode(&mut cz_bytes).unwrap();
@ -76,12 +61,7 @@ fn cz2_round_trip() {
#[test] #[test]
fn cz3_round_trip() { fn cz3_round_trip() {
for image in TEST_IMAGES { for image in TEST_IMAGES {
let original_cz = DynamicCz::from_raw( let original_cz = DynamicCz::from_raw(CzVersion::CZ3, image.0, image.1, image.2.to_vec());
CzVersion::CZ3,
image.0,
image.1,
image.2.to_vec()
);
let mut cz_bytes = Vec::new(); let mut cz_bytes = Vec::new();
original_cz.encode(&mut cz_bytes).unwrap(); original_cz.encode(&mut cz_bytes).unwrap();
@ -96,12 +76,7 @@ fn cz3_round_trip() {
#[test] #[test]
fn cz4_round_trip() { fn cz4_round_trip() {
for image in TEST_IMAGES { for image in TEST_IMAGES {
let original_cz = DynamicCz::from_raw( let original_cz = DynamicCz::from_raw(CzVersion::CZ4, image.0, image.1, image.2.to_vec());
CzVersion::CZ4,
image.0,
image.1,
image.2.to_vec()
);
let mut cz_bytes = Vec::new(); let mut cz_bytes = Vec::new();
original_cz.encode(&mut cz_bytes).unwrap(); original_cz.encode(&mut cz_bytes).unwrap();

View file

@ -8,10 +8,16 @@ fn main() {
let si = SysinfoBuilder::all_sysinfo().unwrap(); let si = SysinfoBuilder::all_sysinfo().unwrap();
Emitter::default() Emitter::default()
.add_instructions(&build).unwrap() .add_instructions(&build)
.add_instructions(&cargo).unwrap() .unwrap()
.add_instructions(&git2).unwrap() .add_instructions(&cargo)
.add_instructions(&rustc).unwrap() .unwrap()
.add_instructions(&si).unwrap() .add_instructions(&git2)
.emit().unwrap(); .unwrap()
.add_instructions(&rustc)
.unwrap()
.add_instructions(&si)
.unwrap()
.emit()
.unwrap();
} }

View file

@ -1,7 +1,8 @@
use clap::{error::ErrorKind, Command, Error, Parser, Subcommand}; use clap::{error::ErrorKind, Command, Error, Parser, Subcommand};
use std::{ use std::{
fs, fs,
path::{Path, PathBuf}, process::exit, path::{Path, PathBuf},
process::exit,
}; };
/// Utility to maniuplate CZ image files from the LUCA System game engine by /// Utility to maniuplate CZ image files from the LUCA System game engine by

View file

@ -83,7 +83,7 @@ fn main() {
Some(c) => c, Some(c) => c,
None => { None => {
exit(0); exit(0);
}, }
}; };
match command { match command {