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
use std;

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum FilterType {
    NoFilter = 0,
    Sub = 1,
    Up = 2,
    Avg = 3,
    Paeth = 4
}

 impl FilterType {  
    /// u8 -> Self. Temporary solution until Rust provides a canonical one.
    pub fn from_u8(n: u8) -> Option<FilterType> {
        match n {
            0 => Some(FilterType::NoFilter),
            1 => Some(FilterType::Sub),
            2 => Some(FilterType::Up),
            3 => Some(FilterType::Avg),
            4 => Some(FilterType::Paeth),
            _ => None
        }
    }
}

fn filter_paeth(a: u8, b: u8, c: u8) -> u8 {
    let ia = a as i16;
    let ib = b as i16;
    let ic = c as i16;

    let p = ia + ib - ic;

    let pa = (p - ia).abs();
    let pb = (p - ib).abs();
    let pc = (p - ic).abs();

    if pa <= pb && pa <= pc {
        a
    } else if pb <= pc {
        b
    } else {
        c
    }
}

pub fn unfilter(filter: FilterType, bpp: usize, previous: &[u8], current: &mut [u8]) -> std::result::Result<(), &'static str> {
    use self::FilterType::*;
    assert!(bpp > 0);
    let len = current.len();

    match filter {
        NoFilter => Ok(()),
        Sub => {
            for i in bpp..len {
                current[i] = current[i].wrapping_add(
                    current[i - bpp]
                );
            }
            Ok(())
        }
        Up => {
            if previous.len() < len {
                Err("Filtering failed: not enough data in previous row")
            } else {
                for i in 0..len {
                    current[i] = current[i].wrapping_add(
                        previous[i]
                    );
                }
                Ok(())
            }
        }
        Avg => {
            if previous.len() < len {
                Err("Filtering failed:  not enough data in previous row")
            } else if bpp > len {
                Err("Filtering failed: bytes per pixel is greater than length of row")
            } else {
                for i in 0..bpp {
                    current[i] = current[i].wrapping_add(
                        previous[i] / 2
                    );
                }

                for i in bpp..len {
                    current[i] = current[i].wrapping_add(
                        ((current[i - bpp] as i16 + previous[i] as i16) / 2) as u8
                    );
                }
                Ok(())
            }
        }
        Paeth => {
            if previous.len() < len {
                Err("Filtering failed: not enough data in previous row")
            } else if bpp > len {
                Err("Filtering failed: bytes per pixel is greater than length of row")
            } else {
                for i in 0..bpp {
                    current[i] = current[i].wrapping_add(
                        filter_paeth(0, previous[i], 0)
                    );
                }

                for i in bpp..len {
                    current[i] = current[i].wrapping_add(
                        filter_paeth(current[i - bpp], previous[i], previous[i - bpp])
                    );
                }
                Ok(())
            }
        }
    }
}

pub fn filter(method: FilterType, bpp: usize, previous: &[u8], current: &mut [u8]) {
    use self::FilterType::*;
    assert!(bpp > 0);
    let len  = current.len();

    match method {
        NoFilter => (),
        Sub      => {
            for i in (bpp..len).rev() {
                current[i] = current[i].wrapping_sub(current[i - bpp]);
            }
        }
        Up       => {
            for i in 0..len {
                current[i] = current[i].wrapping_sub(previous[i]);
            }
        }
        Avg  => {
            for i in (bpp..len).rev() {
                current[i] = current[i].wrapping_sub(current[i - bpp].wrapping_add(previous[i]) / 2);
            }

            for i in 0..bpp {
                current[i] = current[i].wrapping_sub(previous[i] / 2);
            }
        }
        Paeth    => {
            for i in (bpp..len).rev() {
                current[i] = current[i].wrapping_sub(filter_paeth(current[i - bpp], previous[i], previous[i - bpp]));
            }

            for i in 0..bpp {
                current[i] = current[i].wrapping_sub(filter_paeth(0, previous[i], 0));
            }
        }
    }
}