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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use std::sync::Arc;
use crate::error::ImageDataError;
use crate::ImageInfo;
pub trait AsImageView {
fn as_image_view(&self) -> Result<ImageView, ImageDataError>;
}
pub fn image_info(image: &impl AsImageView) -> Result<ImageInfo, ImageDataError> {
Ok(image.as_image_view()?.info())
}
#[derive(Debug, Copy, Clone)]
pub struct ImageView<'a> {
info: ImageInfo,
data: &'a [u8],
}
impl<'a> ImageView<'a> {
pub fn new(info: ImageInfo, data: &'a [u8]) -> Self {
Self { info, data }
}
pub fn info(&self) -> ImageInfo {
self.info
}
pub fn data(&self) -> &[u8] {
self.data
}
}
impl<'a> AsImageView for ImageView<'a> {
fn as_image_view(&self) -> Result<ImageView, ImageDataError> {
Ok(*self)
}
}
pub enum Image {
Box(BoxImage),
Arc(ArcImage),
BoxDyn(Box<dyn AsImageView + Send>),
ArcDyn(Arc<dyn AsImageView + Sync + Send>),
Invalid(ImageDataError),
}
impl Clone for Image {
fn clone(&self) -> Self {
match self {
Self::Box(x) => Self::Box(x.clone()),
Self::Arc(x) => Self::Arc(x.clone()),
Self::BoxDyn(x) => match x.as_image_view() {
Ok(view) => Self::Box(BoxImage::new(view.info, view.data.into())),
Err(error) => Self::Invalid(error),
},
Self::ArcDyn(x) => Self::ArcDyn(x.clone()),
Self::Invalid(x) => Self::Invalid(x.clone()),
}
}
}
impl<T: AsImageView> AsImageView for Box<T> {
fn as_image_view(&self) -> Result<ImageView, ImageDataError> {
self.as_ref().as_image_view()
}
}
impl<T: AsImageView> AsImageView for Arc<T> {
fn as_image_view(&self) -> Result<ImageView, ImageDataError> {
self.as_ref().as_image_view()
}
}
#[derive(Debug, Clone)]
pub struct BoxImage {
info: ImageInfo,
data: Box<[u8]>,
}
#[derive(Debug, Clone)]
pub struct ArcImage {
info: ImageInfo,
data: Arc<[u8]>,
}
impl Image {
pub fn as_image_view(&self) -> Result<ImageView, ImageDataError> {
match self {
Self::Box(x) => Ok(x.as_view()),
Self::Arc(x) => Ok(x.as_view()),
Self::BoxDyn(x) => x.as_image_view(),
Self::ArcDyn(x) => x.as_image_view(),
Self::Invalid(e) => Err(e.clone()),
}
}
}
impl AsImageView for Image {
fn as_image_view(&self) -> Result<ImageView, ImageDataError> {
self.as_image_view()
}
}
impl BoxImage {
pub fn new(info: ImageInfo, data: Box<[u8]>) -> Self {
Self { info, data }
}
pub fn as_view(&self) -> ImageView {
ImageView::new(self.info, &self.data)
}
pub fn info(&self) -> ImageInfo {
self.info
}
pub fn data(&self) -> &[u8] {
&self.data
}
}
impl AsImageView for BoxImage {
fn as_image_view(&self) -> Result<ImageView, ImageDataError> {
Ok(self.as_view())
}
}
impl ArcImage {
pub fn new(info: ImageInfo, data: Arc<[u8]>) -> Self {
Self { info, data }
}
pub fn as_view(&self) -> ImageView {
ImageView::new(self.info, &self.data)
}
pub fn info(&self) -> ImageInfo {
self.info
}
pub fn data(&self) -> &[u8] {
&self.data
}
}
impl AsImageView for ArcImage {
fn as_image_view(&self) -> Result<ImageView, ImageDataError> {
Ok(self.as_view())
}
}
impl From<ImageView<'_>> for BoxImage {
fn from(other: ImageView) -> Self {
Self {
info: other.info,
data: other.data.into(),
}
}
}
impl From<&'_ ImageView<'_>> for BoxImage {
fn from(other: &ImageView) -> Self {
Self {
info: other.info,
data: other.data.into(),
}
}
}
impl From<ImageView<'_>> for ArcImage {
fn from(other: ImageView) -> Self {
Self {
info: other.info,
data: other.data.into(),
}
}
}
impl From<&'_ ImageView<'_>> for ArcImage {
fn from(other: &ImageView) -> Self {
Self {
info: other.info,
data: other.data.into(),
}
}
}
impl From<ImageView<'_>> for Image {
fn from(other: ImageView) -> Self {
Self::Box(BoxImage::from(other))
}
}
impl From<&'_ ImageView<'_>> for Image {
fn from(other: &ImageView) -> Self {
Self::Box(BoxImage::from(other))
}
}
impl From<BoxImage> for ArcImage {
fn from(other: BoxImage) -> Self {
Self {
info: other.info,
data: other.data.into(),
}
}
}
impl From<BoxImage> for Image {
fn from(other: BoxImage) -> Self {
Self::Box(other)
}
}
impl From<ArcImage> for Image {
fn from(other: ArcImage) -> Self {
Self::Arc(other)
}
}
impl From<Box<dyn AsImageView + Send>> for Image {
fn from(other: Box<dyn AsImageView + Send>) -> Self {
Self::BoxDyn(other)
}
}
impl From<Arc<dyn AsImageView + Sync + Send>> for Image {
fn from(other: Arc<dyn AsImageView + Sync + Send>) -> Self {
Self::ArcDyn(other)
}
}
impl<T> From<Box<T>> for Image
where
T: AsImageView + Send + 'static,
{
fn from(other: Box<T>) -> Self {
Self::BoxDyn(other)
}
}
impl<T> From<Arc<T>> for Image
where
T: AsImageView + Send + Sync + 'static,
{
fn from(other: Arc<T>) -> Self {
Self::ArcDyn(other)
}
}