lighthouse_protocol/
frame.rs1use serde::{Serialize, Deserialize, de, Serializer, Deserializer};
2use std::{array, fmt, ops::{Index, IndexMut}};
3
4use crate::{Color, Pos, LIGHTHOUSE_BYTES, LIGHTHOUSE_COLS, LIGHTHOUSE_RECT, LIGHTHOUSE_ROWS, LIGHTHOUSE_SIZE};
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
9pub struct Frame {
10 pixels: [Color; LIGHTHOUSE_SIZE],
11}
12
13impl Frame {
14 pub fn empty() -> Self {
16 Self::fill(Color::BLACK)
17 }
18
19 pub fn new(pixels: [Color; LIGHTHOUSE_SIZE]) -> Self {
22 Self { pixels }
23 }
24
25 pub fn fill(color: Color) -> Self {
27 Self { pixels: [color; LIGHTHOUSE_SIZE] }
28 }
29
30 pub fn generate(f: impl Fn(usize, usize) -> Color) -> Self {
34 let mut frame = Self::empty();
35 for y in 0..LIGHTHOUSE_ROWS {
36 for x in 0..LIGHTHOUSE_COLS {
37 frame.set(x, y, f(x, y));
38 }
39 }
40 frame
41 }
42
43 pub fn get(&self, x: usize, y: usize) -> Color {
45 self[Pos::new(x as i32, y as i32)]
46 }
47
48 pub fn set(&mut self, x: usize, y: usize, color: Color) {
50 self[Pos::new(x as i32, y as i32)] = color;
51 }
52}
53
54impl Index<Pos<i32>> for Frame {
55 type Output = Color;
56
57 fn index(&self, pos: Pos<i32>) -> &Color {
58 &self.pixels[LIGHTHOUSE_RECT.index_of(pos)]
59 }
60}
61
62impl IndexMut<Pos<i32>> for Frame {
63 fn index_mut(&mut self, pos: Pos<i32>) -> &mut Color {
64 &mut self.pixels[LIGHTHOUSE_RECT.index_of(pos)]
65 }
66}
67
68impl From<[Color; LIGHTHOUSE_SIZE]> for Frame {
69 fn from(pixels: [Color; LIGHTHOUSE_SIZE]) -> Self {
70 Self::new(pixels)
71 }
72}
73
74impl From<[u8; LIGHTHOUSE_BYTES]> for Frame {
75 fn from(bytes: [u8; LIGHTHOUSE_BYTES]) -> Self {
76 Self::new(array::from_fn(|pixel_index| {
77 let offset = pixel_index * 3;
78 Color::from([offset, offset + 1, offset + 2].map(|i| bytes[i]))
79 }))
80 }
81}
82
83impl From<Frame> for [Color; LIGHTHOUSE_SIZE] {
84 fn from(frame: Frame) -> Self {
85 frame.pixels
86 }
87}
88
89impl From<Frame> for Vec<u8> {
90 fn from(frame: Frame) -> Self {
91 frame.pixels.iter()
92 .flat_map(|c| [c.red, c.green, c.blue].into_iter())
93 .collect()
94 }
95}
96
97impl From<Frame> for [u8; LIGHTHOUSE_BYTES] {
98 fn from(frame: Frame) -> Self {
99 Vec::from(frame).try_into().unwrap()
104 }
105}
106
107impl Serialize for Frame {
108 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
109 where S: Serializer {
110 let bytes: Vec<u8> = self.pixels.iter()
111 .flat_map(|c| [c.red, c.green, c.blue].into_iter())
112 .collect();
113 serializer.serialize_bytes(&bytes)
114 }
115}
116
117impl<'de> Deserialize<'de> for Frame {
118 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
119 where D: Deserializer<'de> {
120 deserializer.deserialize_bytes(FrameBytesVisitor)
121 }
122}
123
124struct FrameBytesVisitor;
125
126impl<'de> de::Visitor<'de> for FrameBytesVisitor {
127 type Value = Frame;
128
129 fn expecting(&self, f: &mut fmt::Formatter) -> fmt::Result {
130 write!(f, "a byte array whose length is a multiple of 3")
131 }
132
133 fn visit_bytes<E>(self, v: &[u8]) -> Result<Self::Value, E>
134 where E: de::Error {
135 if let Ok(bytes) = <[u8; LIGHTHOUSE_BYTES]>::try_from(v) {
136 Ok(Frame::from(bytes))
137 } else {
138 Err(E::custom(format!("{} (length of byte array) is not {}", v.len(), LIGHTHOUSE_BYTES)))
139 }
140 }
141}
142
143#[cfg(test)]
144mod tests {
145 use crate::{Color, Frame, LIGHTHOUSE_BYTES};
146
147 #[test]
148 fn from_array() {
149 assert_eq!(Frame::from([0u8; LIGHTHOUSE_BYTES]), Frame::empty());
150 assert_eq!(Frame::from([255u8; LIGHTHOUSE_BYTES]), Frame::fill(Color::WHITE));
151 }
152
153 #[test]
154 fn to_array() {
155 assert_eq!(<[u8; LIGHTHOUSE_BYTES]>::from(Frame::empty()), [0u8; LIGHTHOUSE_BYTES]);
156 assert_eq!(<[u8; LIGHTHOUSE_BYTES]>::from(Frame::fill(Color::WHITE)), [255u8; LIGHTHOUSE_BYTES]);
157 }
158}