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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
use machine_vision_formats::{
    ImageBuffer, ImageBufferMutRef, ImageBufferRef, ImageData, ImageMutData, Stride,
};
use timestamped_frame::{ExtraTimeData, FrameTrait, HostTimeData, ImageStrideTime};

mod dynamic_frame;
pub use dynamic_frame::DynamicFrame;

/// Convert a BasicFrame into another BasicFrame with a new pixel_format.
#[macro_export]
macro_rules! new_basic_frame {
    ($x:expr) => {{
        BasicFrame {
            width: $x.width,
            height: $x.height,
            stride: $x.stride,
            image_data: $x.image_data,
            extra: $x.extra,
            pixel_format: std::marker::PhantomData,
        }
    }};
}

/// Return an DynamicFrame variant according to $pixfmt.
#[macro_export]
macro_rules! convert_to_dynamic {
    ($pixfmt:expr, $x:expr) => {{
        match $pixfmt {
            PixFmt::Mono8 => DynamicFrame::Mono8(new_basic_frame!($x)),
            PixFmt::Mono32f => DynamicFrame::Mono32f(new_basic_frame!($x)),
            PixFmt::RGB8 => DynamicFrame::RGB8(new_basic_frame!($x)),
            PixFmt::BayerRG8 => DynamicFrame::BayerRG8(new_basic_frame!($x)),
            PixFmt::BayerRG32f => DynamicFrame::BayerRG32f(new_basic_frame!($x)),
            PixFmt::BayerGB8 => DynamicFrame::BayerGB8(new_basic_frame!($x)),
            PixFmt::BayerGB32f => DynamicFrame::BayerGB32f(new_basic_frame!($x)),
            PixFmt::BayerGR8 => DynamicFrame::BayerGR8(new_basic_frame!($x)),
            PixFmt::BayerGR32f => DynamicFrame::BayerGR32f(new_basic_frame!($x)),
            PixFmt::BayerBG8 => DynamicFrame::BayerBG8(new_basic_frame!($x)),
            PixFmt::BayerBG32f => DynamicFrame::BayerBG32f(new_basic_frame!($x)),
            PixFmt::YUV422 => DynamicFrame::YUV422(new_basic_frame!($x)),

            PixFmt::NV12 => DynamicFrame::NV12(new_basic_frame!($x)),
            PixFmt::YUV444 => DynamicFrame::YUV444(new_basic_frame!($x)),
            _ => {
                panic!("unsupported pixel format {}", $pixfmt);
            }
        }
    }};
}

#[macro_export]
macro_rules! match_all_dynamic_fmts {
    ($self:expr, $x:ident, $block:expr) => {
        match $self {
            DynamicFrame::Mono8($x) => $block,
            DynamicFrame::Mono32f($x) => $block,
            DynamicFrame::RGB8($x) => $block,
            DynamicFrame::BayerRG8($x) => $block,
            DynamicFrame::BayerRG32f($x) => $block,
            DynamicFrame::BayerGB8($x) => $block,
            DynamicFrame::BayerGB32f($x) => $block,
            DynamicFrame::BayerGR8($x) => $block,
            DynamicFrame::BayerGR32f($x) => $block,
            DynamicFrame::BayerBG8($x) => $block,
            DynamicFrame::BayerBG32f($x) => $block,
            DynamicFrame::YUV444($x) => $block,
            DynamicFrame::YUV422($x) => $block,
            DynamicFrame::NV12($x) => $block,
        }
    };
}

/// Image data with a statically typed, strided pixel format.
#[derive(Clone)]
pub struct BasicFrame<F> {
    /// width in pixels
    pub width: u32,
    /// height in pixels
    pub height: u32,
    /// number of bytes in an image row
    pub stride: u32,
    /// raw image data
    pub image_data: Vec<u8>,
    /// pixel format
    pub pixel_format: std::marker::PhantomData<F>,
    /// Additional data, including timestamp information.
    pub extra: Box<dyn HostTimeData>,
}

