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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
use formats::{ImageData, PixFmt, PixelFormat, Stride};
use machine_vision_formats as formats;

use timestamped_frame::{ExtraTimeData, HostTimeData};

use crate::{convert_to_dynamic, match_all_dynamic_fmts, new_basic_frame, BasicExtra, BasicFrame};

macro_rules! new_basic_frame_copy {
    ($x:expr) => {{
        let extra = Box::new(BasicExtra {
            host_timestamp: $x.extra().host_timestamp(),
            host_framenumber: $x.extra().host_framenumber(),
        });
        BasicFrame {
            width: $x.width(),
            height: $x.height(),
            stride: $x.stride() as u32,
            image_data: $x.image_data().to_vec(),
            extra,
            pixel_format: std::marker::PhantomData,
        }
    }};
}

macro_rules! new_basic_frame_move {
    ($x:expr) => {{
        let extra = Box::new(BasicExtra {
            host_timestamp: $x.extra().host_timestamp(),
            host_framenumber: $x.extra().host_framenumber(),
        });
        let width = $x.width();
        let height = $x.height();
        let stride = $x.stride() as u32;
        let image_data: Vec<u8> = $x.into();
        BasicFrame {
            width,
            height,
            stride,
            image_data,
            extra,
            pixel_format: std::marker::PhantomData,
        }
    }};
}

macro_rules! convert_to_dynamic2 {
    ($format_type:ty, $x:expr) => {{
        let pixfmt = formats::pixel_format::pixfmt::<$format_type>().unwrap();
        match pixfmt {
            PixFmt::Mono8 => DynamicFrame::Mono8($x),
            PixFmt::Mono32f => DynamicFrame::Mono32f($x),
            PixFmt::RGB8 => DynamicFrame::RGB8($x),
            PixFmt::BayerRG8 => DynamicFrame::BayerRG8($x),
            PixFmt::BayerRG32f => DynamicFrame::BayerRG32f($x),
            PixFmt::BayerGB8 => DynamicFrame::BayerGB8($x),
            PixFmt::BayerGB32f => DynamicFrame::BayerGB32f($x),
            PixFmt::BayerGR8 => DynamicFrame::BayerGR8($x),
            PixFmt::BayerGR32f => DynamicFrame::BayerGR32f($x),
            PixFmt::BayerBG8 => DynamicFrame::BayerBG8($x),
            PixFmt::BayerBG32f => DynamicFrame::BayerBG32f($x),
            PixFmt::YUV422 => DynamicFrame::YUV422($x),
            _ => {
                panic!("unsupported type {}", pixfmt);
            }
        }
    }};
}

/// An image whose pixel format is known only at runtime.
///
/// When reading an image from disk, for example, its pixel format is not known
/// in advance. This enum represents the different possible formats as different
/// variants.
///
/// Note that we do not implement `ImageData<FMT>` trait because the pixel
/// format (parameterized by FMT) is not known at compile-time for DynamicFrame.
#[allow(non_camel_case_types)]
#[derive(Clone, PartialEq)]
pub enum DynamicFrame {
    Mono8(BasicFrame<formats::pixel_format::Mono8>),
    Mono32f(BasicFrame<formats::pixel_format::Mono32f>),
    RGB8(BasicFrame<formats::pixel_format::RGB8>),
    BayerRG8(BasicFrame<formats::pixel_format::BayerRG8>),
    BayerRG32f(BasicFrame<formats::pixel_format::BayerRG32f>),
    BayerGB8(BasicFrame<formats::pixel_format::BayerGB8>),
    BayerGB32f(BasicFrame<formats::pixel_format::BayerGB32f>),
    BayerGR8(BasicFrame<formats::pixel_format::BayerGR8>),
    BayerGR32f(BasicFrame<formats::pixel_format::BayerGR32f>),
    BayerBG8(BasicFrame<formats::pixel_format::BayerBG8>),
    BayerBG32f(BasicFrame<formats::pixel_format::BayerBG32f>),
    YUV444(BasicFrame<formats::pixel_format::YUV444>),
    YUV422(BasicFrame<formats::pixel_format::YUV422>),
    NV12(BasicFrame<formats::pixel_format::NV12>),
}

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

impl std::fmt::Debug for DynamicFrame {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
        write!(f, "DynamicFrame{{{}, ..}}", self.pixel_format())
    }
}

impl DynamicFrame {
    pub fn new(
        width: u32,
        height: u32,
        stride: u32,
        extra: Box<dyn HostTimeData>,
        image_data: Vec<u8>,
        pixel_format: PixFmt,
    ) -> DynamicFrame {
        // First create a variant with likely the wrong type...
        let wrong_type = DynamicFrame::Mono8(BasicFrame {
            width,
            height,
            stride,
            extra,
            image_data,
            pixel_format: std::marker::PhantomData,
        });
        // ...then convert it to the right type.
        wrong_type.force_pixel_format(pixel_format)
    }
    pub fn copy_from<FMT: PixelFormat>(
        frame: &dyn timestamped_frame::ImageStrideTime<FMT>,
    ) -> Self {
        convert_to_dynamic2!(FMT, new_basic_frame_copy!(frame))
    }

    // TODO: actually implement the From trait
    pub fn from<FRAME, FMT>(frame: FRAME) -> Self
    where
        FRAME: timestamped_frame::ImageStrideTime<FMT> + Into<Vec<u8>>,
        FMT: PixelFormat,
    {
        convert_to_dynamic2!(FMT, new_basic_frame_move!(frame))
    }
}

