/rust/registry/src/index.crates.io-1949cf8c6b5b557f/qoi-0.4.1/src/decode.rs
Line | Count | Source |
1 | | #[cfg(any(feature = "std", feature = "alloc"))] |
2 | | use alloc::{vec, vec::Vec}; |
3 | | #[cfg(feature = "std")] |
4 | | use std::io::Read; |
5 | | |
6 | | // TODO: can be removed once https://github.com/rust-lang/rust/issues/74985 is stable |
7 | | use bytemuck::{cast_slice_mut, Pod}; |
8 | | |
9 | | use crate::consts::{ |
10 | | QOI_HEADER_SIZE, QOI_OP_DIFF, QOI_OP_INDEX, QOI_OP_LUMA, QOI_OP_RGB, QOI_OP_RGBA, QOI_OP_RUN, |
11 | | QOI_PADDING, QOI_PADDING_SIZE, |
12 | | }; |
13 | | use crate::error::{Error, Result}; |
14 | | use crate::header::Header; |
15 | | use crate::pixel::{Pixel, SupportedChannels}; |
16 | | use crate::types::Channels; |
17 | | use crate::utils::{cold, unlikely}; |
18 | | |
19 | | const QOI_OP_INDEX_END: u8 = QOI_OP_INDEX | 0x3f; |
20 | | const QOI_OP_RUN_END: u8 = QOI_OP_RUN | 0x3d; // <- note, 0x3d (not 0x3f) |
21 | | const QOI_OP_DIFF_END: u8 = QOI_OP_DIFF | 0x3f; |
22 | | const QOI_OP_LUMA_END: u8 = QOI_OP_LUMA | 0x3f; |
23 | | |
24 | | #[inline] |
25 | 0 | fn decode_impl_slice<const N: usize, const RGBA: bool>(data: &[u8], out: &mut [u8]) -> Result<usize> |
26 | 0 | where |
27 | 0 | Pixel<N>: SupportedChannels, |
28 | 0 | [u8; N]: Pod, |
29 | | { |
30 | 0 | let mut pixels = cast_slice_mut::<_, [u8; N]>(out); |
31 | 0 | let data_len = data.len(); |
32 | 0 | let mut data = data; |
33 | | |
34 | 0 | let mut index = [Pixel::<4>::new(); 256]; |
35 | 0 | let mut px = Pixel::<N>::new().with_a(0xff); |
36 | | let mut px_rgba: Pixel<4>; |
37 | | |
38 | 0 | while let [px_out, ptail @ ..] = pixels { |
39 | 0 | pixels = ptail; |
40 | 0 | match data { |
41 | 0 | [b1 @ QOI_OP_INDEX..=QOI_OP_INDEX_END, dtail @ ..] => { |
42 | 0 | px_rgba = index[*b1 as usize]; |
43 | 0 | px.update(px_rgba); |
44 | 0 | *px_out = px.into(); |
45 | 0 | data = dtail; |
46 | 0 | continue; |
47 | | } |
48 | 0 | [QOI_OP_RGB, r, g, b, dtail @ ..] => { |
49 | 0 | px.update_rgb(*r, *g, *b); |
50 | 0 | data = dtail; |
51 | 0 | } |
52 | 0 | [QOI_OP_RGBA, r, g, b, a, dtail @ ..] if RGBA => { |
53 | 0 | px.update_rgba(*r, *g, *b, *a); |
54 | 0 | data = dtail; |
55 | 0 | } |
56 | 0 | [b1 @ QOI_OP_RUN..=QOI_OP_RUN_END, dtail @ ..] => { |
57 | 0 | *px_out = px.into(); |
58 | 0 | let run = ((b1 & 0x3f) as usize).min(pixels.len()); |
59 | 0 | let (phead, ptail) = pixels.split_at_mut(run); // can't panic |
60 | 0 | phead.fill(px.into()); |
61 | 0 | pixels = ptail; |
62 | 0 | data = dtail; |
63 | 0 | continue; |
64 | | } |
65 | 0 | [b1 @ QOI_OP_DIFF..=QOI_OP_DIFF_END, dtail @ ..] => { |
66 | 0 | px.update_diff(*b1); |
67 | 0 | data = dtail; |
68 | 0 | } |
69 | 0 | [b1 @ QOI_OP_LUMA..=QOI_OP_LUMA_END, b2, dtail @ ..] => { |
70 | 0 | px.update_luma(*b1, *b2); |
71 | 0 | data = dtail; |
72 | 0 | } |
73 | | _ => { |
74 | 0 | cold(); |
75 | 0 | if unlikely(data.len() < QOI_PADDING_SIZE) { |
76 | 0 | return Err(Error::UnexpectedBufferEnd); |
77 | 0 | } |
78 | | } |
79 | | } |
80 | | |
81 | 0 | px_rgba = px.as_rgba(0xff); |
82 | 0 | index[px_rgba.hash_index() as usize] = px_rgba; |
83 | 0 | *px_out = px.into(); |
84 | | } |
85 | | |
86 | 0 | if unlikely(data.len() < QOI_PADDING_SIZE) { |
87 | 0 | return Err(Error::UnexpectedBufferEnd); |
88 | 0 | } else if unlikely(data[..QOI_PADDING_SIZE] != QOI_PADDING) { |
89 | 0 | return Err(Error::InvalidPadding); |
90 | 0 | } |
91 | | |
92 | 0 | Ok(data_len.saturating_sub(data.len()).saturating_sub(QOI_PADDING_SIZE)) |
93 | 0 | } |
94 | | |
95 | | #[inline] |
96 | 0 | fn decode_impl_slice_all( |
97 | 0 | data: &[u8], out: &mut [u8], channels: u8, src_channels: u8, |
98 | 0 | ) -> Result<usize> { |
99 | 0 | match (channels, src_channels) { |
100 | 0 | (3, 3) => decode_impl_slice::<3, false>(data, out), |
101 | 0 | (3, 4) => decode_impl_slice::<3, true>(data, out), |
102 | 0 | (4, 3) => decode_impl_slice::<4, false>(data, out), |
103 | 0 | (4, 4) => decode_impl_slice::<4, true>(data, out), |
104 | | _ => { |
105 | 0 | cold(); |
106 | 0 | Err(Error::InvalidChannels { channels }) |
107 | | } |
108 | | } |
109 | 0 | } |
110 | | |
111 | | /// Decode the image into a pre-allocated buffer. |
112 | | /// |
113 | | /// Note: the resulting number of channels will match the header. In order to change |
114 | | /// the number of channels, use [`Decoder::with_channels`]. |
115 | | #[inline] |
116 | 0 | pub fn decode_to_buf(buf: impl AsMut<[u8]>, data: impl AsRef<[u8]>) -> Result<Header> { |
117 | 0 | let mut decoder = Decoder::new(&data)?; |
118 | 0 | decoder.decode_to_buf(buf)?; |
119 | 0 | Ok(*decoder.header()) |
120 | 0 | } |
121 | | |
122 | | /// Decode the image into a newly allocated vector. |
123 | | /// |
124 | | /// Note: the resulting number of channels will match the header. In order to change |
125 | | /// the number of channels, use [`Decoder::with_channels`]. |
126 | | #[cfg(any(feature = "std", feature = "alloc"))] |
127 | | #[inline] |
128 | 0 | pub fn decode_to_vec(data: impl AsRef<[u8]>) -> Result<(Header, Vec<u8>)> { |
129 | 0 | let mut decoder = Decoder::new(&data)?; |
130 | 0 | let out = decoder.decode_to_vec()?; |
131 | 0 | Ok((*decoder.header(), out)) |
132 | 0 | } |
133 | | |
134 | | /// Decode the image header from a slice of bytes. |
135 | | #[inline] |
136 | 0 | pub fn decode_header(data: impl AsRef<[u8]>) -> Result<Header> { |
137 | 0 | Header::decode(data) |
138 | 0 | } |
139 | | |
140 | | #[cfg(any(feature = "std"))] |
141 | | #[inline] |
142 | 32 | fn decode_impl_stream<R: Read, const N: usize, const RGBA: bool>( |
143 | 32 | data: &mut R, out: &mut [u8], |
144 | 32 | ) -> Result<()> |
145 | 32 | where |
146 | 32 | Pixel<N>: SupportedChannels, |
147 | 32 | [u8; N]: Pod, |
148 | | { |
149 | 32 | let mut pixels = cast_slice_mut::<_, [u8; N]>(out); |
150 | | |
151 | 32 | let mut index = [Pixel::<N>::new(); 256]; |
152 | 32 | let mut px = Pixel::<N>::new().with_a(0xff); |
153 | | |
154 | 394k | while let [px_out, ptail @ ..] = pixels { |
155 | 394k | pixels = ptail; |
156 | 394k | let mut p = [0]; |
157 | 394k | data.read_exact(&mut p)?; |
158 | 394k | let [b1] = p; |
159 | 30.6k | match b1 { |
160 | 394k | QOI_OP_INDEX..=QOI_OP_INDEX_END => { |
161 | 154k | px = index[b1 as usize]; |
162 | 154k | *px_out = px.into(); |
163 | 154k | continue; |
164 | | } |
165 | | QOI_OP_RGB => { |
166 | 1.88k | let mut p = [0; 3]; |
167 | 1.88k | data.read_exact(&mut p)?; |
168 | 1.88k | px.update_rgb(p[0], p[1], p[2]); |
169 | | } |
170 | 8.50k | QOI_OP_RGBA if RGBA => { |
171 | 8.50k | let mut p = [0; 4]; |
172 | 8.50k | data.read_exact(&mut p)?; |
173 | 8.50k | px.update_rgba(p[0], p[1], p[2], p[3]); |
174 | | } |
175 | 50.1k | QOI_OP_RUN..=QOI_OP_RUN_END => { |
176 | 50.1k | *px_out = px.into(); |
177 | 50.1k | let run = ((b1 & 0x3f) as usize).min(pixels.len()); |
178 | 50.1k | let (phead, ptail) = pixels.split_at_mut(run); // can't panic |
179 | 50.1k | phead.fill(px.into()); |
180 | 50.1k | pixels = ptail; |
181 | 50.1k | continue; |
182 | | } |
183 | 157k | QOI_OP_DIFF..=QOI_OP_DIFF_END => { |
184 | 120k | px.update_diff(b1); |
185 | 120k | } |
186 | 37.0k | QOI_OP_LUMA..=QOI_OP_LUMA_END => { |
187 | 37.0k | let mut p = [0]; |
188 | 37.0k | data.read_exact(&mut p)?; |
189 | 37.0k | let [b2] = p; |
190 | 37.0k | px.update_luma(b1, b2); |
191 | | } |
192 | 22.1k | _ => { |
193 | 22.1k | cold(); |
194 | 22.1k | } |
195 | | } |
196 | | |
197 | 190k | index[px.hash_index() as usize] = px; |
198 | 190k | *px_out = px.into(); |
199 | | } |
200 | | |
201 | 7 | let mut p = [0_u8; QOI_PADDING_SIZE]; |
202 | 7 | data.read_exact(&mut p)?; |
203 | 7 | if unlikely(p != QOI_PADDING) { |
204 | 7 | return Err(Error::InvalidPadding); |
205 | 0 | } |
206 | | |
207 | 0 | Ok(()) |
208 | 32 | } qoi::decode::decode_impl_stream::<std::io::cursor::Cursor<&[u8]>, 3, false> Line | Count | Source | 142 | 14 | fn decode_impl_stream<R: Read, const N: usize, const RGBA: bool>( | 143 | 14 | data: &mut R, out: &mut [u8], | 144 | 14 | ) -> Result<()> | 145 | 14 | where | 146 | 14 | Pixel<N>: SupportedChannels, | 147 | 14 | [u8; N]: Pod, | 148 | | { | 149 | 14 | let mut pixels = cast_slice_mut::<_, [u8; N]>(out); | 150 | | | 151 | 14 | let mut index = [Pixel::<N>::new(); 256]; | 152 | 14 | let mut px = Pixel::<N>::new().with_a(0xff); | 153 | | | 154 | 229k | while let [px_out, ptail @ ..] = pixels { | 155 | 229k | pixels = ptail; | 156 | 229k | let mut p = [0]; | 157 | 229k | data.read_exact(&mut p)?; | 158 | 229k | let [b1] = p; | 159 | 22.1k | match b1 { | 160 | 229k | QOI_OP_INDEX..=QOI_OP_INDEX_END => { | 161 | 94.0k | px = index[b1 as usize]; | 162 | 94.0k | *px_out = px.into(); | 163 | 94.0k | continue; | 164 | | } | 165 | | QOI_OP_RGB => { | 166 | 965 | let mut p = [0; 3]; | 167 | 965 | data.read_exact(&mut p)?; | 168 | 963 | px.update_rgb(p[0], p[1], p[2]); | 169 | | } | 170 | 0 | QOI_OP_RGBA if RGBA => { | 171 | 0 | let mut p = [0; 4]; | 172 | 0 | data.read_exact(&mut p)?; | 173 | 0 | px.update_rgba(p[0], p[1], p[2], p[3]); | 174 | | } | 175 | 35.8k | QOI_OP_RUN..=QOI_OP_RUN_END => { | 176 | 35.8k | *px_out = px.into(); | 177 | 35.8k | let run = ((b1 & 0x3f) as usize).min(pixels.len()); | 178 | 35.8k | let (phead, ptail) = pixels.split_at_mut(run); // can't panic | 179 | 35.8k | phead.fill(px.into()); | 180 | 35.8k | pixels = ptail; | 181 | 35.8k | continue; | 182 | | } | 183 | 76.8k | QOI_OP_DIFF..=QOI_OP_DIFF_END => { | 184 | 43.4k | px.update_diff(b1); | 185 | 43.4k | } | 186 | 33.4k | QOI_OP_LUMA..=QOI_OP_LUMA_END => { | 187 | 33.4k | let mut p = [0]; | 188 | 33.4k | data.read_exact(&mut p)?; | 189 | 33.4k | let [b2] = p; | 190 | 33.4k | px.update_luma(b1, b2); | 191 | | } | 192 | 22.1k | _ => { | 193 | 22.1k | cold(); | 194 | 22.1k | } | 195 | | } | 196 | | | 197 | 99.9k | index[px.hash_index() as usize] = px; | 198 | 99.9k | *px_out = px.into(); | 199 | | } | 200 | | | 201 | 3 | let mut p = [0_u8; QOI_PADDING_SIZE]; | 202 | 3 | data.read_exact(&mut p)?; | 203 | 3 | if unlikely(p != QOI_PADDING) { | 204 | 3 | return Err(Error::InvalidPadding); | 205 | 0 | } | 206 | | | 207 | 0 | Ok(()) | 208 | 14 | } |
Unexecuted instantiation: qoi::decode::decode_impl_stream::<std::io::cursor::Cursor<&[u8]>, 3, true> Unexecuted instantiation: qoi::decode::decode_impl_stream::<std::io::cursor::Cursor<&[u8]>, 4, false> qoi::decode::decode_impl_stream::<std::io::cursor::Cursor<&[u8]>, 4, true> Line | Count | Source | 142 | 18 | fn decode_impl_stream<R: Read, const N: usize, const RGBA: bool>( | 143 | 18 | data: &mut R, out: &mut [u8], | 144 | 18 | ) -> Result<()> | 145 | 18 | where | 146 | 18 | Pixel<N>: SupportedChannels, | 147 | 18 | [u8; N]: Pod, | 148 | | { | 149 | 18 | let mut pixels = cast_slice_mut::<_, [u8; N]>(out); | 150 | | | 151 | 18 | let mut index = [Pixel::<N>::new(); 256]; | 152 | 18 | let mut px = Pixel::<N>::new().with_a(0xff); | 153 | | | 154 | 164k | while let [px_out, ptail @ ..] = pixels { | 155 | 164k | pixels = ptail; | 156 | 164k | let mut p = [0]; | 157 | 164k | data.read_exact(&mut p)?; | 158 | 164k | let [b1] = p; | 159 | 8.50k | match b1 { | 160 | 164k | QOI_OP_INDEX..=QOI_OP_INDEX_END => { | 161 | 60.3k | px = index[b1 as usize]; | 162 | 60.3k | *px_out = px.into(); | 163 | 60.3k | continue; | 164 | | } | 165 | | QOI_OP_RGB => { | 166 | 918 | let mut p = [0; 3]; | 167 | 918 | data.read_exact(&mut p)?; | 168 | 918 | px.update_rgb(p[0], p[1], p[2]); | 169 | | } | 170 | 8.50k | QOI_OP_RGBA if RGBA => { | 171 | 8.50k | let mut p = [0; 4]; | 172 | 8.50k | data.read_exact(&mut p)?; | 173 | 8.50k | px.update_rgba(p[0], p[1], p[2], p[3]); | 174 | | } | 175 | 14.3k | QOI_OP_RUN..=QOI_OP_RUN_END => { | 176 | 14.3k | *px_out = px.into(); | 177 | 14.3k | let run = ((b1 & 0x3f) as usize).min(pixels.len()); | 178 | 14.3k | let (phead, ptail) = pixels.split_at_mut(run); // can't panic | 179 | 14.3k | phead.fill(px.into()); | 180 | 14.3k | pixels = ptail; | 181 | 14.3k | continue; | 182 | | } | 183 | 80.8k | QOI_OP_DIFF..=QOI_OP_DIFF_END => { | 184 | 77.2k | px.update_diff(b1); | 185 | 77.2k | } | 186 | 3.61k | QOI_OP_LUMA..=QOI_OP_LUMA_END => { | 187 | 3.61k | let mut p = [0]; | 188 | 3.61k | data.read_exact(&mut p)?; | 189 | 3.61k | let [b2] = p; | 190 | 3.61k | px.update_luma(b1, b2); | 191 | | } | 192 | 0 | _ => { | 193 | 0 | cold(); | 194 | 0 | } | 195 | | } | 196 | | | 197 | 90.2k | index[px.hash_index() as usize] = px; | 198 | 90.2k | *px_out = px.into(); | 199 | | } | 200 | | | 201 | 4 | let mut p = [0_u8; QOI_PADDING_SIZE]; | 202 | 4 | data.read_exact(&mut p)?; | 203 | 4 | if unlikely(p != QOI_PADDING) { | 204 | 4 | return Err(Error::InvalidPadding); | 205 | 0 | } | 206 | | | 207 | 0 | Ok(()) | 208 | 18 | } |
Unexecuted instantiation: qoi::decode::decode_impl_stream::<_, _, _> |
209 | | |
210 | | #[cfg(feature = "std")] |
211 | | #[inline] |
212 | 32 | fn decode_impl_stream_all<R: Read>( |
213 | 32 | data: &mut R, out: &mut [u8], channels: u8, src_channels: u8, |
214 | 32 | ) -> Result<()> { |
215 | 32 | match (channels, src_channels) { |
216 | 14 | (3, 3) => decode_impl_stream::<_, 3, false>(data, out), |
217 | 0 | (3, 4) => decode_impl_stream::<_, 3, true>(data, out), |
218 | 0 | (4, 3) => decode_impl_stream::<_, 4, false>(data, out), |
219 | 18 | (4, 4) => decode_impl_stream::<_, 4, true>(data, out), |
220 | | _ => { |
221 | 0 | cold(); |
222 | 0 | Err(Error::InvalidChannels { channels }) |
223 | | } |
224 | | } |
225 | 32 | } qoi::decode::decode_impl_stream_all::<std::io::cursor::Cursor<&[u8]>> Line | Count | Source | 212 | 32 | fn decode_impl_stream_all<R: Read>( | 213 | 32 | data: &mut R, out: &mut [u8], channels: u8, src_channels: u8, | 214 | 32 | ) -> Result<()> { | 215 | 32 | match (channels, src_channels) { | 216 | 14 | (3, 3) => decode_impl_stream::<_, 3, false>(data, out), | 217 | 0 | (3, 4) => decode_impl_stream::<_, 3, true>(data, out), | 218 | 0 | (4, 3) => decode_impl_stream::<_, 4, false>(data, out), | 219 | 18 | (4, 4) => decode_impl_stream::<_, 4, true>(data, out), | 220 | | _ => { | 221 | 0 | cold(); | 222 | 0 | Err(Error::InvalidChannels { channels }) | 223 | | } | 224 | | } | 225 | 32 | } |
Unexecuted instantiation: qoi::decode::decode_impl_stream_all::<_> |
226 | | |
227 | | #[doc(hidden)] |
228 | | pub trait Reader: Sized { |
229 | | fn decode_header(&mut self) -> Result<Header>; |
230 | | fn decode_image(&mut self, out: &mut [u8], channels: u8, src_channels: u8) -> Result<()>; |
231 | | } |
232 | | |
233 | | pub struct Bytes<'a>(&'a [u8]); |
234 | | |
235 | | impl<'a> Bytes<'a> { |
236 | | #[inline] |
237 | 0 | pub const fn new(buf: &'a [u8]) -> Self { |
238 | 0 | Self(buf) |
239 | 0 | } |
240 | | |
241 | | #[inline] |
242 | 0 | pub const fn as_slice(&self) -> &[u8] { |
243 | 0 | self.0 |
244 | 0 | } |
245 | | } |
246 | | |
247 | | impl<'a> Reader for Bytes<'a> { |
248 | | #[inline] |
249 | 0 | fn decode_header(&mut self) -> Result<Header> { |
250 | 0 | let header = Header::decode(self.0)?; |
251 | 0 | self.0 = &self.0[QOI_HEADER_SIZE..]; // can't panic |
252 | 0 | Ok(header) |
253 | 0 | } |
254 | | |
255 | | #[inline] |
256 | 0 | fn decode_image(&mut self, out: &mut [u8], channels: u8, src_channels: u8) -> Result<()> { |
257 | 0 | let n_read = decode_impl_slice_all(self.0, out, channels, src_channels)?; |
258 | 0 | self.0 = &self.0[n_read..]; |
259 | 0 | Ok(()) |
260 | 0 | } |
261 | | } |
262 | | |
263 | | #[cfg(feature = "std")] |
264 | | impl<R: Read> Reader for R { |
265 | | #[inline] |
266 | 32 | fn decode_header(&mut self) -> Result<Header> { |
267 | 32 | let mut b = [0; QOI_HEADER_SIZE]; |
268 | 32 | self.read_exact(&mut b)?; |
269 | 32 | Header::decode(b) |
270 | 32 | } <std::io::cursor::Cursor<&[u8]> as qoi::decode::Reader>::decode_header Line | Count | Source | 266 | 32 | fn decode_header(&mut self) -> Result<Header> { | 267 | 32 | let mut b = [0; QOI_HEADER_SIZE]; | 268 | 32 | self.read_exact(&mut b)?; | 269 | 32 | Header::decode(b) | 270 | 32 | } |
Unexecuted instantiation: <_ as qoi::decode::Reader>::decode_header |
271 | | |
272 | | #[inline] |
273 | 32 | fn decode_image(&mut self, out: &mut [u8], channels: u8, src_channels: u8) -> Result<()> { |
274 | 32 | decode_impl_stream_all(self, out, channels, src_channels) |
275 | 32 | } <std::io::cursor::Cursor<&[u8]> as qoi::decode::Reader>::decode_image Line | Count | Source | 273 | 32 | fn decode_image(&mut self, out: &mut [u8], channels: u8, src_channels: u8) -> Result<()> { | 274 | 32 | decode_impl_stream_all(self, out, channels, src_channels) | 275 | 32 | } |
Unexecuted instantiation: <_ as qoi::decode::Reader>::decode_image |
276 | | } |
277 | | |
278 | | /// Decode QOI images from slices or from streams. |
279 | | #[derive(Clone)] |
280 | | pub struct Decoder<R> { |
281 | | reader: R, |
282 | | header: Header, |
283 | | channels: Channels, |
284 | | } |
285 | | |
286 | | impl<'a> Decoder<Bytes<'a>> { |
287 | | /// Creates a new decoder from a slice of bytes. |
288 | | /// |
289 | | /// The header will be decoded immediately upon construction. |
290 | | /// |
291 | | /// Note: this provides the most efficient decoding, but requires the source data to |
292 | | /// be loaded in memory in order to decode it. In order to decode from a generic |
293 | | /// stream, use [`Decoder::from_stream`] instead. |
294 | | #[inline] |
295 | 0 | pub fn new(data: &'a (impl AsRef<[u8]> + ?Sized)) -> Result<Self> { |
296 | 0 | Self::new_impl(Bytes::new(data.as_ref())) |
297 | 0 | } |
298 | | |
299 | | /// Returns the undecoded tail of the input slice of bytes. |
300 | | #[inline] |
301 | 0 | pub const fn data(&self) -> &[u8] { |
302 | 0 | self.reader.as_slice() |
303 | 0 | } |
304 | | } |
305 | | |
306 | | #[cfg(feature = "std")] |
307 | | impl<R: Read> Decoder<R> { |
308 | | /// Creates a new decoder from a generic reader that implements [`Read`](std::io::Read). |
309 | | /// |
310 | | /// The header will be decoded immediately upon construction. |
311 | | /// |
312 | | /// Note: while it's possible to pass a `&[u8]` slice here since it implements `Read`, it |
313 | | /// would be more efficient to use a specialized constructor instead: [`Decoder::new`]. |
314 | | #[inline] |
315 | 32 | pub fn from_stream(reader: R) -> Result<Self> { |
316 | 32 | Self::new_impl(reader) |
317 | 32 | } <qoi::decode::Decoder<std::io::cursor::Cursor<&[u8]>>>::from_stream Line | Count | Source | 315 | 32 | pub fn from_stream(reader: R) -> Result<Self> { | 316 | 32 | Self::new_impl(reader) | 317 | 32 | } |
Unexecuted instantiation: <qoi::decode::Decoder<_>>::from_stream |
318 | | |
319 | | /// Returns an immutable reference to the underlying reader. |
320 | | #[inline] |
321 | 0 | pub const fn reader(&self) -> &R { |
322 | 0 | &self.reader |
323 | 0 | } |
324 | | |
325 | | /// Consumes the decoder and returns the underlying reader back. |
326 | | #[inline] |
327 | | #[allow(clippy::missing_const_for_fn)] |
328 | 0 | pub fn into_reader(self) -> R { |
329 | 0 | self.reader |
330 | 0 | } |
331 | | } |
332 | | |
333 | | impl<R: Reader> Decoder<R> { |
334 | | #[inline] |
335 | 32 | fn new_impl(mut reader: R) -> Result<Self> { |
336 | 32 | let header = reader.decode_header()?; |
337 | 32 | Ok(Self { reader, header, channels: header.channels }) |
338 | 32 | } <qoi::decode::Decoder<std::io::cursor::Cursor<&[u8]>>>::new_impl Line | Count | Source | 335 | 32 | fn new_impl(mut reader: R) -> Result<Self> { | 336 | 32 | let header = reader.decode_header()?; | 337 | 32 | Ok(Self { reader, header, channels: header.channels }) | 338 | 32 | } |
Unexecuted instantiation: <qoi::decode::Decoder<_>>::new_impl |
339 | | |
340 | | /// Returns a new decoder with modified number of channels. |
341 | | /// |
342 | | /// By default, the number of channels in the decoded image will be equal |
343 | | /// to whatever is specified in the header. However, it is also possible |
344 | | /// to decode RGB into RGBA (in which case the alpha channel will be set |
345 | | /// to 255), and vice versa (in which case the alpha channel will be ignored). |
346 | | #[inline] |
347 | 0 | pub const fn with_channels(mut self, channels: Channels) -> Self { |
348 | 0 | self.channels = channels; |
349 | 0 | self |
350 | 0 | } |
351 | | |
352 | | /// Returns the number of channels in the decoded image. |
353 | | /// |
354 | | /// Note: this may differ from the number of channels specified in the header. |
355 | | #[inline] |
356 | 0 | pub const fn channels(&self) -> Channels { |
357 | 0 | self.channels |
358 | 0 | } |
359 | | |
360 | | /// Returns the decoded image header. |
361 | | #[inline] |
362 | 352 | pub const fn header(&self) -> &Header { |
363 | 352 | &self.header |
364 | 352 | } <qoi::decode::Decoder<std::io::cursor::Cursor<&[u8]>>>::header Line | Count | Source | 362 | 352 | pub const fn header(&self) -> &Header { | 363 | 352 | &self.header | 364 | 352 | } |
Unexecuted instantiation: <qoi::decode::Decoder<_>>::header |
365 | | |
366 | | /// The number of bytes the decoded image will take. |
367 | | /// |
368 | | /// Can be used to pre-allocate the buffer to decode the image into. |
369 | | #[inline] |
370 | 32 | pub const fn required_buf_len(&self) -> usize { |
371 | 32 | self.header.n_pixels().saturating_mul(self.channels.as_u8() as usize) |
372 | 32 | } <qoi::decode::Decoder<std::io::cursor::Cursor<&[u8]>>>::required_buf_len Line | Count | Source | 370 | 32 | pub const fn required_buf_len(&self) -> usize { | 371 | 32 | self.header.n_pixels().saturating_mul(self.channels.as_u8() as usize) | 372 | 32 | } |
Unexecuted instantiation: <qoi::decode::Decoder<_>>::required_buf_len |
373 | | |
374 | | /// Decodes the image to a pre-allocated buffer and returns the number of bytes written. |
375 | | /// |
376 | | /// The minimum size of the buffer can be found via [`Decoder::required_buf_len`]. |
377 | | #[inline] |
378 | 32 | pub fn decode_to_buf(&mut self, mut buf: impl AsMut<[u8]>) -> Result<usize> { |
379 | 32 | let buf = buf.as_mut(); |
380 | 32 | let size = self.required_buf_len(); |
381 | 32 | if unlikely(buf.len() < size) { |
382 | 0 | return Err(Error::OutputBufferTooSmall { size: buf.len(), required: size }); |
383 | 32 | } |
384 | 32 | self.reader.decode_image(buf, self.channels.as_u8(), self.header.channels.as_u8())?; |
385 | 0 | Ok(size) |
386 | 32 | } <qoi::decode::Decoder<std::io::cursor::Cursor<&[u8]>>>::decode_to_buf::<&mut [u8]> Line | Count | Source | 378 | 32 | pub fn decode_to_buf(&mut self, mut buf: impl AsMut<[u8]>) -> Result<usize> { | 379 | 32 | let buf = buf.as_mut(); | 380 | 32 | let size = self.required_buf_len(); | 381 | 32 | if unlikely(buf.len() < size) { | 382 | 0 | return Err(Error::OutputBufferTooSmall { size: buf.len(), required: size }); | 383 | 32 | } | 384 | 32 | self.reader.decode_image(buf, self.channels.as_u8(), self.header.channels.as_u8())?; | 385 | 0 | Ok(size) | 386 | 32 | } |
Unexecuted instantiation: <qoi::decode::Decoder<_>>::decode_to_buf::<_> |
387 | | |
388 | | /// Decodes the image into a newly allocated vector of bytes and returns it. |
389 | | #[cfg(any(feature = "std", feature = "alloc"))] |
390 | | #[inline] |
391 | 0 | pub fn decode_to_vec(&mut self) -> Result<Vec<u8>> { |
392 | 0 | let mut out = vec![0; self.header.n_pixels() * self.channels.as_u8() as usize]; |
393 | 0 | let _ = self.decode_to_buf(&mut out)?; |
394 | 0 | Ok(out) |
395 | 0 | } |
396 | | } |