1 |
|
2 | //! Provides a predefined pixel storage.
|
3 | //! Currently only contains a simple flattened vector storage.
|
4 | //! Use the functions `create_pixel_vec::<YourPixelTuple>` and
|
5 | //! `set_pixel_in_vec::<YourPixelTuple>` for reading a predefined pixel vector.
|
6 | //! Use the function `PixelVec::new` to create a pixel vector which can be written to a file.
|
7 |
|
8 | use super::*;
|
9 |
|
10 | /// Store all samples in a single array.
|
11 | /// All samples will be converted to the type `T`.
|
12 | /// This supports all the sample types, `f16`, `f32`, and `u32`.
|
13 | ///
|
14 | /// The flattened vector contains all rows one after another.
|
15 | /// In each row, for each pixel, its red, green, blue, and then alpha
|
16 | /// samples are stored one after another.
|
17 | ///
|
18 | /// Use `PixelVec.compute_pixel_index(position)`
|
19 | /// to compute the flat index of a specific pixel.
|
20 | #[derive (Eq, PartialEq, Clone)]
|
21 | pub struct PixelVec<T> {
|
22 |
|
23 | /// The resolution of this layer.
|
24 | pub resolution: Vec2<usize>,
|
25 |
|
26 | /// The flattened vector contains all rows one after another.
|
27 | /// In each row, for each pixel, its red, green, blue, and then alpha
|
28 | /// samples are stored one after another.
|
29 | ///
|
30 | /// Use `Flattened::compute_pixel_index(image, position)`
|
31 | /// to compute the flat index of a specific pixel.
|
32 | pub pixels: Vec<T>,
|
33 | }
|
34 |
|
35 | impl<Pixel> PixelVec<Pixel> {
|
36 |
|
37 | /// Create a new flattened pixel storage, filled with default pixels.
|
38 | /// Accepts a `Channels` parameter, which is not used, so that it can be passed as a function pointer instead of calling it.
|
39 | pub fn constructor<Channels>(resolution: Vec2<usize>, _: &Channels) -> Self where Pixel: Default + Clone {
|
40 | PixelVec { resolution, pixels: vec![Pixel::default(); resolution.area()] }
|
41 | }
|
42 |
|
43 | /// Examine a pixel of a `PixelVec<T>` image.
|
44 | /// Can usually be used as a function reference instead of calling it directly.
|
45 | #[inline ]
|
46 | pub fn get_pixel(&self, position: Vec2<usize>) -> &Pixel where Pixel: Sync {
|
47 | &self.pixels[self.compute_pixel_index(position)]
|
48 | }
|
49 |
|
50 | /// Update a pixel of a `PixelVec<T>` image.
|
51 | /// Can usually be used as a function reference instead of calling it directly.
|
52 | #[inline ]
|
53 | pub fn set_pixel(&mut self, position: Vec2<usize>, pixel: Pixel) {
|
54 | let index = self.compute_pixel_index(position);
|
55 | self.pixels[index] = pixel;
|
56 | }
|
57 |
|
58 | /// Create a new flattened pixel storage, checking the length of the provided pixels vector.
|
59 | pub fn new(resolution: impl Into<Vec2<usize>>, pixels: Vec<Pixel>) -> Self {
|
60 | let size = resolution.into();
|
61 | assert_eq!(size.area(), pixels.len(), "expected {} samples, but vector length is {}" , size.area(), pixels.len());
|
62 | Self { resolution: size, pixels }
|
63 | }
|
64 |
|
65 | /// Compute the flat index of a specific pixel. Returns a range of either 3 or 4 samples.
|
66 | /// The computed index can be used with `PixelVec.samples[index]`.
|
67 | /// Panics for invalid sample coordinates.
|
68 | #[inline ]
|
69 | pub fn compute_pixel_index(&self, position: Vec2<usize>) -> usize {
|
70 | position.flat_index_for_size(self.resolution)
|
71 | }
|
72 | }
|
73 |
|
74 | use crate::image::validate_results::{ValidateResult, ValidationResult};
|
75 |
|
76 | impl<Px> ValidateResult for PixelVec<Px> where Px: ValidateResult {
|
77 | fn validate_result(&self, other: &Self, options: ValidationOptions, location: impl Fn() -> String) -> ValidationResult {
|
78 | if self.resolution != other.resolution { Err(location() + " > resolution" ) }
|
79 | else { self.pixels.as_slice().validate_result(&other.pixels.as_slice(), options, || location() + " > pixels" ) }
|
80 | }
|
81 | }
|
82 |
|
83 | impl<Px> GetPixel for PixelVec<Px> where Px: Clone + Sync {
|
84 | type Pixel = Px;
|
85 | fn get_pixel(&self, position: Vec2<usize>) -> Self::Pixel {
|
86 | self.get_pixel(position).clone()
|
87 | }
|
88 | }
|
89 |
|
90 | use std::fmt::*;
|
91 |
|
92 | impl<T> Debug for PixelVec<T> {
|
93 | #[inline ] fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
|
94 | write!(formatter, "[ {}; {}]" , std::any::type_name::<T>(), self.pixels.len())
|
95 | }
|
96 | }
|
97 |
|
98 | |