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
use crate::Blend;
use pix::{
Alpha, AlphaMode, AssocGray, Channel, Format, GammaMode, Gray, Translucent,
};
impl<C, A, M, G> Blend for Gray<C, A, M, G>
where
C: Channel,
A: Alpha<Chan = C>,
A: From<C>,
M: AlphaMode,
G: GammaMode,
{
fn over_fallback<B, H>(dst: &mut [Self], src: &[B], clr: Self)
where
B: Format<Chan = H>,
C: From<H>,
H: Channel,
H: From<C>,
{
let clr: AssocGray<H, Translucent<H>, G> = clr.convert();
for (bot, top) in dst.iter_mut().zip(src) {
let src: AssocGray<H, Translucent<H>, G> = top.convert();
let src = clr * src;
*bot = Self::over(*bot, src);
}
}
fn over<B, H>(dst: Self, src: B) -> Self
where
B: Format<Chan = H>,
Self::Chan: From<H>,
H: Channel,
H: From<C>,
{
let dst: AssocGray<H, Translucent<H>, G> = dst.convert();
let src: AssocGray<H, Translucent<H>, G> = src.convert();
let one_minus_src_a = H::MAX - src.alpha().value();
let a = src.alpha().value() + dst.alpha().value() * one_minus_src_a;
let v = src.value() + dst.value() * one_minus_src_a;
AssocGray::<C, Translucent<C>, G>::with_alpha(v, a).convert()
}
}