diff --git a/Cargo.toml b/Cargo.toml index ac83169..f36525e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "dangoware_format" +name = "dpf_format" version = "0.1.0" edition = "2021" diff --git a/src/compression/lossless.rs b/src/compression/lossless.rs index 0233201..80aef9d 100644 --- a/src/compression/lossless.rs +++ b/src/compression/lossless.rs @@ -33,15 +33,18 @@ impl CompressionInfo { pub fn write_into( &self, output: &mut T, - ) -> Result<(), std::io::Error> { + ) -> Result { + let mut size = 0; output.write_u32::(self.chunk_count as u32)?; + size += 4; for chunk in &self.chunks { output.write_u32::(chunk.size_compressed as u32)?; output.write_u32::(chunk.size_raw as u32)?; + size += 8; } - Ok(()) + Ok(size) } pub fn read_from(input: &mut T) -> Self { diff --git a/src/header.rs b/src/header.rs index a2eaad3..9bdfefc 100644 --- a/src/header.rs +++ b/src/header.rs @@ -57,6 +57,10 @@ impl Header { buf.into_inner().try_into().unwrap() } + pub fn len(&self) -> usize { + 19 + } + pub fn read_from(input: &mut T) -> Result { let mut magic = [0u8; 8]; input.read_exact(&mut magic).unwrap(); diff --git a/src/main.rs b/src/main.rs index 0130dbc..2a860ea 100644 --- a/src/main.rs +++ b/src/main.rs @@ -9,11 +9,13 @@ pub mod picture; use std::{fs::File, io::{BufReader, BufWriter}, time::Instant}; use header::{ColorFormat, CompressionType}; -use image::RgbaImage; +use image::{ImageReader, RgbaImage}; use picture::DangoPicture; fn main() { - let input = image::open("transparent2.png").unwrap().to_rgba8(); + let mut input = ImageReader::open("shit.png").unwrap(); + input.no_limits(); + let input = input.decode().unwrap().to_rgba8(); input.save("original.png").unwrap(); let dpf_lossy = DangoPicture::from_raw( @@ -21,7 +23,7 @@ fn main() { input.height(), ColorFormat::Rgba32, CompressionType::LossyDct, - Some(10), + Some(80), input.as_raw().clone() ); @@ -38,12 +40,9 @@ fn main() { println!("Encoding"); let timer = Instant::now(); let mut outfile = BufWriter::new(std::fs::File::create("test-lossy.dpf").unwrap()); - dpf_lossy.encode(&mut outfile).unwrap(); + let size = dpf_lossy.encode(&mut outfile).unwrap(); println!("Encoding took {}ms", timer.elapsed().as_millis()); - - let mut outbuf = Vec::new(); - dpf_lossy.encode(&mut outbuf).unwrap(); - println!("Size is {}Mb", (((outbuf.len() as f32 / 1_000_000.0) * 100.0) as u32 as f32) / 100.0); + println!("Size is {}Mb", (((size as f32 / 1_000_000.0) * 100.0) as u32 as f32) / 100.0); println!("Decoding"); let timer = Instant::now(); @@ -56,12 +55,9 @@ fn main() { println!("Encoding"); let timer = Instant::now(); let mut outfile = BufWriter::new(std::fs::File::create("test-lossless.dpf").unwrap()); - dpf_lossless.encode(&mut outfile).unwrap(); + let size = dpf_lossless.encode(&mut outfile).unwrap(); println!("Encoding took {}ms", timer.elapsed().as_millis()); - - let mut outbuf = Vec::new(); - dpf_lossless.encode(&mut outbuf).unwrap(); - println!("Size is {}Mb", (((outbuf.len() as f32 / 1_000_000.0) * 100.0) as u32 as f32) / 100.0); + println!("Size is {}Mb", (((size as f32 / 1_000_000.0) * 100.0) as u32 as f32) / 100.0); println!("Decoding"); let timer = Instant::now(); diff --git a/src/picture.rs b/src/picture.rs index 3e00289..b1a749e 100644 --- a/src/picture.rs +++ b/src/picture.rs @@ -66,10 +66,14 @@ impl DangoPicture { } } - /// Encode the image into anything that implements [Write] - pub fn encode(&self, mut output: O) -> Result<(), Error> { + /// Encode the image into anything that implements [Write]. Returns the + /// number of bytes written. + pub fn encode(&self, mut output: O) -> Result { + let mut count = 0; + // Write out the header output.write_all(&self.header.to_bytes()).unwrap(); + count += self.header.len(); // Based on the compression type, modify the data accordingly let modified_data = match self.header.compression_type { @@ -98,12 +102,13 @@ impl DangoPicture { let (compressed_data, compression_info) = compress(&modified_data)?; // Write out compression info - compression_info.write_into(&mut output).unwrap(); + count += compression_info.write_into(&mut output).unwrap(); // Write out compressed data output.write_all(&compressed_data).unwrap(); + count += compressed_data.len(); - Ok(()) + Ok(count) } /// Encode and write the image out to a file.