re_types/datatypes/
image_format.rs

1// DO NOT EDIT! This file was auto-generated by crates/build/re_types_builder/src/codegen/rust/api.rs
2// Based on "crates/store/re_types/definitions/rerun/datatypes/image_format.fbs".
3
4#![allow(unused_imports)]
5#![allow(unused_parens)]
6#![allow(clippy::clone_on_copy)]
7#![allow(clippy::cloned_instead_of_copied)]
8#![allow(clippy::map_flatten)]
9#![allow(clippy::needless_question_mark)]
10#![allow(clippy::new_without_default)]
11#![allow(clippy::redundant_closure)]
12#![allow(clippy::too_many_arguments)]
13#![allow(clippy::too_many_lines)]
14
15use ::re_types_core::try_serialize_field;
16use ::re_types_core::SerializationResult;
17use ::re_types_core::{ComponentBatch as _, SerializedComponentBatch};
18use ::re_types_core::{ComponentDescriptor, ComponentName};
19use ::re_types_core::{DeserializationError, DeserializationResult};
20
21/// **Datatype**: The metadata describing the contents of a [`components::ImageBuffer`][crate::components::ImageBuffer].
22#[derive(Clone, Debug, Default, Copy, PartialEq, Eq, Hash)]
23pub struct ImageFormat {
24    /// The width of the image in pixels.
25    pub width: u32,
26
27    /// The height of the image in pixels.
28    pub height: u32,
29
30    /// Used mainly for chroma downsampled formats and differing number of bits per channel.
31    ///
32    /// If specified, this takes precedence over both [`datatypes::ColorModel`][crate::datatypes::ColorModel] and [`datatypes::ChannelDatatype`][crate::datatypes::ChannelDatatype] (which are ignored).
33    pub pixel_format: Option<crate::datatypes::PixelFormat>,
34
35    /// L, RGB, RGBA, …
36    ///
37    /// Also requires a [`datatypes::ChannelDatatype`][crate::datatypes::ChannelDatatype] to fully specify the pixel format.
38    pub color_model: Option<crate::datatypes::ColorModel>,
39
40    /// The data type of each channel (e.g. the red channel) of the image data (U8, F16, …).
41    ///
42    /// Also requires a [`datatypes::ColorModel`][crate::datatypes::ColorModel] to fully specify the pixel format.
43    pub channel_datatype: Option<crate::datatypes::ChannelDatatype>,
44}
45
46::re_types_core::macros::impl_into_cow!(ImageFormat);
47
48impl ::re_types_core::Loggable for ImageFormat {
49    #[inline]
50    fn arrow_datatype() -> arrow::datatypes::DataType {
51        #![allow(clippy::wildcard_imports)]
52        use arrow::datatypes::*;
53        DataType::Struct(Fields::from(vec![
54            Field::new("width", DataType::UInt32, false),
55            Field::new("height", DataType::UInt32, false),
56            Field::new(
57                "pixel_format",
58                <crate::datatypes::PixelFormat>::arrow_datatype(),
59                true,
60            ),
61            Field::new(
62                "color_model",
63                <crate::datatypes::ColorModel>::arrow_datatype(),
64                true,
65            ),
66            Field::new(
67                "channel_datatype",
68                <crate::datatypes::ChannelDatatype>::arrow_datatype(),
69                true,
70            ),
71        ]))
72    }
73
74    fn to_arrow_opt<'a>(
75        data: impl IntoIterator<Item = Option<impl Into<::std::borrow::Cow<'a, Self>>>>,
76    ) -> SerializationResult<arrow::array::ArrayRef>
77    where
78        Self: Clone + 'a,
79    {
80        #![allow(clippy::wildcard_imports)]
81        #![allow(clippy::manual_is_variant_and)]
82        use ::re_types_core::{arrow_helpers::as_array_ref, Loggable as _, ResultExt as _};
83        use arrow::{array::*, buffer::*, datatypes::*};
84        Ok({
85            let fields = Fields::from(vec![
86                Field::new("width", DataType::UInt32, false),
87                Field::new("height", DataType::UInt32, false),
88                Field::new(
89                    "pixel_format",
90                    <crate::datatypes::PixelFormat>::arrow_datatype(),
91                    true,
92                ),
93                Field::new(
94                    "color_model",
95                    <crate::datatypes::ColorModel>::arrow_datatype(),
96                    true,
97                ),
98                Field::new(
99                    "channel_datatype",
100                    <crate::datatypes::ChannelDatatype>::arrow_datatype(),
101                    true,
102                ),
103            ]);
104            let (somes, data): (Vec<_>, Vec<_>) = data
105                .into_iter()
106                .map(|datum| {
107                    let datum: Option<::std::borrow::Cow<'a, Self>> = datum.map(Into::into);
108                    (datum.is_some(), datum)
109                })
110                .unzip();
111            let validity: Option<arrow::buffer::NullBuffer> = {
112                let any_nones = somes.iter().any(|some| !*some);
113                any_nones.then(|| somes.into())
114            };
115            as_array_ref(StructArray::new(
116                fields,
117                vec![
118                    {
119                        let (somes, width): (Vec<_>, Vec<_>) = data
120                            .iter()
121                            .map(|datum| {
122                                let datum = datum.as_ref().map(|datum| datum.width.clone());
123                                (datum.is_some(), datum)
124                            })
125                            .unzip();
126                        let width_validity: Option<arrow::buffer::NullBuffer> = {
127                            let any_nones = somes.iter().any(|some| !*some);
128                            any_nones.then(|| somes.into())
129                        };
130                        as_array_ref(PrimitiveArray::<UInt32Type>::new(
131                            ScalarBuffer::from(
132                                width
133                                    .into_iter()
134                                    .map(|v| v.unwrap_or_default())
135                                    .collect::<Vec<_>>(),
136                            ),
137                            width_validity,
138                        ))
139                    },
140                    {
141                        let (somes, height): (Vec<_>, Vec<_>) = data
142                            .iter()
143                            .map(|datum| {
144                                let datum = datum.as_ref().map(|datum| datum.height.clone());
145                                (datum.is_some(), datum)
146                            })
147                            .unzip();
148                        let height_validity: Option<arrow::buffer::NullBuffer> = {
149                            let any_nones = somes.iter().any(|some| !*some);
150                            any_nones.then(|| somes.into())
151                        };
152                        as_array_ref(PrimitiveArray::<UInt32Type>::new(
153                            ScalarBuffer::from(
154                                height
155                                    .into_iter()
156                                    .map(|v| v.unwrap_or_default())
157                                    .collect::<Vec<_>>(),
158                            ),
159                            height_validity,
160                        ))
161                    },
162                    {
163                        let (somes, pixel_format): (Vec<_>, Vec<_>) = data
164                            .iter()
165                            .map(|datum| {
166                                let datum = datum
167                                    .as_ref()
168                                    .map(|datum| datum.pixel_format.clone())
169                                    .flatten();
170                                (datum.is_some(), datum)
171                            })
172                            .unzip();
173                        let pixel_format_validity: Option<arrow::buffer::NullBuffer> = {
174                            let any_nones = somes.iter().any(|some| !*some);
175                            any_nones.then(|| somes.into())
176                        };
177                        {
178                            _ = pixel_format_validity;
179                            crate::datatypes::PixelFormat::to_arrow_opt(pixel_format)?
180                        }
181                    },
182                    {
183                        let (somes, color_model): (Vec<_>, Vec<_>) = data
184                            .iter()
185                            .map(|datum| {
186                                let datum = datum
187                                    .as_ref()
188                                    .map(|datum| datum.color_model.clone())
189                                    .flatten();
190                                (datum.is_some(), datum)
191                            })
192                            .unzip();
193                        let color_model_validity: Option<arrow::buffer::NullBuffer> = {
194                            let any_nones = somes.iter().any(|some| !*some);
195                            any_nones.then(|| somes.into())
196                        };
197                        {
198                            _ = color_model_validity;
199                            crate::datatypes::ColorModel::to_arrow_opt(color_model)?
200                        }
201                    },
202                    {
203                        let (somes, channel_datatype): (Vec<_>, Vec<_>) = data
204                            .iter()
205                            .map(|datum| {
206                                let datum = datum
207                                    .as_ref()
208                                    .map(|datum| datum.channel_datatype.clone())
209                                    .flatten();
210                                (datum.is_some(), datum)
211                            })
212                            .unzip();
213                        let channel_datatype_validity: Option<arrow::buffer::NullBuffer> = {
214                            let any_nones = somes.iter().any(|some| !*some);
215                            any_nones.then(|| somes.into())
216                        };
217                        {
218                            _ = channel_datatype_validity;
219                            crate::datatypes::ChannelDatatype::to_arrow_opt(channel_datatype)?
220                        }
221                    },
222                ],
223                validity,
224            ))
225        })
226    }
227
228    fn from_arrow_opt(
229        arrow_data: &dyn arrow::array::Array,
230    ) -> DeserializationResult<Vec<Option<Self>>>
231    where
232        Self: Sized,
233    {
234        #![allow(clippy::wildcard_imports)]
235        use ::re_types_core::{arrow_zip_validity::ZipValidity, Loggable as _, ResultExt as _};
236        use arrow::{array::*, buffer::*, datatypes::*};
237        Ok({
238            let arrow_data = arrow_data
239                .as_any()
240                .downcast_ref::<arrow::array::StructArray>()
241                .ok_or_else(|| {
242                    let expected = Self::arrow_datatype();
243                    let actual = arrow_data.data_type().clone();
244                    DeserializationError::datatype_mismatch(expected, actual)
245                })
246                .with_context("rerun.datatypes.ImageFormat")?;
247            if arrow_data.is_empty() {
248                Vec::new()
249            } else {
250                let (arrow_data_fields, arrow_data_arrays) =
251                    (arrow_data.fields(), arrow_data.columns());
252                let arrays_by_name: ::std::collections::HashMap<_, _> = arrow_data_fields
253                    .iter()
254                    .map(|field| field.name().as_str())
255                    .zip(arrow_data_arrays)
256                    .collect();
257                let width = {
258                    if !arrays_by_name.contains_key("width") {
259                        return Err(DeserializationError::missing_struct_field(
260                            Self::arrow_datatype(),
261                            "width",
262                        ))
263                        .with_context("rerun.datatypes.ImageFormat");
264                    }
265                    let arrow_data = &**arrays_by_name["width"];
266                    arrow_data
267                        .as_any()
268                        .downcast_ref::<UInt32Array>()
269                        .ok_or_else(|| {
270                            let expected = DataType::UInt32;
271                            let actual = arrow_data.data_type().clone();
272                            DeserializationError::datatype_mismatch(expected, actual)
273                        })
274                        .with_context("rerun.datatypes.ImageFormat#width")?
275                        .into_iter()
276                };
277                let height = {
278                    if !arrays_by_name.contains_key("height") {
279                        return Err(DeserializationError::missing_struct_field(
280                            Self::arrow_datatype(),
281                            "height",
282                        ))
283                        .with_context("rerun.datatypes.ImageFormat");
284                    }
285                    let arrow_data = &**arrays_by_name["height"];
286                    arrow_data
287                        .as_any()
288                        .downcast_ref::<UInt32Array>()
289                        .ok_or_else(|| {
290                            let expected = DataType::UInt32;
291                            let actual = arrow_data.data_type().clone();
292                            DeserializationError::datatype_mismatch(expected, actual)
293                        })
294                        .with_context("rerun.datatypes.ImageFormat#height")?
295                        .into_iter()
296                };
297                let pixel_format = {
298                    if !arrays_by_name.contains_key("pixel_format") {
299                        return Err(DeserializationError::missing_struct_field(
300                            Self::arrow_datatype(),
301                            "pixel_format",
302                        ))
303                        .with_context("rerun.datatypes.ImageFormat");
304                    }
305                    let arrow_data = &**arrays_by_name["pixel_format"];
306                    crate::datatypes::PixelFormat::from_arrow_opt(arrow_data)
307                        .with_context("rerun.datatypes.ImageFormat#pixel_format")?
308                        .into_iter()
309                };
310                let color_model = {
311                    if !arrays_by_name.contains_key("color_model") {
312                        return Err(DeserializationError::missing_struct_field(
313                            Self::arrow_datatype(),
314                            "color_model",
315                        ))
316                        .with_context("rerun.datatypes.ImageFormat");
317                    }
318                    let arrow_data = &**arrays_by_name["color_model"];
319                    crate::datatypes::ColorModel::from_arrow_opt(arrow_data)
320                        .with_context("rerun.datatypes.ImageFormat#color_model")?
321                        .into_iter()
322                };
323                let channel_datatype = {
324                    if !arrays_by_name.contains_key("channel_datatype") {
325                        return Err(DeserializationError::missing_struct_field(
326                            Self::arrow_datatype(),
327                            "channel_datatype",
328                        ))
329                        .with_context("rerun.datatypes.ImageFormat");
330                    }
331                    let arrow_data = &**arrays_by_name["channel_datatype"];
332                    crate::datatypes::ChannelDatatype::from_arrow_opt(arrow_data)
333                        .with_context("rerun.datatypes.ImageFormat#channel_datatype")?
334                        .into_iter()
335                };
336                ZipValidity::new_with_validity(
337                    ::itertools::izip!(width, height, pixel_format, color_model, channel_datatype),
338                    arrow_data.nulls(),
339                )
340                .map(|opt| {
341                    opt.map(
342                        |(width, height, pixel_format, color_model, channel_datatype)| {
343                            Ok(Self {
344                                width: width
345                                    .ok_or_else(DeserializationError::missing_data)
346                                    .with_context("rerun.datatypes.ImageFormat#width")?,
347                                height: height
348                                    .ok_or_else(DeserializationError::missing_data)
349                                    .with_context("rerun.datatypes.ImageFormat#height")?,
350                                pixel_format,
351                                color_model,
352                                channel_datatype,
353                            })
354                        },
355                    )
356                    .transpose()
357                })
358                .collect::<DeserializationResult<Vec<_>>>()
359                .with_context("rerun.datatypes.ImageFormat")?
360            }
361        })
362    }
363}
364
365impl ::re_byte_size::SizeBytes for ImageFormat {
366    #[inline]
367    fn heap_size_bytes(&self) -> u64 {
368        self.width.heap_size_bytes()
369            + self.height.heap_size_bytes()
370            + self.pixel_format.heap_size_bytes()
371            + self.color_model.heap_size_bytes()
372            + self.channel_datatype.heap_size_bytes()
373    }
374
375    #[inline]
376    fn is_pod() -> bool {
377        <u32>::is_pod()
378            && <u32>::is_pod()
379            && <Option<crate::datatypes::PixelFormat>>::is_pod()
380            && <Option<crate::datatypes::ColorModel>>::is_pod()
381            && <Option<crate::datatypes::ChannelDatatype>>::is_pod()
382    }
383}