Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion examples/explode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
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();
Expand Down
40 changes: 19 additions & 21 deletions src/reader/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<R: Read>(self, r: R) -> Result<Decoder<R>, DecodingError> {
/// [`BufRead`].
pub fn read_info<R: BufRead>(self, r: R) -> Result<Decoder<R>, DecodingError> {
Decoder::with_no_init(r, StreamingDecoder::with_options(&self), self).init()
}
}

struct ReadDecoder<R: Read> {
reader: io::BufReader<R>,
struct ReadDecoder<R: BufRead> {
reader: R,
decoder: StreamingDecoder,
at_eof: bool,
}

impl<R: Read> ReadDecoder<R> {
impl<R: BufRead> ReadDecoder<R> {
#[inline(never)]
fn decode_next(
&mut self,
Expand All @@ -227,7 +228,7 @@ impl<R: Read> ReadDecoder<R> {
Ok(None)
}

fn into_inner(self) -> io::BufReader<R> {
fn into_inner(self) -> R {
self.reader
}

Expand All @@ -242,7 +243,7 @@ impl<R: Read> ReadDecoder<R> {

#[allow(dead_code)]
/// GIF decoder. Create [`DecodeOptions`] to get started, and call [`DecodeOptions::read_info`].
pub struct Decoder<R: Read> {
pub struct Decoder<R: BufRead> {
decoder: ReadDecoder<R>,
pixel_converter: PixelConverter,
bg_color: Option<u8>,
Expand All @@ -251,10 +252,7 @@ pub struct Decoder<R: Read> {
current_frame_data_type: FrameDataType,
}

impl<R> Decoder<R>
where
R: Read,
{
impl<R: BufRead> Decoder<R> {
/// Create a new decoder with default options.
#[inline]
pub fn new(reader: R) -> Result<Self, DecodingError> {
Expand All @@ -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,
},
Expand Down Expand Up @@ -469,8 +467,8 @@ where
self.decoder.decoder.height()
}

/// Abort decoding and recover the `io::Read` instance
pub fn into_inner(self) -> io::BufReader<R> {
/// Abort decoding and recover the `io::BufRead` instance
pub fn into_inner(self) -> R {
self.decoder.into_inner()
}

Expand All @@ -489,7 +487,7 @@ where
}
}

impl<R: Read> IntoIterator for Decoder<R> {
impl<R: BufRead> IntoIterator for Decoder<R> {
type Item = Result<Frame<'static>, DecodingError>;
type IntoIter = DecoderIter<R>;

Expand All @@ -503,23 +501,23 @@ impl<R: Read> IntoIterator for Decoder<R> {
}

/// Use `decoder.into_iter()` to iterate over the frames
pub struct DecoderIter<R: Read> {
pub struct DecoderIter<R: BufRead> {
inner: Decoder<R>,
ended: bool,
}

impl<R: Read> DecoderIter<R> {
/// Abort decoding and recover the `io::Read` instance
impl<R: BufRead> DecoderIter<R> {
/// 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<R> {
pub fn into_inner(self) -> R {
self.inner.into_inner()
}
}

impl<R: Read> FusedIterator for DecoderIter<R> {}
impl<R: BufRead> FusedIterator for DecoderIter<R> {}

impl<R: Read> Iterator for DecoderIter<R> {
impl<R: BufRead> Iterator for DecoderIter<R> {
type Item = Result<Frame<'static>, DecodingError>;

fn next(&mut self) -> Option<Self::Item> {
Expand Down
2 changes: 1 addition & 1 deletion tests/check_testimages.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
5 changes: 3 additions & 2 deletions tests/decode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, &[
Expand Down Expand Up @@ -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]
Expand Down
3 changes: 2 additions & 1 deletion tests/roundtrip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading