diff --git a/src/decode.rs b/src/decode.rs index 669599b..28a355e 100644 --- a/src/decode.rs +++ b/src/decode.rs @@ -178,3 +178,19 @@ pub fn qoi_decode_to_vec(data: impl AsRef<[u8]>, channels: u8) -> Result<(Header _ => Err(Error::InvalidChannels { channels }), } } + +#[inline] +pub fn qoi_decode_header(data: impl AsRef<[u8]>) -> Result
{ + let data = data.as_ref(); + if data.len() < QOI_HEADER_SIZE { + return Err(Error::InputBufferTooSmall { size: data.len(), required: QOI_HEADER_SIZE }); + } + let header = unsafe { + // Safety: we have just checked the length above + Header::from_bytes(*(data.as_ptr() as *const _)) + }; + if header.magic != QOI_MAGIC { + return Err(Error::InvalidMagic { magic: header.magic }); + } + Ok(header) +} diff --git a/src/lib.rs b/src/lib.rs index 6221cd7..a2eef47 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -10,7 +10,7 @@ pub mod canonical; pub mod consts; pub use crate::colorspace::ColorSpace; -pub use crate::decode::qoi_decode_to_vec; +pub use crate::decode::{qoi_decode_header, qoi_decode_to_vec}; pub use crate::encode::{encode_size_required, qoi_encode_to_buf, qoi_encode_to_vec}; pub use crate::error::{Error, Result}; pub use crate::header::Header;