summaryrefslogtreecommitdiff
path: root/racer-tracer/src/image.rs
blob: 1df4cc5a550a826da5b9e9f2cb4a95e0fabe4a77 (plain)
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
pub struct Image {
    pub aspect_ratio: f64,
    pub width: usize,
    pub height: usize,
}

impl Image {
    pub fn new(width: usize, height: usize) -> Image {
        Image {
            aspect_ratio: width as f64 / height as f64,
            width,
            height,
        }
    }
}

// TODO: SubImage and Image can probably be the same struct
impl From<&Image> for SubImage {
    fn from(image: &Image) -> Self {
        SubImage {
            x: 0,
            y: 0,
            width: image.width,
            height: image.height,
            screen_width: image.width,
            screen_height: image.height,
        }
    }
}

pub struct SubImage {
    pub x: usize,
    pub y: usize,
    pub screen_width: usize,
    pub screen_height: usize,
    pub width: usize,
    pub height: usize,
}

pub trait QuadSplit {
    fn quad_split(&self) -> [SubImage; 4];
}

impl QuadSplit for SubImage {
    fn quad_split(&self) -> [SubImage; 4] {
        let half_w = self.width / 2;
        let half_h = self.height / 2;

        [
            // Top Left
            SubImage {
                x: self.x,
                y: self.y,
                width: half_w,
                height: half_h,
                screen_width: self.screen_width,
                screen_height: self.screen_height,
            },
            // Top Right
            SubImage {
                x: self.x + half_w,
                y: self.y,
                width: half_w,
                height: half_h,
                screen_width: self.screen_width,
                screen_height: self.screen_height,
            },
            // Bottom Left
            SubImage {
                x: self.x,
                y: self.y + half_h,
                width: half_w,
                height: half_h,
                screen_width: self.screen_width,
                screen_height: self.screen_height,
            },
            // Bottom Right
            SubImage {
                x: self.x + half_w,
                y: self.y + half_h,
                width: half_w,
                height: half_h,
                screen_width: self.screen_width,
                screen_height: self.screen_height,
            },
        ]
    }
}