Skip to content

Commit 306c6d2

Browse files
committed
Provide ImageReader::skip
1 parent 54d3c0c commit 306c6d2

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

src/io/image_reader_type.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,42 @@ impl ImageReader<'_> {
409409
})
410410
}
411411

412+
/// Skip the next image, discard its image data.
413+
///
414+
/// This will attempt to read the image data with as little allocation as possible while still
415+
/// running the usual verification routines. It will inform the underlying decoder that it is
416+
/// uninterested in all of the image data, then run its decoding routine.
417+
pub fn skip(&mut self) -> ImageResult<()> {
418+
const EMPTY: crate::math::Rect = crate::math::Rect {
419+
x: 0,
420+
y: 0,
421+
width: 0,
422+
height: 0,
423+
};
424+
425+
// This is just an information..
426+
let _ = self.inner.viewbox(EMPTY);
427+
428+
// Some decoders may still want a buffer, so we can't fully ignore it.
429+
let layout = self.inner.peek_layout()?;
430+
// This is technically redundant but it's also cheap.
431+
self.limits.check_dimensions(layout.width, layout.height)?;
432+
let bytes = layout.total_bytes();
433+
434+
if bytes < 512 {
435+
let mut stack = [0u8; 512];
436+
self.inner.read_image(&mut stack[..bytes as usize])?;
437+
} else {
438+
// Check that we do not allocate a bigger buffer than we are allowed to
439+
// FIXME: should this rather go in `DynamicImage::from_decoder` somehow?
440+
self.limits.reserve(bytes)?;
441+
DynamicImage::decoder_to_image(self.inner.as_mut(), layout)?;
442+
self.limits.free(bytes);
443+
}
444+
445+
Ok(())
446+
}
447+
412448
/// Query the layout that the image will have.
413449
pub fn layout(&mut self) -> ImageResult<crate::ImageLayout> {
414450
self.inner.peek_layout()

0 commit comments

Comments
 (0)