png/decoder/
read_decoder.rs
1use super::stream::{
2 DecodeOptions, Decoded, DecodingError, FormatErrorInner, StreamingDecoder, CHUNK_BUFFER_SIZE,
3};
4use super::Limits;
5
6use std::io::{BufRead, BufReader, ErrorKind, Read};
7
8use crate::chunk;
9use crate::common::Info;
10
11pub(crate) struct ReadDecoder<R: Read> {
21 reader: BufReader<R>,
22 decoder: StreamingDecoder,
23}
24
25impl<R: Read> ReadDecoder<R> {
26 pub fn new(r: R) -> Self {
27 Self {
28 reader: BufReader::with_capacity(CHUNK_BUFFER_SIZE, r),
29 decoder: StreamingDecoder::new(),
30 }
31 }
32
33 pub fn with_options(r: R, options: DecodeOptions) -> Self {
34 let mut decoder = StreamingDecoder::new_with_options(options);
35 decoder.limits = Limits::default();
36
37 Self {
38 reader: BufReader::with_capacity(CHUNK_BUFFER_SIZE, r),
39 decoder,
40 }
41 }
42
43 pub fn set_limits(&mut self, limits: Limits) {
44 self.decoder.limits = limits;
45 }
46
47 pub fn reserve_bytes(&mut self, bytes: usize) -> Result<(), DecodingError> {
48 self.decoder.limits.reserve_bytes(bytes)
49 }
50
51 pub fn set_ignore_text_chunk(&mut self, ignore_text_chunk: bool) {
52 self.decoder.set_ignore_text_chunk(ignore_text_chunk);
53 }
54
55 pub fn set_ignore_iccp_chunk(&mut self, ignore_iccp_chunk: bool) {
56 self.decoder.set_ignore_iccp_chunk(ignore_iccp_chunk);
57 }
58
59 pub fn ignore_checksums(&mut self, ignore_checksums: bool) {
60 self.decoder.set_ignore_adler32(ignore_checksums);
61 self.decoder.set_ignore_crc(ignore_checksums);
62 }
63
64 fn decode_next(&mut self, image_data: &mut Vec<u8>) -> Result<Decoded, DecodingError> {
67 let (consumed, result) = {
68 let buf = self.reader.fill_buf()?;
69 if buf.is_empty() {
70 return Err(DecodingError::IoError(ErrorKind::UnexpectedEof.into()));
71 }
72 self.decoder.update(buf, image_data)?
73 };
74 self.reader.consume(consumed);
75 Ok(result)
76 }
77
78 fn decode_next_without_image_data(&mut self) -> Result<Decoded, DecodingError> {
79 let mut buf = Vec::new();
83 let state = self.decode_next(&mut buf)?;
84 assert!(buf.is_empty());
85 Ok(state)
86 }
87
88 fn decode_next_and_discard_image_data(&mut self) -> Result<Decoded, DecodingError> {
89 let mut to_be_discarded = Vec::new();
90 self.decode_next(&mut to_be_discarded)
91 }
92
93 pub fn read_header_info(&mut self) -> Result<&Info<'static>, DecodingError> {
97 while self.info().is_none() {
98 if let Decoded::ImageEnd = self.decode_next_without_image_data()? {
99 unreachable!()
100 }
101 }
102 Ok(self.info().unwrap())
103 }
104
105 pub fn read_until_image_data(&mut self) -> Result<(), DecodingError> {
109 loop {
110 match self.decode_next_without_image_data()? {
111 Decoded::ChunkBegin(_, chunk::IDAT) | Decoded::ChunkBegin(_, chunk::fdAT) => break,
112 Decoded::ImageEnd => {
113 return Err(DecodingError::Format(
114 FormatErrorInner::MissingImageData.into(),
115 ))
116 }
117 _ => {}
120 }
121 }
122 Ok(())
123 }
124
125 pub fn decode_image_data(
130 &mut self,
131 image_data: &mut Vec<u8>,
132 ) -> Result<ImageDataCompletionStatus, DecodingError> {
133 match self.decode_next(image_data)? {
134 Decoded::ImageData => Ok(ImageDataCompletionStatus::ExpectingMoreData),
135 Decoded::ImageDataFlushed => Ok(ImageDataCompletionStatus::Done),
136 Decoded::Nothing
138 | Decoded::ChunkComplete(_, _)
139 | Decoded::ChunkBegin(_, _)
140 | Decoded::PartialChunk(_) => Ok(ImageDataCompletionStatus::ExpectingMoreData),
141 unexpected => unreachable!("{:?}", unexpected),
144 }
145 }
146
147 pub fn finish_decoding_image_data(&mut self) -> Result<(), DecodingError> {
151 loop {
152 let mut to_be_discarded = vec![];
153 if let ImageDataCompletionStatus::Done = self.decode_image_data(&mut to_be_discarded)? {
154 return Ok(());
155 }
156 }
157 }
158
159 pub fn read_until_end_of_input(&mut self) -> Result<(), DecodingError> {
163 while !matches!(
164 self.decode_next_and_discard_image_data()?,
165 Decoded::ImageEnd
166 ) {}
167 Ok(())
168 }
169
170 pub fn info(&self) -> Option<&Info<'static>> {
171 self.decoder.info.as_ref()
172 }
173}
174
175#[derive(Debug, Eq, PartialEq)]
176pub(crate) enum ImageDataCompletionStatus {
177 ExpectingMoreData,
178 Done,
179}