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
#![warn(missing_docs)]
extern crate framing;
extern crate scrap;
pub use scrap::Display;
use framing::video::{VideoFrame, Rgba};
use std::{io, mem, ptr};
pub struct Capturer {
capturer: scrap::Capturer
}
impl Capturer {
pub fn new(display: Display) -> io::Result<Self> {
let capturer = scrap::Capturer::new(display)?;
Ok(Capturer { capturer })
}
pub fn width(&self) -> usize { self.capturer.width() }
pub fn height(&self) -> usize { self.capturer.height() }
pub fn frame(&mut self) -> io::Result<Frame> {
Ok(Frame {
width: self.width(),
height: self.height(),
frame: self.capturer.frame()?
})
}
}
pub struct Frame<'a> {
frame: scrap::Frame<'a>,
width: usize,
height: usize
}
impl<'a> VideoFrame for Frame<'a> {
type Pixel = Rgba;
fn width(&self) -> usize { self.width }
fn height(&self) -> usize { self.height }
unsafe fn pixel(&self, x: usize, y: usize) -> Self::Pixel {
let mut pix: [u8; 4] = mem::uninitialized();
let off = 4 * (y * self.width + x) as isize;
ptr::copy_nonoverlapping(
self.frame.as_ptr().offset(off),
pix.as_mut_ptr(),
4
);
let base = pix.as_mut_ptr();
mem::swap(&mut *base, &mut *base.offset(2));
pix.into()
}
}
unsafe impl<'a> Sync for Frame<'a> {}
unsafe impl<'a> Send for Frame<'a> {}