-
Notifications
You must be signed in to change notification settings - Fork 451
Refactor MaxImageDimensionSubscriber
#7850
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
5fa4475
Simplify and robustify handling of image loading
emilk 9493923
Add `MediaType::is_video`
emilk 97c7eb3
Refactor `re_renderer::Video` constructor
emilk 8b57ed4
Add profile scopes to video loading
emilk 1263409
Handle videos in `MaxImageDimensions`
emilk 23469b0
Handle images and videos without media types
emilk e4e30b4
Disable the double-video parsing
emilk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,14 @@ | ||
use ahash::HashMap; | ||
use arrow2::array::Array; | ||
use nohash_hasher::IntMap; | ||
use once_cell::sync::OnceCell; | ||
|
||
use re_chunk_store::{ChunkStore, ChunkStoreSubscriber, ChunkStoreSubscriberHandle}; | ||
use re_log_types::{EntityPath, StoreId}; | ||
use re_types::{ | ||
archetypes::EncodedImage, | ||
components::{Blob, ImageFormat, MediaType}, | ||
external::image, | ||
Archetype, Loggable, | ||
Loggable, | ||
}; | ||
|
||
#[derive(Debug, Clone, Default)] | ||
|
@@ -17,11 +17,12 @@ pub struct MaxDimensions { | |
pub height: u32, | ||
} | ||
|
||
/// The size of the largest image and/or video at a given entity path. | ||
#[derive(Default, Debug, Clone)] | ||
pub struct MaxImageDimensions(IntMap<EntityPath, MaxDimensions>); | ||
|
||
impl MaxImageDimensions { | ||
/// Accesses the image dimension information for a given store | ||
/// Accesses the image/video dimension information for a given store | ||
pub fn access<T>( | ||
store_id: &StoreId, | ||
f: impl FnOnce(&IntMap<EntityPath, MaxDimensions>) -> T, | ||
|
@@ -76,6 +77,7 @@ impl ChunkStoreSubscriber for MaxImageDimensionSubscriber { | |
continue; | ||
} | ||
|
||
// Handle `Image`, `DepthImage`, `SegmentationImage`… | ||
if let Some(all_dimensions) = event.diff.chunk.components().get(&ImageFormat::name()) { | ||
for new_dim in all_dimensions.iter().filter_map(|array| { | ||
array | ||
|
@@ -89,62 +91,75 @@ impl ChunkStoreSubscriber for MaxImageDimensionSubscriber { | |
.entry(event.diff.chunk.entity_path().clone()) | ||
.or_default(); | ||
|
||
max_dim.height = max_dim.height.max(new_dim.height); | ||
max_dim.width = max_dim.width.max(new_dim.width); | ||
max_dim.height = max_dim.height.max(new_dim.height); | ||
} | ||
} | ||
|
||
// TODO(jleibs): Image blob/mediatypes should have their own component | ||
// Is there a more canonical way to check the indicators for a chunk? | ||
if event | ||
.diff | ||
.chunk | ||
.components() | ||
.get(&EncodedImage::indicator().name()) | ||
.is_some() | ||
// Handle `ImageEncoded`, `AssetVideo`… | ||
let blobs = event.diff.chunk.iter_component_arrays(&Blob::name()); | ||
let media_types = event.diff.chunk.iter_component_arrays(&MediaType::name()); | ||
for (blob, media_type) in | ||
itertools::izip!(blobs, media_types.map(Some).chain(std::iter::repeat(None))) | ||
{ | ||
let media_types = event.diff.chunk.iter_component_arrays(&MediaType::name()); | ||
let blobs = event.diff.chunk.iter_component_arrays(&Blob::name()); | ||
for (media, blob) in media_types.zip(blobs) { | ||
let Ok(media) = MediaType::from_arrow_opt(media.as_ref()) else { | ||
continue; | ||
}; | ||
let Ok(blob) = Blob::from_arrow_opt(blob.as_ref()) else { | ||
continue; | ||
}; | ||
if let (media, Some(Some(blob))) = (media.first(), blob.first()) { | ||
let image_bytes = blob.0.as_slice(); | ||
|
||
let mut reader = image::io::Reader::new(std::io::Cursor::new(image_bytes)); | ||
|
||
if let Some(Some(media)) = media { | ||
if let Some(format) = image::ImageFormat::from_mime_type(&media.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); | ||
} | ||
} | ||
|
||
if let Ok((width, height)) = reader.into_dimensions() { | ||
let max_dim = self | ||
.max_dimensions | ||
.entry(event.store_id.clone()) | ||
.or_default() | ||
.0 | ||
.entry(event.diff.chunk.entity_path().clone()) | ||
.or_default(); | ||
|
||
max_dim.height = max_dim.height.max(height); | ||
max_dim.width = max_dim.width.max(width); | ||
} | ||
} | ||
if let Some([width, height]) = size_from_blob(blob.as_ref(), media_type.as_deref()) | ||
{ | ||
let max_dim = self | ||
.max_dimensions | ||
.entry(event.store_id.clone()) | ||
.or_default() | ||
.0 | ||
.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); | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
fn size_from_blob(blob: &dyn Array, media_type: Option<&dyn Array>) -> Option<[u32; 2]> { | ||
re_tracing::profile_function!(); | ||
|
||
let blob = Blob::from_arrow_opt(blob).ok()?.first()?.clone()?; | ||
|
||
let media_type: Option<MediaType> = media_type | ||
.and_then(|media_type| MediaType::from_arrow_opt(media_type).ok()) | ||
.and_then(|list| list.first().cloned()) | ||
.flatten(); | ||
|
||
let media_type = MediaType::or_guess_from_data(media_type, &blob)?; | ||
|
||
if media_type.is_image() { | ||
re_tracing::profile_scope!("image"); | ||
|
||
let image_bytes = blob.0.as_slice(); | ||
|
||
let mut reader = image::io::Reader::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"); | ||
if true { | ||
None // TODO(#7821): Use the VideoCache here so we make sure we only load each video ONCE | ||
} else { | ||
re_video::VideoData::load_from_bytes(&blob, &media_type) | ||
.ok() | ||
.map(|video| video.dimensions()) | ||
} | ||
Comment on lines
+155
to
+161
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is the core of it. I'll work on having access to …or we add functionality to That would be easy if we had: (…and used memory-mapping) |
||
} else { | ||
None | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: