Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix encoded image being suggested for non-image blobs (like video) #7428

Merged
merged 2 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions crates/store/re_types/src/components/media_type_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,11 @@ impl MediaType {
}
}
}

/// Returns `true` if this is an image media type.
pub fn is_image(&self) -> bool {
self.as_str().starts_with("image/")
}
}

impl std::fmt::Display for MediaType {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use itertools::Itertools as _;

use re_space_view::HybridResults;
use re_space_view::{diff_component_filter, HybridResults};
use re_types::{
archetypes::EncodedImage,
components::{Blob, DrawOrder, MediaType, Opacity},
Expand All @@ -10,7 +10,7 @@ use re_viewer_context::{
ApplicableEntities, IdentifiedViewSystem, ImageDecodeCache, QueryContext,
SpaceViewSystemExecutionError, TypedComponentFallbackProvider, ViewContext,
ViewContextCollection, ViewQuery, VisualizableEntities, VisualizableFilterContext,
VisualizerQueryInfo, VisualizerSystem,
VisualizerAdditionalApplicabilityFilter, VisualizerQueryInfo, VisualizerSystem,
};

use crate::{
Expand Down Expand Up @@ -42,11 +42,32 @@ impl IdentifiedViewSystem for EncodedImageVisualizer {
}
}

struct ImageMediaTypeFilter;

impl VisualizerAdditionalApplicabilityFilter for ImageMediaTypeFilter {
/// Marks entities only as applicable for `EncodedImage` if they have an image media type.
///
/// Otherwise the image encoder might be suggested for other blobs like video.
fn update_applicability(&mut self, event: &re_chunk_store::ChunkStoreEvent) -> bool {
diff_component_filter(event, |media_type: &re_types::components::MediaType| {
media_type.is_image()
}) || diff_component_filter(event, |image: &re_types::components::Blob| {
MediaType::guess_from_data(&image.0).map_or(false, |media| media.is_image())
})
}
Comment on lines +51 to +57
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not new to this PR, but I think there is room for improvement of the naming of these things, maybe:

Suggested change
fn update_applicability(&mut self, event: &re_chunk_store::ChunkStoreEvent) -> bool {
diff_component_filter(event, |media_type: &re_types::components::MediaType| {
media_type.is_image()
}) || diff_component_filter(event, |image: &re_types::components::Blob| {
MediaType::guess_from_data(&image.0).map_or(false, |media| media.is_image())
})
}
fn is_applicable(&mut self, event: &re_chunk_store::ChunkStoreEvent) -> bool {
is_any_component(event, |media_type: &re_types::components::MediaType| {
media_type.is_image()
}) || is_any_component(event, |image: &re_types::components::Blob| {
MediaType::guess_from_data(&image.0).map_or(false, |media| media.is_image())
})
}

Also, does this really need &mut self?

anyway, no need to fix now. Just thinking out loud.

Copy link
Member Author

@Wumpf Wumpf Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not gonna change the trait and all associated implemenations now in this PR ;)

The semantics of update are correct and very relevant as this is an incremental and stateful update of the applicability. Details are on the trait's documentation

Copy link
Member Author

@Wumpf Wumpf Sep 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

&mut self is for convenience if a trait implementor wants to track additional data that influence applicability

}

impl VisualizerSystem for EncodedImageVisualizer {
fn visualizer_query_info(&self) -> VisualizerQueryInfo {
VisualizerQueryInfo::from_archetype::<EncodedImage>()
}

fn applicability_filter(
&self,
) -> Option<Box<dyn re_viewer_context::VisualizerAdditionalApplicabilityFilter>> {
Some(Box::new(ImageMediaTypeFilter))
}

fn filter_visualizable_entities(
&self,
entities: ApplicableEntities,
Expand Down
Loading