1 | //! All IO functionality needed for TIFF decoding |
2 | |
3 | use std::convert::TryFrom; |
4 | use std::io::{self, BufRead, BufReader, Read, Seek, Take}; |
5 | |
6 | /// Byte order of the TIFF file. |
7 | #[derive (Clone, Copy, Debug)] |
8 | pub enum ByteOrder { |
9 | /// little endian byte order |
10 | LittleEndian, |
11 | /// big endian byte order |
12 | BigEndian, |
13 | } |
14 | |
15 | /// Reader that is aware of the byte order. |
16 | pub trait EndianReader: Read { |
17 | /// Byte order that should be adhered to |
18 | fn byte_order(&self) -> ByteOrder; |
19 | |
20 | /// Reads an u16 |
21 | #[inline (always)] |
22 | fn read_u16(&mut self) -> Result<u16, io::Error> { |
23 | let mut n = [0u8; 2]; |
24 | self.read_exact(&mut n)?; |
25 | Ok(match self.byte_order() { |
26 | ByteOrder::LittleEndian => u16::from_le_bytes(n), |
27 | ByteOrder::BigEndian => u16::from_be_bytes(n), |
28 | }) |
29 | } |
30 | |
31 | /// Reads an i8 |
32 | #[inline (always)] |
33 | fn read_i8(&mut self) -> Result<i8, io::Error> { |
34 | let mut n = [0u8; 1]; |
35 | self.read_exact(&mut n)?; |
36 | Ok(match self.byte_order() { |
37 | ByteOrder::LittleEndian => i8::from_le_bytes(n), |
38 | ByteOrder::BigEndian => i8::from_be_bytes(n), |
39 | }) |
40 | } |
41 | |
42 | /// Reads an i16 |
43 | #[inline (always)] |
44 | fn read_i16(&mut self) -> Result<i16, io::Error> { |
45 | let mut n = [0u8; 2]; |
46 | self.read_exact(&mut n)?; |
47 | Ok(match self.byte_order() { |
48 | ByteOrder::LittleEndian => i16::from_le_bytes(n), |
49 | ByteOrder::BigEndian => i16::from_be_bytes(n), |
50 | }) |
51 | } |
52 | |
53 | /// Reads an u32 |
54 | #[inline (always)] |
55 | fn read_u32(&mut self) -> Result<u32, io::Error> { |
56 | let mut n = [0u8; 4]; |
57 | self.read_exact(&mut n)?; |
58 | Ok(match self.byte_order() { |
59 | ByteOrder::LittleEndian => u32::from_le_bytes(n), |
60 | ByteOrder::BigEndian => u32::from_be_bytes(n), |
61 | }) |
62 | } |
63 | |
64 | /// Reads an i32 |
65 | #[inline (always)] |
66 | fn read_i32(&mut self) -> Result<i32, io::Error> { |
67 | let mut n = [0u8; 4]; |
68 | self.read_exact(&mut n)?; |
69 | Ok(match self.byte_order() { |
70 | ByteOrder::LittleEndian => i32::from_le_bytes(n), |
71 | ByteOrder::BigEndian => i32::from_be_bytes(n), |
72 | }) |
73 | } |
74 | |
75 | /// Reads an u64 |
76 | #[inline (always)] |
77 | fn read_u64(&mut self) -> Result<u64, io::Error> { |
78 | let mut n = [0u8; 8]; |
79 | self.read_exact(&mut n)?; |
80 | Ok(match self.byte_order() { |
81 | ByteOrder::LittleEndian => u64::from_le_bytes(n), |
82 | ByteOrder::BigEndian => u64::from_be_bytes(n), |
83 | }) |
84 | } |
85 | |
86 | /// Reads an i64 |
87 | #[inline (always)] |
88 | fn read_i64(&mut self) -> Result<i64, io::Error> { |
89 | let mut n = [0u8; 8]; |
90 | self.read_exact(&mut n)?; |
91 | Ok(match self.byte_order() { |
92 | ByteOrder::LittleEndian => i64::from_le_bytes(n), |
93 | ByteOrder::BigEndian => i64::from_be_bytes(n), |
94 | }) |
95 | } |
96 | |
97 | /// Reads an f32 |
98 | #[inline (always)] |
99 | fn read_f32(&mut self) -> Result<f32, io::Error> { |
100 | let mut n = [0u8; 4]; |
101 | self.read_exact(&mut n)?; |
102 | Ok(f32::from_bits(match self.byte_order() { |
103 | ByteOrder::LittleEndian => u32::from_le_bytes(n), |
104 | ByteOrder::BigEndian => u32::from_be_bytes(n), |
105 | })) |
106 | } |
107 | |
108 | /// Reads an f64 |
109 | #[inline (always)] |
110 | fn read_f64(&mut self) -> Result<f64, io::Error> { |
111 | let mut n = [0u8; 8]; |
112 | self.read_exact(&mut n)?; |
113 | Ok(f64::from_bits(match self.byte_order() { |
114 | ByteOrder::LittleEndian => u64::from_le_bytes(n), |
115 | ByteOrder::BigEndian => u64::from_be_bytes(n), |
116 | })) |
117 | } |
118 | } |
119 | |
120 | /// |
121 | /// # READERS |
122 | /// |
123 | |
124 | /// |
125 | /// ## Deflate Reader |
126 | /// |
127 | |
128 | pub type DeflateReader<R> = flate2::read::ZlibDecoder<R>; |
129 | |
130 | /// |
131 | /// ## LZW Reader |
132 | /// |
133 | |
134 | /// Reader that decompresses LZW streams |
135 | pub struct LZWReader<R: Read> { |
136 | reader: BufReader<Take<R>>, |
137 | decoder: weezl::decode::Decoder, |
138 | } |
139 | |
140 | impl<R: Read> LZWReader<R> { |
141 | /// Wraps a reader |
142 | pub fn new(reader: R, compressed_length: usize) -> LZWReader<R> { |
143 | Self { |
144 | reader: BufReader::with_capacity( |
145 | (32 * 1024).min(compressed_length), |
146 | inner:reader.take(limit:u64::try_from(compressed_length).unwrap()), |
147 | ), |
148 | decoder: weezl::decode::Decoder::with_tiff_size_switch(order:weezl::BitOrder::Msb, size:8), |
149 | } |
150 | } |
151 | } |
152 | |
153 | impl<R: Read> Read for LZWReader<R> { |
154 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
155 | loop { |
156 | let result = self.decoder.decode_bytes(self.reader.fill_buf()?, buf); |
157 | self.reader.consume(result.consumed_in); |
158 | |
159 | match result.status { |
160 | Ok(weezl::LzwStatus::Ok) => { |
161 | if result.consumed_out == 0 { |
162 | continue; |
163 | } else { |
164 | return Ok(result.consumed_out); |
165 | } |
166 | } |
167 | Ok(weezl::LzwStatus::NoProgress) => { |
168 | assert_eq!(result.consumed_in, 0); |
169 | assert_eq!(result.consumed_out, 0); |
170 | assert!(self.reader.buffer().is_empty()); |
171 | return Err(io::Error::new( |
172 | io::ErrorKind::UnexpectedEof, |
173 | "no lzw end code found" , |
174 | )); |
175 | } |
176 | Ok(weezl::LzwStatus::Done) => { |
177 | return Ok(result.consumed_out); |
178 | } |
179 | Err(err) => return Err(io::Error::new(io::ErrorKind::InvalidData, err)), |
180 | } |
181 | } |
182 | } |
183 | } |
184 | |
185 | /// |
186 | /// ## PackBits Reader |
187 | /// |
188 | |
189 | enum PackBitsReaderState { |
190 | Header, |
191 | Literal, |
192 | Repeat { value: u8 }, |
193 | } |
194 | |
195 | /// Reader that unpacks Apple's `PackBits` format |
196 | pub struct PackBitsReader<R: Read> { |
197 | reader: Take<R>, |
198 | state: PackBitsReaderState, |
199 | count: usize, |
200 | } |
201 | |
202 | impl<R: Read> PackBitsReader<R> { |
203 | /// Wraps a reader |
204 | pub fn new(reader: R, length: u64) -> Self { |
205 | Self { |
206 | reader: reader.take(limit:length), |
207 | state: PackBitsReaderState::Header, |
208 | count: 0, |
209 | } |
210 | } |
211 | } |
212 | |
213 | impl<R: Read> Read for PackBitsReader<R> { |
214 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
215 | while let PackBitsReaderState::Header = self.state { |
216 | if self.reader.limit() == 0 { |
217 | return Ok(0); |
218 | } |
219 | let mut header: [u8; 1] = [0]; |
220 | self.reader.read_exact(&mut header)?; |
221 | let h = header[0] as i8; |
222 | if (-127..=-1).contains(&h) { |
223 | let mut data: [u8; 1] = [0]; |
224 | self.reader.read_exact(&mut data)?; |
225 | self.state = PackBitsReaderState::Repeat { value: data[0] }; |
226 | self.count = (1 - h as isize) as usize; |
227 | } else if h >= 0 { |
228 | self.state = PackBitsReaderState::Literal; |
229 | self.count = h as usize + 1; |
230 | } else { |
231 | // h = -128 is a no-op. |
232 | } |
233 | } |
234 | |
235 | let length = buf.len().min(self.count); |
236 | let actual = match self.state { |
237 | PackBitsReaderState::Literal => self.reader.read(&mut buf[..length])?, |
238 | PackBitsReaderState::Repeat { value } => { |
239 | for b in &mut buf[..length] { |
240 | *b = value; |
241 | } |
242 | |
243 | length |
244 | } |
245 | PackBitsReaderState::Header => unreachable!(), |
246 | }; |
247 | |
248 | self.count -= actual; |
249 | if self.count == 0 { |
250 | self.state = PackBitsReaderState::Header; |
251 | } |
252 | Ok(actual) |
253 | } |
254 | } |
255 | |
256 | /// |
257 | /// ## SmartReader Reader |
258 | /// |
259 | |
260 | /// Reader that is aware of the byte order. |
261 | #[derive (Debug)] |
262 | pub struct SmartReader<R> |
263 | where |
264 | R: Read, |
265 | { |
266 | reader: R, |
267 | pub byte_order: ByteOrder, |
268 | } |
269 | |
270 | impl<R> SmartReader<R> |
271 | where |
272 | R: Read, |
273 | { |
274 | /// Wraps a reader |
275 | pub fn wrap(reader: R, byte_order: ByteOrder) -> SmartReader<R> { |
276 | SmartReader { reader, byte_order } |
277 | } |
278 | pub fn into_inner(self) -> R { |
279 | self.reader |
280 | } |
281 | } |
282 | impl<R: Read + Seek> SmartReader<R> { |
283 | pub fn goto_offset(&mut self, offset: u64) -> io::Result<()> { |
284 | self.seek(io::SeekFrom::Start(offset)).map(|_| ()) |
285 | } |
286 | } |
287 | |
288 | impl<R> EndianReader for SmartReader<R> |
289 | where |
290 | R: Read, |
291 | { |
292 | #[inline (always)] |
293 | fn byte_order(&self) -> ByteOrder { |
294 | self.byte_order |
295 | } |
296 | } |
297 | |
298 | impl<R: Read> Read for SmartReader<R> { |
299 | #[inline ] |
300 | fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> { |
301 | self.reader.read(buf) |
302 | } |
303 | } |
304 | |
305 | impl<R: Read + Seek> Seek for SmartReader<R> { |
306 | #[inline ] |
307 | fn seek(&mut self, pos: io::SeekFrom) -> io::Result<u64> { |
308 | self.reader.seek(pos) |
309 | } |
310 | } |
311 | |
312 | #[cfg (test)] |
313 | mod test { |
314 | use super::*; |
315 | |
316 | #[test ] |
317 | fn test_packbits() { |
318 | let encoded = vec![ |
319 | 0xFE, 0xAA, 0x02, 0x80, 0x00, 0x2A, 0xFD, 0xAA, 0x03, 0x80, 0x00, 0x2A, 0x22, 0xF7, |
320 | 0xAA, |
321 | ]; |
322 | let encoded_len = encoded.len(); |
323 | |
324 | let buff = io::Cursor::new(encoded); |
325 | let mut decoder = PackBitsReader::new(buff, encoded_len as u64); |
326 | |
327 | let mut decoded = Vec::new(); |
328 | decoder.read_to_end(&mut decoded).unwrap(); |
329 | |
330 | let expected = vec![ |
331 | 0xAA, 0xAA, 0xAA, 0x80, 0x00, 0x2A, 0xAA, 0xAA, 0xAA, 0xAA, 0x80, 0x00, 0x2A, 0x22, |
332 | 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, |
333 | ]; |
334 | assert_eq!(decoded, expected); |
335 | } |
336 | } |
337 | |