impl DynamicFrame {
    /// Return the image as a `BasicFrame` of the given pixel format.
    ///
    /// This is done by moving the data. No copy is made.
    ///
    /// If the image is a different pixel format than requested, None will be
    /// returned.
    ///
    /// To convert the image data if necessary, use [Self::into_pixel_format].
    pub fn as_basic<FMT>(self) -> Option<BasicFrame<FMT>>
    where
        FMT: PixelFormat,
    {
        let pixfmt = formats::pixel_format::pixfmt::<FMT>().unwrap();
        if pixfmt == self.pixel_format() {
            let width = self.width();
            let height = self.height();
            let stride = self.stride() as u32;
            let (image_data, extra) = self.into_data_extra();
            Some(BasicFrame {
                width,
                height,
                stride,
                extra,
                image_data,
                pixel_format: std::marker::PhantomData,
            })
        } else {
            None
        }
    }

    #[cfg(feature = "convert-image")]
    /// Return the image as a `BasicFrame` converting the data to the requested
    /// pixel format as necessary.
    ///
    /// To avoid converting the data, use [Self::as_basic].
    pub fn into_pixel_format<FMT>(self) -> Result<BasicFrame<FMT>, convert_image::Error>
    where
        FMT: PixelFormat,
    {
        let pixfmt = formats::pixel_format::pixfmt::<FMT>().unwrap();
        if pixfmt == self.pixel_format() {
            // Fast path. Simply return the data.
            let width = self.width();
            let height = self.height();
            let stride = self.stride() as u32;
            let (image_data, extra) = self.into_data_extra();
            Ok(BasicFrame {
                width,
                height,
                stride,
                extra,
                image_data,
                pixel_format: std::marker::PhantomData,
            })
        } else {
            let width = self.width();
            let height = self.height();
            let extra = match_all_dynamic_fmts!(&self, x, { x.extra.clone() });

            let dest_fmt = machine_vision_formats::pixel_format::pixfmt::<FMT>().unwrap();

            // Allocate buffer for new image.
            let dest_stride = dest_fmt.bits_per_pixel() as usize * width as usize / 8;
            let dest_size = height as usize * dest_stride;
            let mut dest_buf = vec![0u8; dest_size];

            {
                let mut dest = image_iter::ReinterpretedImageMut {
                    orig: &mut dest_buf,
                    width,
                    height,
                    stride: dest_stride,
                    fmt: std::marker::PhantomData::<FMT>,
                };

                match_all_dynamic_fmts!(&self, x, convert_image::convert_into(x, &mut dest)?);
            }

            let image_data = dest_buf;

            Ok(BasicFrame {
                width,
                height,
                stride: dest_stride as u32,
                extra,
                image_data,
                pixel_format: std::marker::PhantomData,
            })
        }
    }

    pub fn pixel_format(&self) -> PixFmt {
        use DynamicFrame::*;
        match self {
            Mono8(_) => PixFmt::Mono8,
            Mono32f(_) => PixFmt::Mono32f,
            RGB8(_) => PixFmt::RGB8,
            BayerRG8(_) => PixFmt::BayerRG8,
            BayerRG32f(_) => PixFmt::BayerRG32f,
            BayerGB8(_) => PixFmt::BayerGB8,
            BayerGB32f(_) => PixFmt::BayerGB32f,
            BayerGR8(_) => PixFmt::BayerGR8,
            BayerGR32f(_) => PixFmt::BayerGR32f,
            BayerBG8(_) => PixFmt::BayerBG8,
            BayerBG32f(_) => PixFmt::BayerBG32f,
            YUV444(_) => PixFmt::YUV444,
            YUV422(_) => PixFmt::YUV422,
            NV12(_) => PixFmt::NV12,
        }
    }
    /// Force the frame into a new pixel format without altering the data.
    pub fn force_pixel_format(self, pixfmt: PixFmt) -> DynamicFrame {
        match_all_dynamic_fmts!(self, x, { convert_to_dynamic!(pixfmt, x) })
    }
    pub fn width(&self) -> u32 {
        match_all_dynamic_fmts!(self, x, { x.width() })
    }
    pub fn height(&self) -> u32 {
        match_all_dynamic_fmts!(self, x, { x.height() })
    }
    /// Get a view of the image data.
    ///
    /// Note that this discards any type information about the pixel format.
    pub fn image_data_without_format(&self) -> &[u8] {
        match_all_dynamic_fmts!(self, x, &x.image_data)
    }
    pub fn into_data_extra(self) -> (Vec<u8>, Box<dyn HostTimeData>) {
        match_all_dynamic_fmts!(self, x, { (x.image_data, x.extra) })
    }
}

impl ExtraTimeData for DynamicFrame {
    fn extra(&self) -> &dyn HostTimeData {
        match_all_dynamic_fmts!(self, x, { x.extra.as_ref() })
    }
}

impl From<DynamicFrame> for Vec<u8> {
    fn from(orig: DynamicFrame) -> Self {
        match_all_dynamic_fmts!(orig, x, { x.image_data })
    }
}

impl Stride for DynamicFrame {
    fn stride(&self) -> usize {
        match_all_dynamic_fmts!(self, x, { x.stride() })
    }
}