re_view_spatial/
max_image_dimension_subscriber.rs

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
use nohash_hasher::IntMap;
use once_cell::sync::OnceCell;

use re_chunk_store::{ChunkStore, ChunkStoreSubscriberHandle, PerStoreChunkSubscriber};
use re_log_types::{EntityPath, StoreId};
use re_types::{
    archetypes,
    components::{Blob, ImageFormat, MediaType},
    external::image,
    Component as _, Loggable as _,
};

bitflags::bitflags! {
    #[derive(Debug, Clone, Copy, Default)]
    pub struct ImageTypes: u8 {
        const IMAGE = 0b0001;
        const ENCODED_IMAGE = 0b0010;
        const SEGMENTATION_IMAGE = 0b0100;
        const DEPTH_IMAGE = 0b1000;
        const VIDEO = 0b10000;
    }
}

#[derive(Debug, Clone, Default)]
pub struct MaxDimensions {
    pub width: u32,
    pub height: u32,
    pub image_types: ImageTypes,
}

/// The size of the largest image and/or video at a given entity path.
#[derive(Default, Clone)]
pub struct MaxImageDimensionsStoreSubscriber {
    max_dimensions: IntMap<EntityPath, MaxDimensions>,
}

impl MaxImageDimensionsStoreSubscriber {
    /// Accesses the image/video dimension information for a given store
    pub fn access<T>(
        store_id: &StoreId,
        f: impl FnOnce(&IntMap<EntityPath, MaxDimensions>) -> T,
    ) -> Option<T> {
        ChunkStore::with_per_store_subscriber_once(
            Self::subscription_handle(),
            store_id,
            move |subscriber: &Self| f(&subscriber.max_dimensions),
        )
    }
}

impl MaxImageDimensionsStoreSubscriber {
    /// Accesses the global store subscriber.
    ///
    /// Lazily registers the subscriber if it hasn't been registered yet.
    pub fn subscription_handle() -> ChunkStoreSubscriberHandle {
        static SUBSCRIPTION: OnceCell<ChunkStoreSubscriberHandle> = OnceCell::new();
        *SUBSCRIPTION.get_or_init(ChunkStore::register_per_store_subscriber::<Self>)
    }
}

impl PerStoreChunkSubscriber for MaxImageDimensionsStoreSubscriber {
    #[inline]
    fn name() -> String {
        "MaxImageDimensionStoreSubscriber".to_owned()
    }

    fn on_events<'a>(&mut self, events: impl Iterator<Item = &'a re_chunk_store::ChunkStoreEvent>) {
        re_tracing::profile_function!();

        for event in events {
            if event.diff.kind != re_chunk_store::ChunkStoreDiffKind::Addition {
                // Max image dimensions are strictly additive
                continue;
            }

            // Handle `Image`, `DepthImage`, `SegmentationImage`…
            let components = event.diff.chunk.components();
            if let Some(all_dimensions) = components
                .get(&ImageFormat::name())
                .and_then(|per_desc| per_desc.values().next())
            {
                for new_dim in all_dimensions.iter().filter_map(|array| {
                    array.and_then(|array| {
                        let array = arrow::array::ArrayRef::from(array);
                        ImageFormat::from_arrow(&array).ok()?.into_iter().next()
                    })
                }) {
                    let max_dim = self
                        .max_dimensions
                        .entry(event.diff.chunk.entity_path().clone())
                        .or_default();

                    max_dim.width = max_dim.width.max(new_dim.width);
                    max_dim.height = max_dim.height.max(new_dim.height);
                }
            }
            // TODO(andreas): this should be part of the ImageFormat component's tag instead.
            if components.contains_key(&archetypes::Image::descriptor_indicator().component_name) {
                self.max_dimensions
                    .entry(event.diff.chunk.entity_path().clone())
                    .or_default()
                    .image_types
                    .insert(ImageTypes::IMAGE);
            }
            if components
                .contains_key(&archetypes::SegmentationImage::descriptor_indicator().component_name)
            {
                self.max_dimensions
                    .entry(event.diff.chunk.entity_path().clone())
                    .or_default()
                    .image_types
                    .insert(ImageTypes::SEGMENTATION_IMAGE);
            }
            if components
                .contains_key(&archetypes::DepthImage::descriptor_indicator().component_name)
            {
                self.max_dimensions
                    .entry(event.diff.chunk.entity_path().clone())
                    .or_default()
                    .image_types
                    .insert(ImageTypes::DEPTH_IMAGE);
            }

            // Handle `ImageEncoded`, `AssetVideo`…
            let blobs = event.diff.chunk.iter_slices::<&[u8]>(Blob::name());
            let media_types = event.diff.chunk.iter_slices::<String>(MediaType::name());
            for (blob, media_type) in
                itertools::izip!(blobs, media_types.map(Some).chain(std::iter::repeat(None)))
            {
                if let Some(blob) = blob.first() {
                    let media_type =
                        media_type.and_then(|v| v.first().map(|v| MediaType(v.clone().into())));
                    let Some(media_type) = MediaType::or_guess_from_data(media_type, blob) else {
                        continue;
                    };

                    if let Some([width, height]) = size_from_blob(blob, &media_type) {
                        let max_dim = self
                            .max_dimensions
                            .entry(event.diff.chunk.entity_path().clone())
                            .or_default();
                        max_dim.width = max_dim.width.max(width);
                        max_dim.height = max_dim.height.max(height);

                        // TODO(andreas): this should be part of the Blob component's tag instead.
                        if media_type.is_image() {
                            max_dim.image_types.insert(ImageTypes::ENCODED_IMAGE);
                        } else if media_type.is_video() {
                            max_dim.image_types.insert(ImageTypes::VIDEO);
                        }
                    }
                }
            }
        }
    }
}

fn size_from_blob(blob: &[u8], media_type: &MediaType) -> Option<[u32; 2]> {
    re_tracing::profile_function!();

    if media_type.is_image() {
        re_tracing::profile_scope!("image");

        let image_bytes = blob;

        let mut reader = image::ImageReader::new(std::io::Cursor::new(image_bytes));

        if let Some(format) = image::ImageFormat::from_mime_type(&media_type.0) {
            reader.set_format(format);
        }

        if reader.format().is_none() {
            if let Ok(format) = image::guess_format(image_bytes) {
                // Weirdly enough, `reader.decode` doesn't do this for us.
                reader.set_format(format);
            }
        }

        reader.into_dimensions().ok().map(|size| size.into())
    } else if media_type.is_video() {
        re_tracing::profile_scope!("video");
        re_video::VideoData::load_from_bytes(blob, media_type)
            .ok()
            .map(|video| video.dimensions())
    } else {
        None
    }
}