impl<F> PartialEq for BasicFrame<F>
where
    F: machine_vision_formats::PixelFormat,
{
    fn eq(&self, other: &BasicFrame<F>) -> bool {
        if self.width != other.width {
            return false;
        }
        if self.height != other.height {
            return false;
        }

        // We do enforce that `stride` is equal

        // We know `pixel_format` is the same due to type F.

        // Hmm, `extra` does not implement PartialEq.
        if self.extra.host_framenumber() != other.extra.host_framenumber() {
            return false;
        }
        if self.extra.host_timestamp() != other.extra.host_timestamp() {
            return false;
        }

        // Finally, check the buffers for equality in all regions where the pixels should be equal.
        let valid_size =
            usize::try_from(self.height).unwrap() * usize::try_from(self.stride).unwrap();
        let a_row_iter =
            self.image_data[..valid_size].chunks_exact(self.stride.try_into().unwrap());
        let b_row_iter =
            other.image_data[..valid_size].chunks_exact(other.stride.try_into().unwrap());

        let fmt = machine_vision_formats::pixel_format::pixfmt::<F>().unwrap();
        let valid_stride = fmt.bits_per_pixel() as usize * self.width as usize / 8;

        for (a_row, b_row) in a_row_iter.zip(b_row_iter) {
            if a_row[..valid_stride] != b_row[..valid_stride] {
                return false;
            }
        }
        true
    }
}

fn _test_basic_frame_is_send<F: Send>() {
    // Compile-time test to ensure BasicFrame implements Send trait.
    fn implements<T: Send>() {}
    implements::<BasicFrame<F>>();
}

fn _test_basic_frame_is_frame_trait<F>() {
    // Compile-time test to ensure BasicFrame implements FrameTrait trait.
    fn implements<T: FrameTrait<F>, F>() {}
    implements::<BasicFrame<F>, F>();
}

impl<F> BasicFrame<F> {
    pub fn copy_from(frame: &dyn ImageStrideTime<F>) -> BasicFrame<F> {
        let width = frame.width();
        let height = frame.height();
        let stride = frame.stride() as u32;
        let host_timestamp = frame.extra().host_timestamp();
        let host_framenumber = frame.extra().host_framenumber();
        let extra = Box::new(BasicExtra {
            host_timestamp,
            host_framenumber,
        });

        let image_data = frame.image_data().to_vec(); // copy data

        Self {
            width,
            height,
            stride,
            image_data,
            extra,
            pixel_format: std::marker::PhantomData,
        }
    }
}

impl<F> ExtraTimeData for BasicFrame<F> {
    fn extra(&self) -> &dyn HostTimeData {
        self.extra.as_ref()
    }
}

#[derive(Clone, Debug)]
pub struct BasicExtra {
    /// timestamp from host computer
    pub host_timestamp: chrono::DateTime<chrono::Utc>,
    /// framenumber from host computer
    pub host_framenumber: usize,
}

impl HostTimeData for BasicExtra {
    fn host_timestamp(&self) -> chrono::DateTime<chrono::Utc> {
        self.host_timestamp
    }
    fn host_framenumber(&self) -> usize {
        self.host_framenumber
    }
}

impl<F> ImageData<F> for BasicFrame<F> {
    fn width(&self) -> u32 {
        self.width
    }
    fn height(&self) -> u32 {
        self.height
    }
    fn buffer_ref(&self) -> ImageBufferRef<'_, F> {
        ImageBufferRef::new(&self.image_data)
    }
    fn buffer(self) -> ImageBuffer<F> {
        ImageBuffer::new(self.image_data)
    }
}

impl<F> ImageMutData<F> for BasicFrame<F> {
    fn buffer_mut_ref(&mut self) -> ImageBufferMutRef<'_, F> {
        ImageBufferMutRef::new(&mut self.image_data)
    }
}

impl<F> Stride for BasicFrame<F> {
    fn stride(&self) -> usize {
        self.stride as usize
    }
}

impl<F> From<BasicFrame<F>> for Vec<u8> {
    fn from(orig: BasicFrame<F>) -> Vec<u8> {
        orig.image_data
    }
}

impl<F> From<Box<BasicFrame<F>>> for Vec<u8> {
    fn from(orig: Box<BasicFrame<F>>) -> Vec<u8> {
        orig.image_data
    }
}

// impl<FRAME, FMT, EXTRA> From<Box<FRAME>> for BasicFrame<FMT, EXTRA>
// where
//     FRAME: FrameTrait<FMT, EXTRA>,
//     EXTRA: HostTimeData,
//     Vec<u8>: From<Box<FRAME>>,
// {
//     fn from(frame: Box<FRAME>) -> BasicFrame<FMT, EXTRA> {
//         assert_eq!(machine_vision_formats::pixel_format::pixfmt::<FMT>().unwrap(), frame.pix_fmt());
//         let width = frame.width();
//         let height = frame.height();
//         let stride = frame.stride() as u32;
//         let (image_data, extra) = frame.into_data_extra();

//         BasicFrame {
//             width,
//             height,
//             stride,
//             image_data,
//             pixel_format: std::marker::PhantomData,
//             extra,
//         }
//     }
// }