-
Notifications
You must be signed in to change notification settings - Fork 621
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
Decoding out of image::io::Reader directly into user-controlled buffer #2162
Comments
This is actually being addressed in the 0.25 release! The let decoder = Reader::open("path/to/image.png")?.into_decoder()?;
let size = decoder.total_bytes();
decoder.read_image(&mut buf[..size])?
If you only need to know the format, you can call |
Amazing! Do you have a rough ETA for 0.25 (like, is it a few weeks away, a few months, or a few years)? For the PS, I was more wondering whether there was a way to decode something that is in a |
There's currently a 0.25.0-preview.0 release out. Assuming no glaring issues are found with it, a full 0.25.0 should be at most a few weeks away. As far as taking advantage of the whole image being in memory, that's actually something that's been discussed in the context of the |
0.25.0 is now released! |
I would like to be able to decode from an
io::Reader
into a buffer I control.My specific use case for this functionality is to avoid an allocation + copy on the critical path of a very hot loop that has to extract the raw image bytes from an image elsewhere in memory.
This is more generally applicable to anyone who may want to eliminate an extra copy in their applications.
Draft
I think most of the functionality already exists, it's just private in the form of
free_functions::load_inner
:https://github.com/image-rs/image/blob/2b513ae9a6ac306e752914f562e7d408f096ba3f/src/io/free_functions.rs#L900-L111
plus a variant of
decoder_to_vec
:image/src/image.rs
Lines 708 to 722 in 2b513ae
that re-uses an existing
Vec
instead of allocating a new one. The latter is easy enough to replicate, and so probably doesn't need to be provided by theimage
crate (though it'd be a nice-to-have).The easiest way to provide this functionality is likely to add a method to
io::Reader
that exposes theImageDecoder
. Might be tricky given theVisitor
pattern, but hopefully doable. Perhaps via a provided callback function?I'll tack on to the end here that we currently use a
Cursor
over a&[u8]
to construct anio::Reader
, which feels a little odd. Is there a nicer way to getwith_guessed_format
without having to go via an I/O trait when you have the image in a buffer already, or is this the intended way?The text was updated successfully, but these errors were encountered: