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 2 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
2 changes: 1 addition & 1 deletion src/codecs/bmp/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1526,7 +1526,7 @@ impl<'a, R: 'a + Read + Seek> ImageDecoder<'a> for BmpDecoder<R> {
}

impl<'a, R: 'a + Read + Seek> ImageDecoderRect<'a> for BmpDecoder<R> {
fn read_rect_with_progress<F: Fn(Progress)>(
fn read_rect_with_progress<F: FnMut(Progress)>(
johannesvollmer marked this conversation as resolved.
Show resolved Hide resolved
&mut self,
x: u32,
y: u32,
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/dxt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl<'a, R: 'a + Read> ImageDecoder<'a> for DxtDecoder<R> {
}

impl<'a, R: 'a + Read + Seek> ImageDecoderRect<'a> for DxtDecoder<R> {
fn read_rect_with_progress<F: Fn(Progress)>(
fn read_rect_with_progress<F: FnMut(Progress)>(
&mut self,
x: u32,
y: u32,
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/farbfeld.rs
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,7 @@ impl<'a, R: 'a + Read> ImageDecoder<'a> for FarbfeldDecoder<R> {
}

impl<'a, R: 'a + Read + Seek> ImageDecoderRect<'a> for FarbfeldDecoder<R> {
fn read_rect_with_progress<F: Fn(Progress)>(
fn read_rect_with_progress<F: FnMut(Progress)>(
&mut self,
x: u32,
y: u32,
Expand Down
2 changes: 1 addition & 1 deletion src/codecs/hdr/decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ impl<'a, R: 'a + BufRead> ImageDecoder<'a> for HdrAdapter<R> {
}

impl<'a, R: 'a + BufRead + Seek> ImageDecoderRect<'a> for HdrAdapter<R> {
fn read_rect_with_progress<F: Fn(Progress)>(
fn read_rect_with_progress<F: FnMut(Progress)>(
&mut self,
x: u32,
y: u32,
Expand Down
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
19 changes: 10 additions & 9 deletions src/image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -425,14 +425,14 @@ pub(crate) fn load_rect<'a, D, F, F1, F2, E>(
width: u32,
height: u32,
buf: &mut [u8],
progress_callback: F,
mut progress_callback: F,
decoder: &mut D,
mut seek_scanline: F1,
mut read_scanline: F2,
) -> ImageResult<()>
where
D: ImageDecoder<'a>,
F: Fn(Progress),
F: FnMut(Progress),
F1: FnMut(&mut D, u64) -> io::Result<()>,
F2: FnMut(&mut D, &mut [u8]) -> Result<(), E>,
ImageError: From<E>,
Expand Down Expand Up @@ -463,6 +463,11 @@ where
let mut tmp_scanline = None;

{
progress_callback(Progress {
current: 0,
total: total_bytes,
});

johannesvollmer marked this conversation as resolved.
Show resolved Hide resolved
// Read a range of the image starting from byte number `start` and continuing until byte
// number `end`. Updates `current_scanline` and `bytes_read` appropiately.
let mut read_image_range = |mut start: u64, end: u64| -> ImageResult<()> {
Expand Down Expand Up @@ -545,10 +550,6 @@ where
)));
}

progress_callback(Progress {
current: 0,
total: total_bytes,
});
if x == 0 && width == u64::from(dimensions.0) {
let start = x * bytes_per_pixel + y * row_bytes;
let end = (x + width) * bytes_per_pixel + (y + height - 1) * row_bytes;
Expand Down Expand Up @@ -688,10 +689,10 @@ pub trait ImageDecoder<'a>: Sized {

/// Same as `read_image` but periodically calls the provided callback to give updates on loading
/// progress.
fn read_image_with_progress<F: Fn(Progress)>(
fn read_image_with_progress<F: FnMut(Progress)>(
self,
buf: &mut [u8],
progress_callback: F,
mut progress_callback: F,
) -> ImageResult<()> {
assert_eq!(u64::try_from(buf.len()), Ok(self.total_bytes()));

Expand Down Expand Up @@ -767,7 +768,7 @@ 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)>(
fn read_rect_with_progress<F: FnMut(Progress)>(
&mut self,
x: u32,
y: u32,
Expand Down