New Decoder creation function, more metadata
The qoi_header is specified as knowing the width, height, channels, and colorspace. I figure this information would be captured by the decoder, if anywhere. I've added more properties to the struct, and created a new, fallible construction function.
This commit is contained in:
@@ -35,6 +35,7 @@ fn try_read_u32<I: Iterator<Item=u8>>(bytes: &mut I) -> Result<u32, DecodeError>
|
||||
}
|
||||
|
||||
mod codec_utils {
|
||||
use super::DecodeError;
|
||||
use super::PixelRGBA;
|
||||
pub(crate) fn hash(pixel: PixelRGBA) -> u8 {
|
||||
pixel
|
||||
@@ -45,10 +46,24 @@ mod codec_utils {
|
||||
.wrapping_add(pixel.a.wrapping_mul(11))
|
||||
% 64
|
||||
}
|
||||
|
||||
pub (crate) fn read_u8_rewrap<I: Iterator<Item = u8>>(bytes: &mut I) -> Result<u8, DecodeError> {
|
||||
if let Some(byte) = bytes.next() {
|
||||
return Ok(byte);
|
||||
} else {
|
||||
return Err(DecodeError::EarlyIteratorExhaustion);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
struct Decoder<I: Iterator<Item = u8>> {
|
||||
// Image metadata
|
||||
width: u32,
|
||||
height: u32,
|
||||
channels: u8,
|
||||
colorspace: u8,
|
||||
|
||||
// QOI codec state information
|
||||
back_buffer: [PixelRGBA; 64],
|
||||
prev_pixel: PixelRGBA,
|
||||
@@ -61,6 +76,20 @@ impl<I> Decoder<I>
|
||||
where
|
||||
I: Iterator<Item = u8>,
|
||||
{
|
||||
fn try_new(mut bytes: I) -> Result<Self, DecodeError> {
|
||||
let _ = try_read_magic(&mut bytes)?;
|
||||
Ok(Self {
|
||||
width: try_read_u32(&mut bytes)?,
|
||||
height: try_read_u32(&mut bytes)?,
|
||||
channels: codec_utils::read_u8_rewrap(&mut bytes)?,
|
||||
colorspace: codec_utils::read_u8_rewrap(&mut bytes)?,
|
||||
back_buffer: [PixelRGBA::zero(); 64],
|
||||
prev_pixel: PixelRGBA { r:0, g:0, b:0, a: 255},
|
||||
bytes,
|
||||
run_len: 0,
|
||||
})
|
||||
}
|
||||
|
||||
fn new(bytes: I) -> Self {
|
||||
Self {
|
||||
back_buffer: [PixelRGBA::zero(); 64],
|
||||
|
||||
Reference in New Issue
Block a user