diff --git a/src/binio.rs b/src/binio.rs index 0e8bc89..459f1cb 100644 --- a/src/binio.rs +++ b/src/binio.rs @@ -99,14 +99,15 @@ impl<'a, O: Write + WriteBytesExt> BitWriter<'a, O> { panic!("Must write 1 or more bytes.") } - self.output.write_all(&data.to_le_bytes()[..byte_len]).unwrap(); + self.output + .write_all(&data.to_le_bytes()[..byte_len]) + .unwrap(); self.byte_offset += byte_len; self.byte_size = self.byte_offset + (self.bit_offset + 7) / 8; } } - /// A simple way to read individual bits from an input implementing [Read]. pub struct BitReader<'a, I: Read + ReadBytesExt> { input: &'a mut I, diff --git a/src/compression/dct.rs b/src/compression/dct.rs new file mode 100644 index 0000000..8b13789 --- /dev/null +++ b/src/compression/dct.rs @@ -0,0 +1 @@ + diff --git a/src/compression/lossless.rs b/src/compression/lossless.rs index 5d5dfe0..29da92f 100644 --- a/src/compression/lossless.rs +++ b/src/compression/lossless.rs @@ -1,4 +1,7 @@ -use std::{collections::HashMap, io::{Cursor, Read, Write}}; +use std::{ + collections::HashMap, + io::{Cursor, Read, Write}, +}; use byteorder::{ReadBytesExt, WriteBytesExt, LE}; @@ -118,7 +121,7 @@ fn compress_lzw(data: &[u8], last: Vec) -> (usize, Vec, Vec) { if dictionary_count >= 0x3FFFE { count -= 1; - break + break; } } @@ -145,10 +148,7 @@ fn compress_lzw(data: &[u8], last: Vec) -> (usize, Vec, Vec) { (count, output_buf, last_element) } -pub fn decompress( - input: &mut T, - chunk_info: &CompressionInfo, -) -> Vec { +pub fn decompress(input: &mut T, chunk_info: &CompressionInfo) -> Vec { let mut output_buf: Vec = vec![]; for block in &chunk_info.chunks { @@ -173,7 +173,6 @@ fn decompress_lzw(input_data: &[u8], size: usize) -> Vec { } let mut dictionary_count = dictionary.len() as u64; - let mut result = Vec::with_capacity(size); let data_size = input_data.len(); diff --git a/src/compression/mod.rs b/src/compression/mod.rs deleted file mode 100644 index 91711ee..0000000 --- a/src/compression/mod.rs +++ /dev/null @@ -1 +0,0 @@ -mod lossless; diff --git a/src/header.rs b/src/header.rs index 09180e7..0953c49 100644 --- a/src/header.rs +++ b/src/header.rs @@ -1,5 +1,5 @@ -use std::io::{Cursor, Write}; use byteorder::{WriteBytesExt, LE}; +use std::io::{Cursor, Write}; pub struct Header { pub magic: [u8; 8], diff --git a/src/main.rs b/src/main.rs index f9d9e19..322841e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,23 +1,24 @@ mod compression { + pub mod dct; pub mod lossless; } +mod binio; mod header; mod operations; -mod binio; mod picture; -use std::{fs::File, io::{BufReader, BufWriter}, time::Instant}; use picture::DangoPicture; +use std::{ + fs::File, + io::{BufReader, BufWriter}, + time::Instant, +}; use image::RgbaImage; fn main() { let image_data = image::open("littlespace.png").unwrap().to_rgba8(); - let encoded_dpf = DangoPicture::from_raw( - image_data.width(), - image_data.height(), - &image_data - ); + let encoded_dpf = DangoPicture::from_raw(image_data.width(), image_data.height(), &image_data); let timer = Instant::now(); let mut outfile = BufWriter::new(File::create("test.dpf").unwrap()); @@ -32,7 +33,8 @@ fn main() { let out_image = RgbaImage::from_raw( decoded_dpf.header.width, decoded_dpf.header.height, - decoded_dpf.bitmap.into() - ).unwrap(); + decoded_dpf.bitmap.into(), + ) + .unwrap(); out_image.save("test.png").unwrap(); } diff --git a/src/picture.rs b/src/picture.rs index bd40bb6..2624aa5 100644 --- a/src/picture.rs +++ b/src/picture.rs @@ -6,7 +6,7 @@ use thiserror::Error; use crate::{ compression::lossless::{compress, decompress, ChunkInfo, CompressionInfo}, header::Header, - operations::{diff_line, line_diff} + operations::{diff_line, line_diff}, }; pub struct DangoPicture { @@ -45,7 +45,7 @@ impl DangoPicture { input.read_exact(&mut magic).unwrap(); if magic != *b"dangoimg" { - return Err(Error::InvalidIdentifier(magic)) + return Err(Error::InvalidIdentifier(magic)); } let header = Header { @@ -70,10 +70,7 @@ impl DangoPicture { let bitmap = line_diff(header.width, header.height, &preprocessed_bitmap); - Ok(DangoPicture { - header, - bitmap - }) + Ok(DangoPicture { header, bitmap }) } pub fn from_raw(width: u32, height: u32, bitmap: &[u8]) -> Self {