Add decode::qoi_decode_header()

This commit is contained in:
Ivan Smirnov 2021-12-01 16:01:58 +00:00
parent d7c8346e39
commit f17d2b6201
2 changed files with 17 additions and 1 deletions

View file

@ -178,3 +178,19 @@ pub fn qoi_decode_to_vec(data: impl AsRef<[u8]>, channels: u8) -> Result<(Header
_ => Err(Error::InvalidChannels { channels }), _ => Err(Error::InvalidChannels { channels }),
} }
} }
#[inline]
pub fn qoi_decode_header(data: impl AsRef<[u8]>) -> Result<Header> {
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)
}

View file

@ -10,7 +10,7 @@ pub mod canonical;
pub mod consts; pub mod consts;
pub use crate::colorspace::ColorSpace; 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::encode::{encode_size_required, qoi_encode_to_buf, qoi_encode_to_vec};
pub use crate::error::{Error, Result}; pub use crate::error::{Error, Result};
pub use crate::header::Header; pub use crate::header::Header;