From f17d2b6201bad85a12d7b4b2c54c0d30165af8c5 Mon Sep 17 00:00:00 2001 From: Ivan Smirnov Date: Wed, 1 Dec 2021 16:01:58 +0000 Subject: [PATCH] Add decode::qoi_decode_header() --- src/decode.rs | 16 ++++++++++++++++ src/lib.rs | 2 +- 2 files changed, 17 insertions(+), 1 deletion(-) 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;