Track decode progress and stop at W*H pixels
I was thinking about doing some partial parsing where I retrieve a few extra bytes to see if it's a footer, but that's really hard with an iterator as an input stream. Instead, I'm just going to count how many pixels the decoder has output and stop iterating at that point. Now to fix the test cases that don't have an image size assigned.
This commit is contained in:
@@ -70,6 +70,8 @@ pub struct Decoder<I: Iterator<Item = u8>> {
|
||||
|
||||
bytes: I,
|
||||
run_len: u8,
|
||||
// Counter to ensure we stop after getting to Self::width * Self::height pixels
|
||||
output_count: usize,
|
||||
}
|
||||
|
||||
impl<I> Decoder<I>
|
||||
@@ -87,6 +89,7 @@ where
|
||||
prev_pixel: PixelRGBA { r:0, g:0, b:0, a: 255},
|
||||
bytes,
|
||||
run_len: 0,
|
||||
output_count: 0,
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -98,6 +101,13 @@ where
|
||||
type Item = PixelRGBA;
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
// count pixels. When we reach WIDTH x HEIGHT, check for footer and quit.
|
||||
if self.output_count == (self.width * self.height) as usize {
|
||||
return None;
|
||||
} else {
|
||||
self.output_count += 1;
|
||||
}
|
||||
|
||||
if self.run_len > 0 {
|
||||
self.run_len -= 1;
|
||||
Some(self.prev_pixel)
|
||||
@@ -206,6 +216,7 @@ mod test {
|
||||
},
|
||||
bytes,
|
||||
run_len: 0,
|
||||
output_count: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -221,6 +232,7 @@ mod test {
|
||||
prev_pixel,
|
||||
bytes,
|
||||
run_len: 0,
|
||||
output_count: 0,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -238,6 +250,7 @@ mod test {
|
||||
prev_pixel: PixelRGBA { r: 0, g: 0, b: 0, a: 255 },
|
||||
bytes,
|
||||
run_len: 0,
|
||||
output_count: 0,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user