use super::stream::{
DecodeOptions, Decoded, DecodingError, FormatErrorInner, StreamingDecoder, CHUNK_BUFFER_SIZE,
};
use super::Limits;
use std::io::{BufRead, BufReader, ErrorKind, Read};
use crate::chunk;
use crate::common::Info;
pub(crate) struct ReadDecoder<R: Read> {
reader: BufReader<R>,
decoder: StreamingDecoder,
}
impl<R: Read> ReadDecoder<R> {
pub fn new(r: R) -> Self {
Self {
reader: BufReader::with_capacity(CHUNK_BUFFER_SIZE, r),
decoder: StreamingDecoder::new(),
}
}
pub fn with_options(r: R, options: DecodeOptions) -> Self {
let mut decoder = StreamingDecoder::new_with_options(options);
decoder.limits = Limits::default();
Self {
reader: BufReader::with_capacity(CHUNK_BUFFER_SIZE, r),
decoder,
}
}
pub fn set_limits(&mut self, limits: Limits) {
self.decoder.limits = limits;
}
pub fn reserve_bytes(&mut self, bytes: usize) -> Result<(), DecodingError> {
self.decoder.limits.reserve_bytes(bytes)
}
pub fn set_ignore_text_chunk(&mut self, ignore_text_chunk: bool) {
self.decoder.set_ignore_text_chunk(ignore_text_chunk);
}
pub fn set_ignore_iccp_chunk(&mut self, ignore_iccp_chunk: bool) {
self.decoder.set_ignore_iccp_chunk(ignore_iccp_chunk);
}
pub fn ignore_checksums(&mut self, ignore_checksums: bool) {
self.decoder.set_ignore_adler32(ignore_checksums);
self.decoder.set_ignore_crc(ignore_checksums);
}
fn decode_next(&mut self, image_data: &mut Vec<u8>) -> Result<Decoded, DecodingError> {
let (consumed, result) = {
let buf = self.reader.fill_buf()?;
if buf.is_empty() {
return Err(DecodingError::IoError(ErrorKind::UnexpectedEof.into()));
}
self.decoder.update(buf, image_data)?
};
self.reader.consume(consumed);
Ok(result)
}
fn decode_next_without_image_data(&mut self) -> Result<Decoded, DecodingError> {
let mut buf = Vec::new();
let state = self.decode_next(&mut buf)?;
assert!(buf.is_empty());
Ok(state)
}
fn decode_next_and_discard_image_data(&mut self) -> Result<Decoded, DecodingError> {
let mut to_be_discarded = Vec::new();
self.decode_next(&mut to_be_discarded)
}
pub fn read_header_info(&mut self) -> Result<&Info<'static>, DecodingError> {
while self.info().is_none() {
if let Decoded::ImageEnd = self.decode_next_without_image_data()? {
unreachable!()
}
}
Ok(self.info().unwrap())
}
pub fn read_until_image_data(&mut self) -> Result<(), DecodingError> {
loop {
match self.decode_next_without_image_data()? {
Decoded::ChunkBegin(_, chunk::IDAT) | Decoded::ChunkBegin(_, chunk::fdAT) => break,
Decoded::ImageEnd => {
return Err(DecodingError::Format(
FormatErrorInner::MissingImageData.into(),
))
}
_ => {}
}
}
Ok(())
}
pub fn decode_image_data(
&mut self,
image_data: &mut Vec<u8>,
) -> Result<ImageDataCompletionStatus, DecodingError> {
match self.decode_next(image_data)? {
Decoded::ImageData => Ok(ImageDataCompletionStatus::ExpectingMoreData),
Decoded::ImageDataFlushed => Ok(ImageDataCompletionStatus::Done),
Decoded::Nothing
| Decoded::ChunkComplete(_, _)
| Decoded::ChunkBegin(_, _)
| Decoded::PartialChunk(_) => Ok(ImageDataCompletionStatus::ExpectingMoreData),
unexpected => unreachable!("{:?}", unexpected),
}
}
pub fn finish_decoding_image_data(&mut self) -> Result<(), DecodingError> {
loop {
let mut to_be_discarded = vec![];
if let ImageDataCompletionStatus::Done = self.decode_image_data(&mut to_be_discarded)? {
return Ok(());
}
}
}
pub fn read_until_end_of_input(&mut self) -> Result<(), DecodingError> {
while !matches!(
self.decode_next_and_discard_image_data()?,
Decoded::ImageEnd
) {}
Ok(())
}
pub fn info(&self) -> Option<&Info<'static>> {
self.decoder.info.as_ref()
}
}
#[derive(Debug, Eq, PartialEq)]
pub(crate) enum ImageDataCompletionStatus {
ExpectingMoreData,
Done,
}