buffer_graphics_lib/
scaling.rs1use crate::image::Image;
2
3pub(crate) fn scale_nearest_neighbor(image: &Image, x_scale: usize, y_scale: usize) -> Image {
4 let new_width = image.width() * x_scale;
5 let new_height = image.height() * y_scale;
6 let mut new_image = Image::new_blank(new_width, new_height);
7 let x_scale = 1.0 / x_scale as f32;
8 let y_scale = 1.0 / y_scale as f32;
9 for y in 0..new_height {
10 for x in 0..new_width {
11 let px = (x as f32 * x_scale).floor() as usize;
12 let py = (y as f32 * y_scale).floor() as usize;
13 new_image.set_pixel(x, y, image.get_pixel(px, py));
14 }
15 }
16 new_image
17}
18
19pub(crate) fn scale_epx(image: &Image) -> Image {
20 let new_width = image.width() * 2;
21 let new_height = image.height() * 2;
22 let mut new_image = Image::new_blank(new_width, new_height);
23 for x in 0..image.width() {
24 for y in 0..image.height() {
25 let mut p1 = image.get_pixel(x, y);
26 let mut p2 = p1;
27 let mut p3 = p1;
28 let mut p4 = p1;
29 let a = image.get_pixel(x, if y > 0 { y - 1 } else { y });
30 let c = image.get_pixel(if x > 0 { x - 1 } else { x }, y);
31 let b = image.get_pixel(if x < image.width() - 2 { x + 1 } else { x }, y);
32 let d = image.get_pixel(x, if y < image.height() - 2 { y + 1 } else { y });
33
34 if c == a && c != d && a != b {
35 p1 = a
36 }
37 if a == b && a != c && b != d {
38 p2 = b
39 }
40 if d == c && d != b && c != a {
41 p3 = c
42 }
43 if b == d && b != a && d != c {
44 p4 = d
45 }
46
47 let nx = x * 2;
48 let ny = y * 2;
49 new_image.set_pixel(nx, ny, p1);
50 new_image.set_pixel(nx + 1, ny, p2);
51 new_image.set_pixel(nx, ny + 1, p3);
52 new_image.set_pixel(nx + 1, ny + 1, p4);
53 }
54 }
55 new_image
56}