Change struct to grab input iterator, not slice

I want to iterate over the input elements, and I couldn't figure out how
to get the slice lifetime to be specified properly. I've swapped the
slice for any Iterator that emits u8's.
This commit is contained in:
2024-05-06 11:49:42 -05:00
parent b77f32b2df
commit 4a31b1e20a

View File

@@ -27,16 +27,16 @@ const QOI_OP_DIFF: u8 = 0b0100_0000;
const QOI_OP_LUMA: u8 = 0b1000_0000;
const QOI_OP_RUN: u8 = 0b1100_0000;
struct Decoder<'input> {
struct Decoder<I: Iterator<Item=u8>> {
// QOI codec state information
back_buffer: [PixelRGBA; 64],
prev_pixel: PixelRGBA,
bytes: &'input [u8], // input byte slice
bytes: I,
}
impl<'input> Decoder<'input> {
fn new(qoi_bytes: &'input [u8]) -> Self {
impl <I> Decoder <I> where I: Iterator<Item=u8> {
fn new(bytes: I) -> Self {
Self {
back_buffer: [PixelRGBA::zero(); 64],
prev_pixel: PixelRGBA {
@@ -45,7 +45,7 @@ impl<'input> Decoder<'input> {
b: 0,
a: 255,
},
bytes: qoi_bytes,
bytes,
}
}
@@ -54,27 +54,29 @@ impl<'input> Decoder<'input> {
// then extracting back-referenced data out of it. A complete test should
// feed in other valid operations that populate the backbuffer, and then index
// op codes to demonstrate the indexing operations.
fn new_with_backbuffer(qoi_bytes: &'input [u8], back_buffer: [PixelRGBA; 64]) -> Self {
fn new_with_backbuffer(bytes: I, back_buffer: [PixelRGBA; 64]) -> Self {
Self {
back_buffer,
prev_pixel: PixelRGBA {
r: 0, g: 0, b: 0, a: 255,
},
bytes: qoi_bytes
bytes
}
}
// A hack to unit test the run behavior. Same idea as the new_with_backbuffer()
// function, but for testing a run of pixels.
fn new_with_previous_pixel(qoi_bytes: &'input [u8], prev_pixel: PixelRGBA) -> Self {
fn new_with_previous_pixel(bytes: I, prev_pixel: PixelRGBA) -> Self {
Self {
back_buffer: [PixelRGBA::zero(); 64],
prev_pixel,
bytes: qoi_bytes,
bytes,
}
}
fn hash(pixel: PixelRGBA) -> u8 {
}
mod codec_utils {
use super::PixelRGBA;
pub(crate) fn hash(pixel: PixelRGBA) -> u8 {
pixel
.r
.wrapping_mul(3)
@@ -85,11 +87,12 @@ impl<'input> Decoder<'input> {
}
}
impl<'input> Iterator for Decoder<'input> {
impl<'input, I> Iterator for Decoder<I> where I: Iterator<Item=u8> {
type Item = PixelRGBA;
fn next(&mut self) -> Option<Self::Item> {
todo!("Implement the Iterator trait for Decoder")
let byte = self.bytes.next()?;
todo!();
}
}
@@ -106,7 +109,7 @@ mod test {
a: 255,
};
let expected = 39;
assert_eq!(Decoder::hash(pixel), expected);
assert_eq!(codec_utils::hash(pixel), expected);
}
#[test]
@@ -125,7 +128,7 @@ mod test {
PixelRGBA{ r: 0x00, g: 0x00, b: 0x00, a: 0xFF}
];
let decoder = Decoder::new(&compressed);
let decoder = Decoder::new(compressed.into_iter());
let result = decoder.collect::<Vec<PixelRGBA>>();
assert_eq!(result, expected);
}
@@ -144,7 +147,7 @@ mod test {
PixelRGBA{ r: 0x10, g: 0x20, b: 0x30, a: 0x40 },
];
let decoder = Decoder::new(&compressed);
let decoder = Decoder::new(compressed.into_iter());
let result: Vec<PixelRGBA> = decoder.collect();
assert_eq!(result, expected);
}
@@ -179,7 +182,7 @@ mod test {
PixelRGBA::zero(),
];
let decoder = Decoder::new_with_backbuffer(&compressed, backbuffer);
let decoder = Decoder::new_with_backbuffer(compressed.into_iter(), backbuffer);
let result: Vec<PixelRGBA> = decoder.collect();
assert_eq!(result, expected);
}
@@ -210,7 +213,7 @@ mod test {
PixelRGBA::new(0, 0, 0, 255),
];
let decoder = Decoder::new(&compressed);
let decoder = Decoder::new(compressed.into_iter());
let result: Vec<PixelRGBA> = decoder.collect();
assert_eq!(result, expected);
}
@@ -235,7 +238,7 @@ mod test {
PixelRGBA::new(37, 19, 28, 255),
];
let decoder = Decoder::new(&compressed);
let decoder = Decoder::new(compressed.into_iter());
let result: Vec<PixelRGBA> = decoder.collect();
assert_eq!(result, expected);
}
@@ -254,7 +257,7 @@ mod test {
// lets pretend an encoder did this for some reason. The decoder can still
// unpack this correctly, it's just a sub-optimal compression is all.
let decoder = Decoder::new_with_previous_pixel(&compressed, init_pixel);
let decoder = Decoder::new_with_previous_pixel(compressed.into_iter(), init_pixel);
let result: Vec<PixelRGBA> = decoder.collect();
assert_eq!(result, expected);
}