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
pub mod context;
pub mod device;
pub mod stream;
pub use context::Context;
use std::{convert::TryInto, str};
use crate::format::PixelFormat;
impl From<&[u8; 4]> for PixelFormat {
fn from(fourcc: &[u8; 4]) -> Self {
if fourcc == b"GREY" {
PixelFormat::Gray(8)
} else if fourcc == b"Y16 " {
PixelFormat::Gray(16)
} else if fourcc == b"Z16 " {
PixelFormat::Depth(16)
}
else if fourcc == b"BGR3" {
PixelFormat::Bgr(24)
} else if fourcc == b"RGB3" {
PixelFormat::Rgb(24)
}
else if fourcc == b"MJPG" {
PixelFormat::Jpeg
}
else {
PixelFormat::Custom(String::from(str::from_utf8(fourcc).unwrap()))
}
}
}
impl TryInto<[u8; 4]> for PixelFormat {
type Error = ();
fn try_into(self) -> Result<[u8; 4], Self::Error> {
match self {
PixelFormat::Custom(repr) => {
let repr_bytes = repr.as_bytes();
if repr_bytes.len() <= 4 {
let mut bytes = [0u8; 4];
for i in 0..repr_bytes.len() {
bytes[i] = repr_bytes[i];
}
Ok(bytes)
} else {
Err(())
}
}
PixelFormat::Gray(8) => Ok(*b"GREY"),
PixelFormat::Gray(16) => Ok(*b"Y16 "),
PixelFormat::Depth(16) => Ok(*b"Z16 "),
PixelFormat::Bgr(24) => Ok(*b"BGR3"),
PixelFormat::Rgb(24) => Ok(*b"RGB3"),
PixelFormat::Rgb(32) => Ok(*b"AB24"),
PixelFormat::Jpeg => Ok(*b"MJPG"),
_ => Err(()),
}
}
}