Skip to content

Conversation

@RunDevelopment
Copy link
Contributor

@RunDevelopment RunDevelopment commented Nov 25, 2025

Right now, plugin decoders can only be used when decoding a file. It's impossible to use them to decode arbitrary BufRead+Seek data. This is because hooks are currently only used by ImageReader and the only way to set its internal format to an extension is via ImageReader::open(&Path).

This PR solves this API limitation by adding a new set_format_with_extension method, which sets the internal format to the given extension string.

Bike shedding: I'm not sure about the name. Other options I've considered: set_format_from_extension, set_format_by_extension, set_extension.


Related to #2616

/// Supply the extension of the format as which to interpret the read image.
///
/// The extension must be without the leading dot. E.g. `"png"` or `"jpeg"`.
pub fn set_format_with_extension(&mut self, ext: OsString) {
Copy link
Member

Choose a reason for hiding this comment

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

I think those names will be confusing. We have ImageFormat::from_extension but a very important point of this method is that it will not use a core decoder but a registered hook. My gut feeling is set_decoder_from_extension. And would you add a comment about the resolution order?

The decoder is chosen first checking the list of registered hooks for the extension, see [hooks::decoding_hook_registered]. If no custom hook is found it will consider the builtin format which corresponds to the extension as if by [ImageFormat::from_extension].

Or something of sorts. For consistency with either decoding_hook_registered or ImageFormat::from_extension the argument should be &OsStr or impl AsRef<OsStr> respectively. Though I think we've been trying to reduce the amount of generic interfaces and the upside is small. The allocation to an owned variant doesn't really matter, we might change the internals to only store an index into the hook list or something.

Copy link
Contributor

Choose a reason for hiding this comment

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

Should we switch things so that ImageReader always tries the hooks even if you specify a built-in format by calling with_format or set_format? We'd have to pick one specific file extension to use if you specified JPEG, TIFF, and PNM but I'm not sure that would actually be a problem. Hopefully no one would register different decoders for .jpg and .jpeg and then be upset at the result...

Copy link
Contributor

Choose a reason for hiding this comment

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

Created #2683 to try that out

@fintelia
Copy link
Contributor

fintelia commented Dec 2, 2025

Another option is to use with_format/set_format and rename the existing methods:

  • with_format -> with_builtin_format
  • set_format -> set_builtin_format

Not entirely sure how I feel about that though, since it would add a bit more churn

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants