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
use crate::svgtree::{self, AId, EId};
use super::{Input, Kind, Primitive};
#[derive(Clone, Debug)]
pub struct ComponentTransfer {
    
    
    
    pub input: Input,
    
    pub func_r: TransferFunction,
    
    pub func_g: TransferFunction,
    
    pub func_b: TransferFunction,
    
    pub func_a: TransferFunction,
}
#[derive(Clone, Debug)]
pub enum TransferFunction {
    
    Identity,
    
    
    
    Table(Vec<f64>),
    
    
    
    Discrete(Vec<f64>),
    
    #[allow(missing_docs)]
    Linear {
        slope: f64,
        intercept: f64,
    },
    
    #[allow(missing_docs)]
    Gamma {
        amplitude: f64,
        exponent: f64,
        offset: f64,
    },
}
pub(crate) fn convert(fe: svgtree::Node, primitives: &[Primitive]) -> Kind {
    let mut kind = ComponentTransfer {
        input: super::resolve_input(fe, AId::In, primitives),
        func_r: TransferFunction::Identity,
        func_g: TransferFunction::Identity,
        func_b: TransferFunction::Identity,
        func_a: TransferFunction::Identity,
    };
    for child in fe.children().filter(|n| n.is_element()) {
        if let Some(func) = convert_transfer_function(child) {
            match child.tag_name().unwrap() {
                EId::FeFuncR => kind.func_r = func,
                EId::FeFuncG => kind.func_g = func,
                EId::FeFuncB => kind.func_b = func,
                EId::FeFuncA => kind.func_a = func,
                _ => {}
            }
        }
    }
    Kind::ComponentTransfer(kind)
}
fn convert_transfer_function(node: svgtree::Node) -> Option<TransferFunction> {
    match node.attribute(AId::Type)? {
        "identity" => {
            Some(TransferFunction::Identity)
        }
        "table" => {
            match node.attribute::<&Vec<f64>>(AId::TableValues) {
                Some(values) => Some(TransferFunction::Table(values.clone())),
                None => Some(TransferFunction::Table(Vec::new())),
            }
        }
        "discrete" => {
            match node.attribute::<&Vec<f64>>(AId::TableValues) {
                Some(values) => Some(TransferFunction::Discrete(values.clone())),
                None => Some(TransferFunction::Discrete(Vec::new())),
            }
        }
        "linear" => {
            Some(TransferFunction::Linear {
                slope: node.attribute(AId::Slope).unwrap_or(1.0),
                intercept: node.attribute(AId::Intercept).unwrap_or(0.0),
            })
        }
        "gamma" => {
            Some(TransferFunction::Gamma {
                amplitude: node.attribute(AId::Amplitude).unwrap_or(1.0),
                exponent: node.attribute(AId::Exponent).unwrap_or(1.0),
                offset: node.attribute(AId::Offset).unwrap_or(0.0),
            })
        }
        _ => None,
    }
}