/
draw_frame.rs
47 lines (40 loc) · 1.26 KB
/
draw_frame.rs
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
use crate::image::Image;
use crate::processor::Processor;
impl Processor {
pub fn draw_frame(image: &mut Image) {
let width: u32 = image.width.into();
let height: u32 = image.height.into();
// paint to top and bottom
for y in [0, height - 1].iter() {
for x in 0..width {
let p = ((y * width + x) * 4) as usize;
image.paint(p, 255);
}
}
// paint to left and right
for x in [0, width - 1].iter() {
for y in 0..height {
let p = ((y * width + x) * 4) as usize;
image.paint(p, 255);
}
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_draw_frame() {
let pixels = vec![
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0,
];
let expect = vec![
255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 0, 0, 0, 0,
255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0, 255, 255, 255, 0,
];
let mut image = Image::new(3, 3, pixels);
Processor::draw_frame(&mut image);
assert_eq!(expect, image.pixels());
}
}