pub struct Reader<R>where
R: Read,{ /* private fields */ }
Expand description
A multi-format image reader.
Wraps an input reader to facilitate automatic detection of an image’s format, appropriate
decoding method, and dispatches into the set of supported ImageDecoder
implementations.
§Usage
Opening a file, deducing the format based on the file path automatically, and trying to decode the image contained can be performed by constructing the reader and immediately consuming it.
let image = Reader::open("path/to/image.png")?
.decode()?;
It is also possible to make a guess based on the content. This is especially handy if the
source is some blob in memory and you have constructed the reader in another way. Here is an
example with a pnm
black-and-white subformat that encodes its pixel matrix with ascii values.
use std::io::Cursor;
use image::ImageFormat;
let raw_data = b"P1 2 2\n\
0 1\n\
1 0\n";
let mut reader = Reader::new(Cursor::new(raw_data))
.with_guessed_format()
.expect("Cursor io never fails");
assert_eq!(reader.format(), Some(ImageFormat::Pnm));
let image = reader.decode()?;
As a final fallback or if only a specific format must be used, the reader always allows manual
specification of the supposed image format with set_format
.
Implementations§
source§impl<R> Reader<R>where
R: Read,
impl<R> Reader<R>where
R: Read,
sourcepub fn new(buffered_reader: R) -> Reader<R>
pub fn new(buffered_reader: R) -> Reader<R>
Create a new image reader without a preset format.
Assumes the reader is already buffered. For optimal performance,
consider wrapping the reader with a BufReader::new()
.
It is possible to guess the format based on the content of the read object with
with_guessed_format
, or to set the format directly with set_format
.
sourcepub fn with_format(buffered_reader: R, format: ImageFormat) -> Reader<R>
pub fn with_format(buffered_reader: R, format: ImageFormat) -> Reader<R>
Construct a reader with specified format.
Assumes the reader is already buffered. For optimal performance,
consider wrapping the reader with a BufReader::new()
.
sourcepub fn format(&self) -> Option<ImageFormat>
pub fn format(&self) -> Option<ImageFormat>
Get the currently determined format.
sourcepub fn set_format(&mut self, format: ImageFormat)
pub fn set_format(&mut self, format: ImageFormat)
Supply the format as which to interpret the read image.
sourcepub fn clear_format(&mut self)
pub fn clear_format(&mut self)
Remove the current information on the image format.
Note that many operations require format information to be present and will return e.g. an
ImageError::Unsupported
when the image format has not been set.
sourcepub fn into_inner(self) -> R
pub fn into_inner(self) -> R
Unwrap the reader.
source§impl Reader<BufReader<File>>
impl Reader<BufReader<File>>
sourcepub fn open<P>(path: P) -> Result<Reader<BufReader<File>>, Error>
pub fn open<P>(path: P) -> Result<Reader<BufReader<File>>, Error>
Open a file to read, format will be guessed from path.
This will not attempt any io operation on the opened file.
If you want to inspect the content for a better guess on the format, which does not depend
on file extensions, follow this call with a call to with_guessed_format
.
source§impl<R> Reader<R>
impl<R> Reader<R>
sourcepub fn with_guessed_format(self) -> Result<Reader<R>, Error>
pub fn with_guessed_format(self) -> Result<Reader<R>, Error>
Make a format guess based on the content, replacing it on success.
Returns Ok
with the guess if no io error occurs. Additionally, replaces the current
format if the guess was successful. If the guess was unable to determine a format then
the current format of the reader is unchanged.
Returns an error if the underlying reader fails. The format is unchanged. The error is a
std::io::Error
and not ImageError
since the only error case is an error when the
underlying reader seeks.
When an error occurs, the reader may not have been properly reset and it is potentially hazardous to continue with more io.
§Usage
This supplements the path based type deduction from open
with content based deduction.
This is more common in Linux and UNIX operating systems and also helpful if the path can
not be directly controlled.
let image = Reader::open("image.unknown")?
.with_guessed_format()?
.decode()?;
sourcepub fn into_dimensions(self) -> Result<(u32, u32), ImageError>
pub fn into_dimensions(self) -> Result<(u32, u32), ImageError>
Read the image dimensions.
Uses the current format to construct the correct reader for the format.
If no format was determined, returns an ImageError::Unsupported
.
sourcepub fn decode(self) -> Result<DynamicImage, ImageError>
pub fn decode(self) -> Result<DynamicImage, ImageError>
Read the image (replaces load
).
Uses the current format to construct the correct reader for the format.
If no format was determined, returns an ImageError::Unsupported
.
Auto Trait Implementations§
impl<R> Freeze for Reader<R>where
R: Freeze,
impl<R> RefUnwindSafe for Reader<R>where
R: RefUnwindSafe,
impl<R> Send for Reader<R>where
R: Send,
impl<R> Sync for Reader<R>where
R: Sync,
impl<R> Unpin for Reader<R>where
R: Unpin,
impl<R> UnwindSafe for Reader<R>where
R: UnwindSafe,
Blanket Implementations§
source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for Swhere
T: Real + Zero + Arithmetics + Clone,
Swp: WhitePoint<T>,
Dwp: WhitePoint<T>,
D: AdaptFrom<S, Swp, Dwp, T>,
source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<T>,
source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
source§impl<T, Res> Apply<Res> for Twhere
T: ?Sized,
impl<T, Res> Apply<Res> for Twhere
T: ?Sized,
source§impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
impl<T, C> ArraysFrom<C> for Twhere
C: IntoArrays<T>,
source§fn arrays_from(colors: C) -> T
fn arrays_from(colors: C) -> T
source§impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
impl<T, C> ArraysInto<C> for Twhere
C: FromArrays<T>,
source§fn arrays_into(self) -> C
fn arrays_into(self) -> C
source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
source§impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
impl<WpParam, T, U> Cam16IntoUnclamped<WpParam, T> for Uwhere
T: FromCam16Unclamped<WpParam, U>,
source§type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
type Scalar = <T as FromCam16Unclamped<WpParam, U>>::Scalar
parameters
when converting.source§fn cam16_into_unclamped(
self,
parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>,
) -> T
fn cam16_into_unclamped( self, parameters: BakedParameters<WpParam, <U as Cam16IntoUnclamped<WpParam, T>>::Scalar>, ) -> T
self
into C
, using the provided parameters.source§impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
impl<T, C> ComponentsFrom<C> for Twhere
C: IntoComponents<T>,
source§fn components_from(colors: C) -> T
fn components_from(colors: C) -> T
source§impl<T> Downcast for Twhere
T: Any,
impl<T> Downcast for Twhere
T: Any,
source§fn into_any(self: Box<T>) -> Box<dyn Any>
fn into_any(self: Box<T>) -> Box<dyn Any>
Box<dyn Trait>
(where Trait: Downcast
) to Box<dyn Any>
. Box<dyn Any>
can
then be further downcast
into Box<ConcreteType>
where ConcreteType
implements Trait
.source§fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
fn into_any_rc(self: Rc<T>) -> Rc<dyn Any>
Rc<Trait>
(where Trait: Downcast
) to Rc<Any>
. Rc<Any>
can then be
further downcast
into Rc<ConcreteType>
where ConcreteType
implements Trait
.source§fn as_any(&self) -> &(dyn Any + 'static)
fn as_any(&self) -> &(dyn Any + 'static)
&Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &Any
’s vtable from &Trait
’s.source§fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
fn as_any_mut(&mut self) -> &mut (dyn Any + 'static)
&mut Trait
(where Trait: Downcast
) to &Any
. This is needed since Rust cannot
generate &mut Any
’s vtable from &mut Trait
’s.source§impl<T> DowncastSync for T
impl<T> DowncastSync for T
source§impl<T> FromAngle<T> for T
impl<T> FromAngle<T> for T
source§fn from_angle(angle: T) -> T
fn from_angle(angle: T) -> T
angle
.source§impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
impl<T, U> FromStimulus<U> for Twhere
U: IntoStimulus<T>,
source§fn from_stimulus(other: U) -> T
fn from_stimulus(other: U) -> T
other
into Self
, while performing the appropriate scaling,
rounding and clamping.source§impl<T> Instrument for T
impl<T> Instrument for T
source§fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
fn instrument(self, span: Span) -> Instrumented<Self> ⓘ
source§fn in_current_span(self) -> Instrumented<Self> ⓘ
fn in_current_span(self) -> Instrumented<Self> ⓘ
source§impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
impl<T, U> IntoAngle<U> for Twhere
U: FromAngle<T>,
source§fn into_angle(self) -> U
fn into_angle(self) -> U
T
.source§impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
impl<WpParam, T, U> IntoCam16Unclamped<WpParam, T> for Uwhere
T: Cam16FromUnclamped<WpParam, U>,
source§type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
type Scalar = <T as Cam16FromUnclamped<WpParam, U>>::Scalar
parameters
when converting.source§fn into_cam16_unclamped(
self,
parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>,
) -> T
fn into_cam16_unclamped( self, parameters: BakedParameters<WpParam, <U as IntoCam16Unclamped<WpParam, T>>::Scalar>, ) -> T
self
into C
, using the provided parameters.source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
source§fn into_color(self) -> U
fn into_color(self) -> U
source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
source§impl<T> IntoEither for T
impl<T> IntoEither for T
source§fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moresource§impl<T> IntoStimulus<T> for T
impl<T> IntoStimulus<T> for T
source§fn into_stimulus(self) -> T
fn into_stimulus(self) -> T
self
into T
, while performing the appropriate scaling,
rounding and clamping.source§impl<T> Pointable for T
impl<T> Pointable for T
source§impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
impl<T, C> TryComponentsInto<C> for Twhere
C: TryFromComponents<T>,
source§type Error = <C as TryFromComponents<T>>::Error
type Error = <C as TryFromComponents<T>>::Error
try_into_colors
fails to cast.source§fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
fn try_components_into(self) -> Result<C, <T as TryComponentsInto<C>>::Error>
source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds
error is returned which contains
the unclamped color. Read more