From 8ccf6bd475baf0a46de70dd5f040a0b26eeef059 Mon Sep 17 00:00:00 2001 From: Robert Garrett Date: Mon, 13 Oct 2025 10:47:25 -0500 Subject: [PATCH] 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. --- src/decoder.rs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/decoder.rs b/src/decoder.rs index ad4ba28..aa11ca5 100644 --- a/src/decoder.rs +++ b/src/decoder.rs @@ -70,6 +70,8 @@ pub struct Decoder> { bytes: I, run_len: u8, + // Counter to ensure we stop after getting to Self::width * Self::height pixels + output_count: usize, } impl Decoder @@ -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 { + // 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, } }