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
use crate::color::Color;
use crate::image::Image;
use crate::ty::Type;
use std::marker::PhantomData;
#[cfg_attr(feature = "ser", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, PartialEq, Clone)]
pub struct ImageBuf<T: Type, C: Color> {
width: usize,
height: usize,
data: Vec<T>,
_color: PhantomData<C>,
}
impl<T: Type, C: Color> Image<T, C> for ImageBuf<T, C> {
fn shape(&self) -> (usize, usize, usize) {
(self.width, self.height, C::channels())
}
fn data(&self) -> &[T] {
self.data.as_ref()
}
fn data_mut(&mut self) -> &mut [T] {
self.data.as_mut()
}
}
impl<T: Type, C: Color> ImageBuf<T, C> {
pub fn new(width: usize, height: usize) -> Self {
ImageBuf {
width,
height,
data: vec![T::zero(); width * height * C::channels()],
_color: PhantomData,
}
}
pub fn inner(self) -> Vec<T> {
self.data
}
pub fn new_like(&self) -> Self {
Self::new(self.width, self.height)
}
pub fn new_like_with_type<U: Type>(&self) -> ImageBuf<U, C> {
ImageBuf::new(self.width, self.height)
}
pub fn new_like_with_color<D: Color>(&self) -> ImageBuf<T, D> {
ImageBuf::new(self.width, self.height)
}
pub fn new_from(width: usize, height: usize, data: Vec<T>) -> Self {
ImageBuf {
width,
height,
data,
_color: PhantomData,
}
}
}