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
// ops.rs       Compositing operations
//
// Copyright (c) 2020  Douglas P Lau
//
//! Compositing and blending operations.
//!
//! Used in `Raster` methods [composite_color], [composite_matte] and
//! [composite_raster].
//!
//! [composite_color]: ../struct.Raster.html#method.composite_color
//! [composite_matte]: ../struct.Raster.html#method.composite_matte
//! [composite_raster]: ../struct.Raster.html#method.composite_raster
use crate::chan::Channel;

/// Blending operation for compositing.
///
/// This trait is *sealed*, and cannot be implemented outside of this crate.
pub trait Blend: Copy + Clone {
    /// Composite a destination and source
    ///
    /// * `dst` Destination channel
    /// * `da1` One minus destination *alpha*
    /// * `src` Source channel
    /// * `sa1` One minus source *alpha*
    fn composite<C: Channel>(dst: &mut C, da1: C, src: &C, sa1: C);
}

/// Source only (ignore destination)
#[derive(Clone, Copy)]
pub struct Src;

/// Destination only (ignore source)
#[derive(Clone, Copy)]
pub struct Dest;

/// Source Over compositing (standard *alpha* blending)
#[derive(Clone, Copy)]
pub struct SrcOver;

/// Destination Over compositing (*alpha* blending behind destination)
#[derive(Clone, Copy)]
pub struct DestOver;

/// Source Out compositing (remove destination from source)
#[derive(Clone, Copy)]
pub struct SrcOut;

/// Destination Out compositing (remove source from destination)
#[derive(Clone, Copy)]
pub struct DestOut;

/// Source In compositing (mask source with destination *alpha*)
#[derive(Clone, Copy)]
pub struct SrcIn;

/// Destination In compositing (mask destination with source *alpha*)
#[derive(Clone, Copy)]
pub struct DestIn;

/// Source Atop compositing (overlay and mask source atop destination)
#[derive(Clone, Copy)]
pub struct SrcAtop;

/// Destination Atop compositing (overlay and mask destination atop source)
#[derive(Clone, Copy)]
pub struct DestAtop;

/// Xor compositing (source or destination with no overlap)
#[derive(Clone, Copy)]
pub struct Xor;

/// Clear (set to default)
#[derive(Clone, Copy)]
pub struct Clear;

/// Plus, or Lighter compositing (source added to destination)
#[derive(Clone, Copy)]
pub struct Plus;

impl Blend for Src {
    fn composite<C: Channel>(dst: &mut C, _da1: C, src: &C, _sa1: C) {
        *dst = *src;
    }
}

impl Blend for Dest {
    fn composite<C: Channel>(_dst: &mut C, _da1: C, _src: &C, _sa1: C) {
        // leave _dst as is
    }
}

impl Blend for SrcOver {
    fn composite<C: Channel>(dst: &mut C, _da1: C, src: &C, sa1: C) {
        *dst = *src + *dst * sa1;
    }
}

impl Blend for DestOver {
    fn composite<C: Channel>(dst: &mut C, da1: C, src: &C, _sa1: C) {
        *dst = *src * da1 + *dst;
    }
}

impl Blend for SrcOut {
    fn composite<C: Channel>(dst: &mut C, da1: C, src: &C, _sa1: C) {
        *dst = *src * da1;
    }
}

impl Blend for DestOut {
    fn composite<C: Channel>(dst: &mut C, _da1: C, _src: &C, sa1: C) {
        *dst = *dst * sa1;
    }
}

impl Blend for SrcIn {
    fn composite<C: Channel>(dst: &mut C, da1: C, src: &C, _sa1: C) {
        let da = C::MAX - da1;
        *dst = *src * da;
    }
}

impl Blend for DestIn {
    fn composite<C: Channel>(dst: &mut C, _da1: C, _src: &C, sa1: C) {
        let sa = C::MAX - sa1;
        *dst = *dst * sa;
    }
}

impl Blend for SrcAtop {
    fn composite<C: Channel>(dst: &mut C, da1: C, src: &C, sa1: C) {
        let da = C::MAX - da1;
        *dst = *src * da + *dst * sa1;
    }
}

impl Blend for DestAtop {
    fn composite<C: Channel>(dst: &mut C, da1: C, src: &C, sa1: C) {
        let sa = C::MAX - sa1;
        *dst = *src * da1 + *dst * sa;
    }
}

impl Blend for Xor {
    fn composite<C: Channel>(dst: &mut C, da1: C, src: &C, sa1: C) {
        *dst = *src * da1 + *dst * sa1;
    }
}

impl Blend for Clear {
    fn composite<C: Channel>(dst: &mut C, _da1: C, _src: &C, _sa1: C) {
        *dst = C::default();
    }
}

impl Blend for Plus {
    fn composite<C: Channel>(dst: &mut C, _da1: C, src: &C, _sa1: C) {
        *dst = *src + *dst;
    }
}