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

make progress callback mutable #1694

Closed
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
4 changes: 2 additions & 2 deletions src/codecs/openexr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,10 +152,10 @@ impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for OpenExrDecoder<R> {
}

// reads with or without alpha, depending on `self.alpha_preference` and `self.alpha_present_in_file`
fn read_image_with_progress<F: Fn(Progress)>(
fn read_image_with_progress<F: FnMut(Progress)>(
Copy link
Contributor Author

@johannesvollmer johannesvollmer Apr 7, 2022

Choose a reason for hiding this comment

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

So, @HeroicKatora will there be any problem with recursion of the progress callback? If yes, is it even relevant, would it happen in the real world?

Copy link
Member

Choose a reason for hiding this comment

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

No, I don't believe so. Only a really small fraction will be recursive, if any, and our default implementation handles this gracefully.

self,
unaligned_bytes: &mut [u8],
progress_callback: F,
mut progress_callback: F,
) -> ImageResult<()> {
let blocks_in_header = self.selected_exr_header().chunk_count as u64;
let channel_count = self.color_type().channel_count() as usize;
Expand Down
43 changes: 39 additions & 4 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -686,8 +686,8 @@ pub trait ImageDecoder<'a>: Sized {
self.read_image_with_progress(buf, |_| {})
}

/// Same as `read_image` but periodically calls the provided callback to give updates on loading
/// progress.
/// Same as `read_image_with_progress_mut`, but the callback cannot mutate state.
/// Use `read_image_with_progress_mut` if you want to mutate your state from within the callback.
fn read_image_with_progress<F: Fn(Progress)>(
self,
buf: &mut [u8],
Expand Down Expand Up @@ -720,6 +720,22 @@ pub trait ImageDecoder<'a>: Sized {
Ok(())
}

/// Same as `read_image` but periodically calls the provided callback to give updates on loading
/// progress.
// TODO find a way to have only _mut versions, eliminating refcells
fn read_image_with_progress_mut<F: FnMut(Progress)>(
self,
buf: &mut [u8],
progress_callback: F,
) -> ImageResult<()> {
let mutable_callback_cell = std::cell::RefCell::new(progress_callback);
self.read_image_with_progress(buf, |progress| {
if let Ok(mut progress_callback) = mutable_callback_cell.try_borrow_mut() {
progress_callback(progress)
}
})
}

/// Set decoding limits for this decoder. See [`Limits`] for the different kinds of
/// limits that is possible to set.
///
Expand Down Expand Up @@ -755,6 +771,17 @@ pub trait ImageDecoderRect<'a>: ImageDecoder<'a> + Sized {
self.read_rect_with_progress(x, y, width, height, buf, |_| {})
}

/// Same as `read_rect_with_progress_mut`, but the callback is not mutable.
fn read_rect_with_progress<F: Fn(Progress)>(
&mut self,
x: u32,
y: u32,
width: u32,
height: u32,
buf: &mut [u8],
progress_callback: F,
) -> ImageResult<()>;

/// Decode a rectangular section of the image, periodically reporting progress.
///
/// The output buffer will be filled with fields specified by
Expand All @@ -767,15 +794,23 @@ pub trait ImageDecoderRect<'a>: ImageDecoder<'a> + Sized {
///
/// This function will panic if the output buffer isn't at least
/// `color_type().bytes_per_pixel() * color_type().channel_count() * width * height` bytes long.
fn read_rect_with_progress<F: Fn(Progress)>(
// TODO find a way to have only _mut versions, eliminating refcells
fn read_rect_with_progress_mut<F: FnMut(Progress)>(
&mut self,
x: u32,
y: u32,
width: u32,
height: u32,
buf: &mut [u8],
progress_callback: F,
) -> ImageResult<()>;
) -> ImageResult<()> {
let mutable_callback_cell = std::cell::RefCell::new(progress_callback);
self.read_rect_with_progress(x, y, width, height, buf, |progress| {
if let Ok(mut progress_callback) = mutable_callback_cell.try_borrow_mut() {
progress_callback(progress)
}
})
}
}

/// AnimationDecoder trait
Expand Down