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:
43
src/main.rs
43
src/main.rs
@@ -27,16 +27,16 @@ const QOI_OP_DIFF: u8 = 0b0100_0000;
|
|||||||
const QOI_OP_LUMA: u8 = 0b1000_0000;
|
const QOI_OP_LUMA: u8 = 0b1000_0000;
|
||||||
const QOI_OP_RUN: u8 = 0b1100_0000;
|
const QOI_OP_RUN: u8 = 0b1100_0000;
|
||||||
|
|
||||||
struct Decoder<'input> {
|
struct Decoder<I: Iterator<Item=u8>> {
|
||||||
// QOI codec state information
|
// QOI codec state information
|
||||||
back_buffer: [PixelRGBA; 64],
|
back_buffer: [PixelRGBA; 64],
|
||||||
prev_pixel: PixelRGBA,
|
prev_pixel: PixelRGBA,
|
||||||
|
|
||||||
bytes: &'input [u8], // input byte slice
|
bytes: I,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'input> Decoder<'input> {
|
impl <I> Decoder <I> where I: Iterator<Item=u8> {
|
||||||
fn new(qoi_bytes: &'input [u8]) -> Self {
|
fn new(bytes: I) -> Self {
|
||||||
Self {
|
Self {
|
||||||
back_buffer: [PixelRGBA::zero(); 64],
|
back_buffer: [PixelRGBA::zero(); 64],
|
||||||
prev_pixel: PixelRGBA {
|
prev_pixel: PixelRGBA {
|
||||||
@@ -45,7 +45,7 @@ impl<'input> Decoder<'input> {
|
|||||||
b: 0,
|
b: 0,
|
||||||
a: 255,
|
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
|
// then extracting back-referenced data out of it. A complete test should
|
||||||
// feed in other valid operations that populate the backbuffer, and then index
|
// feed in other valid operations that populate the backbuffer, and then index
|
||||||
// op codes to demonstrate the indexing operations.
|
// 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 {
|
Self {
|
||||||
back_buffer,
|
back_buffer,
|
||||||
prev_pixel: PixelRGBA {
|
prev_pixel: PixelRGBA {
|
||||||
r: 0, g: 0, b: 0, a: 255,
|
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()
|
// A hack to unit test the run behavior. Same idea as the new_with_backbuffer()
|
||||||
// function, but for testing a run of pixels.
|
// 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 {
|
Self {
|
||||||
back_buffer: [PixelRGBA::zero(); 64],
|
back_buffer: [PixelRGBA::zero(); 64],
|
||||||
prev_pixel,
|
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
|
pixel
|
||||||
.r
|
.r
|
||||||
.wrapping_mul(3)
|
.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;
|
type Item = PixelRGBA;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
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,
|
a: 255,
|
||||||
};
|
};
|
||||||
let expected = 39;
|
let expected = 39;
|
||||||
assert_eq!(Decoder::hash(pixel), expected);
|
assert_eq!(codec_utils::hash(pixel), expected);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -125,7 +128,7 @@ mod test {
|
|||||||
PixelRGBA{ r: 0x00, g: 0x00, b: 0x00, a: 0xFF}
|
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>>();
|
let result = decoder.collect::<Vec<PixelRGBA>>();
|
||||||
assert_eq!(result, expected);
|
assert_eq!(result, expected);
|
||||||
}
|
}
|
||||||
@@ -144,7 +147,7 @@ mod test {
|
|||||||
PixelRGBA{ r: 0x10, g: 0x20, b: 0x30, a: 0x40 },
|
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();
|
let result: Vec<PixelRGBA> = decoder.collect();
|
||||||
assert_eq!(result, expected);
|
assert_eq!(result, expected);
|
||||||
}
|
}
|
||||||
@@ -179,7 +182,7 @@ mod test {
|
|||||||
PixelRGBA::zero(),
|
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();
|
let result: Vec<PixelRGBA> = decoder.collect();
|
||||||
assert_eq!(result, expected);
|
assert_eq!(result, expected);
|
||||||
}
|
}
|
||||||
@@ -210,7 +213,7 @@ mod test {
|
|||||||
PixelRGBA::new(0, 0, 0, 255),
|
PixelRGBA::new(0, 0, 0, 255),
|
||||||
];
|
];
|
||||||
|
|
||||||
let decoder = Decoder::new(&compressed);
|
let decoder = Decoder::new(compressed.into_iter());
|
||||||
let result: Vec<PixelRGBA> = decoder.collect();
|
let result: Vec<PixelRGBA> = decoder.collect();
|
||||||
assert_eq!(result, expected);
|
assert_eq!(result, expected);
|
||||||
}
|
}
|
||||||
@@ -235,7 +238,7 @@ mod test {
|
|||||||
PixelRGBA::new(37, 19, 28, 255),
|
PixelRGBA::new(37, 19, 28, 255),
|
||||||
];
|
];
|
||||||
|
|
||||||
let decoder = Decoder::new(&compressed);
|
let decoder = Decoder::new(compressed.into_iter());
|
||||||
let result: Vec<PixelRGBA> = decoder.collect();
|
let result: Vec<PixelRGBA> = decoder.collect();
|
||||||
assert_eq!(result, expected);
|
assert_eq!(result, expected);
|
||||||
}
|
}
|
||||||
@@ -254,7 +257,7 @@ mod test {
|
|||||||
// lets pretend an encoder did this for some reason. The decoder can still
|
// 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.
|
// 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();
|
let result: Vec<PixelRGBA> = decoder.collect();
|
||||||
assert_eq!(result, expected);
|
assert_eq!(result, expected);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user