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, } }