/src/image/src/images/buffer.rs
Line | Count | Source |
1 | | //! Contains the generic `ImageBuffer` struct. |
2 | | use num_traits::Zero; |
3 | | use std::fmt; |
4 | | use std::marker::PhantomData; |
5 | | use std::ops::{Deref, DerefMut, Index, IndexMut, Range}; |
6 | | use std::path::Path; |
7 | | use std::slice::{ChunksExact, ChunksExactMut}; |
8 | | |
9 | | use crate::color::{FromColor, FromPrimitive, Luma, LumaA, Rgb, Rgba}; |
10 | | use crate::error::{ |
11 | | ImageResult, ParameterError, ParameterErrorKind, UnsupportedError, UnsupportedErrorKind, |
12 | | }; |
13 | | use crate::flat::{FlatSamples, SampleLayout}; |
14 | | use crate::math::Rect; |
15 | | use crate::metadata::cicp::{CicpApplicable, CicpPixelCast, CicpRgb, ColorComponentForCicp}; |
16 | | use crate::traits::{EncodableLayout, Pixel, PixelWithColorType}; |
17 | | use crate::utils::expand_packed; |
18 | | use crate::{ |
19 | | metadata::{Cicp, CicpColorPrimaries, CicpTransferCharacteristics, CicpTransform}, |
20 | | save_buffer, save_buffer_with_format, write_buffer_with_format, ImageError, |
21 | | }; |
22 | | use crate::{DynamicImage, GenericImage, GenericImageView, ImageEncoder, ImageFormat}; |
23 | | |
24 | | /// Iterate over pixel refs. |
25 | | pub struct Pixels<'a, P: Pixel + 'a> |
26 | | where |
27 | | P::Subpixel: 'a, |
28 | | { |
29 | | chunks: ChunksExact<'a, P::Subpixel>, |
30 | | } |
31 | | |
32 | | impl<'a, P: Pixel + 'a> Iterator for Pixels<'a, P> |
33 | | where |
34 | | P::Subpixel: 'a, |
35 | | { |
36 | | type Item = &'a P; |
37 | | |
38 | | #[inline(always)] |
39 | 0 | fn next(&mut self) -> Option<&'a P> { |
40 | 0 | self.chunks.next().map(|v| <P as Pixel>::from_slice(v)) Unexecuted instantiation: <image::images::buffer::Pixels<image::color::Luma<f32>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::Pixels<image::color::Rgba<f32>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::Pixels<image::color::Rgba<u8>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::Pixels<image::color::Rgba<u16>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::Pixels<image::color::LumaA<f32>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::Pixels<image::color::LumaA<u8>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::Pixels<image::color::LumaA<u16>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::Pixels<image::color::Rgb<u16>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::Pixels<image::color::Luma<u8>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::Pixels<image::color::Luma<u16>> as core::iter::traits::iterator::Iterator>::next::{closure#0} |
41 | 0 | } Unexecuted instantiation: <image::images::buffer::Pixels<image::color::Luma<f32>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::Pixels<image::color::LumaA<f32>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::Pixels<image::color::Rgba<u8>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::Pixels<image::color::Rgba<u16>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::Pixels<image::color::LumaA<u8>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::Pixels<image::color::LumaA<u16>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::Pixels<image::color::Rgba<f32>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::Pixels<image::color::Rgb<u16>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::Pixels<image::color::Luma<u8>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::Pixels<image::color::Luma<u16>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::Pixels<image::color::Rgba<u16>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::Pixels<image::color::LumaA<u8>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::Pixels<image::color::LumaA<u16>> as core::iter::traits::iterator::Iterator>::next |
42 | | |
43 | | #[inline(always)] |
44 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
45 | 0 | let len = self.len(); |
46 | 0 | (len, Some(len)) |
47 | 0 | } |
48 | | } |
49 | | |
50 | | impl<'a, P: Pixel + 'a> ExactSizeIterator for Pixels<'a, P> |
51 | | where |
52 | | P::Subpixel: 'a, |
53 | | { |
54 | 0 | fn len(&self) -> usize { |
55 | 0 | self.chunks.len() |
56 | 0 | } Unexecuted instantiation: <image::images::buffer::Pixels<_> as core::iter::traits::exact_size::ExactSizeIterator>::len Unexecuted instantiation: <image::images::buffer::Pixels<image::color::Rgb<u8>> as core::iter::traits::exact_size::ExactSizeIterator>::len Unexecuted instantiation: <image::images::buffer::Pixels<image::color::Rgba<u8>> as core::iter::traits::exact_size::ExactSizeIterator>::len |
57 | | } |
58 | | |
59 | | impl<'a, P: Pixel + 'a> DoubleEndedIterator for Pixels<'a, P> |
60 | | where |
61 | | P::Subpixel: 'a, |
62 | | { |
63 | | #[inline(always)] |
64 | 0 | fn next_back(&mut self) -> Option<&'a P> { |
65 | 0 | self.chunks.next_back().map(|v| <P as Pixel>::from_slice(v)) |
66 | 0 | } |
67 | | } |
68 | | |
69 | | impl<P: Pixel> Clone for Pixels<'_, P> { |
70 | 0 | fn clone(&self) -> Self { |
71 | 0 | Pixels { |
72 | 0 | chunks: self.chunks.clone(), |
73 | 0 | } |
74 | 0 | } |
75 | | } |
76 | | |
77 | | impl<P: Pixel> fmt::Debug for Pixels<'_, P> |
78 | | where |
79 | | P::Subpixel: fmt::Debug, |
80 | | { |
81 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
82 | 0 | f.debug_struct("Pixels") |
83 | 0 | .field("chunks", &self.chunks) |
84 | 0 | .finish() |
85 | 0 | } |
86 | | } |
87 | | |
88 | | /// Iterate over mutable pixel refs. |
89 | | pub struct PixelsMut<'a, P: Pixel + 'a> |
90 | | where |
91 | | P::Subpixel: 'a, |
92 | | { |
93 | | chunks: ChunksExactMut<'a, P::Subpixel>, |
94 | | } |
95 | | |
96 | | impl<'a, P: Pixel + 'a> Iterator for PixelsMut<'a, P> |
97 | | where |
98 | | P::Subpixel: 'a, |
99 | | { |
100 | | type Item = &'a mut P; |
101 | | |
102 | | #[inline(always)] |
103 | 4.33G | fn next(&mut self) -> Option<&'a mut P> { |
104 | 4.33G | self.chunks.next().map(|v| <P as Pixel>::from_slice_mut(v)) Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::Rgb<f32>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::Rgb<u8>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::Rgb<u16>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::Luma<u8>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::Luma<u16>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::Rgba<f32>> as core::iter::traits::iterator::Iterator>::next::{closure#0}<image::images::buffer::PixelsMut<image::color::Rgba<u8>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Line | Count | Source | 104 | 4.33G | self.chunks.next().map(|v| <P as Pixel>::from_slice_mut(v)) |
Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::Rgba<u16>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::LumaA<u8>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::LumaA<u16>> as core::iter::traits::iterator::Iterator>::next::{closure#0} |
105 | 4.33G | } <image::images::buffer::PixelsMut<image::color::Rgba<u8>> as core::iter::traits::iterator::Iterator>::next Line | Count | Source | 103 | 4.33G | fn next(&mut self) -> Option<&'a mut P> { | 104 | 4.33G | self.chunks.next().map(|v| <P as Pixel>::from_slice_mut(v)) | 105 | 4.33G | } |
Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::Rgb<f32>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::Rgb<u8>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::Rgb<u16>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::Luma<u8>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::Luma<u16>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::Rgba<f32>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::Rgba<u16>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::LumaA<u8>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::LumaA<u16>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::PixelsMut<image::color::Rgba<u8>> as core::iter::traits::iterator::Iterator>::next |
106 | | |
107 | | #[inline(always)] |
108 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
109 | 0 | let len = self.len(); |
110 | 0 | (len, Some(len)) |
111 | 0 | } |
112 | | } |
113 | | |
114 | | impl<'a, P: Pixel + 'a> ExactSizeIterator for PixelsMut<'a, P> |
115 | | where |
116 | | P::Subpixel: 'a, |
117 | | { |
118 | 0 | fn len(&self) -> usize { |
119 | 0 | self.chunks.len() |
120 | 0 | } |
121 | | } |
122 | | |
123 | | impl<'a, P: Pixel + 'a> DoubleEndedIterator for PixelsMut<'a, P> |
124 | | where |
125 | | P::Subpixel: 'a, |
126 | | { |
127 | | #[inline(always)] |
128 | 0 | fn next_back(&mut self) -> Option<&'a mut P> { |
129 | 0 | self.chunks |
130 | 0 | .next_back() |
131 | 0 | .map(|v| <P as Pixel>::from_slice_mut(v)) |
132 | 0 | } |
133 | | } |
134 | | |
135 | | impl<P: Pixel> fmt::Debug for PixelsMut<'_, P> |
136 | | where |
137 | | P::Subpixel: fmt::Debug, |
138 | | { |
139 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
140 | 0 | f.debug_struct("PixelsMut") |
141 | 0 | .field("chunks", &self.chunks) |
142 | 0 | .finish() |
143 | 0 | } |
144 | | } |
145 | | |
146 | | /// Iterate over rows of an image |
147 | | /// |
148 | | /// This iterator is created with [`ImageBuffer::rows`]. See its document for details. |
149 | | /// |
150 | | /// [`ImageBuffer::rows`]: ../struct.ImageBuffer.html#method.rows |
151 | | pub struct Rows<'a, P: Pixel + 'a> |
152 | | where |
153 | | <P as Pixel>::Subpixel: 'a, |
154 | | { |
155 | | pixels: ChunksExact<'a, P::Subpixel>, |
156 | | } |
157 | | |
158 | | impl<'a, P: Pixel + 'a> Rows<'a, P> { |
159 | | /// Construct the iterator from image pixels. This is not public since it has a (hidden) panic |
160 | | /// condition. The `pixels` slice must be large enough so that all pixels are addressable. |
161 | 0 | fn with_image(pixels: &'a [P::Subpixel], width: u32, height: u32) -> Self { |
162 | 0 | let row_len = (width as usize) * usize::from(<P as Pixel>::CHANNEL_COUNT); |
163 | 0 | if row_len == 0 { |
164 | 0 | Rows { |
165 | 0 | pixels: [].chunks_exact(1), |
166 | 0 | } |
167 | | } else { |
168 | 0 | let pixels = pixels |
169 | 0 | .get(..row_len * height as usize) |
170 | 0 | .expect("Pixel buffer has too few subpixels"); |
171 | | // Rows are physically present. In particular, height is smaller than `usize::MAX` as |
172 | | // all subpixels can be indexed. |
173 | 0 | Rows { |
174 | 0 | pixels: pixels.chunks_exact(row_len), |
175 | 0 | } |
176 | | } |
177 | 0 | } Unexecuted instantiation: <image::images::buffer::Rows<image::color::Rgba<u8>>>::with_image Unexecuted instantiation: <image::images::buffer::Rows<image::color::Rgba<u16>>>::with_image Unexecuted instantiation: <image::images::buffer::Rows<image::color::LumaA<u8>>>::with_image Unexecuted instantiation: <image::images::buffer::Rows<image::color::LumaA<u16>>>::with_image |
178 | | } |
179 | | |
180 | | impl<'a, P: Pixel + 'a> Iterator for Rows<'a, P> |
181 | | where |
182 | | P::Subpixel: 'a, |
183 | | { |
184 | | type Item = Pixels<'a, P>; |
185 | | |
186 | | #[inline(always)] |
187 | 0 | fn next(&mut self) -> Option<Pixels<'a, P>> { |
188 | 0 | let row = self.pixels.next()?; |
189 | 0 | Some(Pixels { |
190 | 0 | // Note: this is not reached when CHANNEL_COUNT is 0. |
191 | 0 | chunks: row.chunks_exact(<P as Pixel>::CHANNEL_COUNT as usize), |
192 | 0 | }) |
193 | 0 | } Unexecuted instantiation: <image::images::buffer::Rows<image::color::Rgba<u8>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::Rows<image::color::Rgba<u16>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::Rows<image::color::LumaA<u8>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::Rows<image::color::LumaA<u16>> as core::iter::traits::iterator::Iterator>::next |
194 | | |
195 | | #[inline(always)] |
196 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
197 | 0 | let len = self.len(); |
198 | 0 | (len, Some(len)) |
199 | 0 | } |
200 | | } |
201 | | |
202 | | impl<'a, P: Pixel + 'a> ExactSizeIterator for Rows<'a, P> |
203 | | where |
204 | | P::Subpixel: 'a, |
205 | | { |
206 | 0 | fn len(&self) -> usize { |
207 | 0 | self.pixels.len() |
208 | 0 | } |
209 | | } |
210 | | |
211 | | impl<'a, P: Pixel + 'a> DoubleEndedIterator for Rows<'a, P> |
212 | | where |
213 | | P::Subpixel: 'a, |
214 | | { |
215 | | #[inline(always)] |
216 | 0 | fn next_back(&mut self) -> Option<Pixels<'a, P>> { |
217 | 0 | let row = self.pixels.next_back()?; |
218 | 0 | Some(Pixels { |
219 | 0 | // Note: this is not reached when CHANNEL_COUNT is 0. |
220 | 0 | chunks: row.chunks_exact(<P as Pixel>::CHANNEL_COUNT as usize), |
221 | 0 | }) |
222 | 0 | } |
223 | | } |
224 | | |
225 | | impl<P: Pixel> Clone for Rows<'_, P> { |
226 | 0 | fn clone(&self) -> Self { |
227 | 0 | Rows { |
228 | 0 | pixels: self.pixels.clone(), |
229 | 0 | } |
230 | 0 | } |
231 | | } |
232 | | |
233 | | impl<P: Pixel> fmt::Debug for Rows<'_, P> |
234 | | where |
235 | | P::Subpixel: fmt::Debug, |
236 | | { |
237 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
238 | 0 | f.debug_struct("Rows") |
239 | 0 | .field("pixels", &self.pixels) |
240 | 0 | .finish() |
241 | 0 | } |
242 | | } |
243 | | |
244 | | /// Iterate over mutable rows of an image |
245 | | /// |
246 | | /// This iterator is created with [`ImageBuffer::rows_mut`]. See its document for details. |
247 | | /// |
248 | | /// [`ImageBuffer::rows_mut`]: ../struct.ImageBuffer.html#method.rows_mut |
249 | | pub struct RowsMut<'a, P: Pixel + 'a> |
250 | | where |
251 | | <P as Pixel>::Subpixel: 'a, |
252 | | { |
253 | | pixels: ChunksExactMut<'a, P::Subpixel>, |
254 | | } |
255 | | |
256 | | impl<'a, P: Pixel + 'a> RowsMut<'a, P> { |
257 | | /// Construct the iterator from image pixels. This is not public since it has a (hidden) panic |
258 | | /// condition. The `pixels` slice must be large enough so that all pixels are addressable. |
259 | 0 | fn with_image(pixels: &'a mut [P::Subpixel], width: u32, height: u32) -> Self { |
260 | 0 | let row_len = (width as usize) * usize::from(<P as Pixel>::CHANNEL_COUNT); |
261 | 0 | if row_len == 0 { |
262 | 0 | RowsMut { |
263 | 0 | pixels: [].chunks_exact_mut(1), |
264 | 0 | } |
265 | | } else { |
266 | 0 | let pixels = pixels |
267 | 0 | .get_mut(..row_len * height as usize) |
268 | 0 | .expect("Pixel buffer has too few subpixels"); |
269 | | // Rows are physically present. In particular, height is smaller than `usize::MAX` as |
270 | | // all subpixels can be indexed. |
271 | 0 | RowsMut { |
272 | 0 | pixels: pixels.chunks_exact_mut(row_len), |
273 | 0 | } |
274 | | } |
275 | 0 | } |
276 | | } |
277 | | |
278 | | impl<'a, P: Pixel + 'a> Iterator for RowsMut<'a, P> |
279 | | where |
280 | | P::Subpixel: 'a, |
281 | | { |
282 | | type Item = PixelsMut<'a, P>; |
283 | | |
284 | | #[inline(always)] |
285 | 0 | fn next(&mut self) -> Option<PixelsMut<'a, P>> { |
286 | 0 | let row = self.pixels.next()?; |
287 | 0 | Some(PixelsMut { |
288 | 0 | // Note: this is not reached when CHANNEL_COUNT is 0. |
289 | 0 | chunks: row.chunks_exact_mut(<P as Pixel>::CHANNEL_COUNT as usize), |
290 | 0 | }) |
291 | 0 | } |
292 | | |
293 | | #[inline(always)] |
294 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
295 | 0 | let len = self.len(); |
296 | 0 | (len, Some(len)) |
297 | 0 | } |
298 | | } |
299 | | |
300 | | impl<'a, P: Pixel + 'a> ExactSizeIterator for RowsMut<'a, P> |
301 | | where |
302 | | P::Subpixel: 'a, |
303 | | { |
304 | 0 | fn len(&self) -> usize { |
305 | 0 | self.pixels.len() |
306 | 0 | } |
307 | | } |
308 | | |
309 | | impl<'a, P: Pixel + 'a> DoubleEndedIterator for RowsMut<'a, P> |
310 | | where |
311 | | P::Subpixel: 'a, |
312 | | { |
313 | | #[inline(always)] |
314 | 0 | fn next_back(&mut self) -> Option<PixelsMut<'a, P>> { |
315 | 0 | let row = self.pixels.next_back()?; |
316 | 0 | Some(PixelsMut { |
317 | 0 | // Note: this is not reached when CHANNEL_COUNT is 0. |
318 | 0 | chunks: row.chunks_exact_mut(<P as Pixel>::CHANNEL_COUNT as usize), |
319 | 0 | }) |
320 | 0 | } |
321 | | } |
322 | | |
323 | | impl<P: Pixel> fmt::Debug for RowsMut<'_, P> |
324 | | where |
325 | | P::Subpixel: fmt::Debug, |
326 | | { |
327 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
328 | 0 | f.debug_struct("RowsMut") |
329 | 0 | .field("pixels", &self.pixels) |
330 | 0 | .finish() |
331 | 0 | } |
332 | | } |
333 | | |
334 | | /// Enumerate the pixels of an image. |
335 | | pub struct EnumeratePixels<'a, P: Pixel + 'a> |
336 | | where |
337 | | <P as Pixel>::Subpixel: 'a, |
338 | | { |
339 | | pixels: Pixels<'a, P>, |
340 | | x: u32, |
341 | | y: u32, |
342 | | width: u32, |
343 | | } |
344 | | |
345 | | impl<'a, P: Pixel + 'a> Iterator for EnumeratePixels<'a, P> |
346 | | where |
347 | | P::Subpixel: 'a, |
348 | | { |
349 | | type Item = (u32, u32, &'a P); |
350 | | |
351 | | #[inline(always)] |
352 | 0 | fn next(&mut self) -> Option<(u32, u32, &'a P)> { |
353 | 0 | if self.x >= self.width { |
354 | 0 | self.x = 0; |
355 | 0 | self.y += 1; |
356 | 0 | } |
357 | 0 | let (x, y) = (self.x, self.y); |
358 | 0 | self.x += 1; |
359 | 0 | self.pixels.next().map(|p| (x, y, p)) |
360 | 0 | } |
361 | | |
362 | | #[inline(always)] |
363 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
364 | 0 | let len = self.len(); |
365 | 0 | (len, Some(len)) |
366 | 0 | } |
367 | | } |
368 | | |
369 | | impl<'a, P: Pixel + 'a> ExactSizeIterator for EnumeratePixels<'a, P> |
370 | | where |
371 | | P::Subpixel: 'a, |
372 | | { |
373 | 0 | fn len(&self) -> usize { |
374 | 0 | self.pixels.len() |
375 | 0 | } |
376 | | } |
377 | | |
378 | | impl<P: Pixel> Clone for EnumeratePixels<'_, P> { |
379 | 0 | fn clone(&self) -> Self { |
380 | 0 | EnumeratePixels { |
381 | 0 | pixels: self.pixels.clone(), |
382 | 0 | ..*self |
383 | 0 | } |
384 | 0 | } |
385 | | } |
386 | | |
387 | | impl<P: Pixel> fmt::Debug for EnumeratePixels<'_, P> |
388 | | where |
389 | | P::Subpixel: fmt::Debug, |
390 | | { |
391 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
392 | 0 | f.debug_struct("EnumeratePixels") |
393 | 0 | .field("pixels", &self.pixels) |
394 | 0 | .field("x", &self.x) |
395 | 0 | .field("y", &self.y) |
396 | 0 | .field("width", &self.width) |
397 | 0 | .finish() |
398 | 0 | } |
399 | | } |
400 | | |
401 | | /// Enumerate the rows of an image. |
402 | | pub struct EnumerateRows<'a, P: Pixel + 'a> |
403 | | where |
404 | | <P as Pixel>::Subpixel: 'a, |
405 | | { |
406 | | rows: Rows<'a, P>, |
407 | | y: u32, |
408 | | width: u32, |
409 | | } |
410 | | |
411 | | impl<'a, P: Pixel + 'a> Iterator for EnumerateRows<'a, P> |
412 | | where |
413 | | P::Subpixel: 'a, |
414 | | { |
415 | | type Item = (u32, EnumeratePixels<'a, P>); |
416 | | |
417 | | #[inline(always)] |
418 | 0 | fn next(&mut self) -> Option<(u32, EnumeratePixels<'a, P>)> { |
419 | 0 | let y = self.y; |
420 | 0 | self.y += 1; |
421 | 0 | self.rows.next().map(|r| { |
422 | 0 | ( |
423 | 0 | y, |
424 | 0 | EnumeratePixels { |
425 | 0 | x: 0, |
426 | 0 | y, |
427 | 0 | width: self.width, |
428 | 0 | pixels: r, |
429 | 0 | }, |
430 | 0 | ) |
431 | 0 | }) |
432 | 0 | } |
433 | | |
434 | | #[inline(always)] |
435 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
436 | 0 | let len = self.len(); |
437 | 0 | (len, Some(len)) |
438 | 0 | } |
439 | | } |
440 | | |
441 | | impl<'a, P: Pixel + 'a> ExactSizeIterator for EnumerateRows<'a, P> |
442 | | where |
443 | | P::Subpixel: 'a, |
444 | | { |
445 | 0 | fn len(&self) -> usize { |
446 | 0 | self.rows.len() |
447 | 0 | } |
448 | | } |
449 | | |
450 | | impl<P: Pixel> Clone for EnumerateRows<'_, P> { |
451 | 0 | fn clone(&self) -> Self { |
452 | 0 | EnumerateRows { |
453 | 0 | rows: self.rows.clone(), |
454 | 0 | ..*self |
455 | 0 | } |
456 | 0 | } |
457 | | } |
458 | | |
459 | | impl<P: Pixel> fmt::Debug for EnumerateRows<'_, P> |
460 | | where |
461 | | P::Subpixel: fmt::Debug, |
462 | | { |
463 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
464 | 0 | f.debug_struct("EnumerateRows") |
465 | 0 | .field("rows", &self.rows) |
466 | 0 | .field("y", &self.y) |
467 | 0 | .field("width", &self.width) |
468 | 0 | .finish() |
469 | 0 | } |
470 | | } |
471 | | |
472 | | /// Enumerate the pixels of an image. |
473 | | pub struct EnumeratePixelsMut<'a, P: Pixel + 'a> |
474 | | where |
475 | | <P as Pixel>::Subpixel: 'a, |
476 | | { |
477 | | pixels: PixelsMut<'a, P>, |
478 | | x: u32, |
479 | | y: u32, |
480 | | width: u32, |
481 | | } |
482 | | |
483 | | impl<'a, P: Pixel + 'a> Iterator for EnumeratePixelsMut<'a, P> |
484 | | where |
485 | | P::Subpixel: 'a, |
486 | | { |
487 | | type Item = (u32, u32, &'a mut P); |
488 | | |
489 | | #[inline(always)] |
490 | 4.33G | fn next(&mut self) -> Option<(u32, u32, &'a mut P)> { |
491 | 4.33G | if self.x >= self.width { |
492 | 1.23M | self.x = 0; |
493 | 1.23M | self.y += 1; |
494 | 4.32G | } |
495 | 4.33G | let (x, y) = (self.x, self.y); |
496 | 4.33G | self.x += 1; |
497 | 4.33G | self.pixels.next().map(|p| (x, y, p)) Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::Rgb<f32>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::Rgb<u8>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::Rgb<u16>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::Luma<u8>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::Luma<u16>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::Rgba<f32>> as core::iter::traits::iterator::Iterator>::next::{closure#0}<image::images::buffer::EnumeratePixelsMut<image::color::Rgba<u8>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Line | Count | Source | 497 | 4.33G | self.pixels.next().map(|p| (x, y, p)) |
Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::Rgba<u16>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::LumaA<u8>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::LumaA<u16>> as core::iter::traits::iterator::Iterator>::next::{closure#0} |
498 | 4.33G | } <image::images::buffer::EnumeratePixelsMut<image::color::Rgba<u8>> as core::iter::traits::iterator::Iterator>::next Line | Count | Source | 490 | 4.33G | fn next(&mut self) -> Option<(u32, u32, &'a mut P)> { | 491 | 4.33G | if self.x >= self.width { | 492 | 1.23M | self.x = 0; | 493 | 1.23M | self.y += 1; | 494 | 4.32G | } | 495 | 4.33G | let (x, y) = (self.x, self.y); | 496 | 4.33G | self.x += 1; | 497 | 4.33G | self.pixels.next().map(|p| (x, y, p)) | 498 | 4.33G | } |
Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::Rgb<f32>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::Rgb<u8>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::Rgb<u16>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::Luma<u8>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::Luma<u16>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::Rgba<f32>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::Rgba<u16>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::LumaA<u8>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <image::images::buffer::EnumeratePixelsMut<image::color::LumaA<u16>> as core::iter::traits::iterator::Iterator>::next |
499 | | |
500 | | #[inline(always)] |
501 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
502 | 0 | let len = self.len(); |
503 | 0 | (len, Some(len)) |
504 | 0 | } |
505 | | } |
506 | | |
507 | | impl<'a, P: Pixel + 'a> ExactSizeIterator for EnumeratePixelsMut<'a, P> |
508 | | where |
509 | | P::Subpixel: 'a, |
510 | | { |
511 | 0 | fn len(&self) -> usize { |
512 | 0 | self.pixels.len() |
513 | 0 | } |
514 | | } |
515 | | |
516 | | impl<P: Pixel> fmt::Debug for EnumeratePixelsMut<'_, P> |
517 | | where |
518 | | P::Subpixel: fmt::Debug, |
519 | | { |
520 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
521 | 0 | f.debug_struct("EnumeratePixelsMut") |
522 | 0 | .field("pixels", &self.pixels) |
523 | 0 | .field("x", &self.x) |
524 | 0 | .field("y", &self.y) |
525 | 0 | .field("width", &self.width) |
526 | 0 | .finish() |
527 | 0 | } |
528 | | } |
529 | | |
530 | | /// Enumerate the rows of an image. |
531 | | pub struct EnumerateRowsMut<'a, P: Pixel + 'a> |
532 | | where |
533 | | <P as Pixel>::Subpixel: 'a, |
534 | | { |
535 | | rows: RowsMut<'a, P>, |
536 | | y: u32, |
537 | | width: u32, |
538 | | } |
539 | | |
540 | | impl<'a, P: Pixel + 'a> Iterator for EnumerateRowsMut<'a, P> |
541 | | where |
542 | | P::Subpixel: 'a, |
543 | | { |
544 | | type Item = (u32, EnumeratePixelsMut<'a, P>); |
545 | | |
546 | | #[inline(always)] |
547 | 0 | fn next(&mut self) -> Option<(u32, EnumeratePixelsMut<'a, P>)> { |
548 | 0 | let y = self.y; |
549 | 0 | self.y += 1; |
550 | 0 | self.rows.next().map(|r| { |
551 | 0 | ( |
552 | 0 | y, |
553 | 0 | EnumeratePixelsMut { |
554 | 0 | x: 0, |
555 | 0 | y, |
556 | 0 | width: self.width, |
557 | 0 | pixels: r, |
558 | 0 | }, |
559 | 0 | ) |
560 | 0 | }) |
561 | 0 | } |
562 | | |
563 | | #[inline(always)] |
564 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
565 | 0 | let len = self.len(); |
566 | 0 | (len, Some(len)) |
567 | 0 | } |
568 | | } |
569 | | |
570 | | impl<'a, P: Pixel + 'a> ExactSizeIterator for EnumerateRowsMut<'a, P> |
571 | | where |
572 | | P::Subpixel: 'a, |
573 | | { |
574 | 0 | fn len(&self) -> usize { |
575 | 0 | self.rows.len() |
576 | 0 | } |
577 | | } |
578 | | |
579 | | impl<P: Pixel> fmt::Debug for EnumerateRowsMut<'_, P> |
580 | | where |
581 | | P::Subpixel: fmt::Debug, |
582 | | { |
583 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
584 | 0 | f.debug_struct("EnumerateRowsMut") |
585 | 0 | .field("rows", &self.rows) |
586 | 0 | .field("y", &self.y) |
587 | 0 | .field("width", &self.width) |
588 | 0 | .finish() |
589 | 0 | } |
590 | | } |
591 | | |
592 | | /// Generic image buffer |
593 | | /// |
594 | | /// This is an image parameterised by its Pixel types, represented by a width and height and a |
595 | | /// container of channel data. It provides direct access to its pixels and implements the |
596 | | /// [`GenericImageView`] and [`GenericImage`] traits. In many ways, this is the standard buffer |
597 | | /// implementing those traits. Using this concrete type instead of a generic type parameter has |
598 | | /// been shown to improve performance. |
599 | | /// |
600 | | /// The crate defines a few type aliases with regularly used pixel types for your convenience, such |
601 | | /// as [`RgbImage`], [`GrayImage`] etc. |
602 | | /// |
603 | | /// [`GenericImage`]: trait.GenericImage.html |
604 | | /// [`GenericImageView`]: trait.GenericImageView.html |
605 | | /// [`RgbImage`]: type.RgbImage.html |
606 | | /// [`GrayImage`]: type.GrayImage.html |
607 | | /// |
608 | | /// To convert between images of different Pixel types use [`DynamicImage`]. |
609 | | /// |
610 | | /// You can retrieve a complete description of the buffer's layout and contents through |
611 | | /// [`as_flat_samples`] and [`as_flat_samples_mut`]. This can be handy to also use the contents in |
612 | | /// a foreign language, map it as a GPU host buffer or other similar tasks. |
613 | | /// |
614 | | /// [`DynamicImage`]: enum.DynamicImage.html |
615 | | /// [`as_flat_samples`]: #method.as_flat_samples |
616 | | /// [`as_flat_samples_mut`]: #method.as_flat_samples_mut |
617 | | /// |
618 | | /// ## Examples |
619 | | /// |
620 | | /// Create a simple canvas and paint a small cross. |
621 | | /// |
622 | | /// ``` |
623 | | /// use image::{RgbImage, Rgb}; |
624 | | /// |
625 | | /// let mut img = RgbImage::new(32, 32); |
626 | | /// |
627 | | /// for x in 15..=17 { |
628 | | /// for y in 8..24 { |
629 | | /// img.put_pixel(x, y, Rgb([255, 0, 0])); |
630 | | /// img.put_pixel(y, x, Rgb([255, 0, 0])); |
631 | | /// } |
632 | | /// } |
633 | | /// ``` |
634 | | /// |
635 | | /// Overlays an image on top of a larger background raster. |
636 | | /// |
637 | | /// ```no_run |
638 | | /// use image::{GenericImage, GenericImageView, ImageBuffer, open}; |
639 | | /// |
640 | | /// let on_top = open("path/to/some.png").unwrap().into_rgb8(); |
641 | | /// let mut img = ImageBuffer::from_fn(512, 512, |x, y| { |
642 | | /// if (x + y) % 2 == 0 { |
643 | | /// image::Rgb([0, 0, 0]) |
644 | | /// } else { |
645 | | /// image::Rgb([255, 255, 255]) |
646 | | /// } |
647 | | /// }); |
648 | | /// |
649 | | /// image::imageops::overlay(&mut img, &on_top, 128, 128); |
650 | | /// ``` |
651 | | /// |
652 | | /// Convert an `RgbaImage` to a `GrayImage`. |
653 | | /// |
654 | | /// ```no_run |
655 | | /// use image::{open, DynamicImage}; |
656 | | /// |
657 | | /// let rgba = open("path/to/some.png").unwrap().into_rgba8(); |
658 | | /// let gray = DynamicImage::ImageRgba8(rgba).into_luma8(); |
659 | | /// ``` |
660 | | #[derive(Hash, PartialEq, Eq)] |
661 | | pub struct ImageBuffer<P: Pixel, Container> { |
662 | | width: u32, |
663 | | height: u32, |
664 | | _phantom: PhantomData<P>, |
665 | | color: CicpRgb, |
666 | | data: Container, |
667 | | } |
668 | | |
669 | | // generic implementation, shared along all image buffers |
670 | | impl<P, Container> ImageBuffer<P, Container> |
671 | | where |
672 | | P: Pixel, |
673 | | Container: Deref<Target = [P::Subpixel]>, |
674 | | { |
675 | | /// Constructs a buffer from a generic container |
676 | | /// (for example a `Vec` or a slice) |
677 | | /// |
678 | | /// Returns `None` if the container is not big enough (including when the image dimensions |
679 | | /// necessitate an allocation of more bytes than supported by the container). |
680 | 5.94k | pub fn from_raw(width: u32, height: u32, buf: Container) -> Option<ImageBuffer<P, Container>> { |
681 | 5.94k | if Self::check_image_fits(width, height, buf.len()) { |
682 | 5.94k | Some(ImageBuffer { |
683 | 5.94k | data: buf, |
684 | 5.94k | width, |
685 | 5.94k | height, |
686 | 5.94k | color: Cicp::SRGB.into_rgb(), |
687 | 5.94k | _phantom: PhantomData, |
688 | 5.94k | }) |
689 | | } else { |
690 | 0 | None |
691 | | } |
692 | 5.94k | } <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::from_raw Line | Count | Source | 680 | 273 | pub fn from_raw(width: u32, height: u32, buf: Container) -> Option<ImageBuffer<P, Container>> { | 681 | 273 | if Self::check_image_fits(width, height, buf.len()) { | 682 | 273 | Some(ImageBuffer { | 683 | 273 | data: buf, | 684 | 273 | width, | 685 | 273 | height, | 686 | 273 | color: Cicp::SRGB.into_rgb(), | 687 | 273 | _phantom: PhantomData, | 688 | 273 | }) | 689 | | } else { | 690 | 0 | None | 691 | | } | 692 | 273 | } |
<image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::from_raw Line | Count | Source | 680 | 453 | pub fn from_raw(width: u32, height: u32, buf: Container) -> Option<ImageBuffer<P, Container>> { | 681 | 453 | if Self::check_image_fits(width, height, buf.len()) { | 682 | 453 | Some(ImageBuffer { | 683 | 453 | data: buf, | 684 | 453 | width, | 685 | 453 | height, | 686 | 453 | color: Cicp::SRGB.into_rgb(), | 687 | 453 | _phantom: PhantomData, | 688 | 453 | }) | 689 | | } else { | 690 | 0 | None | 691 | | } | 692 | 453 | } |
<image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::from_raw Line | Count | Source | 680 | 85 | pub fn from_raw(width: u32, height: u32, buf: Container) -> Option<ImageBuffer<P, Container>> { | 681 | 85 | if Self::check_image_fits(width, height, buf.len()) { | 682 | 85 | Some(ImageBuffer { | 683 | 85 | data: buf, | 684 | 85 | width, | 685 | 85 | height, | 686 | 85 | color: Cicp::SRGB.into_rgb(), | 687 | 85 | _phantom: PhantomData, | 688 | 85 | }) | 689 | | } else { | 690 | 0 | None | 691 | | } | 692 | 85 | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::from_raw <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::from_raw Line | Count | Source | 680 | 543 | pub fn from_raw(width: u32, height: u32, buf: Container) -> Option<ImageBuffer<P, Container>> { | 681 | 543 | if Self::check_image_fits(width, height, buf.len()) { | 682 | 543 | Some(ImageBuffer { | 683 | 543 | data: buf, | 684 | 543 | width, | 685 | 543 | height, | 686 | 543 | color: Cicp::SRGB.into_rgb(), | 687 | 543 | _phantom: PhantomData, | 688 | 543 | }) | 689 | | } else { | 690 | 0 | None | 691 | | } | 692 | 543 | } |
<image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::from_raw Line | Count | Source | 680 | 159 | pub fn from_raw(width: u32, height: u32, buf: Container) -> Option<ImageBuffer<P, Container>> { | 681 | 159 | if Self::check_image_fits(width, height, buf.len()) { | 682 | 159 | Some(ImageBuffer { | 683 | 159 | data: buf, | 684 | 159 | width, | 685 | 159 | height, | 686 | 159 | color: Cicp::SRGB.into_rgb(), | 687 | 159 | _phantom: PhantomData, | 688 | 159 | }) | 689 | | } else { | 690 | 0 | None | 691 | | } | 692 | 159 | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::from_raw Line | Count | Source | 680 | 1 | pub fn from_raw(width: u32, height: u32, buf: Container) -> Option<ImageBuffer<P, Container>> { | 681 | 1 | if Self::check_image_fits(width, height, buf.len()) { | 682 | 1 | Some(ImageBuffer { | 683 | 1 | data: buf, | 684 | 1 | width, | 685 | 1 | height, | 686 | 1 | color: Cicp::SRGB.into_rgb(), | 687 | 1 | _phantom: PhantomData, | 688 | 1 | }) | 689 | | } else { | 690 | 0 | None | 691 | | } | 692 | 1 | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::from_raw Line | Count | Source | 680 | 4.22k | pub fn from_raw(width: u32, height: u32, buf: Container) -> Option<ImageBuffer<P, Container>> { | 681 | 4.22k | if Self::check_image_fits(width, height, buf.len()) { | 682 | 4.22k | Some(ImageBuffer { | 683 | 4.22k | data: buf, | 684 | 4.22k | width, | 685 | 4.22k | height, | 686 | 4.22k | color: Cicp::SRGB.into_rgb(), | 687 | 4.22k | _phantom: PhantomData, | 688 | 4.22k | }) | 689 | | } else { | 690 | 0 | None | 691 | | } | 692 | 4.22k | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, &mut [u8]>>::from_raw Line | Count | Source | 680 | 102 | pub fn from_raw(width: u32, height: u32, buf: Container) -> Option<ImageBuffer<P, Container>> { | 681 | 102 | if Self::check_image_fits(width, height, buf.len()) { | 682 | 102 | Some(ImageBuffer { | 683 | 102 | data: buf, | 684 | 102 | width, | 685 | 102 | height, | 686 | 102 | color: Cicp::SRGB.into_rgb(), | 687 | 102 | _phantom: PhantomData, | 688 | 102 | }) | 689 | | } else { | 690 | 0 | None | 691 | | } | 692 | 102 | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::from_raw Line | Count | Source | 680 | 31 | pub fn from_raw(width: u32, height: u32, buf: Container) -> Option<ImageBuffer<P, Container>> { | 681 | 31 | if Self::check_image_fits(width, height, buf.len()) { | 682 | 31 | Some(ImageBuffer { | 683 | 31 | data: buf, | 684 | 31 | width, | 685 | 31 | height, | 686 | 31 | color: Cicp::SRGB.into_rgb(), | 687 | 31 | _phantom: PhantomData, | 688 | 31 | }) | 689 | | } else { | 690 | 0 | None | 691 | | } | 692 | 31 | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::from_raw <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::from_raw Line | Count | Source | 680 | 44 | pub fn from_raw(width: u32, height: u32, buf: Container) -> Option<ImageBuffer<P, Container>> { | 681 | 44 | if Self::check_image_fits(width, height, buf.len()) { | 682 | 44 | Some(ImageBuffer { | 683 | 44 | data: buf, | 684 | 44 | width, | 685 | 44 | height, | 686 | 44 | color: Cicp::SRGB.into_rgb(), | 687 | 44 | _phantom: PhantomData, | 688 | 44 | }) | 689 | | } else { | 690 | 0 | None | 691 | | } | 692 | 44 | } |
<image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::from_raw Line | Count | Source | 680 | 30 | pub fn from_raw(width: u32, height: u32, buf: Container) -> Option<ImageBuffer<P, Container>> { | 681 | 30 | if Self::check_image_fits(width, height, buf.len()) { | 682 | 30 | Some(ImageBuffer { | 683 | 30 | data: buf, | 684 | 30 | width, | 685 | 30 | height, | 686 | 30 | color: Cicp::SRGB.into_rgb(), | 687 | 30 | _phantom: PhantomData, | 688 | 30 | }) | 689 | | } else { | 690 | 0 | None | 691 | | } | 692 | 30 | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, &[u8]>>::from_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, &[u16]>>::from_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, &[u8]>>::from_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, &[u16]>>::from_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, &[u8]>>::from_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, &[u16]>>::from_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, &[u8]>>::from_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, &[u16]>>::from_raw |
693 | | |
694 | | /// Returns the underlying raw buffer |
695 | 3.66k | pub fn into_raw(self) -> Container { |
696 | 3.66k | self.data |
697 | 3.66k | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::into_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::into_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::into_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::into_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::into_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::into_raw <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::into_raw Line | Count | Source | 695 | 3.66k | pub fn into_raw(self) -> Container { | 696 | 3.66k | self.data | 697 | 3.66k | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::into_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::into_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::into_raw |
698 | | |
699 | | /// Returns the underlying raw buffer |
700 | 0 | pub fn as_raw(&self) -> &Container { |
701 | 0 | &self.data |
702 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::as_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::as_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::as_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::as_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::as_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::as_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::as_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::as_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::as_raw Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::as_raw |
703 | | |
704 | | /// The width and height of this image. |
705 | 0 | pub fn dimensions(&self) -> (u32, u32) { |
706 | 0 | (self.width, self.height) |
707 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, &[u16]>>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, &[u8]>>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, &[u16]>>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, &[u16]>>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, &[u8]>>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, &[u16]>>::dimensions |
708 | | |
709 | | /// The width of this image. |
710 | 1.83k | pub fn width(&self) -> u32 { |
711 | 1.83k | self.width |
712 | 1.83k | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::width Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::width Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::width Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::width Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::width Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::width <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::width Line | Count | Source | 710 | 1.83k | pub fn width(&self) -> u32 { | 711 | 1.83k | self.width | 712 | 1.83k | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::width Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::width Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::width |
713 | | |
714 | | /// The height of this image. |
715 | 1.83k | pub fn height(&self) -> u32 { |
716 | 1.83k | self.height |
717 | 1.83k | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::height Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::height Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::height Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::height Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::height Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::height <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::height Line | Count | Source | 715 | 1.83k | pub fn height(&self) -> u32 { | 716 | 1.83k | self.height | 717 | 1.83k | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::height Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::height Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::height |
718 | | |
719 | | // TODO: choose name under which to expose. |
720 | 1.83k | pub(crate) fn inner_pixels(&self) -> &[P::Subpixel] { |
721 | 1.83k | let len = Self::image_buffer_len(self.width, self.height).unwrap(); |
722 | 1.83k | &self.data[..len] |
723 | 1.83k | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::inner_pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::inner_pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::inner_pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::inner_pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::inner_pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::inner_pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::inner_pixels <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::inner_pixels Line | Count | Source | 720 | 1.83k | pub(crate) fn inner_pixels(&self) -> &[P::Subpixel] { | 721 | 1.83k | let len = Self::image_buffer_len(self.width, self.height).unwrap(); | 722 | 1.83k | &self.data[..len] | 723 | 1.83k | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::inner_pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::inner_pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::inner_pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::inner_pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, &[u8]>>::inner_pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, &[u16]>>::inner_pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, &[u8]>>::inner_pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, &[u16]>>::inner_pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, &[u8]>>::inner_pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, &[u16]>>::inner_pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, &[u8]>>::inner_pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, &[u16]>>::inner_pixels |
724 | | |
725 | | /// Returns an iterator over the pixels of this image. |
726 | | /// The iteration order is x = 0 to width then y = 0 to height |
727 | 0 | pub fn pixels(&self) -> Pixels<'_, P> { |
728 | 0 | Pixels { |
729 | 0 | chunks: self |
730 | 0 | .inner_pixels() |
731 | 0 | .chunks_exact(<P as Pixel>::CHANNEL_COUNT as usize), |
732 | 0 | } |
733 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, &[u8]>>::pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, &[u16]>>::pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, &[u8]>>::pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, &[u16]>>::pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, &[u8]>>::pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, &[u16]>>::pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, &[u8]>>::pixels Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, &[u16]>>::pixels |
734 | | |
735 | | /// Returns an iterator over the rows of this image. |
736 | | /// |
737 | | /// Only non-empty rows can be iterated in this manner. In particular the iterator will not |
738 | | /// yield any item when the width of the image is `0` or a pixel type without any channels is |
739 | | /// used. This ensures that its length can always be represented by `usize`. |
740 | 0 | pub fn rows(&self) -> Rows<'_, P> { |
741 | 0 | Rows::with_image(&self.data, self.width, self.height) |
742 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::rows Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::rows Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::rows Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::rows |
743 | | |
744 | | /// Enumerates over the pixels of the image. |
745 | | /// The iterator yields the coordinates of each pixel |
746 | | /// along with a reference to them. |
747 | | /// The iteration order is x = 0 to width then y = 0 to height |
748 | | /// Starting from the top left. |
749 | 0 | pub fn enumerate_pixels(&self) -> EnumeratePixels<'_, P> { |
750 | 0 | EnumeratePixels { |
751 | 0 | pixels: self.pixels(), |
752 | 0 | x: 0, |
753 | 0 | y: 0, |
754 | 0 | width: self.width, |
755 | 0 | } |
756 | 0 | } |
757 | | |
758 | | /// Enumerates over the rows of the image. |
759 | | /// The iterator yields the y-coordinate of each row |
760 | | /// along with a reference to them. |
761 | 0 | pub fn enumerate_rows(&self) -> EnumerateRows<'_, P> { |
762 | 0 | EnumerateRows { |
763 | 0 | rows: self.rows(), |
764 | 0 | y: 0, |
765 | 0 | width: self.width, |
766 | 0 | } |
767 | 0 | } |
768 | | |
769 | | /// Gets a reference to the pixel at location `(x, y)` |
770 | | /// |
771 | | /// # Panics |
772 | | /// |
773 | | /// Panics if `(x, y)` is out of the bounds `(width, height)`. |
774 | | #[inline] |
775 | | #[track_caller] |
776 | 11.7M | pub fn get_pixel(&self, x: u32, y: u32) -> &P { |
777 | 11.7M | match self.pixel_indices(x, y) { |
778 | 0 | None => panic!( |
779 | 0 | "Image index {:?} out of bounds {:?}", |
780 | 0 | (x, y), |
781 | 0 | (self.width, self.height) |
782 | | ), |
783 | 11.7M | Some(pixel_indices) => <P as Pixel>::from_slice(&self.data[pixel_indices]), |
784 | | } |
785 | 11.7M | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::get_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::get_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::get_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::get_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::get_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::get_pixel <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::get_pixel Line | Count | Source | 776 | 11.7M | pub fn get_pixel(&self, x: u32, y: u32) -> &P { | 777 | 11.7M | match self.pixel_indices(x, y) { | 778 | 0 | None => panic!( | 779 | 0 | "Image index {:?} out of bounds {:?}", | 780 | 0 | (x, y), | 781 | 0 | (self.width, self.height) | 782 | | ), | 783 | 11.7M | Some(pixel_indices) => <P as Pixel>::from_slice(&self.data[pixel_indices]), | 784 | | } | 785 | 11.7M | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::get_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::get_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::get_pixel |
786 | | |
787 | | /// Gets a reference to the pixel at location `(x, y)` or returns `None` if |
788 | | /// the index is out of the bounds `(width, height)`. |
789 | 0 | pub fn get_pixel_checked(&self, x: u32, y: u32) -> Option<&P> { |
790 | 0 | if x >= self.width { |
791 | 0 | return None; |
792 | 0 | } |
793 | 0 | let num_channels = <P as Pixel>::CHANNEL_COUNT as usize; |
794 | 0 | let i = (y as usize) |
795 | 0 | .saturating_mul(self.width as usize) |
796 | 0 | .saturating_add(x as usize) |
797 | 0 | .saturating_mul(num_channels); |
798 | | |
799 | 0 | self.data |
800 | 0 | .get(i..i.checked_add(num_channels)?) |
801 | 0 | .map(|pixel_indices| <P as Pixel>::from_slice(pixel_indices)) |
802 | 0 | } |
803 | | |
804 | | /// Test that the image fits inside the buffer. |
805 | | /// |
806 | | /// Verifies that the maximum image of pixels inside the bounds is smaller than the provided |
807 | | /// length. Note that as a corrolary we also have that the index calculation of pixels inside |
808 | | /// the bounds will not overflow. |
809 | 5.94k | fn check_image_fits(width: u32, height: u32, len: usize) -> bool { |
810 | 5.94k | let checked_len = Self::image_buffer_len(width, height); |
811 | 5.94k | checked_len.is_some_and(|min_len| min_len <= len) <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::check_image_fits::{closure#0}Line | Count | Source | 811 | 273 | checked_len.is_some_and(|min_len| min_len <= len) |
<image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::check_image_fits::{closure#0}Line | Count | Source | 811 | 453 | checked_len.is_some_and(|min_len| min_len <= len) |
<image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::check_image_fits::{closure#0}Line | Count | Source | 811 | 85 | checked_len.is_some_and(|min_len| min_len <= len) |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::check_image_fits::{closure#0}<image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::check_image_fits::{closure#0}Line | Count | Source | 811 | 543 | checked_len.is_some_and(|min_len| min_len <= len) |
<image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::check_image_fits::{closure#0}Line | Count | Source | 811 | 159 | checked_len.is_some_and(|min_len| min_len <= len) |
<image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::check_image_fits::{closure#0}Line | Count | Source | 811 | 1 | checked_len.is_some_and(|min_len| min_len <= len) |
<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::check_image_fits::{closure#0}Line | Count | Source | 811 | 4.22k | checked_len.is_some_and(|min_len| min_len <= len) |
<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, &mut [u8]>>::check_image_fits::{closure#0}Line | Count | Source | 811 | 102 | checked_len.is_some_and(|min_len| min_len <= len) |
<image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::check_image_fits::{closure#0}Line | Count | Source | 811 | 31 | checked_len.is_some_and(|min_len| min_len <= len) |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::check_image_fits::{closure#0}<image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::check_image_fits::{closure#0}Line | Count | Source | 811 | 44 | checked_len.is_some_and(|min_len| min_len <= len) |
<image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::check_image_fits::{closure#0}Line | Count | Source | 811 | 30 | checked_len.is_some_and(|min_len| min_len <= len) |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, &[u8]>>::check_image_fits::{closure#0}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, &[u16]>>::check_image_fits::{closure#0}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, &[u8]>>::check_image_fits::{closure#0}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, &[u16]>>::check_image_fits::{closure#0}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, &[u8]>>::check_image_fits::{closure#0}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, &[u16]>>::check_image_fits::{closure#0}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, &[u8]>>::check_image_fits::{closure#0}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, &[u16]>>::check_image_fits::{closure#0} |
812 | 5.94k | } <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::check_image_fits Line | Count | Source | 809 | 273 | fn check_image_fits(width: u32, height: u32, len: usize) -> bool { | 810 | 273 | let checked_len = Self::image_buffer_len(width, height); | 811 | 273 | checked_len.is_some_and(|min_len| min_len <= len) | 812 | 273 | } |
<image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::check_image_fits Line | Count | Source | 809 | 453 | fn check_image_fits(width: u32, height: u32, len: usize) -> bool { | 810 | 453 | let checked_len = Self::image_buffer_len(width, height); | 811 | 453 | checked_len.is_some_and(|min_len| min_len <= len) | 812 | 453 | } |
<image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::check_image_fits Line | Count | Source | 809 | 85 | fn check_image_fits(width: u32, height: u32, len: usize) -> bool { | 810 | 85 | let checked_len = Self::image_buffer_len(width, height); | 811 | 85 | checked_len.is_some_and(|min_len| min_len <= len) | 812 | 85 | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::check_image_fits <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::check_image_fits Line | Count | Source | 809 | 543 | fn check_image_fits(width: u32, height: u32, len: usize) -> bool { | 810 | 543 | let checked_len = Self::image_buffer_len(width, height); | 811 | 543 | checked_len.is_some_and(|min_len| min_len <= len) | 812 | 543 | } |
<image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::check_image_fits Line | Count | Source | 809 | 159 | fn check_image_fits(width: u32, height: u32, len: usize) -> bool { | 810 | 159 | let checked_len = Self::image_buffer_len(width, height); | 811 | 159 | checked_len.is_some_and(|min_len| min_len <= len) | 812 | 159 | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::check_image_fits Line | Count | Source | 809 | 1 | fn check_image_fits(width: u32, height: u32, len: usize) -> bool { | 810 | 1 | let checked_len = Self::image_buffer_len(width, height); | 811 | 1 | checked_len.is_some_and(|min_len| min_len <= len) | 812 | 1 | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::check_image_fits Line | Count | Source | 809 | 4.22k | fn check_image_fits(width: u32, height: u32, len: usize) -> bool { | 810 | 4.22k | let checked_len = Self::image_buffer_len(width, height); | 811 | 4.22k | checked_len.is_some_and(|min_len| min_len <= len) | 812 | 4.22k | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, &mut [u8]>>::check_image_fits Line | Count | Source | 809 | 102 | fn check_image_fits(width: u32, height: u32, len: usize) -> bool { | 810 | 102 | let checked_len = Self::image_buffer_len(width, height); | 811 | 102 | checked_len.is_some_and(|min_len| min_len <= len) | 812 | 102 | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::check_image_fits Line | Count | Source | 809 | 31 | fn check_image_fits(width: u32, height: u32, len: usize) -> bool { | 810 | 31 | let checked_len = Self::image_buffer_len(width, height); | 811 | 31 | checked_len.is_some_and(|min_len| min_len <= len) | 812 | 31 | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::check_image_fits <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::check_image_fits Line | Count | Source | 809 | 44 | fn check_image_fits(width: u32, height: u32, len: usize) -> bool { | 810 | 44 | let checked_len = Self::image_buffer_len(width, height); | 811 | 44 | checked_len.is_some_and(|min_len| min_len <= len) | 812 | 44 | } |
<image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::check_image_fits Line | Count | Source | 809 | 30 | fn check_image_fits(width: u32, height: u32, len: usize) -> bool { | 810 | 30 | let checked_len = Self::image_buffer_len(width, height); | 811 | 30 | checked_len.is_some_and(|min_len| min_len <= len) | 812 | 30 | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, &[u8]>>::check_image_fits Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, &[u16]>>::check_image_fits Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, &[u8]>>::check_image_fits Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, &[u16]>>::check_image_fits Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, &[u8]>>::check_image_fits Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, &[u16]>>::check_image_fits Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, &[u8]>>::check_image_fits Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, &[u16]>>::check_image_fits |
813 | | |
814 | 7.87k | fn image_buffer_len(width: u32, height: u32) -> Option<usize> { |
815 | 7.87k | Some(<P as Pixel>::CHANNEL_COUNT as usize) |
816 | 7.87k | .and_then(|size| size.checked_mul(width as usize)) <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::image_buffer_len::{closure#0}Line | Count | Source | 816 | 273 | .and_then(|size| size.checked_mul(width as usize)) |
<image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::image_buffer_len::{closure#0}Line | Count | Source | 816 | 453 | .and_then(|size| size.checked_mul(width as usize)) |
<image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::image_buffer_len::{closure#0}Line | Count | Source | 816 | 85 | .and_then(|size| size.checked_mul(width as usize)) |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::image_buffer_len::{closure#0}<image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::image_buffer_len::{closure#0}Line | Count | Source | 816 | 543 | .and_then(|size| size.checked_mul(width as usize)) |
<image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::image_buffer_len::{closure#0}Line | Count | Source | 816 | 159 | .and_then(|size| size.checked_mul(width as usize)) |
<image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::image_buffer_len::{closure#0}Line | Count | Source | 816 | 1 | .and_then(|size| size.checked_mul(width as usize)) |
<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::image_buffer_len::{closure#0}Line | Count | Source | 816 | 6.05k | .and_then(|size| size.checked_mul(width as usize)) |
<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, &mut [u8]>>::image_buffer_len::{closure#0}Line | Count | Source | 816 | 204 | .and_then(|size| size.checked_mul(width as usize)) |
<image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::image_buffer_len::{closure#0}Line | Count | Source | 816 | 31 | .and_then(|size| size.checked_mul(width as usize)) |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::image_buffer_len::{closure#0}<image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::image_buffer_len::{closure#0}Line | Count | Source | 816 | 44 | .and_then(|size| size.checked_mul(width as usize)) |
<image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::image_buffer_len::{closure#0}Line | Count | Source | 816 | 30 | .and_then(|size| size.checked_mul(width as usize)) |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, &[u8]>>::image_buffer_len::{closure#0}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, &[u16]>>::image_buffer_len::{closure#0}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, &[u8]>>::image_buffer_len::{closure#0}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, &[u16]>>::image_buffer_len::{closure#0}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, &[u8]>>::image_buffer_len::{closure#0}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, &[u16]>>::image_buffer_len::{closure#0}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, &[u8]>>::image_buffer_len::{closure#0}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, &[u16]>>::image_buffer_len::{closure#0} |
817 | 7.87k | .and_then(|size| size.checked_mul(height as usize)) <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::image_buffer_len::{closure#1}Line | Count | Source | 817 | 273 | .and_then(|size| size.checked_mul(height as usize)) |
<image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::image_buffer_len::{closure#1}Line | Count | Source | 817 | 453 | .and_then(|size| size.checked_mul(height as usize)) |
<image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::image_buffer_len::{closure#1}Line | Count | Source | 817 | 85 | .and_then(|size| size.checked_mul(height as usize)) |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::image_buffer_len::{closure#1}<image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::image_buffer_len::{closure#1}Line | Count | Source | 817 | 543 | .and_then(|size| size.checked_mul(height as usize)) |
<image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::image_buffer_len::{closure#1}Line | Count | Source | 817 | 159 | .and_then(|size| size.checked_mul(height as usize)) |
<image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::image_buffer_len::{closure#1}Line | Count | Source | 817 | 1 | .and_then(|size| size.checked_mul(height as usize)) |
<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::image_buffer_len::{closure#1}Line | Count | Source | 817 | 6.05k | .and_then(|size| size.checked_mul(height as usize)) |
<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, &mut [u8]>>::image_buffer_len::{closure#1}Line | Count | Source | 817 | 204 | .and_then(|size| size.checked_mul(height as usize)) |
<image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::image_buffer_len::{closure#1}Line | Count | Source | 817 | 31 | .and_then(|size| size.checked_mul(height as usize)) |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::image_buffer_len::{closure#1}<image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::image_buffer_len::{closure#1}Line | Count | Source | 817 | 44 | .and_then(|size| size.checked_mul(height as usize)) |
<image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::image_buffer_len::{closure#1}Line | Count | Source | 817 | 30 | .and_then(|size| size.checked_mul(height as usize)) |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, &[u8]>>::image_buffer_len::{closure#1}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, &[u16]>>::image_buffer_len::{closure#1}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, &[u8]>>::image_buffer_len::{closure#1}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, &[u16]>>::image_buffer_len::{closure#1}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, &[u8]>>::image_buffer_len::{closure#1}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, &[u16]>>::image_buffer_len::{closure#1}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, &[u8]>>::image_buffer_len::{closure#1}Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, &[u16]>>::image_buffer_len::{closure#1} |
818 | 7.87k | } <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::image_buffer_len Line | Count | Source | 814 | 273 | fn image_buffer_len(width: u32, height: u32) -> Option<usize> { | 815 | 273 | Some(<P as Pixel>::CHANNEL_COUNT as usize) | 816 | 273 | .and_then(|size| size.checked_mul(width as usize)) | 817 | 273 | .and_then(|size| size.checked_mul(height as usize)) | 818 | 273 | } |
<image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::image_buffer_len Line | Count | Source | 814 | 453 | fn image_buffer_len(width: u32, height: u32) -> Option<usize> { | 815 | 453 | Some(<P as Pixel>::CHANNEL_COUNT as usize) | 816 | 453 | .and_then(|size| size.checked_mul(width as usize)) | 817 | 453 | .and_then(|size| size.checked_mul(height as usize)) | 818 | 453 | } |
<image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::image_buffer_len Line | Count | Source | 814 | 85 | fn image_buffer_len(width: u32, height: u32) -> Option<usize> { | 815 | 85 | Some(<P as Pixel>::CHANNEL_COUNT as usize) | 816 | 85 | .and_then(|size| size.checked_mul(width as usize)) | 817 | 85 | .and_then(|size| size.checked_mul(height as usize)) | 818 | 85 | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::image_buffer_len <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::image_buffer_len Line | Count | Source | 814 | 543 | fn image_buffer_len(width: u32, height: u32) -> Option<usize> { | 815 | 543 | Some(<P as Pixel>::CHANNEL_COUNT as usize) | 816 | 543 | .and_then(|size| size.checked_mul(width as usize)) | 817 | 543 | .and_then(|size| size.checked_mul(height as usize)) | 818 | 543 | } |
<image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::image_buffer_len Line | Count | Source | 814 | 159 | fn image_buffer_len(width: u32, height: u32) -> Option<usize> { | 815 | 159 | Some(<P as Pixel>::CHANNEL_COUNT as usize) | 816 | 159 | .and_then(|size| size.checked_mul(width as usize)) | 817 | 159 | .and_then(|size| size.checked_mul(height as usize)) | 818 | 159 | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::image_buffer_len Line | Count | Source | 814 | 1 | fn image_buffer_len(width: u32, height: u32) -> Option<usize> { | 815 | 1 | Some(<P as Pixel>::CHANNEL_COUNT as usize) | 816 | 1 | .and_then(|size| size.checked_mul(width as usize)) | 817 | 1 | .and_then(|size| size.checked_mul(height as usize)) | 818 | 1 | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::image_buffer_len Line | Count | Source | 814 | 6.05k | fn image_buffer_len(width: u32, height: u32) -> Option<usize> { | 815 | 6.05k | Some(<P as Pixel>::CHANNEL_COUNT as usize) | 816 | 6.05k | .and_then(|size| size.checked_mul(width as usize)) | 817 | 6.05k | .and_then(|size| size.checked_mul(height as usize)) | 818 | 6.05k | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, &mut [u8]>>::image_buffer_len Line | Count | Source | 814 | 204 | fn image_buffer_len(width: u32, height: u32) -> Option<usize> { | 815 | 204 | Some(<P as Pixel>::CHANNEL_COUNT as usize) | 816 | 204 | .and_then(|size| size.checked_mul(width as usize)) | 817 | 204 | .and_then(|size| size.checked_mul(height as usize)) | 818 | 204 | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::image_buffer_len Line | Count | Source | 814 | 31 | fn image_buffer_len(width: u32, height: u32) -> Option<usize> { | 815 | 31 | Some(<P as Pixel>::CHANNEL_COUNT as usize) | 816 | 31 | .and_then(|size| size.checked_mul(width as usize)) | 817 | 31 | .and_then(|size| size.checked_mul(height as usize)) | 818 | 31 | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::image_buffer_len <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::image_buffer_len Line | Count | Source | 814 | 44 | fn image_buffer_len(width: u32, height: u32) -> Option<usize> { | 815 | 44 | Some(<P as Pixel>::CHANNEL_COUNT as usize) | 816 | 44 | .and_then(|size| size.checked_mul(width as usize)) | 817 | 44 | .and_then(|size| size.checked_mul(height as usize)) | 818 | 44 | } |
<image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::image_buffer_len Line | Count | Source | 814 | 30 | fn image_buffer_len(width: u32, height: u32) -> Option<usize> { | 815 | 30 | Some(<P as Pixel>::CHANNEL_COUNT as usize) | 816 | 30 | .and_then(|size| size.checked_mul(width as usize)) | 817 | 30 | .and_then(|size| size.checked_mul(height as usize)) | 818 | 30 | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, &[u8]>>::image_buffer_len Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, &[u16]>>::image_buffer_len Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, &[u8]>>::image_buffer_len Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, &[u16]>>::image_buffer_len Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, &[u8]>>::image_buffer_len Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, &[u16]>>::image_buffer_len Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, &[u8]>>::image_buffer_len Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, &[u16]>>::image_buffer_len |
819 | | |
820 | | #[inline(always)] |
821 | 11.7M | fn pixel_indices(&self, x: u32, y: u32) -> Option<Range<usize>> { |
822 | 11.7M | if x >= self.width || y >= self.height { |
823 | 0 | return None; |
824 | 11.7M | } |
825 | | |
826 | 11.7M | Some(self.pixel_indices_unchecked(x, y)) |
827 | 11.7M | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::pixel_indices Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::pixel_indices Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::pixel_indices Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::pixel_indices Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::pixel_indices Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::pixel_indices <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::pixel_indices Line | Count | Source | 821 | 11.7M | fn pixel_indices(&self, x: u32, y: u32) -> Option<Range<usize>> { | 822 | 11.7M | if x >= self.width || y >= self.height { | 823 | 0 | return None; | 824 | 11.7M | } | 825 | | | 826 | 11.7M | Some(self.pixel_indices_unchecked(x, y)) | 827 | 11.7M | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::pixel_indices Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::pixel_indices Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::pixel_indices |
828 | | |
829 | | #[inline(always)] |
830 | 11.7M | fn pixel_indices_unchecked(&self, x: u32, y: u32) -> Range<usize> { |
831 | 11.7M | let no_channels = <P as Pixel>::CHANNEL_COUNT as usize; |
832 | | // If in bounds, this can't overflow as we have tested that at construction! |
833 | 11.7M | let min_index = (y as usize * self.width as usize + x as usize) * no_channels; |
834 | 11.7M | min_index..min_index + no_channels |
835 | 11.7M | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::pixel_indices_unchecked Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::pixel_indices_unchecked Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::pixel_indices_unchecked Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::pixel_indices_unchecked Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::pixel_indices_unchecked Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::pixel_indices_unchecked <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::pixel_indices_unchecked Line | Count | Source | 830 | 11.7M | fn pixel_indices_unchecked(&self, x: u32, y: u32) -> Range<usize> { | 831 | 11.7M | let no_channels = <P as Pixel>::CHANNEL_COUNT as usize; | 832 | | // If in bounds, this can't overflow as we have tested that at construction! | 833 | 11.7M | let min_index = (y as usize * self.width as usize + x as usize) * no_channels; | 834 | 11.7M | min_index..min_index + no_channels | 835 | 11.7M | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::pixel_indices_unchecked Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::pixel_indices_unchecked Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::pixel_indices_unchecked |
836 | | |
837 | | /// Get the format of the buffer when viewed as a matrix of samples. |
838 | 0 | pub fn sample_layout(&self) -> SampleLayout { |
839 | | // None of these can overflow, as all our memory is addressable. |
840 | 0 | SampleLayout::row_major_packed(<P as Pixel>::CHANNEL_COUNT, self.width, self.height) |
841 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::sample_layout Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::sample_layout Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::sample_layout Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::sample_layout Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::sample_layout Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::sample_layout Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::sample_layout Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::sample_layout Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::sample_layout Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::sample_layout |
842 | | |
843 | | /// Return the raw sample buffer with its stride an dimension information. |
844 | | /// |
845 | | /// The returned buffer is guaranteed to be well formed in all cases. It is laid out by |
846 | | /// colors, width then height, meaning `channel_stride <= width_stride <= height_stride`. All |
847 | | /// strides are in numbers of elements but those are mostly `u8` in which case the strides are |
848 | | /// also byte strides. |
849 | 0 | pub fn into_flat_samples(self) -> FlatSamples<Container> |
850 | 0 | where |
851 | 0 | Container: AsRef<[P::Subpixel]>, |
852 | | { |
853 | | // None of these can overflow, as all our memory is addressable. |
854 | 0 | let layout = self.sample_layout(); |
855 | 0 | FlatSamples { |
856 | 0 | samples: self.data, |
857 | 0 | layout, |
858 | 0 | color_hint: None, // TODO: the pixel type might contain P::COLOR_TYPE if it satisfies PixelWithColorType |
859 | 0 | } |
860 | 0 | } |
861 | | |
862 | | /// Return a view on the raw sample buffer. |
863 | | /// |
864 | | /// See [`into_flat_samples`](#method.into_flat_samples) for more details. |
865 | 0 | pub fn as_flat_samples(&self) -> FlatSamples<&[P::Subpixel]> |
866 | 0 | where |
867 | 0 | Container: AsRef<[P::Subpixel]>, |
868 | | { |
869 | 0 | let layout = self.sample_layout(); |
870 | 0 | FlatSamples { |
871 | 0 | samples: self.data.as_ref(), |
872 | 0 | layout, |
873 | 0 | color_hint: None, // TODO: the pixel type might contain P::COLOR_TYPE if it satisfies PixelWithColorType |
874 | 0 | } |
875 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::as_flat_samples Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::as_flat_samples Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::as_flat_samples Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::as_flat_samples Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::as_flat_samples Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::as_flat_samples Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::as_flat_samples Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::as_flat_samples Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::as_flat_samples Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::as_flat_samples |
876 | | |
877 | | /// Return a mutable view on the raw sample buffer. |
878 | | /// |
879 | | /// See [`into_flat_samples`](#method.into_flat_samples) for more details. |
880 | 0 | pub fn as_flat_samples_mut(&mut self) -> FlatSamples<&mut [P::Subpixel]> |
881 | 0 | where |
882 | 0 | Container: AsMut<[P::Subpixel]>, |
883 | | { |
884 | 0 | let layout = self.sample_layout(); |
885 | 0 | FlatSamples { |
886 | 0 | samples: self.data.as_mut(), |
887 | 0 | layout, |
888 | 0 | color_hint: None, // TODO: the pixel type might contain P::COLOR_TYPE if it satisfies PixelWithColorType |
889 | 0 | } |
890 | 0 | } |
891 | | } |
892 | | |
893 | | impl<P, Container> ImageBuffer<P, Container> |
894 | | where |
895 | | P: Pixel, |
896 | | Container: Deref<Target = [P::Subpixel]> + DerefMut, |
897 | | { |
898 | | // TODO: choose name under which to expose. |
899 | 102 | pub(crate) fn inner_pixels_mut(&mut self) -> &mut [P::Subpixel] { |
900 | 102 | let len = Self::image_buffer_len(self.width, self.height).unwrap(); |
901 | 102 | &mut self.data[..len] |
902 | 102 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::inner_pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::inner_pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::inner_pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::inner_pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::inner_pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::inner_pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::inner_pixels_mut <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, &mut [u8]>>::inner_pixels_mut Line | Count | Source | 899 | 102 | pub(crate) fn inner_pixels_mut(&mut self) -> &mut [P::Subpixel] { | 900 | 102 | let len = Self::image_buffer_len(self.width, self.height).unwrap(); | 901 | 102 | &mut self.data[..len] | 902 | 102 | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::inner_pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::inner_pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::inner_pixels_mut |
903 | | |
904 | | /// Returns an iterator over the mutable pixels of this image. |
905 | 102 | pub fn pixels_mut(&mut self) -> PixelsMut<'_, P> { |
906 | 102 | PixelsMut { |
907 | 102 | chunks: self |
908 | 102 | .inner_pixels_mut() |
909 | 102 | .chunks_exact_mut(<P as Pixel>::CHANNEL_COUNT as usize), |
910 | 102 | } |
911 | 102 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::pixels_mut <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, &mut [u8]>>::pixels_mut Line | Count | Source | 905 | 102 | pub fn pixels_mut(&mut self) -> PixelsMut<'_, P> { | 906 | 102 | PixelsMut { | 907 | 102 | chunks: self | 908 | 102 | .inner_pixels_mut() | 909 | 102 | .chunks_exact_mut(<P as Pixel>::CHANNEL_COUNT as usize), | 910 | 102 | } | 911 | 102 | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::pixels_mut |
912 | | |
913 | | /// Returns an iterator over the mutable rows of this image. |
914 | | /// |
915 | | /// Only non-empty rows can be iterated in this manner. In particular the iterator will not |
916 | | /// yield any item when the width of the image is `0` or a pixel type without any channels is |
917 | | /// used. This ensures that its length can always be represented by `usize`. |
918 | 0 | pub fn rows_mut(&mut self) -> RowsMut<'_, P> { |
919 | 0 | RowsMut::with_image(&mut self.data, self.width, self.height) |
920 | 0 | } |
921 | | |
922 | | /// Enumerates over the pixels of the image. |
923 | | /// The iterator yields the coordinates of each pixel |
924 | | /// along with a mutable reference to them. |
925 | 102 | pub fn enumerate_pixels_mut(&mut self) -> EnumeratePixelsMut<'_, P> { |
926 | 102 | let width = self.width; |
927 | 102 | EnumeratePixelsMut { |
928 | 102 | pixels: self.pixels_mut(), |
929 | 102 | x: 0, |
930 | 102 | y: 0, |
931 | 102 | width, |
932 | 102 | } |
933 | 102 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::enumerate_pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::enumerate_pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::enumerate_pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::enumerate_pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::enumerate_pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::enumerate_pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::enumerate_pixels_mut <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, &mut [u8]>>::enumerate_pixels_mut Line | Count | Source | 925 | 102 | pub fn enumerate_pixels_mut(&mut self) -> EnumeratePixelsMut<'_, P> { | 926 | 102 | let width = self.width; | 927 | 102 | EnumeratePixelsMut { | 928 | 102 | pixels: self.pixels_mut(), | 929 | 102 | x: 0, | 930 | 102 | y: 0, | 931 | 102 | width, | 932 | 102 | } | 933 | 102 | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::enumerate_pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::enumerate_pixels_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::enumerate_pixels_mut |
934 | | |
935 | | /// Enumerates over the rows of the image. |
936 | | /// The iterator yields the y-coordinate of each row |
937 | | /// along with a mutable reference to them. |
938 | 0 | pub fn enumerate_rows_mut(&mut self) -> EnumerateRowsMut<'_, P> { |
939 | 0 | let width = self.width; |
940 | 0 | EnumerateRowsMut { |
941 | 0 | rows: self.rows_mut(), |
942 | 0 | y: 0, |
943 | 0 | width, |
944 | 0 | } |
945 | 0 | } |
946 | | |
947 | | /// Gets a reference to the mutable pixel at location `(x, y)` |
948 | | /// |
949 | | /// # Panics |
950 | | /// |
951 | | /// Panics if `(x, y)` is out of the bounds `(width, height)`. |
952 | | #[inline] |
953 | | #[track_caller] |
954 | 0 | pub fn get_pixel_mut(&mut self, x: u32, y: u32) -> &mut P { |
955 | 0 | match self.pixel_indices(x, y) { |
956 | 0 | None => panic!( |
957 | 0 | "Image index {:?} out of bounds {:?}", |
958 | 0 | (x, y), |
959 | 0 | (self.width, self.height) |
960 | | ), |
961 | 0 | Some(pixel_indices) => <P as Pixel>::from_slice_mut(&mut self.data[pixel_indices]), |
962 | | } |
963 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::get_pixel_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::get_pixel_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::get_pixel_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::get_pixel_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::get_pixel_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::get_pixel_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::get_pixel_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::get_pixel_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::get_pixel_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::get_pixel_mut |
964 | | |
965 | | /// Gets a reference to the mutable pixel at location `(x, y)` or returns |
966 | | /// `None` if the index is out of the bounds `(width, height)`. |
967 | 0 | pub fn get_pixel_mut_checked(&mut self, x: u32, y: u32) -> Option<&mut P> { |
968 | 0 | if x >= self.width { |
969 | 0 | return None; |
970 | 0 | } |
971 | 0 | let num_channels = <P as Pixel>::CHANNEL_COUNT as usize; |
972 | 0 | let i = (y as usize) |
973 | 0 | .saturating_mul(self.width as usize) |
974 | 0 | .saturating_add(x as usize) |
975 | 0 | .saturating_mul(num_channels); |
976 | | |
977 | 0 | self.data |
978 | 0 | .get_mut(i..i.checked_add(num_channels)?) |
979 | 0 | .map(|pixel_indices| <P as Pixel>::from_slice_mut(pixel_indices)) |
980 | 0 | } |
981 | | |
982 | | /// Puts a pixel at location `(x, y)` |
983 | | /// |
984 | | /// # Panics |
985 | | /// |
986 | | /// Panics if `(x, y)` is out of the bounds `(width, height)`. |
987 | | #[inline] |
988 | | #[track_caller] |
989 | 0 | pub fn put_pixel(&mut self, x: u32, y: u32, pixel: P) { |
990 | 0 | *self.get_pixel_mut(x, y) = pixel; |
991 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::put_pixel |
992 | | |
993 | | /// Crop this image in place, removing pixels outside of the bounding rectangle. |
994 | | /// |
995 | | /// This behaves similar to [`imageops::crop`](crate::imageops::crop) except no additional |
996 | | /// allocation takes place. The width and height of this buffer are adjusted as part of the |
997 | | /// operation. The selection is shrunk to the overlap with this image if it is out of bounds. |
998 | | /// |
999 | | /// The pixel buffer is *not* shrunk and will continue to occupy the same amount of memory as |
1000 | | /// before. See [`ImageBuffer::shrink_to_fit`] if the container is a [`Vec`]. |
1001 | | /// |
1002 | | /// # Examples |
1003 | | /// |
1004 | | /// ``` |
1005 | | /// use image::{RgbImage, math::Rect}; |
1006 | | /// |
1007 | | /// let mut img = RgbImage::new(128, 128); |
1008 | | /// img.put_pixel(64, 64, image::Rgb([255, 0, 0])); |
1009 | | /// |
1010 | | /// let selection = Rect::from_xy_ranges(64..128, 64..128); |
1011 | | /// img.crop_in_place(selection); |
1012 | | /// |
1013 | | /// assert_eq!(img.dimensions(), (64, 64)); |
1014 | | /// assert_eq!(img.get_pixel(0, 0), &image::Rgb([255, 0, 0])); |
1015 | | /// ``` |
1016 | | /// |
1017 | | /// Selections beyond the image bounds are clamped. |
1018 | | /// |
1019 | | /// ``` |
1020 | | /// use image::{RgbImage, math::Rect}; |
1021 | | /// |
1022 | | /// let mut img = RgbImage::new(32, 32); |
1023 | | /// let selection = Rect::from_xy_ranges(16..40, 16..24); |
1024 | | /// # use image::GenericImageView as _; |
1025 | | /// # assert_eq!(image::imageops::crop(&img, selection).dimensions(), (16, 8)); |
1026 | | /// |
1027 | | /// img.crop_in_place(selection); |
1028 | | /// assert_eq!(img.dimensions(), (16, 8)); |
1029 | | /// ``` |
1030 | 0 | pub fn crop_in_place(&mut self, selection: Rect) { |
1031 | 0 | let selection = selection.crop_dimms(self); |
1032 | | // We're now running essentially `copy_within` with differing source and destination row |
1033 | | // pitches. The above ensures that the target row pitch is smaller than our current one and |
1034 | | // we copy to offset `0` so always all data backwards. |
1035 | | // |
1036 | | // Since `selection` describes a smaller layout than our own, all indices that are computed |
1037 | | // from the pitches as type `usize` are within the bounds of the type and the underlying |
1038 | | // storage and we assume them to be valid. (If the `DerefMut` is malicious you'll get |
1039 | | // panics at runtime but that is not our problem, violating the contract of the container |
1040 | | // type for channels). |
1041 | 0 | let rowlen = (selection.width as usize) * usize::from(<P as Pixel>::CHANNEL_COUNT); |
1042 | | |
1043 | 0 | for y in 0..selection.height { |
1044 | 0 | let sy = selection.y + y; |
1045 | 0 | let source = self.pixel_indices_unchecked(selection.x, sy).start; |
1046 | 0 | let dst = y as usize * rowlen; |
1047 | 0 | self.data.copy_within(source..source + rowlen, dst); |
1048 | 0 | } |
1049 | | |
1050 | 0 | self.width = selection.width; |
1051 | 0 | self.height = selection.height; |
1052 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::crop_in_place Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::crop_in_place Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::crop_in_place Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::crop_in_place Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::crop_in_place Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::crop_in_place Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::crop_in_place Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::crop_in_place Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::crop_in_place Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::crop_in_place |
1053 | | } |
1054 | | |
1055 | | impl<P: Pixel, Container> ImageBuffer<P, Container> { |
1056 | | /// Define the color space for the image. |
1057 | | /// |
1058 | | /// The color data is unchanged. Reinterprets the existing red, blue, green channels as points |
1059 | | /// in the new set of primary colors, changing the apparent shade of pixels. |
1060 | | /// |
1061 | | /// Note that the primaries also define a reference whitepoint When this buffer contains Luma |
1062 | | /// data, the luminance channel is interpreted as the `Y` channel of a related `YCbCr` color |
1063 | | /// space as if by a non-constant chromaticity derived matrix. That is, coefficients are *not* |
1064 | | /// applied in the linear RGB space but use encoded channel values. (In a color space with the |
1065 | | /// linear transfer function there is no difference). |
1066 | | /// |
1067 | | /// The default color space is [`Cicp::SRGB`]. |
1068 | 3.90k | pub fn set_rgb_primaries(&mut self, color: CicpColorPrimaries) { |
1069 | 3.90k | self.color.primaries = color; |
1070 | 3.90k | } <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::set_rgb_primaries Line | Count | Source | 1068 | 273 | pub fn set_rgb_primaries(&mut self, color: CicpColorPrimaries) { | 1069 | 273 | self.color.primaries = color; | 1070 | 273 | } |
<image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::set_rgb_primaries Line | Count | Source | 1068 | 453 | pub fn set_rgb_primaries(&mut self, color: CicpColorPrimaries) { | 1069 | 453 | self.color.primaries = color; | 1070 | 453 | } |
<image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::set_rgb_primaries Line | Count | Source | 1068 | 85 | pub fn set_rgb_primaries(&mut self, color: CicpColorPrimaries) { | 1069 | 85 | self.color.primaries = color; | 1070 | 85 | } |
<image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::set_rgb_primaries Line | Count | Source | 1068 | 543 | pub fn set_rgb_primaries(&mut self, color: CicpColorPrimaries) { | 1069 | 543 | self.color.primaries = color; | 1070 | 543 | } |
<image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::set_rgb_primaries Line | Count | Source | 1068 | 159 | pub fn set_rgb_primaries(&mut self, color: CicpColorPrimaries) { | 1069 | 159 | self.color.primaries = color; | 1070 | 159 | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::set_rgb_primaries Line | Count | Source | 1068 | 1 | pub fn set_rgb_primaries(&mut self, color: CicpColorPrimaries) { | 1069 | 1 | self.color.primaries = color; | 1070 | 1 | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::set_rgb_primaries Line | Count | Source | 1068 | 2.28k | pub fn set_rgb_primaries(&mut self, color: CicpColorPrimaries) { | 1069 | 2.28k | self.color.primaries = color; | 1070 | 2.28k | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::set_rgb_primaries Line | Count | Source | 1068 | 31 | pub fn set_rgb_primaries(&mut self, color: CicpColorPrimaries) { | 1069 | 31 | self.color.primaries = color; | 1070 | 31 | } |
<image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::set_rgb_primaries Line | Count | Source | 1068 | 44 | pub fn set_rgb_primaries(&mut self, color: CicpColorPrimaries) { | 1069 | 44 | self.color.primaries = color; | 1070 | 44 | } |
<image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::set_rgb_primaries Line | Count | Source | 1068 | 30 | pub fn set_rgb_primaries(&mut self, color: CicpColorPrimaries) { | 1069 | 30 | self.color.primaries = color; | 1070 | 30 | } |
|
1071 | | |
1072 | | /// Define the transfer function for the image. |
1073 | | /// |
1074 | | /// The color data is unchanged. Reinterprets all (non-alpha) components in the image, |
1075 | | /// potentially changing the apparent shade of pixels. Individual components are always |
1076 | | /// interpreted as encoded numbers. To denote numbers in a linear RGB space, use |
1077 | | /// [`CicpTransferCharacteristics::Linear`]. |
1078 | | /// |
1079 | | /// The default color space is [`Cicp::SRGB`]. |
1080 | 3.90k | pub fn set_transfer_function(&mut self, tf: CicpTransferCharacteristics) { |
1081 | 3.90k | self.color.transfer = tf; |
1082 | 3.90k | } <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::set_transfer_function Line | Count | Source | 1080 | 273 | pub fn set_transfer_function(&mut self, tf: CicpTransferCharacteristics) { | 1081 | 273 | self.color.transfer = tf; | 1082 | 273 | } |
<image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::set_transfer_function Line | Count | Source | 1080 | 453 | pub fn set_transfer_function(&mut self, tf: CicpTransferCharacteristics) { | 1081 | 453 | self.color.transfer = tf; | 1082 | 453 | } |
<image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::set_transfer_function Line | Count | Source | 1080 | 85 | pub fn set_transfer_function(&mut self, tf: CicpTransferCharacteristics) { | 1081 | 85 | self.color.transfer = tf; | 1082 | 85 | } |
<image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::set_transfer_function Line | Count | Source | 1080 | 543 | pub fn set_transfer_function(&mut self, tf: CicpTransferCharacteristics) { | 1081 | 543 | self.color.transfer = tf; | 1082 | 543 | } |
<image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::set_transfer_function Line | Count | Source | 1080 | 159 | pub fn set_transfer_function(&mut self, tf: CicpTransferCharacteristics) { | 1081 | 159 | self.color.transfer = tf; | 1082 | 159 | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::set_transfer_function Line | Count | Source | 1080 | 1 | pub fn set_transfer_function(&mut self, tf: CicpTransferCharacteristics) { | 1081 | 1 | self.color.transfer = tf; | 1082 | 1 | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::set_transfer_function Line | Count | Source | 1080 | 2.28k | pub fn set_transfer_function(&mut self, tf: CicpTransferCharacteristics) { | 1081 | 2.28k | self.color.transfer = tf; | 1082 | 2.28k | } |
<image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::set_transfer_function Line | Count | Source | 1080 | 31 | pub fn set_transfer_function(&mut self, tf: CicpTransferCharacteristics) { | 1081 | 31 | self.color.transfer = tf; | 1082 | 31 | } |
<image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::set_transfer_function Line | Count | Source | 1080 | 44 | pub fn set_transfer_function(&mut self, tf: CicpTransferCharacteristics) { | 1081 | 44 | self.color.transfer = tf; | 1082 | 44 | } |
<image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::set_transfer_function Line | Count | Source | 1080 | 30 | pub fn set_transfer_function(&mut self, tf: CicpTransferCharacteristics) { | 1081 | 30 | self.color.transfer = tf; | 1082 | 30 | } |
|
1083 | | |
1084 | | /// Get the Cicp encoding of this buffer's color data. |
1085 | 0 | pub fn color_space(&self) -> Cicp { |
1086 | 0 | self.color.into() |
1087 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::color_space |
1088 | | |
1089 | | /// Set primaries and transfer characteristics from a Cicp color space. |
1090 | | /// |
1091 | | /// Returns an error if `cicp` uses features that are not support with an RGB color space, e.g. |
1092 | | /// a matrix or narrow range (studio encoding) channels. |
1093 | 0 | pub fn set_color_space(&mut self, cicp: Cicp) -> ImageResult<()> { |
1094 | 0 | self.color = cicp.try_into_rgb()?; |
1095 | 0 | Ok(()) |
1096 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::set_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::set_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::set_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::set_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::set_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::set_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::set_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::set_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::set_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::set_color_space |
1097 | | |
1098 | 0 | pub(crate) fn set_rgb_color_space(&mut self, color: CicpRgb) { |
1099 | 0 | self.color = color; |
1100 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::set_rgb_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::set_rgb_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::set_rgb_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::set_rgb_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::set_rgb_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::set_rgb_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::set_rgb_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::set_rgb_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::set_rgb_color_space Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::set_rgb_color_space |
1101 | | } |
1102 | | |
1103 | | impl<P, Container> ImageBuffer<P, Container> |
1104 | | where |
1105 | | P: Pixel, |
1106 | | [P::Subpixel]: EncodableLayout, |
1107 | | Container: Deref<Target = [P::Subpixel]>, |
1108 | | { |
1109 | | /// Saves the buffer to a file at the path specified. |
1110 | | /// |
1111 | | /// The image format is derived from the file extension. |
1112 | 0 | pub fn save<Q>(&self, path: Q) -> ImageResult<()> |
1113 | 0 | where |
1114 | 0 | Q: AsRef<Path>, |
1115 | 0 | P: PixelWithColorType, |
1116 | | { |
1117 | 0 | save_buffer( |
1118 | 0 | path, |
1119 | 0 | self.inner_pixels().as_bytes(), |
1120 | 0 | self.width(), |
1121 | 0 | self.height(), |
1122 | | <P as PixelWithColorType>::COLOR_TYPE, |
1123 | | ) |
1124 | 0 | } |
1125 | | } |
1126 | | |
1127 | | impl<P, Container> ImageBuffer<P, Container> |
1128 | | where |
1129 | | P: Pixel, |
1130 | | [P::Subpixel]: EncodableLayout, |
1131 | | Container: Deref<Target = [P::Subpixel]>, |
1132 | | { |
1133 | | /// Saves the buffer to a file at the specified path in |
1134 | | /// the specified format. |
1135 | | /// |
1136 | | /// See [`save_buffer_with_format`](fn.save_buffer_with_format.html) for |
1137 | | /// supported types. |
1138 | 0 | pub fn save_with_format<Q>(&self, path: Q, format: ImageFormat) -> ImageResult<()> |
1139 | 0 | where |
1140 | 0 | Q: AsRef<Path>, |
1141 | 0 | P: PixelWithColorType, |
1142 | | { |
1143 | | // This is valid as the subpixel is u8. |
1144 | 0 | save_buffer_with_format( |
1145 | 0 | path, |
1146 | 0 | self.inner_pixels().as_bytes(), |
1147 | 0 | self.width(), |
1148 | 0 | self.height(), |
1149 | | <P as PixelWithColorType>::COLOR_TYPE, |
1150 | 0 | format, |
1151 | | ) |
1152 | 0 | } |
1153 | | } |
1154 | | |
1155 | | impl<P, Container> ImageBuffer<P, Container> |
1156 | | where |
1157 | | P: Pixel, |
1158 | | [P::Subpixel]: EncodableLayout, |
1159 | | Container: Deref<Target = [P::Subpixel]>, |
1160 | | { |
1161 | | /// Writes the buffer to a writer in the specified format. |
1162 | | /// |
1163 | | /// Assumes the writer is buffered. In most cases, you should wrap your writer in a `BufWriter` |
1164 | | /// for best performance. |
1165 | 1.83k | pub fn write_to<W>(&self, writer: &mut W, format: ImageFormat) -> ImageResult<()> |
1166 | 1.83k | where |
1167 | 1.83k | W: std::io::Write + std::io::Seek, |
1168 | 1.83k | P: PixelWithColorType, |
1169 | | { |
1170 | | // This is valid as the subpixel is u8. |
1171 | 1.83k | write_buffer_with_format( |
1172 | 1.83k | writer, |
1173 | 1.83k | self.inner_pixels().as_bytes(), |
1174 | 1.83k | self.width(), |
1175 | 1.83k | self.height(), |
1176 | | <P as PixelWithColorType>::COLOR_TYPE, |
1177 | 1.83k | format, |
1178 | | ) |
1179 | 1.83k | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<_, _>>::write_to::<_> <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::write_to::<std::io::cursor::Cursor<alloc::vec::Vec<u8>>> Line | Count | Source | 1165 | 1.83k | pub fn write_to<W>(&self, writer: &mut W, format: ImageFormat) -> ImageResult<()> | 1166 | 1.83k | where | 1167 | 1.83k | W: std::io::Write + std::io::Seek, | 1168 | 1.83k | P: PixelWithColorType, | 1169 | | { | 1170 | | // This is valid as the subpixel is u8. | 1171 | 1.83k | write_buffer_with_format( | 1172 | 1.83k | writer, | 1173 | 1.83k | self.inner_pixels().as_bytes(), | 1174 | 1.83k | self.width(), | 1175 | 1.83k | self.height(), | 1176 | | <P as PixelWithColorType>::COLOR_TYPE, | 1177 | 1.83k | format, | 1178 | | ) | 1179 | 1.83k | } |
|
1180 | | } |
1181 | | |
1182 | | impl<P, Container> ImageBuffer<P, Container> |
1183 | | where |
1184 | | P: Pixel, |
1185 | | [P::Subpixel]: EncodableLayout, |
1186 | | Container: Deref<Target = [P::Subpixel]>, |
1187 | | { |
1188 | | /// Writes the buffer with the given encoder. |
1189 | 0 | pub fn write_with_encoder<E>(&self, encoder: E) -> ImageResult<()> |
1190 | 0 | where |
1191 | 0 | E: ImageEncoder, |
1192 | 0 | P: PixelWithColorType, |
1193 | | { |
1194 | | // This is valid as the subpixel is u8. |
1195 | 0 | encoder.write_image( |
1196 | 0 | self.inner_pixels().as_bytes(), |
1197 | 0 | self.width(), |
1198 | 0 | self.height(), |
1199 | | <P as PixelWithColorType>::COLOR_TYPE, |
1200 | | ) |
1201 | 0 | } |
1202 | | } |
1203 | | |
1204 | | impl<P, Container> Default for ImageBuffer<P, Container> |
1205 | | where |
1206 | | P: Pixel, |
1207 | | Container: Default, |
1208 | | { |
1209 | 0 | fn default() -> Self { |
1210 | 0 | Self { |
1211 | 0 | width: 0, |
1212 | 0 | height: 0, |
1213 | 0 | _phantom: PhantomData, |
1214 | 0 | color: Cicp::SRGB_LINEAR.into_rgb(), |
1215 | 0 | data: Default::default(), |
1216 | 0 | } |
1217 | 0 | } |
1218 | | } |
1219 | | |
1220 | | impl<P, Container> Deref for ImageBuffer<P, Container> |
1221 | | where |
1222 | | P: Pixel, |
1223 | | Container: Deref<Target = [P::Subpixel]>, |
1224 | | { |
1225 | | type Target = [P::Subpixel]; |
1226 | | |
1227 | 0 | fn deref(&self) -> &<Self as Deref>::Target { |
1228 | 0 | &self.data |
1229 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>> as core::ops::deref::Deref>::deref Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>> as core::ops::deref::Deref>::deref Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>> as core::ops::deref::Deref>::deref Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>> as core::ops::deref::Deref>::deref Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>> as core::ops::deref::Deref>::deref Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>> as core::ops::deref::Deref>::deref Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>> as core::ops::deref::Deref>::deref Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>> as core::ops::deref::Deref>::deref Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>> as core::ops::deref::Deref>::deref Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>> as core::ops::deref::Deref>::deref |
1230 | | } |
1231 | | |
1232 | | impl<P, Container> DerefMut for ImageBuffer<P, Container> |
1233 | | where |
1234 | | P: Pixel, |
1235 | | Container: Deref<Target = [P::Subpixel]> + DerefMut, |
1236 | | { |
1237 | 0 | fn deref_mut(&mut self) -> &mut <Self as Deref>::Target { |
1238 | 0 | &mut self.data |
1239 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>> as core::ops::deref::DerefMut>::deref_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>> as core::ops::deref::DerefMut>::deref_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>> as core::ops::deref::DerefMut>::deref_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>> as core::ops::deref::DerefMut>::deref_mut Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>> as core::ops::deref::DerefMut>::deref_mut |
1240 | | } |
1241 | | |
1242 | | impl<P, Container> Index<(u32, u32)> for ImageBuffer<P, Container> |
1243 | | where |
1244 | | P: Pixel, |
1245 | | Container: Deref<Target = [P::Subpixel]>, |
1246 | | { |
1247 | | type Output = P; |
1248 | | |
1249 | 0 | fn index(&self, (x, y): (u32, u32)) -> &P { |
1250 | 0 | self.get_pixel(x, y) |
1251 | 0 | } |
1252 | | } |
1253 | | |
1254 | | impl<P, Container> IndexMut<(u32, u32)> for ImageBuffer<P, Container> |
1255 | | where |
1256 | | P: Pixel, |
1257 | | Container: Deref<Target = [P::Subpixel]> + DerefMut, |
1258 | | { |
1259 | 0 | fn index_mut(&mut self, (x, y): (u32, u32)) -> &mut P { |
1260 | 0 | self.get_pixel_mut(x, y) |
1261 | 0 | } |
1262 | | } |
1263 | | |
1264 | | impl<P, Container> Clone for ImageBuffer<P, Container> |
1265 | | where |
1266 | | P: Pixel, |
1267 | | Container: Deref<Target = [P::Subpixel]> + Clone, |
1268 | | { |
1269 | 0 | fn clone(&self) -> ImageBuffer<P, Container> { |
1270 | 0 | ImageBuffer { |
1271 | 0 | data: self.data.clone(), |
1272 | 0 | width: self.width, |
1273 | 0 | height: self.height, |
1274 | 0 | color: self.color, |
1275 | 0 | _phantom: PhantomData, |
1276 | 0 | } |
1277 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>> as core::clone::Clone>::clone Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>> as core::clone::Clone>::clone Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>> as core::clone::Clone>::clone Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>> as core::clone::Clone>::clone Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>> as core::clone::Clone>::clone Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>> as core::clone::Clone>::clone Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>> as core::clone::Clone>::clone Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>> as core::clone::Clone>::clone Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>> as core::clone::Clone>::clone Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>> as core::clone::Clone>::clone |
1278 | | |
1279 | 0 | fn clone_from(&mut self, source: &Self) { |
1280 | 0 | self.data.clone_from(&source.data); |
1281 | 0 | self.width = source.width; |
1282 | 0 | self.height = source.height; |
1283 | 0 | self.color = source.color; |
1284 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>> as core::clone::Clone>::clone_from Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>> as core::clone::Clone>::clone_from Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>> as core::clone::Clone>::clone_from Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>> as core::clone::Clone>::clone_from Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>> as core::clone::Clone>::clone_from Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>> as core::clone::Clone>::clone_from Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>> as core::clone::Clone>::clone_from Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>> as core::clone::Clone>::clone_from Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>> as core::clone::Clone>::clone_from Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>> as core::clone::Clone>::clone_from |
1285 | | } |
1286 | | |
1287 | | impl<P, Container> fmt::Debug for ImageBuffer<P, Container> |
1288 | | where |
1289 | | P: Pixel + fmt::Debug, |
1290 | | Container: fmt::Debug, |
1291 | | { |
1292 | 0 | fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
1293 | 0 | let mut pixel = std::any::type_name::<P>(); |
1294 | 0 | pixel = pixel.strip_prefix("image::color::").unwrap_or(pixel); |
1295 | | |
1296 | 0 | let mut debug_struct = f.debug_struct(&format!("ImageBuffer::<{pixel}, _>")); |
1297 | 0 | debug_struct.field("width", &self.width); |
1298 | 0 | debug_struct.field("height", &self.height); |
1299 | | |
1300 | 0 | if let Some(color_name) = self.color.known_name() { |
1301 | 0 | debug_struct.field("color", &color_name); |
1302 | 0 | } else { |
1303 | 0 | debug_struct.field("color", &self.color); |
1304 | 0 | } |
1305 | | |
1306 | 0 | debug_struct.finish() |
1307 | 0 | } |
1308 | | } |
1309 | | |
1310 | | impl<P, Container> GenericImageView for ImageBuffer<P, Container> |
1311 | | where |
1312 | | P: Pixel, |
1313 | | Container: Deref<Target = [P::Subpixel]> + Deref, |
1314 | | { |
1315 | | type Pixel = P; |
1316 | | |
1317 | 0 | fn dimensions(&self) -> (u32, u32) { |
1318 | 0 | self.dimensions() |
1319 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>> as image::images::generic_image::GenericImageView>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImageView>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImageView>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImageView>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImageView>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>> as image::images::generic_image::GenericImageView>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImageView>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImageView>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImageView>::dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImageView>::dimensions |
1320 | | |
1321 | 0 | fn get_pixel(&self, x: u32, y: u32) -> P { |
1322 | 0 | *self.get_pixel(x, y) |
1323 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>> as image::images::generic_image::GenericImageView>::get_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImageView>::get_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImageView>::get_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImageView>::get_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImageView>::get_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>> as image::images::generic_image::GenericImageView>::get_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImageView>::get_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImageView>::get_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImageView>::get_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImageView>::get_pixel |
1324 | | |
1325 | | /// Returns the pixel located at (x, y), ignoring bounds checking. |
1326 | | #[inline(always)] |
1327 | 0 | unsafe fn unsafe_get_pixel(&self, x: u32, y: u32) -> P { |
1328 | 0 | let indices = self.pixel_indices_unchecked(x, y); |
1329 | 0 | *<P as Pixel>::from_slice(self.data.get_unchecked(indices)) |
1330 | 0 | } |
1331 | | |
1332 | 0 | fn buffer_with_dimensions(&self, width: u32, height: u32) -> ImageBuffer<P, Vec<P::Subpixel>> { |
1333 | 0 | let mut buffer = ImageBuffer::new(width, height); |
1334 | 0 | buffer.copy_color_space_from(self); |
1335 | 0 | buffer |
1336 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>> as image::images::generic_image::GenericImageView>::buffer_with_dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImageView>::buffer_with_dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImageView>::buffer_with_dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImageView>::buffer_with_dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImageView>::buffer_with_dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>> as image::images::generic_image::GenericImageView>::buffer_with_dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImageView>::buffer_with_dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImageView>::buffer_with_dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImageView>::buffer_with_dimensions Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImageView>::buffer_with_dimensions |
1337 | | } |
1338 | | |
1339 | | impl<P, Container> GenericImage for ImageBuffer<P, Container> |
1340 | | where |
1341 | | P: Pixel, |
1342 | | Container: Deref<Target = [P::Subpixel]> + DerefMut, |
1343 | | { |
1344 | 0 | fn get_pixel_mut(&mut self, x: u32, y: u32) -> &mut P { |
1345 | 0 | self.get_pixel_mut(x, y) |
1346 | 0 | } |
1347 | | |
1348 | 0 | fn put_pixel(&mut self, x: u32, y: u32, pixel: P) { |
1349 | 0 | *self.get_pixel_mut(x, y) = pixel; |
1350 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>> as image::images::generic_image::GenericImage>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImage>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImage>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImage>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImage>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>> as image::images::generic_image::GenericImage>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImage>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImage>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImage>::put_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImage>::put_pixel |
1351 | | |
1352 | | /// Puts a pixel at location (x, y), ignoring bounds checking. |
1353 | | #[inline(always)] |
1354 | 0 | unsafe fn unsafe_put_pixel(&mut self, x: u32, y: u32, pixel: P) { |
1355 | 0 | let indices = self.pixel_indices_unchecked(x, y); |
1356 | 0 | let p = <P as Pixel>::from_slice_mut(self.data.get_unchecked_mut(indices)); |
1357 | 0 | *p = pixel; |
1358 | 0 | } |
1359 | | |
1360 | | /// Put a pixel at location (x, y), taking into account alpha channels |
1361 | | /// |
1362 | | /// DEPRECATED: This method will be removed. Blend the pixel directly instead. |
1363 | 0 | fn blend_pixel(&mut self, x: u32, y: u32, p: P) { |
1364 | 0 | self.get_pixel_mut(x, y).blend(&p); |
1365 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>> as image::images::generic_image::GenericImage>::blend_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImage>::blend_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImage>::blend_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImage>::blend_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImage>::blend_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>> as image::images::generic_image::GenericImage>::blend_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImage>::blend_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImage>::blend_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>> as image::images::generic_image::GenericImage>::blend_pixel Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>> as image::images::generic_image::GenericImage>::blend_pixel |
1366 | | |
1367 | 0 | fn copy_within(&mut self, source: Rect, x: u32, y: u32) -> bool { |
1368 | | let Rect { |
1369 | 0 | x: sx, |
1370 | 0 | y: sy, |
1371 | 0 | width, |
1372 | 0 | height, |
1373 | 0 | } = source; |
1374 | 0 | let dx = x; |
1375 | 0 | let dy = y; |
1376 | 0 | assert!(sx < self.width() && dx < self.width()); |
1377 | 0 | assert!(sy < self.height() && dy < self.height()); |
1378 | 0 | if self.width() - dx.max(sx) < width || self.height() - dy.max(sy) < height { |
1379 | 0 | return false; |
1380 | 0 | } |
1381 | | |
1382 | 0 | if sy < dy { |
1383 | 0 | for y in (0..height).rev() { |
1384 | 0 | let sy = sy + y; |
1385 | 0 | let dy = dy + y; |
1386 | 0 | let Range { start, .. } = self.pixel_indices_unchecked(sx, sy); |
1387 | 0 | let Range { end, .. } = self.pixel_indices_unchecked(sx + width - 1, sy); |
1388 | 0 | let dst = self.pixel_indices_unchecked(dx, dy).start; |
1389 | 0 | self.data.copy_within(start..end, dst); |
1390 | 0 | } |
1391 | | } else { |
1392 | 0 | for y in 0..height { |
1393 | 0 | let sy = sy + y; |
1394 | 0 | let dy = dy + y; |
1395 | 0 | let Range { start, .. } = self.pixel_indices_unchecked(sx, sy); |
1396 | 0 | let Range { end, .. } = self.pixel_indices_unchecked(sx + width - 1, sy); |
1397 | 0 | let dst = self.pixel_indices_unchecked(dx, dy).start; |
1398 | 0 | self.data.copy_within(start..end, dst); |
1399 | 0 | } |
1400 | | } |
1401 | 0 | true |
1402 | 0 | } |
1403 | | } |
1404 | | |
1405 | | // concrete implementation for `Vec`-backed buffers |
1406 | | // TODO: I think that rustc does not "see" this impl any more: the impl with |
1407 | | // Container meets the same requirements. At least, I got compile errors that |
1408 | | // there is no such function as `into_vec`, whereas `into_raw` did work, and |
1409 | | // `into_vec` is redundant anyway, because `into_raw` will give you the vector, |
1410 | | // and it is more generic. |
1411 | | impl<P: Pixel> ImageBuffer<P, Vec<P::Subpixel>> { |
1412 | | /// Creates a new image buffer based on a `Vec<P::Subpixel>`. |
1413 | | /// |
1414 | | /// all the pixels of this image have a value of zero, regardless of the data type or number of channels. |
1415 | | /// |
1416 | | /// The color space is initially set to [`sRGB`][`Cicp::SRGB`]. |
1417 | | /// |
1418 | | /// # Panics |
1419 | | /// |
1420 | | /// Panics when the resulting image is larger than the maximum size of a vector. |
1421 | | #[must_use] |
1422 | 0 | pub fn new(width: u32, height: u32) -> ImageBuffer<P, Vec<P::Subpixel>> { |
1423 | 0 | let size = Self::image_buffer_len(width, height) |
1424 | 0 | .expect("Buffer length in `ImageBuffer::new` overflows usize"); |
1425 | 0 | ImageBuffer { |
1426 | 0 | data: vec![Zero::zero(); size], |
1427 | 0 | width, |
1428 | 0 | height, |
1429 | 0 | color: Cicp::SRGB.into_rgb(), |
1430 | 0 | _phantom: PhantomData, |
1431 | 0 | } |
1432 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::new Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::new Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::new Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::new Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::new Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::new Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::new Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::new Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::new Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::new |
1433 | | |
1434 | | /// Constructs a new `ImageBuffer` by copying a pixel |
1435 | | /// |
1436 | | /// # Panics |
1437 | | /// |
1438 | | /// Panics when the resulting image is larger than the maximum size of a vector. |
1439 | 0 | pub fn from_pixel(width: u32, height: u32, pixel: P) -> ImageBuffer<P, Vec<P::Subpixel>> { |
1440 | 0 | let mut buf = ImageBuffer::new(width, height); |
1441 | 0 | for p in buf.pixels_mut() { |
1442 | 0 | *p = pixel; |
1443 | 0 | } |
1444 | 0 | buf |
1445 | 0 | } |
1446 | | |
1447 | | /// Constructs a new `ImageBuffer` by repeated application of the supplied function. |
1448 | | /// |
1449 | | /// The arguments to the function are the pixel's x and y coordinates. |
1450 | | /// |
1451 | | /// # Panics |
1452 | | /// |
1453 | | /// Panics when the resulting image is larger than the maximum size of a vector. |
1454 | 0 | pub fn from_fn<F>(width: u32, height: u32, mut f: F) -> ImageBuffer<P, Vec<P::Subpixel>> |
1455 | 0 | where |
1456 | 0 | F: FnMut(u32, u32) -> P, |
1457 | | { |
1458 | 0 | let mut buf = ImageBuffer::new(width, height); |
1459 | 0 | for (x, y, p) in buf.enumerate_pixels_mut() { |
1460 | 0 | *p = f(x, y); |
1461 | 0 | } |
1462 | 0 | buf |
1463 | 0 | } |
1464 | | |
1465 | | /// Creates an image buffer out of an existing buffer. |
1466 | | /// Returns None if the buffer is not big enough. |
1467 | | #[must_use] |
1468 | 1.83k | pub fn from_vec( |
1469 | 1.83k | width: u32, |
1470 | 1.83k | height: u32, |
1471 | 1.83k | buf: Vec<P::Subpixel>, |
1472 | 1.83k | ) -> Option<ImageBuffer<P, Vec<P::Subpixel>>> { |
1473 | 1.83k | ImageBuffer::from_raw(width, height, buf) |
1474 | 1.83k | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::from_vec Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::from_vec Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::from_vec Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::from_vec Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::from_vec Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::from_vec Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::from_vec <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::from_vec Line | Count | Source | 1468 | 1.83k | pub fn from_vec( | 1469 | 1.83k | width: u32, | 1470 | 1.83k | height: u32, | 1471 | 1.83k | buf: Vec<P::Subpixel>, | 1472 | 1.83k | ) -> Option<ImageBuffer<P, Vec<P::Subpixel>>> { | 1473 | 1.83k | ImageBuffer::from_raw(width, height, buf) | 1474 | 1.83k | } |
Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::from_vec Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::from_vec Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::from_vec Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::from_vec |
1475 | | |
1476 | | /// Consumes the image buffer and returns the underlying data |
1477 | | /// as an owned buffer |
1478 | | #[must_use] |
1479 | 1.83k | pub fn into_vec(self) -> Vec<P::Subpixel> { |
1480 | 1.83k | self.into_raw() |
1481 | 1.83k | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<_, alloc::vec::Vec<<_ as image::traits::Pixel>::Subpixel>>>::into_vec <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::into_vec Line | Count | Source | 1479 | 1.83k | pub fn into_vec(self) -> Vec<P::Subpixel> { | 1480 | 1.83k | self.into_raw() | 1481 | 1.83k | } |
|
1482 | | |
1483 | | /// Shrink the length and capacity of the data buffer to fit the image size. |
1484 | | /// |
1485 | | /// This is useful after shrinking an image in-place, e.g. via cropping, to free unused memory |
1486 | | /// or in case the image was created from a buffer with excess capacity. |
1487 | | /// |
1488 | | /// ``` |
1489 | | /// use image::RgbImage; |
1490 | | /// |
1491 | | /// let data = vec![0u8; 10000]; |
1492 | | /// // `from_raw` allows excess data |
1493 | | /// let mut img = RgbImage::from_raw(16, 16, data).unwrap(); |
1494 | | /// img.shrink_to_fit(); |
1495 | | /// |
1496 | | /// assert_eq!(img.into_vec().len(), 16 * 16 * 3); |
1497 | | /// ``` |
1498 | 0 | pub fn shrink_to_fit(&mut self) { |
1499 | 0 | let need = self.inner_pixels().len(); |
1500 | 0 | self.data.truncate(need); |
1501 | 0 | self.data.shrink_to_fit(); |
1502 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::shrink_to_fit Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::shrink_to_fit Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::shrink_to_fit Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::shrink_to_fit Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::shrink_to_fit Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::shrink_to_fit Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::shrink_to_fit Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::shrink_to_fit Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::shrink_to_fit Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::shrink_to_fit |
1503 | | |
1504 | | /// Transfer the meta data, not the pixel values. |
1505 | | /// |
1506 | | /// This will reinterpret all the pixels. |
1507 | | /// |
1508 | | /// We may want to export this but under what name? |
1509 | 0 | pub(crate) fn copy_color_space_from<O: Pixel, C>(&mut self, other: &ImageBuffer<O, C>) { |
1510 | 0 | self.color = other.color; |
1511 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgb<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgb<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgb<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Luma<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Luma<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Luma<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgba<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgba<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgba<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::LumaA<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::LumaA<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgb<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgb<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgb<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Luma<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Luma<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgba<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgba<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgba<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::LumaA<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::LumaA<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgb<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgb<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgb<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Luma<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Luma<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgba<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgba<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgba<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::LumaA<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::LumaA<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Luma<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Luma<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgb<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgb<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgb<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgba<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgba<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgba<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::LumaA<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::LumaA<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Luma<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Luma<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgb<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgb<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgb<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgba<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgba<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgba<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::LumaA<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::LumaA<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Luma<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Luma<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgb<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgb<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgb<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgba<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgba<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgba<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::LumaA<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::LumaA<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgba<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgba<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgba<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgb<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgb<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgb<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Luma<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Luma<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::LumaA<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::LumaA<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::LumaA<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgba<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgba<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgba<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgb<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgb<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgb<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Luma<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Luma<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::LumaA<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::LumaA<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgba<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgba<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgba<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgb<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgb<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgb<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Luma<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Luma<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::LumaA<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::LumaA<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::LumaA<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::LumaA<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgb<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgb<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgb<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Luma<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Luma<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgba<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgba<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>>>::copy_color_space_from::<image::color::Rgba<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::LumaA<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::LumaA<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgb<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgb<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgb<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Luma<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Luma<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgba<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgba<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgba<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::LumaA<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::LumaA<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgb<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgb<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgb<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Luma<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Luma<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgba<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgba<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::copy_color_space_from::<image::color::Rgba<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgba<u16>, &[u16]> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Rgb<u16>, &[u16]> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Luma<u8>, &[u8]> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::Luma<u16>, &[u16]> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::LumaA<u8>, &[u8]> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_color_space_from::<image::color::LumaA<u16>, &[u16]> |
1512 | | } |
1513 | | |
1514 | | impl<S, Container> ImageBuffer<Rgb<S>, Container> |
1515 | | where |
1516 | | Rgb<S>: PixelWithColorType<Subpixel = S>, |
1517 | | Container: DerefMut<Target = [S]>, |
1518 | | { |
1519 | | /// Construct an image by swapping `Bgr` channels into an `Rgb` order. |
1520 | 0 | pub fn from_raw_bgr(width: u32, height: u32, container: Container) -> Option<Self> { |
1521 | 0 | let mut img = Self::from_raw(width, height, container)?; |
1522 | | |
1523 | 0 | for pix in img.pixels_mut() { |
1524 | 0 | pix.0.reverse(); |
1525 | 0 | } |
1526 | | |
1527 | 0 | Some(img) |
1528 | 0 | } |
1529 | | |
1530 | | /// Return the underlying raw buffer after converting it into `Bgr` channel order. |
1531 | 0 | pub fn into_raw_bgr(mut self) -> Container { |
1532 | 0 | for pix in self.pixels_mut() { |
1533 | 0 | pix.0.reverse(); |
1534 | 0 | } |
1535 | | |
1536 | 0 | self.into_raw() |
1537 | 0 | } |
1538 | | } |
1539 | | |
1540 | | impl<S, Container> ImageBuffer<Rgba<S>, Container> |
1541 | | where |
1542 | | Rgba<S>: PixelWithColorType<Subpixel = S>, |
1543 | | Container: DerefMut<Target = [S]>, |
1544 | | { |
1545 | | /// Construct an image by swapping `BgrA` channels into an `RgbA` order. |
1546 | 0 | pub fn from_raw_bgra(width: u32, height: u32, container: Container) -> Option<Self> { |
1547 | 0 | let mut img = Self::from_raw(width, height, container)?; |
1548 | | |
1549 | 0 | for pix in img.pixels_mut() { |
1550 | 0 | pix.0[..3].reverse(); |
1551 | 0 | } |
1552 | | |
1553 | 0 | Some(img) |
1554 | 0 | } |
1555 | | |
1556 | | /// Return the underlying raw buffer after converting it into `BgrA` channel order. |
1557 | 0 | pub fn into_raw_bgra(mut self) -> Container { |
1558 | 0 | for pix in self.pixels_mut() { |
1559 | 0 | pix.0[..3].reverse(); |
1560 | 0 | } |
1561 | | |
1562 | 0 | self.into_raw() |
1563 | 0 | } |
1564 | | } |
1565 | | |
1566 | | /// Provides color conversions for whole image buffers. |
1567 | | pub trait ConvertBuffer<T> { |
1568 | | /// Converts `self` to a buffer of type T |
1569 | | /// |
1570 | | /// A generic implementation is provided to convert any image buffer to a image buffer |
1571 | | /// based on a `Vec<T>`. |
1572 | | fn convert(&self) -> T; |
1573 | | } |
1574 | | |
1575 | | // concrete implementation Luma -> Rgba |
1576 | | impl GrayImage { |
1577 | | /// Expands a color palette by re-using the existing buffer. |
1578 | | /// Assumes 8 bit per pixel. Uses an optionally transparent index to |
1579 | | /// adjust it's alpha value accordingly. |
1580 | | #[must_use] |
1581 | 0 | pub fn expand_palette( |
1582 | 0 | self, |
1583 | 0 | palette: &[(u8, u8, u8)], |
1584 | 0 | transparent_idx: Option<u8>, |
1585 | 0 | ) -> RgbaImage { |
1586 | 0 | let (width, height) = self.dimensions(); |
1587 | 0 | let mut data = self.into_raw(); |
1588 | 0 | let entries = data.len(); |
1589 | 0 | data.resize(entries.checked_mul(4).unwrap(), 0); |
1590 | 0 | let mut buffer = ImageBuffer::from_vec(width, height, data).unwrap(); |
1591 | 0 | expand_packed(&mut buffer, 4, 8, |idx, pixel| { |
1592 | 0 | let (r, g, b) = palette[idx as usize]; |
1593 | 0 | let a = if let Some(t_idx) = transparent_idx { |
1594 | 0 | if t_idx == idx { |
1595 | 0 | 0 |
1596 | | } else { |
1597 | 0 | 255 |
1598 | | } |
1599 | | } else { |
1600 | 0 | 255 |
1601 | | }; |
1602 | 0 | pixel[0] = r; |
1603 | 0 | pixel[1] = g; |
1604 | 0 | pixel[2] = b; |
1605 | 0 | pixel[3] = a; |
1606 | 0 | }); |
1607 | 0 | buffer |
1608 | 0 | } |
1609 | | } |
1610 | | |
1611 | | /// This copies the color space information but is somewhat wrong, in numeric terms this conversion |
1612 | | /// fails to actually convert rgb/luma with consistent treatment. But this trait impl is too |
1613 | | /// generic to handle it correctly (missing any CICP related parameter for the coefficients) so the |
1614 | | /// best effort here is to copy the metadata and have slighly incorrect color. May you've only been |
1615 | | /// adding an alpha channel or converting sample types, which is fine. |
1616 | | /// |
1617 | | /// It will very likely be deprecated in a future release. |
1618 | | impl<Container, FromType: Pixel, ToType: Pixel> |
1619 | | ConvertBuffer<ImageBuffer<ToType, Vec<ToType::Subpixel>>> for ImageBuffer<FromType, Container> |
1620 | | where |
1621 | | Container: Deref<Target = [FromType::Subpixel]>, |
1622 | | ToType: FromColor<FromType>, |
1623 | | { |
1624 | | /// # Examples |
1625 | | /// Convert RGB image to gray image. |
1626 | | /// ```no_run |
1627 | | /// use image::buffer::ConvertBuffer; |
1628 | | /// use image::GrayImage; |
1629 | | /// |
1630 | | /// let image_path = "examples/fractal.png"; |
1631 | | /// let image = image::open(&image_path) |
1632 | | /// .expect("Open file failed") |
1633 | | /// .to_rgba8(); |
1634 | | /// |
1635 | | /// let gray_image: GrayImage = image.convert(); |
1636 | | /// ``` |
1637 | 0 | fn convert(&self) -> ImageBuffer<ToType, Vec<ToType::Subpixel>> { |
1638 | 0 | let mut buffer: ImageBuffer<ToType, Vec<ToType::Subpixel>> = |
1639 | 0 | ImageBuffer::new(self.width, self.height); |
1640 | 0 | buffer.copy_color_space_from(self); |
1641 | 0 | for (to, from) in buffer.pixels_mut().zip(self.pixels()) { |
1642 | 0 | to.from_color(from); |
1643 | 0 | } |
1644 | 0 | buffer |
1645 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<f32>, alloc::vec::Vec<f32>> as image::images::buffer::ConvertBuffer<image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>>::convert Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<f32>, alloc::vec::Vec<f32>> as image::images::buffer::ConvertBuffer<image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>>::convert Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, &[u16]> as image::images::buffer::ConvertBuffer<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>>::convert Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, &[u8]> as image::images::buffer::ConvertBuffer<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>>::convert Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, &[u16]> as image::images::buffer::ConvertBuffer<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>>::convert Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, &[u16]> as image::images::buffer::ConvertBuffer<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>>::convert Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, &[u8]> as image::images::buffer::ConvertBuffer<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>>::convert Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, &[u16]> as image::images::buffer::ConvertBuffer<image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>>::convert |
1646 | | } |
1647 | | |
1648 | | /// Inputs to [`ImageBuffer::copy_from_color_space`]. |
1649 | | #[non_exhaustive] |
1650 | | #[derive(Default)] |
1651 | | pub struct ConvertColorOptions { |
1652 | | /// A pre-calculated transform. This is only used when the actual colors of the input and |
1653 | | /// output image match the color spaces with which the was constructed. |
1654 | | /// |
1655 | | /// FIXME: Clarify that the transform is cheap to clone, i.e. internally an Arc of precomputed |
1656 | | /// tables and not expensive despite having `Clone`. |
1657 | | pub(crate) transform: Option<CicpTransform>, |
1658 | | /// Make sure we can later add options that are bound to the thread. That does not mean that |
1659 | | /// all attributes will be bound to the thread, only that we can add `!Sync` options later. You |
1660 | | /// should be constructing the options at the call site with each attribute being cheap to move |
1661 | | /// into here. |
1662 | | pub(crate) _auto_traits: PhantomData<std::rc::Rc<()>>, |
1663 | | } |
1664 | | |
1665 | | impl ConvertColorOptions { |
1666 | 0 | pub(crate) fn as_transform( |
1667 | 0 | &mut self, |
1668 | 0 | from_color: Cicp, |
1669 | 0 | into_color: Cicp, |
1670 | 0 | ) -> Result<&CicpTransform, ImageError> { |
1671 | 0 | if let Some(tr) = &self.transform { |
1672 | 0 | tr.check_applicable(from_color, into_color)?; |
1673 | 0 | } |
1674 | | |
1675 | 0 | if self.transform.is_none() { |
1676 | 0 | self.transform = CicpTransform::new(from_color, into_color); |
1677 | 0 | } |
1678 | | |
1679 | 0 | self.transform.as_ref().ok_or_else(|| { |
1680 | 0 | ImageError::Unsupported(UnsupportedError::from_format_and_kind( |
1681 | 0 | crate::error::ImageFormatHint::Unknown, |
1682 | | // One of them is responsible. |
1683 | 0 | UnsupportedErrorKind::ColorspaceCicp(if from_color.qualify_stability() { |
1684 | 0 | into_color |
1685 | | } else { |
1686 | 0 | from_color |
1687 | | }), |
1688 | | )) |
1689 | 0 | }) |
1690 | 0 | } |
1691 | | |
1692 | 0 | pub(crate) fn as_transform_fn<FromType, IntoType>( |
1693 | 0 | &mut self, |
1694 | 0 | from_color: Cicp, |
1695 | 0 | into_color: Cicp, |
1696 | 0 | ) -> Result<&'_ CicpApplicable<'_, FromType::Subpixel>, ImageError> |
1697 | 0 | where |
1698 | 0 | FromType: PixelWithColorType, |
1699 | 0 | IntoType: PixelWithColorType, |
1700 | | { |
1701 | 0 | Ok(self |
1702 | 0 | .as_transform(from_color, into_color)? |
1703 | 0 | .supported_transform_fn::<FromType, IntoType>()) |
1704 | 0 | } Unexecuted instantiation: <image::images::buffer::ConvertColorOptions>::as_transform_fn::<image::color::Rgb<f32>, image::color::Rgb<f32>> Unexecuted instantiation: <image::images::buffer::ConvertColorOptions>::as_transform_fn::<image::color::Rgb<f32>, image::color::Rgba<f32>> Unexecuted instantiation: <image::images::buffer::ConvertColorOptions>::as_transform_fn::<image::color::Rgb<u8>, image::color::Rgb<u8>> Unexecuted instantiation: <image::images::buffer::ConvertColorOptions>::as_transform_fn::<image::color::Rgb<u8>, image::color::Rgba<u8>> Unexecuted instantiation: <image::images::buffer::ConvertColorOptions>::as_transform_fn::<image::color::Rgb<u16>, image::color::Rgb<u16>> Unexecuted instantiation: <image::images::buffer::ConvertColorOptions>::as_transform_fn::<image::color::Rgb<u16>, image::color::Rgba<u16>> Unexecuted instantiation: <image::images::buffer::ConvertColorOptions>::as_transform_fn::<image::color::Rgba<f32>, image::color::Rgba<f32>> Unexecuted instantiation: <image::images::buffer::ConvertColorOptions>::as_transform_fn::<image::color::Rgba<f32>, image::color::Rgb<f32>> Unexecuted instantiation: <image::images::buffer::ConvertColorOptions>::as_transform_fn::<image::color::Rgba<u8>, image::color::Rgba<u8>> Unexecuted instantiation: <image::images::buffer::ConvertColorOptions>::as_transform_fn::<image::color::Rgba<u8>, image::color::Rgb<u8>> Unexecuted instantiation: <image::images::buffer::ConvertColorOptions>::as_transform_fn::<image::color::Rgba<u16>, image::color::Rgba<u16>> Unexecuted instantiation: <image::images::buffer::ConvertColorOptions>::as_transform_fn::<image::color::Rgba<u16>, image::color::Rgb<u16>> |
1705 | | } |
1706 | | |
1707 | | impl<C, SelfPixel: Pixel> ImageBuffer<SelfPixel, C> |
1708 | | where |
1709 | | SelfPixel: PixelWithColorType, |
1710 | | C: Deref<Target = [SelfPixel::Subpixel]> + DerefMut, |
1711 | | { |
1712 | | /// Convert the color data to another pixel type, the color space. |
1713 | | /// |
1714 | | /// This method is supposed to be called by exposed monomorphized methods, not directly by |
1715 | | /// users. In particular it serves to implement `DynamicImage`'s casts that go beyond those |
1716 | | /// offered by `PixelWithColorType` and include, e.g., `LumaAlpha<f32>`. |
1717 | | /// |
1718 | | /// Before exposing this method, decide if we want a design like [`DynamicImage::to`] (many |
1719 | | /// trait parameters) with color space aware `FromColor` or if we want a design that takes a |
1720 | | /// `ColorType` parameter / `PixelWithColorType`. The latter is not quite as flexible but |
1721 | | /// allows much greater internal changes that do not tie in with the _external_ stable API. |
1722 | 0 | pub(crate) fn cast_in_color_space<IntoPixel>( |
1723 | 0 | &self, |
1724 | 0 | ) -> ImageBuffer<IntoPixel, Vec<IntoPixel::Subpixel>> |
1725 | 0 | where |
1726 | 0 | SelfPixel: Pixel, |
1727 | 0 | IntoPixel: Pixel, |
1728 | 0 | IntoPixel: CicpPixelCast<SelfPixel>, |
1729 | 0 | SelfPixel::Subpixel: ColorComponentForCicp, |
1730 | 0 | IntoPixel::Subpixel: ColorComponentForCicp + FromPrimitive<SelfPixel::Subpixel>, |
1731 | | { |
1732 | 0 | let vec = self |
1733 | 0 | .color |
1734 | 0 | .cast_pixels::<SelfPixel, IntoPixel>(self.inner_pixels(), &|| [0.2126, 0.7152, 0.0722]); |
1735 | 0 | let mut buffer = ImageBuffer::from_vec(self.width, self.height, vec) |
1736 | 0 | .expect("cast_pixels returned the right number of pixels"); |
1737 | 0 | buffer.copy_color_space_from(self); |
1738 | 0 | buffer |
1739 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Rgb<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Rgb<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Rgb<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Luma<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Luma<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Luma<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Rgba<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Rgba<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Rgba<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::LumaA<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::LumaA<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::LumaA<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgb<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgb<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgb<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Luma<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Luma<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Luma<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgba<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgba<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgba<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::LumaA<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::LumaA<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::LumaA<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgb<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgb<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgb<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Luma<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Luma<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Luma<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgba<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgba<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgba<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::LumaA<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::LumaA<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::LumaA<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Luma<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Luma<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Luma<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgb<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgb<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgb<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgba<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgba<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgba<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::LumaA<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::LumaA<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::LumaA<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Luma<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Luma<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Luma<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgb<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgb<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgb<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgba<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgba<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgba<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::LumaA<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::LumaA<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Luma<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::LumaA<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Rgba<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Rgba<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Rgba<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Rgb<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Rgb<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Rgb<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Luma<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Luma<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::Luma<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::LumaA<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::LumaA<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::cast_in_color_space::<image::color::LumaA<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgba<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgba<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgba<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgb<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgb<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgb<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Luma<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Luma<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Luma<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::LumaA<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::LumaA<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::LumaA<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgba<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgba<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgba<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgb<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgb<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgb<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Luma<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Luma<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Luma<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::LumaA<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::LumaA<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::LumaA<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::LumaA<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::LumaA<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::LumaA<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgb<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgb<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgb<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Luma<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Luma<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Luma<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgba<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgba<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u8>, alloc::vec::Vec<u8>>>::cast_in_color_space::<image::color::Rgba<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::LumaA<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::LumaA<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::LumaA<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgb<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgb<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgb<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Luma<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Luma<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Luma<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgba<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgba<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::LumaA<u16>, alloc::vec::Vec<u16>>>::cast_in_color_space::<image::color::Rgba<u16>> |
1740 | | |
1741 | | /// Copy pixel data from one buffer to another, calculating equivalent color representations |
1742 | | /// for the target's color space. |
1743 | | /// |
1744 | | /// Returns `Ok` if: |
1745 | | /// - Both images to have the same dimensions, otherwise returns a [`ImageError::Parameter`]. |
1746 | | /// - The primaries and transfer functions of both image's color spaces must be supported, |
1747 | | /// otherwise returns a [`ImageError::Unsupported`]. |
1748 | | /// - The pixel's channel layout must be supported for conversion, otherwise returns a |
1749 | | /// [`ImageError::Unsupported`]. You can rely on RGB and RGBA always being supported. If a |
1750 | | /// layout is supported for one color space it is supported for all of them. |
1751 | | /// |
1752 | | /// To copy color data of arbitrary channel layouts use `DynamicImage` with the overhead of |
1753 | | /// having data converted into and from RGB representation. |
1754 | 0 | pub fn copy_from_color_space<FromType, D>( |
1755 | 0 | &mut self, |
1756 | 0 | from: &ImageBuffer<FromType, D>, |
1757 | 0 | mut options: ConvertColorOptions, |
1758 | 0 | ) -> ImageResult<()> |
1759 | 0 | where |
1760 | 0 | FromType: Pixel<Subpixel = SelfPixel::Subpixel> + PixelWithColorType, |
1761 | 0 | D: Deref<Target = [SelfPixel::Subpixel]>, |
1762 | | { |
1763 | 0 | if self.dimensions() != from.dimensions() { |
1764 | 0 | return Err(ImageError::Parameter(ParameterError::from_kind( |
1765 | 0 | ParameterErrorKind::DimensionMismatch, |
1766 | 0 | ))); |
1767 | 0 | } |
1768 | | |
1769 | 0 | let transform = options |
1770 | 0 | .as_transform_fn::<FromType, SelfPixel>(from.color_space(), self.color_space())?; |
1771 | | |
1772 | 0 | let from = from.inner_pixels(); |
1773 | 0 | let into = self.inner_pixels_mut(); |
1774 | | |
1775 | 0 | debug_assert_eq!( |
1776 | 0 | from.len() / usize::from(FromType::CHANNEL_COUNT), |
1777 | 0 | into.len() / usize::from(SelfPixel::CHANNEL_COUNT), |
1778 | 0 | "Diverging pixel count despite same size", |
1779 | | ); |
1780 | | |
1781 | 0 | transform(from, into); |
1782 | | |
1783 | 0 | Ok(()) |
1784 | 0 | } Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::copy_from_color_space::<image::color::Rgb<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<f32>, alloc::vec::Vec<f32>>>::copy_from_color_space::<image::color::Rgba<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::copy_from_color_space::<image::color::Rgb<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u8>, alloc::vec::Vec<u8>>>::copy_from_color_space::<image::color::Rgba<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::copy_from_color_space::<image::color::Rgb<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgb<u16>, alloc::vec::Vec<u16>>>::copy_from_color_space::<image::color::Rgba<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::copy_from_color_space::<image::color::Rgba<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<f32>, alloc::vec::Vec<f32>>>::copy_from_color_space::<image::color::Rgb<f32>, alloc::vec::Vec<f32>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_from_color_space::<image::color::Rgba<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u8>, alloc::vec::Vec<u8>>>::copy_from_color_space::<image::color::Rgb<u8>, alloc::vec::Vec<u8>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::copy_from_color_space::<image::color::Rgba<u16>, alloc::vec::Vec<u16>> Unexecuted instantiation: <image::images::buffer::ImageBuffer<image::color::Rgba<u16>, alloc::vec::Vec<u16>>>::copy_from_color_space::<image::color::Rgb<u16>, alloc::vec::Vec<u16>> |
1785 | | |
1786 | | /// Convert this buffer into a newly allocated buffer, changing the color representation. |
1787 | | /// |
1788 | | /// This will avoid an allocation if the target layout or the color conversion is not supported |
1789 | | /// (yet). |
1790 | | /// |
1791 | | /// See [`ImageBuffer::copy_from_color_space`] if you intend to assign to an existing buffer, |
1792 | | /// swapping the argument with `self`. |
1793 | 0 | pub fn to_color_space<IntoType>( |
1794 | 0 | &self, |
1795 | 0 | color: Cicp, |
1796 | 0 | mut options: ConvertColorOptions, |
1797 | 0 | ) -> Result<ImageBuffer<IntoType, Vec<SelfPixel::Subpixel>>, ImageError> |
1798 | 0 | where |
1799 | 0 | IntoType: Pixel<Subpixel = SelfPixel::Subpixel> + PixelWithColorType, |
1800 | | { |
1801 | 0 | let transform = |
1802 | 0 | options.as_transform_fn::<SelfPixel, IntoType>(self.color_space(), color)?; |
1803 | | |
1804 | 0 | let (width, height) = self.dimensions(); |
1805 | 0 | let mut target = ImageBuffer::new(width, height); |
1806 | | |
1807 | 0 | let from = self.inner_pixels(); |
1808 | 0 | let into = target.inner_pixels_mut(); |
1809 | | |
1810 | 0 | transform(from, into); |
1811 | | |
1812 | 0 | Ok(target) |
1813 | 0 | } |
1814 | | |
1815 | | /// Apply a color space to an image, transforming the pixel representation. |
1816 | 0 | pub fn apply_color_space( |
1817 | 0 | &mut self, |
1818 | 0 | color: Cicp, |
1819 | 0 | mut options: ConvertColorOptions, |
1820 | 0 | ) -> ImageResult<()> { |
1821 | 0 | if self.color_space() == color { |
1822 | 0 | return Ok(()); |
1823 | 0 | } |
1824 | | |
1825 | 0 | let transform = |
1826 | 0 | options.as_transform_fn::<SelfPixel, SelfPixel>(self.color_space(), color)?; |
1827 | | |
1828 | 0 | let mut scratch = [<SelfPixel::Subpixel as crate::Primitive>::DEFAULT_MIN_VALUE; 1200]; |
1829 | 0 | let chunk_len = scratch.len() / usize::from(<SelfPixel as Pixel>::CHANNEL_COUNT) |
1830 | 0 | * usize::from(<SelfPixel as Pixel>::CHANNEL_COUNT); |
1831 | | |
1832 | 0 | for chunk in self.data.chunks_mut(chunk_len) { |
1833 | 0 | let scratch = &mut scratch[..chunk.len()]; |
1834 | 0 | scratch.copy_from_slice(chunk); |
1835 | 0 | transform(scratch, chunk); |
1836 | 0 | } |
1837 | | |
1838 | 0 | self.color = color.into_rgb(); |
1839 | | |
1840 | 0 | Ok(()) |
1841 | 0 | } |
1842 | | } |
1843 | | |
1844 | | /// Sendable Rgb image buffer |
1845 | | pub type RgbImage = ImageBuffer<Rgb<u8>, Vec<u8>>; |
1846 | | /// Sendable Rgb + alpha channel image buffer |
1847 | | pub type RgbaImage = ImageBuffer<Rgba<u8>, Vec<u8>>; |
1848 | | /// Sendable grayscale image buffer |
1849 | | pub type GrayImage = ImageBuffer<Luma<u8>, Vec<u8>>; |
1850 | | /// Sendable grayscale + alpha channel image buffer |
1851 | | pub type GrayAlphaImage = ImageBuffer<LumaA<u8>, Vec<u8>>; |
1852 | | /// Sendable 16-bit Rgb image buffer |
1853 | | pub(crate) type Rgb16Image = ImageBuffer<Rgb<u16>, Vec<u16>>; |
1854 | | /// Sendable 16-bit Rgb + alpha channel image buffer |
1855 | | pub(crate) type Rgba16Image = ImageBuffer<Rgba<u16>, Vec<u16>>; |
1856 | | /// Sendable 16-bit grayscale image buffer |
1857 | | pub(crate) type Gray16Image = ImageBuffer<Luma<u16>, Vec<u16>>; |
1858 | | /// Sendable 16-bit grayscale + alpha channel image buffer |
1859 | | pub(crate) type GrayAlpha16Image = ImageBuffer<LumaA<u16>, Vec<u16>>; |
1860 | | |
1861 | | /// An image buffer for 32-bit float RGB pixels, |
1862 | | /// where the backing container is a flattened vector of floats. |
1863 | | pub type Rgb32FImage = ImageBuffer<Rgb<f32>, Vec<f32>>; |
1864 | | |
1865 | | /// An image buffer for 32-bit float RGBA pixels, |
1866 | | /// where the backing container is a flattened vector of floats. |
1867 | | pub type Rgba32FImage = ImageBuffer<Rgba<f32>, Vec<f32>>; |
1868 | | |
1869 | | impl From<DynamicImage> for RgbImage { |
1870 | 0 | fn from(value: DynamicImage) -> Self { |
1871 | 0 | value.into_rgb8() |
1872 | 0 | } |
1873 | | } |
1874 | | |
1875 | | impl From<DynamicImage> for RgbaImage { |
1876 | 0 | fn from(value: DynamicImage) -> Self { |
1877 | 0 | value.into_rgba8() |
1878 | 0 | } |
1879 | | } |
1880 | | |
1881 | | impl From<DynamicImage> for GrayImage { |
1882 | 0 | fn from(value: DynamicImage) -> Self { |
1883 | 0 | value.into_luma8() |
1884 | 0 | } |
1885 | | } |
1886 | | |
1887 | | impl From<DynamicImage> for GrayAlphaImage { |
1888 | 0 | fn from(value: DynamicImage) -> Self { |
1889 | 0 | value.into_luma_alpha8() |
1890 | 0 | } |
1891 | | } |
1892 | | |
1893 | | impl From<DynamicImage> for Rgb16Image { |
1894 | 0 | fn from(value: DynamicImage) -> Self { |
1895 | 0 | value.into_rgb16() |
1896 | 0 | } |
1897 | | } |
1898 | | |
1899 | | impl From<DynamicImage> for Rgba16Image { |
1900 | 0 | fn from(value: DynamicImage) -> Self { |
1901 | 0 | value.into_rgba16() |
1902 | 0 | } |
1903 | | } |
1904 | | |
1905 | | impl From<DynamicImage> for Gray16Image { |
1906 | 0 | fn from(value: DynamicImage) -> Self { |
1907 | 0 | value.into_luma16() |
1908 | 0 | } |
1909 | | } |
1910 | | |
1911 | | impl From<DynamicImage> for GrayAlpha16Image { |
1912 | 0 | fn from(value: DynamicImage) -> Self { |
1913 | 0 | value.into_luma_alpha16() |
1914 | 0 | } |
1915 | | } |
1916 | | |
1917 | | impl From<DynamicImage> for Rgba32FImage { |
1918 | 0 | fn from(value: DynamicImage) -> Self { |
1919 | 0 | value.into_rgba32f() |
1920 | 0 | } |
1921 | | } |
1922 | | |
1923 | | #[cfg(test)] |
1924 | | mod test { |
1925 | | use super::{GrayImage, ImageBuffer, RgbImage}; |
1926 | | use crate::math::Rect; |
1927 | | use crate::metadata::Cicp; |
1928 | | use crate::metadata::CicpMatrixCoefficients; |
1929 | | use crate::metadata::CicpTransform; |
1930 | | use crate::metadata::CicpVideoFullRangeFlag; |
1931 | | use crate::GenericImage as _; |
1932 | | use crate::ImageFormat; |
1933 | | use crate::{Luma, LumaA, Pixel, Rgb, Rgba}; |
1934 | | use num_traits::Zero; |
1935 | | |
1936 | | #[test] |
1937 | | /// Tests if image buffers from slices work |
1938 | | fn slice_buffer() { |
1939 | | let data = [0; 9]; |
1940 | | let buf: ImageBuffer<Luma<u8>, _> = ImageBuffer::from_raw(3, 3, &data[..]).unwrap(); |
1941 | | assert_eq!(&*buf, &data[..]); |
1942 | | } |
1943 | | |
1944 | | macro_rules! new_buffer_zero_test { |
1945 | | ($test_name:ident, $pxt:ty) => { |
1946 | | #[test] |
1947 | | fn $test_name() { |
1948 | | let buffer = ImageBuffer::<$pxt, Vec<<$pxt as Pixel>::Subpixel>>::new(2, 2); |
1949 | | assert!(buffer |
1950 | | .iter() |
1951 | | .all(|p| *p == <$pxt as Pixel>::Subpixel::zero())); |
1952 | | } |
1953 | | }; |
1954 | | } |
1955 | | |
1956 | | new_buffer_zero_test!(luma_u8_zero_test, Luma<u8>); |
1957 | | new_buffer_zero_test!(luma_u16_zero_test, Luma<u16>); |
1958 | | new_buffer_zero_test!(luma_f32_zero_test, Luma<f32>); |
1959 | | new_buffer_zero_test!(luma_a_u8_zero_test, LumaA<u8>); |
1960 | | new_buffer_zero_test!(luma_a_u16_zero_test, LumaA<u16>); |
1961 | | new_buffer_zero_test!(luma_a_f32_zero_test, LumaA<f32>); |
1962 | | new_buffer_zero_test!(rgb_u8_zero_test, Rgb<u8>); |
1963 | | new_buffer_zero_test!(rgb_u16_zero_test, Rgb<u16>); |
1964 | | new_buffer_zero_test!(rgb_f32_zero_test, Rgb<f32>); |
1965 | | new_buffer_zero_test!(rgb_a_u8_zero_test, Rgba<u8>); |
1966 | | new_buffer_zero_test!(rgb_a_u16_zero_test, Rgba<u16>); |
1967 | | new_buffer_zero_test!(rgb_a_f32_zero_test, Rgba<f32>); |
1968 | | |
1969 | | #[test] |
1970 | | fn get_pixel() { |
1971 | | let mut a: RgbImage = ImageBuffer::new(10, 10); |
1972 | | { |
1973 | | let b = a.get_mut(3 * 10).unwrap(); |
1974 | | *b = 255; |
1975 | | } |
1976 | | assert_eq!(a.get_pixel(0, 1)[0], 255); |
1977 | | } |
1978 | | |
1979 | | #[test] |
1980 | | fn get_pixel_checked() { |
1981 | | let mut a: RgbImage = ImageBuffer::new(10, 10); |
1982 | | a.get_pixel_mut_checked(0, 1).unwrap()[0] = 255; |
1983 | | |
1984 | | assert_eq!(a.get_pixel_checked(0, 1), Some(&Rgb([255, 0, 0]))); |
1985 | | assert_eq!(a.get_pixel_checked(0, 1).unwrap(), a.get_pixel(0, 1)); |
1986 | | assert_eq!(a.get_pixel_checked(10, 0), None); |
1987 | | assert_eq!(a.get_pixel_checked(0, 10), None); |
1988 | | assert_eq!(a.get_pixel_mut_checked(10, 0), None); |
1989 | | assert_eq!(a.get_pixel_mut_checked(0, 10), None); |
1990 | | |
1991 | | // From image/issues/1672 |
1992 | | const WHITE: Rgb<u8> = Rgb([255_u8, 255, 255]); |
1993 | | let mut a = RgbImage::new(2, 1); |
1994 | | a.put_pixel(1, 0, WHITE); |
1995 | | |
1996 | | assert_eq!(a.get_pixel_checked(1, 0), Some(&WHITE)); |
1997 | | assert_eq!(a.get_pixel_checked(1, 0).unwrap(), a.get_pixel(1, 0)); |
1998 | | } |
1999 | | |
2000 | | #[test] |
2001 | | fn mut_iter() { |
2002 | | let mut a: RgbImage = ImageBuffer::new(10, 10); |
2003 | | { |
2004 | | let val = a.pixels_mut().next().unwrap(); |
2005 | | *val = Rgb([42, 0, 0]); |
2006 | | } |
2007 | | assert_eq!(a.data[0], 42); |
2008 | | } |
2009 | | |
2010 | | #[test] |
2011 | | fn zero_width_zero_height() { |
2012 | | let mut image = RgbImage::new(0, 0); |
2013 | | |
2014 | | assert_eq!(image.rows_mut().count(), 0); |
2015 | | assert_eq!(image.pixels_mut().count(), 0); |
2016 | | assert_eq!(image.rows().count(), 0); |
2017 | | assert_eq!(image.pixels().count(), 0); |
2018 | | } |
2019 | | |
2020 | | #[test] |
2021 | | fn zero_width_nonzero_height() { |
2022 | | let mut image = RgbImage::new(0, 2); |
2023 | | |
2024 | | assert_eq!(image.rows_mut().count(), 0); |
2025 | | assert_eq!(image.pixels_mut().count(), 0); |
2026 | | assert_eq!(image.rows().count(), 0); |
2027 | | assert_eq!(image.pixels().count(), 0); |
2028 | | } |
2029 | | |
2030 | | #[test] |
2031 | | fn nonzero_width_zero_height() { |
2032 | | let mut image = RgbImage::new(2, 0); |
2033 | | |
2034 | | assert_eq!(image.rows_mut().count(), 0); |
2035 | | assert_eq!(image.pixels_mut().count(), 0); |
2036 | | assert_eq!(image.rows().count(), 0); |
2037 | | assert_eq!(image.pixels().count(), 0); |
2038 | | } |
2039 | | |
2040 | | #[test] |
2041 | | fn pixels_on_large_buffer() { |
2042 | | let mut image = RgbImage::from_raw(1, 1, vec![0; 6]).unwrap(); |
2043 | | |
2044 | | assert_eq!(image.pixels().count(), 1); |
2045 | | assert_eq!(image.enumerate_pixels().count(), 1); |
2046 | | assert_eq!(image.pixels_mut().count(), 1); |
2047 | | assert_eq!(image.enumerate_pixels_mut().count(), 1); |
2048 | | |
2049 | | assert_eq!(image.rows().count(), 1); |
2050 | | assert_eq!(image.rows_mut().count(), 1); |
2051 | | } |
2052 | | |
2053 | | #[test] |
2054 | | fn default() { |
2055 | | let image = ImageBuffer::<Rgb<u8>, Vec<u8>>::default(); |
2056 | | assert_eq!(image.dimensions(), (0, 0)); |
2057 | | } |
2058 | | |
2059 | | #[test] |
2060 | | #[rustfmt::skip] |
2061 | | fn test_image_buffer_copy_within_oob() { |
2062 | | let mut image: GrayImage = ImageBuffer::from_raw(4, 4, vec![0u8; 16]).unwrap(); |
2063 | | assert!(!image.copy_within(Rect { x: 0, y: 0, width: 5, height: 4 }, 0, 0)); |
2064 | | assert!(!image.copy_within(Rect { x: 0, y: 0, width: 4, height: 5 }, 0, 0)); |
2065 | | assert!(!image.copy_within(Rect { x: 1, y: 0, width: 4, height: 4 }, 0, 0)); |
2066 | | assert!(!image.copy_within(Rect { x: 0, y: 0, width: 4, height: 4 }, 1, 0)); |
2067 | | assert!(!image.copy_within(Rect { x: 0, y: 1, width: 4, height: 4 }, 0, 0)); |
2068 | | assert!(!image.copy_within(Rect { x: 0, y: 0, width: 4, height: 4 }, 0, 1)); |
2069 | | assert!(!image.copy_within(Rect { x: 1, y: 1, width: 4, height: 4 }, 0, 0)); |
2070 | | } |
2071 | | |
2072 | | #[test] |
2073 | | fn test_image_buffer_copy_within_tl() { |
2074 | | let data = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; |
2075 | | let expected = [0, 1, 2, 3, 4, 0, 1, 2, 8, 4, 5, 6, 12, 8, 9, 10]; |
2076 | | let mut image: GrayImage = ImageBuffer::from_raw(4, 4, Vec::from(&data[..])).unwrap(); |
2077 | | assert!(image.copy_within( |
2078 | | Rect { |
2079 | | x: 0, |
2080 | | y: 0, |
2081 | | width: 3, |
2082 | | height: 3 |
2083 | | }, |
2084 | | 1, |
2085 | | 1 |
2086 | | )); |
2087 | | assert_eq!(&image.into_raw(), &expected); |
2088 | | } |
2089 | | |
2090 | | #[test] |
2091 | | fn test_image_buffer_copy_within_tr() { |
2092 | | let data = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; |
2093 | | let expected = [0, 1, 2, 3, 1, 2, 3, 7, 5, 6, 7, 11, 9, 10, 11, 15]; |
2094 | | let mut image: GrayImage = ImageBuffer::from_raw(4, 4, Vec::from(&data[..])).unwrap(); |
2095 | | assert!(image.copy_within( |
2096 | | Rect { |
2097 | | x: 1, |
2098 | | y: 0, |
2099 | | width: 3, |
2100 | | height: 3 |
2101 | | }, |
2102 | | 0, |
2103 | | 1 |
2104 | | )); |
2105 | | assert_eq!(&image.into_raw(), &expected); |
2106 | | } |
2107 | | |
2108 | | #[test] |
2109 | | fn test_image_buffer_copy_within_bl() { |
2110 | | let data = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; |
2111 | | let expected = [0, 4, 5, 6, 4, 8, 9, 10, 8, 12, 13, 14, 12, 13, 14, 15]; |
2112 | | let mut image: GrayImage = ImageBuffer::from_raw(4, 4, Vec::from(&data[..])).unwrap(); |
2113 | | assert!(image.copy_within( |
2114 | | Rect { |
2115 | | x: 0, |
2116 | | y: 1, |
2117 | | width: 3, |
2118 | | height: 3 |
2119 | | }, |
2120 | | 1, |
2121 | | 0 |
2122 | | )); |
2123 | | assert_eq!(&image.into_raw(), &expected); |
2124 | | } |
2125 | | |
2126 | | #[test] |
2127 | | fn test_image_buffer_copy_within_br() { |
2128 | | let data = &[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]; |
2129 | | let expected = [5, 6, 7, 3, 9, 10, 11, 7, 13, 14, 15, 11, 12, 13, 14, 15]; |
2130 | | let mut image: GrayImage = ImageBuffer::from_raw(4, 4, Vec::from(&data[..])).unwrap(); |
2131 | | assert!(image.copy_within( |
2132 | | Rect { |
2133 | | x: 1, |
2134 | | y: 1, |
2135 | | width: 3, |
2136 | | height: 3 |
2137 | | }, |
2138 | | 0, |
2139 | | 0 |
2140 | | )); |
2141 | | assert_eq!(&image.into_raw(), &expected); |
2142 | | } |
2143 | | |
2144 | | #[test] |
2145 | | #[cfg(feature = "png")] |
2146 | | fn write_to_with_large_buffer() { |
2147 | | // A buffer of 1 pixel, padded to 4 bytes as would be common in, e.g. BMP. |
2148 | | |
2149 | | let img: GrayImage = ImageBuffer::from_raw(1, 1, vec![0u8; 4]).unwrap(); |
2150 | | let mut buffer = std::io::Cursor::new(vec![]); |
2151 | | assert!(img.write_to(&mut buffer, ImageFormat::Png).is_ok()); |
2152 | | } |
2153 | | |
2154 | | #[test] |
2155 | | fn exact_size_iter_size_hint() { |
2156 | | // The docs for `std::iter::ExactSizeIterator` requires that the implementation of |
2157 | | // `size_hint` on the iterator returns the same value as the `len` implementation. |
2158 | | |
2159 | | // This test should work for any size image. |
2160 | | const N: u32 = 10; |
2161 | | |
2162 | | let mut image = RgbImage::from_raw(N, N, vec![0; (N * N * 3) as usize]).unwrap(); |
2163 | | |
2164 | | let iter = image.pixels(); |
2165 | | let exact_len = ExactSizeIterator::len(&iter); |
2166 | | assert_eq!(iter.size_hint(), (exact_len, Some(exact_len))); |
2167 | | |
2168 | | let iter = image.pixels_mut(); |
2169 | | let exact_len = ExactSizeIterator::len(&iter); |
2170 | | assert_eq!(iter.size_hint(), (exact_len, Some(exact_len))); |
2171 | | |
2172 | | let iter = image.rows(); |
2173 | | let exact_len = ExactSizeIterator::len(&iter); |
2174 | | assert_eq!(iter.size_hint(), (exact_len, Some(exact_len))); |
2175 | | |
2176 | | let iter = image.rows_mut(); |
2177 | | let exact_len = ExactSizeIterator::len(&iter); |
2178 | | assert_eq!(iter.size_hint(), (exact_len, Some(exact_len))); |
2179 | | |
2180 | | let iter = image.enumerate_pixels(); |
2181 | | let exact_len = ExactSizeIterator::len(&iter); |
2182 | | assert_eq!(iter.size_hint(), (exact_len, Some(exact_len))); |
2183 | | |
2184 | | let iter = image.enumerate_rows(); |
2185 | | let exact_len = ExactSizeIterator::len(&iter); |
2186 | | assert_eq!(iter.size_hint(), (exact_len, Some(exact_len))); |
2187 | | |
2188 | | let iter = image.enumerate_pixels_mut(); |
2189 | | let exact_len = ExactSizeIterator::len(&iter); |
2190 | | assert_eq!(iter.size_hint(), (exact_len, Some(exact_len))); |
2191 | | |
2192 | | let iter = image.enumerate_rows_mut(); |
2193 | | let exact_len = ExactSizeIterator::len(&iter); |
2194 | | assert_eq!(iter.size_hint(), (exact_len, Some(exact_len))); |
2195 | | } |
2196 | | |
2197 | | #[test] |
2198 | | fn color_conversion() { |
2199 | | let mut source = ImageBuffer::from_fn(128, 128, |_, _| Rgb([255, 0, 0])); |
2200 | | let mut target = ImageBuffer::from_fn(128, 128, |_, _| Rgba(Default::default())); |
2201 | | |
2202 | | source.set_rgb_primaries(Cicp::SRGB.primaries); |
2203 | | source.set_transfer_function(Cicp::SRGB.transfer); |
2204 | | |
2205 | | target.set_rgb_primaries(Cicp::DISPLAY_P3.primaries); |
2206 | | target.set_transfer_function(Cicp::DISPLAY_P3.transfer); |
2207 | | |
2208 | | let result = target.copy_from_color_space(&source, Default::default()); |
2209 | | |
2210 | | assert!(result.is_ok(), "{result:?}"); |
2211 | | assert_eq!(target[(0, 0)], Rgba([234u8, 51, 35, 255])); |
2212 | | } |
2213 | | |
2214 | | #[test] |
2215 | | fn gray_conversions() { |
2216 | | let mut source = ImageBuffer::from_fn(128, 128, |_, _| Luma([255u8])); |
2217 | | let mut target = ImageBuffer::from_fn(128, 128, |_, _| Rgba(Default::default())); |
2218 | | |
2219 | | source.set_rgb_primaries(Cicp::SRGB.primaries); |
2220 | | source.set_transfer_function(Cicp::SRGB.transfer); |
2221 | | |
2222 | | target.set_rgb_primaries(Cicp::SRGB.primaries); |
2223 | | target.set_transfer_function(Cicp::SRGB.transfer); |
2224 | | |
2225 | | let result = target.copy_from_color_space(&source, Default::default()); |
2226 | | |
2227 | | assert!(result.is_ok(), "{result:?}"); |
2228 | | assert_eq!(target[(0, 0)], Rgba([u8::MAX; 4])); |
2229 | | } |
2230 | | |
2231 | | #[test] |
2232 | | fn rgb_to_gray_conversion() { |
2233 | | let mut source = ImageBuffer::from_fn(128, 128, |_, _| Rgb([128u8; 3])); |
2234 | | let mut target = ImageBuffer::from_fn(128, 128, |_, _| Luma(Default::default())); |
2235 | | |
2236 | | source.set_rgb_primaries(Cicp::SRGB.primaries); |
2237 | | source.set_transfer_function(Cicp::SRGB.transfer); |
2238 | | |
2239 | | target.set_rgb_primaries(Cicp::SRGB.primaries); |
2240 | | target.set_transfer_function(Cicp::SRGB.transfer); |
2241 | | |
2242 | | let result = target.copy_from_color_space(&source, Default::default()); |
2243 | | |
2244 | | assert!(result.is_ok(), "{result:?}"); |
2245 | | assert_eq!(target[(0, 0)], Luma([128u8])); |
2246 | | } |
2247 | | |
2248 | | #[test] |
2249 | | fn apply_color() { |
2250 | | let mut buffer = ImageBuffer::from_fn(128, 128, |_, _| Rgb([255u8, 0, 0])); |
2251 | | |
2252 | | buffer.set_rgb_primaries(Cicp::SRGB.primaries); |
2253 | | buffer.set_transfer_function(Cicp::SRGB.transfer); |
2254 | | |
2255 | | buffer |
2256 | | .apply_color_space(Cicp::DISPLAY_P3, Default::default()) |
2257 | | .expect("supported transform"); |
2258 | | |
2259 | | buffer.pixels().for_each(|&p| { |
2260 | | assert_eq!(p, Rgb([234u8, 51, 35])); |
2261 | | }); |
2262 | | } |
2263 | | |
2264 | | #[test] |
2265 | | fn to_color() { |
2266 | | let mut source = ImageBuffer::from_fn(128, 128, |_, _| Rgba([255u8, 0, 0, 255])); |
2267 | | source.set_rgb_primaries(Cicp::SRGB.primaries); |
2268 | | source.set_transfer_function(Cicp::SRGB.transfer); |
2269 | | |
2270 | | let target = source |
2271 | | .to_color_space::<Rgb<u8>>(Cicp::DISPLAY_P3, Default::default()) |
2272 | | .expect("supported transform"); |
2273 | | |
2274 | | assert_eq!(target[(0, 0)], Rgb([234u8, 51, 35])); |
2275 | | } |
2276 | | |
2277 | | #[test] |
2278 | | fn transformation_mismatch() { |
2279 | | let mut source = ImageBuffer::from_fn(128, 128, |_, _| Luma([255u8])); |
2280 | | let mut target = ImageBuffer::from_fn(128, 128, |_, _| Rgba(Default::default())); |
2281 | | |
2282 | | source.set_color_space(Cicp::SRGB).unwrap(); |
2283 | | target.set_color_space(Cicp::DISPLAY_P3).unwrap(); |
2284 | | |
2285 | | let options = super::ConvertColorOptions { |
2286 | | transform: CicpTransform::new(Cicp::SRGB, Cicp::SRGB), |
2287 | | ..super::ConvertColorOptions::default() |
2288 | | }; |
2289 | | |
2290 | | let result = target.copy_from_color_space(&source, options); |
2291 | | assert!(matches!(result, Err(crate::ImageError::Parameter(_)))); |
2292 | | } |
2293 | | |
2294 | | #[test] |
2295 | | fn pleasant_debug() { |
2296 | | use super::*; |
2297 | | |
2298 | | assert_eq!( |
2299 | | format!("{:?}", GrayImage::new(100, 100)), |
2300 | | "ImageBuffer::<Luma<u8>, _> { width: 100, height: 100, color: \"sRGB\" }" |
2301 | | ); |
2302 | | assert_eq!( |
2303 | | format!("{:?}", GrayAlphaImage::new(100, 100)), |
2304 | | "ImageBuffer::<LumaA<u8>, _> { width: 100, height: 100, color: \"sRGB\" }" |
2305 | | ); |
2306 | | assert_eq!( |
2307 | | format!("{:?}", RgbImage::new(100, 100)), |
2308 | | "ImageBuffer::<Rgb<u8>, _> { width: 100, height: 100, color: \"sRGB\" }" |
2309 | | ); |
2310 | | assert_eq!( |
2311 | | format!("{:?}", RgbaImage::new(100, 100)), |
2312 | | "ImageBuffer::<Rgba<u8>, _> { width: 100, height: 100, color: \"sRGB\" }" |
2313 | | ); |
2314 | | assert_eq!( |
2315 | | format!("{:?}", Gray16Image::new(100, 100)), |
2316 | | "ImageBuffer::<Luma<u16>, _> { width: 100, height: 100, color: \"sRGB\" }" |
2317 | | ); |
2318 | | assert_eq!( |
2319 | | format!("{:?}", GrayAlpha16Image::new(100, 100)), |
2320 | | "ImageBuffer::<LumaA<u16>, _> { width: 100, height: 100, color: \"sRGB\" }" |
2321 | | ); |
2322 | | assert_eq!( |
2323 | | format!("{:?}", Rgb16Image::new(100, 100)), |
2324 | | "ImageBuffer::<Rgb<u16>, _> { width: 100, height: 100, color: \"sRGB\" }" |
2325 | | ); |
2326 | | assert_eq!( |
2327 | | format!("{:?}", Rgba16Image::new(100, 100)), |
2328 | | "ImageBuffer::<Rgba<u16>, _> { width: 100, height: 100, color: \"sRGB\" }" |
2329 | | ); |
2330 | | assert_eq!( |
2331 | | format!("{:?}", Rgb32FImage::new(100, 100)), |
2332 | | "ImageBuffer::<Rgb<f32>, _> { width: 100, height: 100, color: \"sRGB\" }" |
2333 | | ); |
2334 | | assert_eq!( |
2335 | | format!("{:?}", Rgba32FImage::new(100, 100)), |
2336 | | "ImageBuffer::<Rgba<f32>, _> { width: 100, height: 100, color: \"sRGB\" }" |
2337 | | ); |
2338 | | |
2339 | | let gray8 = ImageBuffer::from_pixel(16, 16, Luma([255u8])); |
2340 | | assert_eq!( |
2341 | | format!("{:?}", gray8), |
2342 | | "ImageBuffer::<Luma<u8>, _> { width: 16, height: 16, color: \"sRGB\" }" |
2343 | | ); |
2344 | | assert_eq!( |
2345 | | format!("{:#?}", gray8), |
2346 | | "ImageBuffer::<Luma<u8>, _> {\n width: 16,\n height: 16,\n color: \"sRGB\",\n}" |
2347 | | ); |
2348 | | |
2349 | | let mut rgba32f = ImageBuffer::from_pixel(16, 16, Rgba([0.0_f32; 4])); |
2350 | | rgba32f.set_color_space(Cicp::DISPLAY_P3).unwrap(); |
2351 | | assert_eq!( |
2352 | | format!("{:?}", rgba32f), |
2353 | | "ImageBuffer::<Rgba<f32>, _> { width: 16, height: 16, color: \"Display P3\" }" |
2354 | | ); |
2355 | | |
2356 | | let mut custom_color_space = ImageBuffer::from_pixel(16, 16, Rgba([0.0_f32; 4])); |
2357 | | custom_color_space |
2358 | | .set_color_space(Cicp { |
2359 | | primaries: CicpColorPrimaries::Rgb240m, |
2360 | | transfer: CicpTransferCharacteristics::LogSqrt, |
2361 | | matrix: CicpMatrixCoefficients::Identity, |
2362 | | full_range: CicpVideoFullRangeFlag::FullRange, |
2363 | | }) |
2364 | | .unwrap(); |
2365 | | assert_eq!( |
2366 | | format!("{:?}", custom_color_space), |
2367 | | "ImageBuffer::<Rgba<f32>, _> { width: 16, height: 16, color: CicpRgb { primaries: Rgb240m, transfer: LogSqrt, luminance: NonConstant } }" |
2368 | | ); |
2369 | | } |
2370 | | } |
2371 | | |
2372 | | #[cfg(test)] |
2373 | | #[cfg(feature = "benchmarks")] |
2374 | | mod benchmarks { |
2375 | | use super::{ConvertBuffer, GrayImage, ImageBuffer, Pixel, RgbImage}; |
2376 | | |
2377 | | #[bench] |
2378 | | fn conversion(b: &mut test::Bencher) { |
2379 | | let mut a: RgbImage = ImageBuffer::new(1000, 1000); |
2380 | | for p in a.pixels_mut() { |
2381 | | let rgb = p.channels_mut(); |
2382 | | rgb[0] = 255; |
2383 | | rgb[1] = 23; |
2384 | | rgb[2] = 42; |
2385 | | } |
2386 | | |
2387 | | assert!(a.data[0] != 0); |
2388 | | b.iter(|| { |
2389 | | let b: GrayImage = a.convert(); |
2390 | | assert!(0 != b.data[0]); |
2391 | | assert!(a.data[0] != b.data[0]); |
2392 | | test::black_box(b); |
2393 | | }); |
2394 | | b.bytes = 1000 * 1000 * 3; |
2395 | | } |
2396 | | |
2397 | | #[bench] |
2398 | | fn image_access_row_by_row(b: &mut test::Bencher) { |
2399 | | let mut a: RgbImage = ImageBuffer::new(1000, 1000); |
2400 | | for p in a.pixels_mut() { |
2401 | | let rgb = p.channels_mut(); |
2402 | | rgb[0] = 255; |
2403 | | rgb[1] = 23; |
2404 | | rgb[2] = 42; |
2405 | | } |
2406 | | |
2407 | | b.iter(move || { |
2408 | | let image: &RgbImage = test::black_box(&a); |
2409 | | let mut sum: usize = 0; |
2410 | | for y in 0..1000 { |
2411 | | for x in 0..1000 { |
2412 | | let pixel = image.get_pixel(x, y); |
2413 | | sum = sum.wrapping_add(pixel[0] as usize); |
2414 | | sum = sum.wrapping_add(pixel[1] as usize); |
2415 | | sum = sum.wrapping_add(pixel[2] as usize); |
2416 | | } |
2417 | | } |
2418 | | test::black_box(sum) |
2419 | | }); |
2420 | | |
2421 | | b.bytes = 1000 * 1000 * 3; |
2422 | | } |
2423 | | |
2424 | | #[bench] |
2425 | | fn image_access_col_by_col(b: &mut test::Bencher) { |
2426 | | let mut a: RgbImage = ImageBuffer::new(1000, 1000); |
2427 | | for p in a.pixels_mut() { |
2428 | | let rgb = p.channels_mut(); |
2429 | | rgb[0] = 255; |
2430 | | rgb[1] = 23; |
2431 | | rgb[2] = 42; |
2432 | | } |
2433 | | |
2434 | | b.iter(move || { |
2435 | | let image: &RgbImage = test::black_box(&a); |
2436 | | let mut sum: usize = 0; |
2437 | | for x in 0..1000 { |
2438 | | for y in 0..1000 { |
2439 | | let pixel = image.get_pixel(x, y); |
2440 | | sum = sum.wrapping_add(pixel[0] as usize); |
2441 | | sum = sum.wrapping_add(pixel[1] as usize); |
2442 | | sum = sum.wrapping_add(pixel[2] as usize); |
2443 | | } |
2444 | | } |
2445 | | test::black_box(sum) |
2446 | | }); |
2447 | | |
2448 | | b.bytes = 1000 * 1000 * 3; |
2449 | | } |
2450 | | } |