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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
use super::RGB;
use super::{Error, Result};
use std::ops::{Index, IndexMut};
use std::path::Path;
#[derive(Clone, Debug, PartialEq)]
pub struct Img<P> {
buf: Vec<P>,
width: u32,
}
impl<P> Img<P> {
pub fn new(buf: impl IntoIterator<Item = P>, width: u32) -> Option<Self> {
let buf: Vec<P> = buf.into_iter().collect();
if width == 0 || buf.len() % width as usize != 0 {
None
} else {
Some(Img { buf, width })
}
}
pub const unsafe fn from_raw_buf(buf: Vec<P>, width: u32) -> Self {
Img { buf, width }
}
pub fn into_vec(self) -> Vec<P> {
self.buf
}
#[inline]
pub fn width(&self) -> u32 {
self.width
}
pub fn iter(&self) -> <&Self as IntoIterator>::IntoIter {
self.into_iter()
}
pub fn iter_mut(&mut self) -> <&mut Self as IntoIterator>::IntoIter {
self.buf.iter_mut()
}
#[inline]
pub fn height(&self) -> u32 {
self.len() as u32 / self.width
}
pub fn convert_with<Q>(self, convert: impl Fn(P) -> Q) -> Img<Q> {
let Img { buf, width } = self;
Img {
buf: buf.into_iter().map(convert).collect(),
width,
}
}
#[inline]
fn idx(&self, (x, y): (u32, u32)) -> usize {
((y * self.width) + x) as usize
}
#[inline]
pub fn len(&self) -> usize {
self.buf.len()
}
pub fn is_empty(&self) -> bool {
self.buf.is_empty()
}
#[inline]
pub fn get(&self, (x, y): (u32, u32)) -> Option<&P> {
self.buf.get(self.idx((x, y)))
}
#[inline]
pub fn size(&self) -> (u32, u32) {
(self.width(), self.height())
}
}
impl<N: From<u8>> Img<RGB<N>> {
pub fn load(path: impl AsRef<Path>) -> Result<Self> {
match image::open(&path).and_then(|img| Ok(img.to_rgb())) {
Err(err) => Err(Error::input(err, path.as_ref())),
Ok(img) => Ok(Img {
buf: img.pixels().map(|p| RGB::from(p.data)).collect(),
width: img.width(),
}),
}
}
}
impl Img<RGB<u8>> {
pub fn save(self, path: &Path) -> Result<()> {
let (width, height) = self.size();
let buf = image::RgbImage::from_raw(width, height, self.raw_buf()).unwrap();
if let Err(err) = buf.save(path) {
Err(Error::output(err, path))
} else {
Ok(())
}
}
pub fn raw_buf(self) -> Vec<u8> {
let mut raw_buf = Vec::with_capacity(self.len() * 3);
for RGB(r, g, b) in self.buf {
raw_buf.push(r);
raw_buf.push(g);
raw_buf.push(b);
}
raw_buf
}
}
impl<P> Index<(u32, u32)> for Img<P> {
type Output = P;
fn index(&self, (x, y): (u32, u32)) -> &P {
&self.buf[self.idx((x, y))]
}
}
impl<P> IndexMut<(u32, u32)> for Img<P> {
fn index_mut(&mut self, (x, y): (u32, u32)) -> &mut P {
let i = self.idx((x, y));
&mut self.buf[i]
}
}
impl<P> IntoIterator for Img<P> {
type Item = P;
type IntoIter = std::vec::IntoIter<P>;
fn into_iter(self) -> Self::IntoIter {
self.buf.into_iter()
}
}
impl<'a, P> IntoIterator for &'a Img<P> {
type Item = &'a P;
type IntoIter = std::slice::Iter<'a, P>;
fn into_iter(self) -> Self::IntoIter {
(&self.buf).iter()
}
}
impl<'a, P> IntoIterator for &'a mut Img<P> {
type Item = &'a mut P;
type IntoIter = std::slice::IterMut<'a, P>;
fn into_iter(self) -> Self::IntoIter {
(&mut self.buf).iter_mut()
}
}