use std::fs::File;
use std::io::{self, BufRead, BufReader, Cursor, Read, Seek, SeekFrom};
use std::path::Path;
use crate::dynimage::DynamicImage;
use crate::error::{ImageFormatHint, UnsupportedError, UnsupportedErrorKind};
use crate::image::ImageFormat;
use crate::{ImageDecoder, ImageError, ImageResult};
use super::free_functions;
pub struct Reader<R: Read + Seek> {
inner: R,
format: Option<ImageFormat>,
limits: super::Limits,
}
impl<'a, R: 'a + BufRead + Seek> Reader<R> {
pub fn new(buffered_reader: R) -> Self {
Reader {
inner: buffered_reader,
format: None,
limits: super::Limits::default(),
}
}
pub fn with_format(buffered_reader: R, format: ImageFormat) -> Self {
Reader {
inner: buffered_reader,
format: Some(format),
limits: super::Limits::default(),
}
}
pub fn format(&self) -> Option<ImageFormat> {
self.format
}
pub fn set_format(&mut self, format: ImageFormat) {
self.format = Some(format);
}
pub fn clear_format(&mut self) {
self.format = None;
}
pub fn no_limits(&mut self) {
self.limits = super::Limits::no_limits();
}
pub fn limits(&mut self, limits: super::Limits) {
self.limits = limits;
}
pub fn into_inner(self) -> R {
self.inner
}
fn make_decoder(
format: ImageFormat,
reader: R,
limits_for_png: super::Limits,
) -> ImageResult<Box<dyn ImageDecoder + 'a>> {
#[allow(unused)]
use crate::codecs::*;
#[allow(unreachable_patterns)]
Ok(match format {
#[cfg(feature = "avif-native")]
ImageFormat::Avif => Box::new(avif::AvifDecoder::new(reader)?),
#[cfg(feature = "png")]
ImageFormat::Png => Box::new(png::PngDecoder::with_limits(reader, limits_for_png)?),
#[cfg(feature = "gif")]
ImageFormat::Gif => Box::new(gif::GifDecoder::new(reader)?),
#[cfg(feature = "jpeg")]
ImageFormat::Jpeg => Box::new(jpeg::JpegDecoder::new(reader)?),
#[cfg(feature = "webp")]
ImageFormat::WebP => Box::new(webp::WebPDecoder::new(reader)?),
#[cfg(feature = "tiff")]
ImageFormat::Tiff => Box::new(tiff::TiffDecoder::new(reader)?),
#[cfg(feature = "tga")]
ImageFormat::Tga => Box::new(tga::TgaDecoder::new(reader)?),
#[cfg(feature = "dds")]
ImageFormat::Dds => Box::new(dds::DdsDecoder::new(reader)?),
#[cfg(feature = "bmp")]
ImageFormat::Bmp => Box::new(bmp::BmpDecoder::new(reader)?),
#[cfg(feature = "ico")]
ImageFormat::Ico => Box::new(ico::IcoDecoder::new(reader)?),
#[cfg(feature = "hdr")]
ImageFormat::Hdr => Box::new(hdr::HdrDecoder::new(reader)?),
#[cfg(feature = "exr")]
ImageFormat::OpenExr => Box::new(openexr::OpenExrDecoder::new(reader)?),
#[cfg(feature = "pnm")]
ImageFormat::Pnm => Box::new(pnm::PnmDecoder::new(reader)?),
#[cfg(feature = "ff")]
ImageFormat::Farbfeld => Box::new(farbfeld::FarbfeldDecoder::new(reader)?),
#[cfg(feature = "qoi")]
ImageFormat::Qoi => Box::new(qoi::QoiDecoder::new(reader)?),
format => {
return Err(ImageError::Unsupported(
ImageFormatHint::Exact(format).into(),
))
}
})
}
pub fn into_decoder(mut self) -> ImageResult<impl ImageDecoder + 'a> {
let mut decoder =
Self::make_decoder(self.require_format()?, self.inner, self.limits.clone())?;
decoder.set_limits(self.limits)?;
Ok(decoder)
}
pub fn with_guessed_format(mut self) -> io::Result<Self> {
let format = self.guess_format()?;
self.format = format.or(self.format);
Ok(self)
}
fn guess_format(&mut self) -> io::Result<Option<ImageFormat>> {
let mut start = [0; 16];
let cur = self.inner.stream_position()?;
let len = io::copy(
&mut self.inner.by_ref().take(16),
&mut Cursor::new(&mut start[..]),
)?;
self.inner.seek(SeekFrom::Start(cur))?;
Ok(free_functions::guess_format_impl(&start[..len as usize]))
}
pub fn into_dimensions(self) -> ImageResult<(u32, u32)> {
self.into_decoder().map(|d| d.dimensions())
}
pub fn decode(mut self) -> ImageResult<DynamicImage> {
let format = self.require_format()?;
let mut limits = self.limits;
let mut decoder = Self::make_decoder(format, self.inner, limits.clone())?;
limits.reserve(decoder.total_bytes())?;
decoder.set_limits(limits)?;
DynamicImage::from_decoder(decoder)
}
fn require_format(&mut self) -> ImageResult<ImageFormat> {
self.format.ok_or_else(|| {
ImageError::Unsupported(UnsupportedError::from_format_and_kind(
ImageFormatHint::Unknown,
UnsupportedErrorKind::Format(ImageFormatHint::Unknown),
))
})
}
}
impl Reader<BufReader<File>> {
pub fn open<P>(path: P) -> io::Result<Self>
where
P: AsRef<Path>,
{
Self::open_impl(path.as_ref())
}
fn open_impl(path: &Path) -> io::Result<Self> {
Ok(Reader {
inner: BufReader::new(File::open(path)?),
format: ImageFormat::from_path(path).ok(),
limits: super::Limits::default(),
})
}
}