re_data_ui/
blob.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
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
use std::sync::Arc;

use re_log_types::hash::Hash64;
use re_log_types::EntityPath;
use re_types::components::{Blob, MediaType, VideoTimestamp};
use re_ui::{
    list_item::{self, PropertyContent},
    UiExt as _,
};
use re_viewer_context::{UiLayout, ViewerContext};

use crate::{
    image::image_preview_ui,
    video::{show_decoded_frame_info, video_result_ui},
    EntityDataUi,
};

impl EntityDataUi for Blob {
    fn entity_data_ui(
        &self,
        ctx: &ViewerContext<'_>,
        ui: &mut egui::Ui,
        ui_layout: UiLayout,
        entity_path: &EntityPath,
        cache_key: Option<Hash64>,
        query: &re_chunk_store::LatestAtQuery,
        _db: &re_entity_db::EntityDb,
    ) {
        let compact_size_string = re_format::format_bytes(self.len() as _);

        // We show the actual mime of the blob here instead of doing
        // a side-lookup of the sibling `MediaType` component.
        // This is part of "showing the data as it is".
        // If the user clicked on the blob, is because they want to see info about the blob,
        // not about a sibling component.
        // This can also help a user debug if they log the contents of `.png` file with a `image/jpeg` `MediaType`.
        let media_type = MediaType::guess_from_data(self);

        if ui_layout.is_single_line() {
            ui.horizontal(|ui| {
                blob_preview_and_save_ui(
                    ctx,
                    ui,
                    ui_layout,
                    query,
                    entity_path,
                    cache_key,
                    self,
                    media_type.as_ref(),
                    None,
                );

                ui.label(compact_size_string);

                if let Some(media_type) = &media_type {
                    ui.label(media_type.to_string())
                        .on_hover_text("Media type (MIME) based on magic header bytes");
                }
            });
        } else {
            let all_digits_size_string = format!("{} B", re_format::format_uint(self.len()));
            let size_string = if self.len() < 1024 {
                all_digits_size_string
            } else {
                format!("{all_digits_size_string} ({compact_size_string})")
            };

            re_ui::list_item::list_item_scope(ui, "blob_info", |ui| {
                ui.list_item_flat_noninteractive(
                    PropertyContent::new("Size").value_text(size_string),
                );

                if let Some(media_type) = &media_type {
                    ui.list_item_flat_noninteractive(
                        PropertyContent::new("Media type").value_text(media_type.as_str()),
                    )
                    .on_hover_text("Media type (MIME) based on magic header bytes");
                } else {
                    ui.list_item_flat_noninteractive(
                        PropertyContent::new("Media type").value_text("?"),
                    )
                    .on_hover_text("Failed to detect media type (Mime) from magic header bytes");
                }

                blob_preview_and_save_ui(
                    ctx,
                    ui,
                    ui_layout,
                    query,
                    entity_path,
                    cache_key,
                    self,
                    media_type.as_ref(),
                    None,
                );
            });
        }
    }
}

