diff --git a/examples/check.rs b/examples/check.rs index ea288f2..b6948f8 100644 --- a/examples/check.rs +++ b/examples/check.rs @@ -6,7 +6,7 @@ fn main() { let mut reader = { let mut options = gif::DecodeOptions::new(); options.allow_unknown_blocks(true); - options.read_info(file).unwrap() + options.read_info(std::io::BufReader::new(file)).unwrap() }; loop { diff --git a/examples/explode.rs b/examples/explode.rs index 3eacc08..15aee4b 100644 --- a/examples/explode.rs +++ b/examples/explode.rs @@ -14,7 +14,7 @@ fn main() -> Result<(), Box> { let input = File::open(&input_path)?; let mut options = gif::DecodeOptions::new(); options.set_color_output(gif::ColorOutput::Indexed); - let mut decoder = options.read_info(input)?; + let mut decoder = options.read_info(std::io::BufReader::new(input))?; let screen_width = decoder.width(); let screen_height = decoder.height(); let global_pal = decoder.global_palette().unwrap_or_default().to_vec(); diff --git a/src/reader/mod.rs b/src/reader/mod.rs index 17310e5..5a816b3 100644 --- a/src/reader/mod.rs +++ b/src/reader/mod.rs @@ -5,7 +5,7 @@ use core::iter::FusedIterator; use core::mem; use core::num::NonZeroU64; use std::io; -use std::io::prelude::*; +use std::io::BufRead; use crate::common::{Block, Frame}; use crate::Repeat; @@ -189,18 +189,19 @@ impl DecodeOptions { /// Reads the logical screen descriptor including the global color palette /// /// Returns a [`Decoder`]. All decoder configuration has to be done beforehand. - pub fn read_info(self, r: R) -> Result, DecodingError> { + /// [`BufRead`]. + pub fn read_info(self, r: R) -> Result, DecodingError> { Decoder::with_no_init(r, StreamingDecoder::with_options(&self), self).init() } } -struct ReadDecoder { - reader: io::BufReader, +struct ReadDecoder { + reader: R, decoder: StreamingDecoder, at_eof: bool, } -impl ReadDecoder { +impl ReadDecoder { #[inline(never)] fn decode_next( &mut self, @@ -227,7 +228,7 @@ impl ReadDecoder { Ok(None) } - fn into_inner(self) -> io::BufReader { + fn into_inner(self) -> R { self.reader } @@ -242,7 +243,7 @@ impl ReadDecoder { #[allow(dead_code)] /// GIF decoder. Create [`DecodeOptions`] to get started, and call [`DecodeOptions::read_info`]. -pub struct Decoder { +pub struct Decoder { decoder: ReadDecoder, pixel_converter: PixelConverter, bg_color: Option, @@ -251,10 +252,7 @@ pub struct Decoder { current_frame_data_type: FrameDataType, } -impl Decoder -where - R: Read, -{ +impl Decoder { /// Create a new decoder with default options. #[inline] pub fn new(reader: R) -> Result { @@ -271,7 +269,7 @@ where fn with_no_init(reader: R, decoder: StreamingDecoder, options: DecodeOptions) -> Self { Self { decoder: ReadDecoder { - reader: io::BufReader::new(reader), + reader: reader, decoder, at_eof: false, }, @@ -469,8 +467,8 @@ where self.decoder.decoder.height() } - /// Abort decoding and recover the `io::Read` instance - pub fn into_inner(self) -> io::BufReader { + /// Abort decoding and recover the `io::BufRead` instance + pub fn into_inner(self) -> R { self.decoder.into_inner() } @@ -489,7 +487,7 @@ where } } -impl IntoIterator for Decoder { +impl IntoIterator for Decoder { type Item = Result, DecodingError>; type IntoIter = DecoderIter; @@ -503,23 +501,23 @@ impl IntoIterator for Decoder { } /// Use `decoder.into_iter()` to iterate over the frames -pub struct DecoderIter { +pub struct DecoderIter { inner: Decoder, ended: bool, } -impl DecoderIter { - /// Abort decoding and recover the `io::Read` instance +impl DecoderIter { + /// Abort decoding and recover the `io::BufRead` instance /// /// Use `for frame in iter.by_ref()` to be able to call this afterwards. - pub fn into_inner(self) -> io::BufReader { + pub fn into_inner(self) -> R { self.inner.into_inner() } } -impl FusedIterator for DecoderIter {} +impl FusedIterator for DecoderIter {} -impl Iterator for DecoderIter { +impl Iterator for DecoderIter { type Item = Result, DecodingError>; fn next(&mut self) -> Option { diff --git a/tests/check_testimages.rs b/tests/check_testimages.rs index e3fecc2..b472d6d 100644 --- a/tests/check_testimages.rs +++ b/tests/check_testimages.rs @@ -67,7 +67,7 @@ fn render_images() { let mut decoder = gif::DecodeOptions::new(); decoder.set_color_output(gif::ColorOutput::RGBA); let file = File::open(path)?; - let mut decoder = decoder.read_info(file)?; + let mut decoder = decoder.read_info(std::io::BufReader::new(file))?; let mut crc = Crc32::new(); while let Some(frame) = decoder.read_next_frame()? { // First sanity check: diff --git a/tests/decode.rs b/tests/decode.rs index 8c9f3ce..0667316 100644 --- a/tests/decode.rs +++ b/tests/decode.rs @@ -5,7 +5,8 @@ use std::fs::File; #[test] fn test_simple_indexed() { - let mut decoder = Decoder::new(File::open("tests/samples/sample_1.gif").unwrap()).unwrap(); + let file = File::open("tests/samples/sample_1.gif").unwrap(); + let mut decoder = Decoder::new(std::io::BufReader::new(file)).unwrap(); let frame = decoder.read_next_frame().unwrap().unwrap(); #[rustfmt::skip] assert_eq!(&*frame.buffer, &[ @@ -156,7 +157,7 @@ fn rebuild_without_reencode(image: &[u8]) { assert!(orig.next().is_none()); assert!(rebuilt.next().is_none()); - assert_eq!(0, rebuilt.into_inner().buffer().len()); + assert_eq!(0, rebuilt.into_inner().len()); } #[test] diff --git a/tests/roundtrip.rs b/tests/roundtrip.rs index dd2a97e..a915b13 100644 --- a/tests/roundtrip.rs +++ b/tests/roundtrip.rs @@ -80,7 +80,8 @@ fn round_trip_from_image(original: &[u8]) { let buffer = encoder.into_inner().unwrap(); { - let mut decoder = Decoder::new(&buffer[..]).expect("Invalid info encoded"); + let mut decoder = + Decoder::new(std::io::BufReader::new(&buffer[..])).expect("Invalid info encoded"); assert_eq!(decoder.width(), width); assert_eq!(decoder.height(), height); assert_eq!(decoder.repeat(), repeat);