summaryrefslogtreecommitdiff
path: root/racer-tracer/src/image.rs
blob: 502a15a2d40d4c96ed5141ea6ae15532ff9e3380 (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
89
90
91
92
93
94
95
96
pub struct Image {
    pub aspect_ratio: f64,
    pub width: usize,
    pub height: usize,
    pub samples_per_pixel: usize,
}

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

// 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,
            samples: image.samples_per_pixel,
            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 samples: 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,
                samples: self.samples,
                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,
                samples: self.samples,
                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,
                samples: self.samples,
                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,
                samples: self.samples,
                screen_width: self.screen_width,
                screen_height: self.screen_height,
            },
        ]
    }
}