#[allow(clippy::too_many_arguments)]
pub fn blob_preview_and_save_ui(
    ctx: &re_viewer_context::ViewerContext<'_>,
    ui: &mut egui::Ui,
    ui_layout: UiLayout,
    query: &re_chunk_store::LatestAtQuery,
    entity_path: &re_log_types::EntityPath,
    blob_cache_key: Option<Hash64>,
    blob: &re_types::datatypes::Blob,
    media_type: Option<&MediaType>,
    video_timestamp: Option<VideoTimestamp>,
) {
    #[allow(unused_assignments)] // Not used when targeting web.
    let mut image = None;
    let mut video_result_for_frame_preview = None;

    if let Some(blob_cache_key) = blob_cache_key {
        if !ui_layout.is_single_line() && ui_layout != UiLayout::Tooltip {
            exif_ui(ui, blob_cache_key, blob);
        }

        // Try to treat it as an image:
        image = ctx
            .store_context
            .caches
            .entry(|c: &mut re_viewer_context::ImageDecodeCache| {
                c.entry(blob_cache_key, blob, media_type)
            })
            .ok();

        if let Some(image) = &image {
            if !ui_layout.is_single_line() {
                ui.list_item_flat_noninteractive(
                    PropertyContent::new("Image format").value_text(image.format.to_string()),
                );
            }

            let colormap = None; // TODO(andreas): Rely on default here for now.
            image_preview_ui(ctx, ui, ui_layout, query, entity_path, image, colormap);
        } else {
            // Try to treat it as a video.
            let video_result =
                ctx.store_context
                    .caches
                    .entry(|c: &mut re_viewer_context::VideoCache| {
                        let debug_name = entity_path.to_string();
                        c.entry(
                            debug_name,
                            blob_cache_key,
                            blob,
                            media_type,
                            ctx.app_options().video_decoder_settings(),
                        )
                    });
            video_result_ui(ui, ui_layout, &video_result);
            video_result_for_frame_preview = Some(video_result);
        }
    }

    if !ui_layout.is_single_line() && ui_layout != UiLayout::Tooltip {
        ui.horizontal(|ui| {
            let text = if cfg!(target_arch = "wasm32") {
                "Download blob…"
            } else {
                "Save blob…"
            };
            if ui.button(text).clicked() {
                let mut file_name = entity_path
                    .last()
                    .map_or("blob", |name| name.unescaped_str())
                    .to_owned();

                if let Some(file_extension) = media_type.as_ref().and_then(|mt| mt.file_extension())
                {
                    file_name.push('.');
                    file_name.push_str(file_extension);
                }

                ctx.command_sender().save_file_dialog(
                    re_capabilities::MainThreadToken::from_egui_ui(ui),
                    &file_name,
                    "Save blob".to_owned(),
                    blob.to_vec(),
                );
            }

            if let Some(image) = image {
                let image_stats = ctx
                    .store_context
                    .caches
                    .entry(|c: &mut re_viewer_context::ImageStatsCache| c.entry(&image));
                let data_range = re_viewer_context::gpu_bridge::image_data_range_heuristic(
                    &image_stats,
                    &image.format,
                );
                crate::image::copy_image_button_ui(ui, &image, data_range);
            }
        });

        // Show a mini video player for video blobs:
        if let Some(video_result) = &video_result_for_frame_preview {
            if let Ok(video) = video_result.as_ref() {
                ui.separator();

                show_decoded_frame_info(
                    ctx.render_ctx(),
                    ui,
                    ui_layout,
                    video,
                    video_timestamp,
                    blob,
                );
            }
        }
    }
}

/// Show EXIF data about the given blob (image), if possible.
fn exif_ui(ui: &mut egui::Ui, key: Hash64, blob: &re_types::datatypes::Blob) {
    let exif_result = ui.ctx().memory_mut(|mem| {
        // Cache EXIF parsing to avoid re-parsing every frame.
        // The parsing is really fast, so this is not really needed.
        let cache = mem
            .caches
            .cache::<egui::cache::FramePublisher<Hash64, Arc<rexif::ExifResult>>>();
        cache.get(&key).cloned().unwrap_or_else(|| {
            re_tracing::profile_scope!("exif-parse");
            let (result, _warnings) = rexif::parse_buffer_quiet(blob);
            let result = Arc::new(result);
            cache.set(key, result.clone());
            result
        })
    });

    if let Ok(exif) = &*exif_result {
        ui.list_item_collapsible_noninteractive_label("EXIF", false, |ui| {
            list_item::list_item_scope(ui, "exif", |ui| {
                for entry in &exif.entries {
                    let tag_string = if entry.tag == rexif::ExifTag::UnknownToMe {
                        "<Unknown tag>".to_owned()
                    } else {
                        entry.tag.to_string()
                    };
                    ui.list_item_flat_noninteractive(
                        list_item::PropertyContent::new(tag_string)
                            .value_text(entry.value_more_readable.to_string()),
                    );
                }
            });
        });
    }
}