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
use image::RgbImage;

use crate::{
    line_sorter::{sort_column, sort_row},
    Options,
};

/// Sorts pixels in the given image with default options
pub fn sort(buf: &mut RgbImage) {
    sort_with_options(buf, &Options::default());
}

/// Sorts pixels in the given image with options
pub fn sort_with_options(buf: &mut RgbImage, options: &Options) {
    for col in 0..buf.width() {
        sort_column(buf, col, options);
    }

    for row in 0..buf.height() {
        sort_row(buf, row, options);
    }
}

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

    #[test]
    fn test_sort() {
        let mut actual = image::open("tests/p1.bmp").unwrap().to_rgb8();
        sort(&mut actual);
        let expected = image::open("tests/p1-brightness_default.bmp")
            .unwrap()
            .to_rgb8();
        assert!(expected.as_raw() == actual.as_raw());
    }

    #[test]
    fn test_sort_with_options_black() {
        let mut actual = image::open("tests/p1.bmp").unwrap().to_rgb8();
        let options = Options {
            mode: Mode::black(),
        };
        sort_with_options(&mut actual, &options);
        let expected = image::open("tests/p1-black_default.bmp").unwrap().to_rgb8();
        assert!(expected.as_raw() == actual.as_raw());
    }

    #[test]
    fn test_sort_with_options_white() {
        let mut actual = image::open("tests/p1.bmp").unwrap().to_rgb8();
        let options = Options {
            mode: Mode::white(),
        };
        sort_with_options(&mut actual, &options);
        let expected = image::open("tests/p1-white_default.bmp").unwrap().to_rgb8();
        assert!(expected.as_raw() == actual.as_raw());
    }
}