diff --git a/sqp/src/header.rs b/sqp/src/header.rs index 018bff8..9173040 100644 --- a/sqp/src/header.rs +++ b/sqp/src/header.rs @@ -7,6 +7,7 @@ use crate::picture::Error; /// A DPF file header. This must be included at the beginning /// of a valid DPF file. +#[derive(Debug, Clone, Copy)] pub struct Header { /// Identifier. Must be set to "dangoimg". pub magic: [u8; 8], diff --git a/sqp/src/picture.rs b/sqp/src/picture.rs index 737c8ca..963d916 100644 --- a/sqp/src/picture.rs +++ b/sqp/src/picture.rs @@ -1,8 +1,6 @@ //! Functions and other utilities surrounding the [`SquishyPicture`] type. use std::{fs::File, io::{self, BufWriter, Read, Write}, path::Path}; - -use byteorder::{ReadBytesExt, WriteBytesExt}; use integer_encoding::VarInt; use thiserror::Error; @@ -146,7 +144,7 @@ impl SquishyPicture { /// Encode the image into anything that implements [`Write`]. /// /// Returns the number of bytes written. - pub fn encode(&self, mut output: O) -> Result { + pub fn encode(&self, mut output: O) -> Result { let mut count = 0; // Write out the header @@ -205,14 +203,14 @@ impl SquishyPicture { } /// Decode the image from anything that implements [`Read`] - pub fn decode(mut input: I) -> Result { + pub fn decode(mut input: I) -> Result { let header = Header::read_from(&mut input)?; let compression_info = CompressionInfo::read_from(&mut input); let pre_bitmap = decompress(&mut input, &compression_info); - let bitmap = match header.compression_type { + let mut bitmap = match header.compression_type { CompressionType::None => pre_bitmap, CompressionType::Lossless => { add_rows( @@ -235,6 +233,8 @@ impl SquishyPicture { }, }; + bitmap.truncate(header.width as usize * header.height as usize * header.color_format.pbc()); + Ok(Self { header, bitmap }) } @@ -242,6 +242,25 @@ impl SquishyPicture { pub fn as_raw(&self) -> &Vec { &self.bitmap } + + /// Get the underlying raw buffer + pub fn into_raw(self) -> Vec { + self.bitmap + } + + /// The width of the image in pixels + pub fn width(&self) -> u32 { + self.header.width + } + + /// The height of the image in pixels + pub fn height(&self) -> u32 { + self.header.height + } + + pub fn color_format(&self) -> ColorFormat { + self.header.color_format + } } /// Decode a stream encoded as varints.