mirror of
https://github.com/Dangoware/sqp.git
synced 2025-04-19 15:22:54 -05:00
Added byte size as return value to some functions
This commit is contained in:
parent
e1a52c7077
commit
68df0df6f4
5 changed files with 28 additions and 20 deletions
|
@ -1,5 +1,5 @@
|
||||||
[package]
|
[package]
|
||||||
name = "dangoware_format"
|
name = "dpf_format"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
|
|
||||||
|
|
|
@ -33,15 +33,18 @@ impl CompressionInfo {
|
||||||
pub fn write_into<T: WriteBytesExt + Write>(
|
pub fn write_into<T: WriteBytesExt + Write>(
|
||||||
&self,
|
&self,
|
||||||
output: &mut T,
|
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)?;
|
output.write_u32::<LE>(self.chunk_count as u32)?;
|
||||||
|
size += 4;
|
||||||
|
|
||||||
for chunk in &self.chunks {
|
for chunk in &self.chunks {
|
||||||
output.write_u32::<LE>(chunk.size_compressed as u32)?;
|
output.write_u32::<LE>(chunk.size_compressed as u32)?;
|
||||||
output.write_u32::<LE>(chunk.size_raw 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 {
|
pub fn read_from<T: Read + ReadBytesExt>(input: &mut T) -> Self {
|
||||||
|
|
|
@ -57,6 +57,10 @@ impl Header {
|
||||||
buf.into_inner().try_into().unwrap()
|
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> {
|
pub fn read_from<T: Read + ReadBytesExt>(input: &mut T) -> Result<Self, Error> {
|
||||||
let mut magic = [0u8; 8];
|
let mut magic = [0u8; 8];
|
||||||
input.read_exact(&mut magic).unwrap();
|
input.read_exact(&mut magic).unwrap();
|
||||||
|
|
22
src/main.rs
22
src/main.rs
|
@ -9,11 +9,13 @@ pub mod picture;
|
||||||
|
|
||||||
use std::{fs::File, io::{BufReader, BufWriter}, time::Instant};
|
use std::{fs::File, io::{BufReader, BufWriter}, time::Instant};
|
||||||
use header::{ColorFormat, CompressionType};
|
use header::{ColorFormat, CompressionType};
|
||||||
use image::RgbaImage;
|
use image::{ImageReader, RgbaImage};
|
||||||
use picture::DangoPicture;
|
use picture::DangoPicture;
|
||||||
|
|
||||||
fn main() {
|
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();
|
input.save("original.png").unwrap();
|
||||||
|
|
||||||
let dpf_lossy = DangoPicture::from_raw(
|
let dpf_lossy = DangoPicture::from_raw(
|
||||||
|
@ -21,7 +23,7 @@ fn main() {
|
||||||
input.height(),
|
input.height(),
|
||||||
ColorFormat::Rgba32,
|
ColorFormat::Rgba32,
|
||||||
CompressionType::LossyDct,
|
CompressionType::LossyDct,
|
||||||
Some(10),
|
Some(80),
|
||||||
input.as_raw().clone()
|
input.as_raw().clone()
|
||||||
);
|
);
|
||||||
|
|
||||||
|
@ -38,12 +40,9 @@ fn main() {
|
||||||
println!("Encoding");
|
println!("Encoding");
|
||||||
let timer = Instant::now();
|
let timer = Instant::now();
|
||||||
let mut outfile = BufWriter::new(std::fs::File::create("test-lossy.dpf").unwrap());
|
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());
|
println!("Encoding took {}ms", timer.elapsed().as_millis());
|
||||||
|
println!("Size is {}Mb", (((size as f32 / 1_000_000.0) * 100.0) as u32 as f32) / 100.0);
|
||||||
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!("Decoding");
|
println!("Decoding");
|
||||||
let timer = Instant::now();
|
let timer = Instant::now();
|
||||||
|
@ -56,12 +55,9 @@ fn main() {
|
||||||
println!("Encoding");
|
println!("Encoding");
|
||||||
let timer = Instant::now();
|
let timer = Instant::now();
|
||||||
let mut outfile = BufWriter::new(std::fs::File::create("test-lossless.dpf").unwrap());
|
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());
|
println!("Encoding took {}ms", timer.elapsed().as_millis());
|
||||||
|
println!("Size is {}Mb", (((size as f32 / 1_000_000.0) * 100.0) as u32 as f32) / 100.0);
|
||||||
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!("Decoding");
|
println!("Decoding");
|
||||||
let timer = Instant::now();
|
let timer = Instant::now();
|
||||||
|
|
|
@ -66,10 +66,14 @@ impl DangoPicture {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encode the image into anything that implements [Write]
|
/// Encode the image into anything that implements [Write]. Returns the
|
||||||
pub fn encode<O: Write + WriteBytesExt>(&self, mut output: O) -> Result<(), Error> {
|
/// 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
|
// Write out the header
|
||||||
output.write_all(&self.header.to_bytes()).unwrap();
|
output.write_all(&self.header.to_bytes()).unwrap();
|
||||||
|
count += self.header.len();
|
||||||
|
|
||||||
// Based on the compression type, modify the data accordingly
|
// Based on the compression type, modify the data accordingly
|
||||||
let modified_data = match self.header.compression_type {
|
let modified_data = match self.header.compression_type {
|
||||||
|
@ -98,12 +102,13 @@ impl DangoPicture {
|
||||||
let (compressed_data, compression_info) = compress(&modified_data)?;
|
let (compressed_data, compression_info) = compress(&modified_data)?;
|
||||||
|
|
||||||
// Write out compression info
|
// Write out compression info
|
||||||
compression_info.write_into(&mut output).unwrap();
|
count += compression_info.write_into(&mut output).unwrap();
|
||||||
|
|
||||||
// Write out compressed data
|
// Write out compressed data
|
||||||
output.write_all(&compressed_data).unwrap();
|
output.write_all(&compressed_data).unwrap();
|
||||||
|
count += compressed_data.len();
|
||||||
|
|
||||||
Ok(())
|
Ok(count)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Encode and write the image out to a file.
|
/// Encode and write the image out to a file.
|
||||||
|
|
Loading…
Reference in a new issue