File size: 4,147 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
use anyhow::Context;

use super::Image;

pub fn convolution2x2<F>(bitmap: &Image, callback: F) -> anyhow::Result<Image>
    where F: Fn(&Image) -> anyhow::Result<u8>
{
    let width: u8 = bitmap.width();
    let height: u8 = bitmap.height();
    if width < 2 || height < 2 {
        return Err(anyhow::anyhow!("too small bitmap, must be 2x2 or bigger"));
    }
    let mut computed_bitmap = Image::zero(width - 1, height - 1);
    let mut conv_bitmap = Image::zero(2, 2);
    for self_y in 0..height-1 {
        for self_x in 0..width-1 {
            for conv_y in 0..2u8 {
                for conv_x in 0..2u8 {
                    let get_x: i32 = (self_x as i32) + (conv_x as i32);
                    let get_y: i32 = (self_y as i32) + (conv_y as i32);
                    let pixel_value: u8 = bitmap.get(get_x, get_y)
                        .ok_or_else(|| anyhow::anyhow!("self.get({},{}) returned None", get_x, get_y))?;
                    conv_bitmap.set(conv_x as i32, conv_y as i32, pixel_value)
                        .ok_or_else(|| anyhow::anyhow!("conv_bitmap.set({},{}) returned None", conv_x, conv_y))?;
                }
            }
            let computed_value: u8 = callback(&conv_bitmap)
                .with_context(|| format!("error in callback when computing ({},{})", self_x, self_y))?;
            computed_bitmap.set(self_x as i32, self_y as i32, computed_value)
                .ok_or_else(|| anyhow::anyhow!("computed_bitmap.set({},{}) returned None", self_x, self_y))?;
        }
    }
    Ok(computed_bitmap)
}

#[allow(dead_code)]
fn conv2x2_max(bm: &Image) -> anyhow::Result<u8> {
    let mut value: u8 = 0;
    for pixel in bm.pixels() {
        value = u8::max(value, *pixel);
    }
    Ok(value)
}

#[allow(dead_code)]
fn conv2x2_min(bm: &Image) -> anyhow::Result<u8> {
    let mut value: u8 = 255;
    for pixel in bm.pixels() {
        value = u8::min(value, *pixel);
    }
    Ok(value)
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::arc::ImageTryCreate;

    #[test]
    fn test_10000_callback() {
        // Arrange
        let pixels: Vec<u8> = vec![
            1, 2, 3,
            4, 5, 6,
            7, 8, 9,
        ];
        let input: Image = Image::try_create(3, 3, pixels).expect("image");

        // Act
        let output: Image = convolution2x2(&input, |bm| {
            let mut sum: u64 = 0;
            for pixel in bm.pixels() {
                sum += *pixel as u64;
            }
            let value = (sum & 255) as u8;
            Ok(value)
        }).expect("image");

        // Assert
        assert_eq!(output.width(), 2);
        assert_eq!(output.height(), 2);
        assert_eq!(output.get(0, 0), Some(1+2+4+5));
        assert_eq!(output.get(1, 0), Some(2+3+5+6));
        assert_eq!(output.get(0, 1), Some(4+5+7+8));
        assert_eq!(output.get(1, 1), Some(5+6+8+9));
    }

    #[test]
    fn test_20000_max() {
        // Arrange
        let pixels: Vec<u8> = vec![
            1,2,3,4,
            5,6,7,8,
            9,10,11,12,
            13,14,15,16,
        ];
        let input: Image = Image::try_create(4, 4, pixels).expect("image");

        // Act
        let output: Image = convolution2x2(&input, conv2x2_max).expect("image");

        // Assert
        let expected_pixels: Vec<u8> = vec![
            6,7,8,
            10,11,12,
            14,15,16,
        ];
        let expected: Image = Image::try_create(3, 3, expected_pixels).expect("image");
        assert_eq!(output, expected);
    }

    #[test]
    fn test_20001_min() {
        // Arrange
        let pixels: Vec<u8> = vec![
            1,2,3,4,
            5,6,7,8,
            9,10,11,12,
            13,14,15,16,
        ];
        let input: Image = Image::try_create(4, 4, pixels).expect("image");

        // Act
        let output: Image = convolution2x2(&input, conv2x2_min).expect("image");

        // Assert
        let expected_pixels: Vec<u8> = vec![
            1,2,3,
            5,6,7,
            9,10,11,
        ];
        let expected: Image = Image::try_create(3, 3, expected_pixels).expect("image");
        assert_eq!(output, expected);
    }
}