1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
use super::*;
#[derive(Eq, PartialEq, Clone)]
pub struct PixelVec<T> {
pub resolution: Vec2<usize>,
pub pixels: Vec<T>,
}
impl<Pixel> PixelVec<Pixel> {
pub fn constructor<Channels>(resolution: Vec2<usize>, _: &Channels) -> Self where Pixel: Default + Clone {
PixelVec { resolution, pixels: vec![Pixel::default(); resolution.area()] }
}
#[inline]
pub fn get_pixel(&self, position: Vec2<usize>) -> &Pixel where Pixel: Sync {
&self.pixels[self.compute_pixel_index(position)]
}
#[inline]
pub fn set_pixel(&mut self, position: Vec2<usize>, pixel: Pixel) {
let index = self.compute_pixel_index(position);
self.pixels[index] = pixel;
}
pub fn new(resolution: impl Into<Vec2<usize>>, pixels: Vec<Pixel>) -> Self {
let size = resolution.into();
assert_eq!(size.area(), pixels.len(), "expected {} samples, but vector length is {}", size.area(), pixels.len());
Self { resolution: size, pixels }
}
#[inline]
pub fn compute_pixel_index(&self, position: Vec2<usize>) -> usize {
position.flat_index_for_size(self.resolution)
}
}
use crate::image::validate_results::{ValidateResult, ValidationResult};
impl<Px> ValidateResult for PixelVec<Px> where Px: ValidateResult {
fn validate_result(&self, other: &Self, options: ValidationOptions, location: String) -> ValidationResult {
if self.resolution != other.resolution { Err(location + " > resolution") }
else { self.pixels.as_slice().validate_result(&other.pixels.as_slice(), options, location + " > pixels") }
}
}
impl<Px> GetPixel for PixelVec<Px> where Px: Clone + Sync {
type Pixel = Px;
fn get_pixel(&self, position: Vec2<usize>) -> Self::Pixel {
self.get_pixel(position).clone()
}
}
use std::fmt::*;
impl<T> Debug for PixelVec<T> {
#[inline] fn fmt(&self, formatter: &mut Formatter<'_>) -> std::fmt::Result {
write!(formatter, "[{}; {}]", std::any::type_name::<T>(), self.pixels.len())
}
}