arko/
pixel_sort_brut.rs

1use crate::io::IO;
2use crate::pixel_sorting::PixelSorting;
3use image::GenericImageView;
4
5pub fn bottom_to_top(io: IO) {
6    let pixel_sort_brut = PixelSortBrut { io };
7    pixel_sort_brut.bottom_to_top()
8}
9
10pub fn left_to_right(io: IO) {
11    let pixel_sort_brut = PixelSortBrut { io };
12    pixel_sort_brut.left_to_right()
13}
14
15pub fn right_to_left(io: IO) {
16    let pixel_sort_brut = PixelSortBrut { io };
17    pixel_sort_brut.right_to_left()
18}
19
20pub fn top_to_bottom(io: IO) {
21    let pixel_sort_brut = PixelSortBrut { io };
22    pixel_sort_brut.top_to_bottom()
23}
24
25struct PixelSortBrut<'a> {
26    io: IO<'a>,
27}
28
29impl<'a> PixelSortBrut<'a> {
30    fn bottom_to_top(&self) {
31        let mut new_img = image::ImageBuffer::new(self.io.in_img.width(), self.io.in_img.height());
32        for w in 0..(self.io.in_img.width() - 1) {
33            let mut strip = Vec::new();
34            strip.push(self.io.in_img.get_pixel(w, self.io.in_img.height() - 1));
35            for h in 1..(self.io.in_img.height() - 1) {
36                strip.push(self.io.in_img.get_pixel(w, self.io.in_img.height() - 1 - h));
37            }
38            let new_strip = self.sort_strip_by_hue(&mut strip);
39            self.prepend_strip_to_image_vertically(
40                &mut new_img,
41                new_strip,
42                self.io.in_img.height() - 1,
43                w,
44            );
45        }
46        new_img.save(self.io.out_img).unwrap();
47    }
48
49    fn left_to_right(&self) {
50        let mut new_img = image::ImageBuffer::new(self.io.in_img.width(), self.io.in_img.height());
51        for h in 0..(self.io.in_img.height() - 1) {
52            let mut strip = Vec::new();
53            strip.push(self.io.in_img.get_pixel(0, h));
54            for w in 1..(self.io.in_img.width() - 1) {
55                strip.push(self.io.in_img.get_pixel(w, h));
56            }
57            let new_strip = self.sort_strip_by_hue(&mut strip);
58            self.append_strip_to_image(&mut new_img, new_strip, h, 0);
59        }
60        new_img.save(self.io.out_img).unwrap();
61    }
62
63    fn right_to_left(&self) {
64        let mut new_img = image::ImageBuffer::new(self.io.in_img.width(), self.io.in_img.height());
65        for h in 0..(self.io.in_img.height() - 1) {
66            let mut strip = Vec::new();
67            strip.push(self.io.in_img.get_pixel(self.io.in_img.width() - 1, h));
68            for w in 1..(self.io.in_img.width() - 1) {
69                strip.push(self.io.in_img.get_pixel(self.io.in_img.width() - 1 - w, h));
70            }
71            let new_strip = self.sort_strip_by_hue(&mut strip);
72            self.prepend_strip_to_image(&mut new_img, new_strip, h, self.io.in_img.width() - 1);
73        }
74        new_img.save(self.io.out_img).unwrap();
75    }
76
77    fn top_to_bottom(&self) {
78        let mut new_img = image::ImageBuffer::new(self.io.in_img.width(), self.io.in_img.height());
79        for w in 0..(self.io.in_img.width() - 1) {
80            let mut strip = Vec::new();
81            strip.push(self.io.in_img.get_pixel(w, 0));
82            for h in 1..(self.io.in_img.height() - 1) {
83                strip.push(self.io.in_img.get_pixel(w, h));
84            }
85            let new_strip = self.sort_strip_by_hue(&mut strip);
86            self.append_strip_to_image_vertically(&mut new_img, new_strip, 0, w);
87        }
88        new_img.save(self.io.out_img).unwrap();
89    }
90}
91impl<'a> PixelSorting for PixelSortBrut<'a> {}