Skip to content

Add with_format_from_path to ImageReaderOptions#3023

Open
RunDevelopment wants to merge 1 commit into
image-rs:mainfrom
RunDevelopment:reader-format-from-path
Open

Add with_format_from_path to ImageReaderOptions#3023
RunDevelopment wants to merge 1 commit into
image-rs:mainfrom
RunDevelopment:reader-format-from-path

Conversation

@RunDevelopment

Copy link
Copy Markdown
Member

This change makes the logic turning a path to a format in ImageReaderOptions::open public using the new ImageReaderOptions::with_format_from_path function.

The main use case here is that it's now possible (and easy) to create an ImageReaderOptions (and ImageReader) with the exact same semantics as ::open but with a custom reader. This previously wasn't possible, because we had no way to set the format based on a path or file extension.

Note: I view this as orthogonal to changes like #2683 and #2662. My main goal here is that ::open shouldn't have magic sauce that users can't recreate. I also want a sort of progressive disclosure, where we don't let the user fall directly into the dark pits of complexity but provide a path downward that they can descend at their own pace.

This resolves #1659. The user can now create their own BufReader with a given capacity and pass it to ImageReaderOptions::new like so:

// works exactly like `ImageReaderOptions::open(path).into_dimensions()` but with custom capacity
let file = File::open(path)?;
let reader = BufReader::with_capacity(1024, file);
let dimensions = ImageReaderOptions::new(reader)
    .with_format_from_path(path)
    .into_dimensions()?;

Bikeshedding: I couldn't decide between making the new method builder-like and setter-like. The things on ImageReaderOptions are a weird mix of functions that look like they belong to a builder and functions that don't. For example, ::limits is a setter and ::format is a getter. I went with with_format_from_path to mimic with_guessed_format, but I'm not married to the idea.

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.

Reduce preallocated buffer of PNG decoder

1 participant