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
97
use crate::image::*;
#[derive(Debug, Clone)]
pub struct FrameBuffer {
pub width: usize,
pub height: usize,
pub color: Vec<u8>,
}
impl FrameBuffer {
pub fn new(width: usize, height: usize) -> FrameBuffer {
FrameBuffer {
width,
height,
color: vec![0; width * height * 4],
}
}
pub fn to_image(&self) -> Image {
Image {
buffer: self.color.clone(),
width: self.width,
height: self.height,
is_error: false,
}
}
pub fn to_image_buffer(&self, buffer: &mut Vec<u8>) {
buffer.clear();
if self.color.len() == buffer.len() {
buffer.copy_from_slice(self.color.as_slice());
}
}
pub fn blit_framebuffer(&mut self, fbuf_blit: &FrameBuffer, offset_x: usize, offset_y: usize) {
let stride = 4;
let extent_width: usize = offset_x + fbuf_blit.width;
let extent_height: usize = offset_y + fbuf_blit.height;
let src_height: usize = fbuf_blit.height;
let dst_height: usize = self.height;
let not_too_big: bool = self.width * self.height < fbuf_blit.width * self.height;
let not_out_of_bounds: bool = extent_width > self.width || extent_height > self.height;
if not_too_big && not_out_of_bounds {
println!("ERROR - FRAMEBUFFER BLIT: Does not fit inside target buffer!");
return;
}
let mut rows_src: Vec<&[u8]> = Vec::new();
fbuf_blit.color.chunks_exact(fbuf_blit.width * stride).enumerate().for_each(|(_, row)| {
rows_src.push(row);
});
let is_equal_size: bool = self.width == fbuf_blit.width && self.height == fbuf_blit.height;
self.color.chunks_exact_mut(self.width * stride).enumerate().for_each(|(i, row_dst)| {
if i >= dst_height { return; }
if i >= offset_y && i < offset_y + src_height {
if is_equal_size {
row_dst.copy_from_slice(rows_src[i]);
} else {
if i >= offset_y && i < (offset_y + rows_src.len()) {
let rightsect = row_dst.split_at_mut(offset_x * stride).1;
let section = rightsect.split_at_mut((extent_width - offset_x) * stride).0;
section.copy_from_slice(rows_src[i-offset_y]);
}
}
}
});
}
}