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
use num_traits::{Bounded, Num, NumCast};
use std::ops::{AddAssign};
use crate::color::{ColorType, Luma, LumaA, Rgb, Rgba, Bgr, Bgra};
pub trait EncodableLayout: seals::EncodableLayout {
fn as_bytes(&self) -> &[u8];
}
impl EncodableLayout for [u8] {
fn as_bytes(&self) -> &[u8] {
bytemuck::cast_slice(self)
}
}
impl EncodableLayout for [u16] {
fn as_bytes(&self) -> &[u8] {
bytemuck::cast_slice(self)
}
}
pub trait Primitive: Copy + NumCast + Num + PartialOrd<Self> + Clone + Bounded {}
impl Primitive for usize {}
impl Primitive for u8 {}
impl Primitive for u16 {}
impl Primitive for u32 {}
impl Primitive for u64 {}
impl Primitive for isize {}
impl Primitive for i8 {}
impl Primitive for i16 {}
impl Primitive for i32 {}
impl Primitive for i64 {}
impl Primitive for f32 {}
impl Primitive for f64 {}
pub trait Enlargeable: Sized + Bounded + NumCast {
type Larger: Primitive + AddAssign + 'static;
fn clamp_from(n: Self::Larger) -> Self {
if n > NumCast::from(Self::max_value()).unwrap() {
Self::max_value()
} else {
NumCast::from(n).unwrap()
}
}
}
impl Enlargeable for u8 {
type Larger = u32;
}
impl Enlargeable for u16 {
type Larger = u32;
}
impl Enlargeable for u32 {
type Larger = u64;
}
pub trait Lerp: Bounded + NumCast {
type Ratio: Primitive;
fn lerp(a: Self, b: Self, ratio: Self::Ratio) -> Self {
let a = <Self::Ratio as NumCast>::from(a).unwrap();
let b = <Self::Ratio as NumCast>::from(b).unwrap();
let res = a + (b - a) * ratio;
if res > NumCast::from(Self::max_value()).unwrap() {
Self::max_value()
} else if res < NumCast::from(0).unwrap() {
NumCast::from(0).unwrap()
} else {
NumCast::from(res).unwrap()
}
}
}
impl Lerp for u8 {
type Ratio = f32;
}
impl Lerp for u16 {
type Ratio = f32;
}
impl Lerp for u32 {
type Ratio = f64;
}
pub trait Pixel: Copy + Clone {
type Subpixel: Primitive;
const CHANNEL_COUNT: u8;
#[deprecated(note="please use CHANNEL_COUNT associated constant")]
fn channel_count() -> u8 {
Self::CHANNEL_COUNT
}
fn channels(&self) -> &[Self::Subpixel];
fn channels_mut(&mut self) -> &mut [Self::Subpixel];
const COLOR_MODEL: &'static str;
#[deprecated(note="please use COLOR_MODEL associated constant")]
fn color_model() -> &'static str {
Self::COLOR_MODEL
}
const COLOR_TYPE: ColorType;
#[deprecated(note="please use COLOR_TYPE associated constant")]
fn color_type() -> ColorType {
Self::COLOR_TYPE
}
fn channels4(
&self,
) -> (
Self::Subpixel,
Self::Subpixel,
Self::Subpixel,
Self::Subpixel,
);
fn from_channels(
a: Self::Subpixel,
b: Self::Subpixel,
c: Self::Subpixel,
d: Self::Subpixel,
) -> Self;
fn from_slice(slice: &[Self::Subpixel]) -> &Self;
fn from_slice_mut(slice: &mut [Self::Subpixel]) -> &mut Self;
fn to_rgb(&self) -> Rgb<Self::Subpixel>;
fn to_rgba(&self) -> Rgba<Self::Subpixel>;
fn to_luma(&self) -> Luma<Self::Subpixel>;
fn to_luma_alpha(&self) -> LumaA<Self::Subpixel>;
fn to_bgr(&self) -> Bgr<Self::Subpixel>;
fn to_bgra(&self) -> Bgra<Self::Subpixel>;
fn map<F>(&self, f: F) -> Self
where
F: FnMut(Self::Subpixel) -> Self::Subpixel;
fn apply<F>(&mut self, f: F)
where
F: FnMut(Self::Subpixel) -> Self::Subpixel;
fn map_with_alpha<F, G>(&self, f: F, g: G) -> Self
where
F: FnMut(Self::Subpixel) -> Self::Subpixel,
G: FnMut(Self::Subpixel) -> Self::Subpixel;
fn apply_with_alpha<F, G>(&mut self, f: F, g: G)
where
F: FnMut(Self::Subpixel) -> Self::Subpixel,
G: FnMut(Self::Subpixel) -> Self::Subpixel;
fn map_without_alpha<F>(&self, f: F) -> Self
where
F: FnMut(Self::Subpixel) -> Self::Subpixel,
{
let mut this = *self;
this.apply_with_alpha(f, |x| x);
this
}
fn apply_without_alpha<F>(&mut self, f: F)
where
F: FnMut(Self::Subpixel) -> Self::Subpixel,
{
self.apply_with_alpha(f, |x| x);
}
fn map2<F>(&self, other: &Self, f: F) -> Self
where
F: FnMut(Self::Subpixel, Self::Subpixel) -> Self::Subpixel;
fn apply2<F>(&mut self, other: &Self, f: F)
where
F: FnMut(Self::Subpixel, Self::Subpixel) -> Self::Subpixel;
fn invert(&mut self);
fn blend(&mut self, other: &Self);
}
mod seals {
pub trait EncodableLayout {}
impl EncodableLayout for [u8] {}
impl EncodableLayout for [u16] {}
}