From 68df0df6f45660f62cd8ff1638bf6af79ab2686f Mon Sep 17 00:00:00 2001
From: G2-Games <ke0bhogsg@gmail.com>
Date: Sun, 28 Jul 2024 19:48:03 -0500
Subject: [PATCH] Added byte size as return value to some functions

---
 Cargo.toml                  |  2 +-
 src/compression/lossless.rs |  7 +++++--
 src/header.rs               |  4 ++++
 src/main.rs                 | 22 +++++++++-------------
 src/picture.rs              | 13 +++++++++----
 5 files changed, 28 insertions(+), 20 deletions(-)

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<T: WriteBytesExt + Write>(
         &self,
         output: &mut T,
-    ) -> Result<(), std::io::Error> {
+    ) -> Result<usize, std::io::Error> {
+        let mut size = 0;
         output.write_u32::<LE>(self.chunk_count as u32)?;
+        size += 4;
 
         for chunk in &self.chunks {
             output.write_u32::<LE>(chunk.size_compressed as u32)?;
             output.write_u32::<LE>(chunk.size_raw as u32)?;
+            size += 8;
         }
 
-        Ok(())
+        Ok(size)
     }
 
     pub fn read_from<T: Read + ReadBytesExt>(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<T: Read + ReadBytesExt>(input: &mut T) -> Result<Self, Error> {
         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<O: Write + WriteBytesExt>(&self, mut output: O) -> Result<(), Error> {
+    /// Encode the image into anything that implements [Write]. Returns the
+    /// number of bytes written.
+    pub fn encode<O: Write + WriteBytesExt>(&self, mut output: O) -> Result<usize, Error> {
+        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.