-
Notifications
You must be signed in to change notification settings - Fork 645
Add decoding hooks #2372
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
Open
fintelia
wants to merge
8
commits into
image-rs:main
Choose a base branch
from
fintelia:decoding-hooks
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add decoding hooks #2372
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
9857762
Add decoding hooks
fintelia 3d7fabb
Implicit lifetimes
fintelia c3b10ca
Make BufReader private
fintelia 9bb1133
formatting
fintelia 0ea8127
Compile fix
fintelia 0d898b0
Pass-through implementations for more trait methods
fintelia a00e332
Block registration of hooks for formats that already have them
fintelia 7b887b6
Block registration of hooks for built-in formats
fintelia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
//! This module provides a way to register decoding hooks for image formats not directly supported | ||
//! by this crate. | ||
|
||
use std::{ | ||
collections::HashMap, | ||
io::{BufRead, BufReader, Read, Seek}, | ||
sync::RwLock, | ||
}; | ||
|
||
use crate::{ImageDecoder, ImageFormat, ImageResult}; | ||
|
||
pub(crate) trait ReadSeek: Read + Seek {} | ||
impl<T: Read + Seek> ReadSeek for T {} | ||
|
||
pub(crate) static DECODING_HOOKS: RwLock<Option<HashMap<ImageFormat, DecodingHook>>> = | ||
RwLock::new(None); | ||
|
||
/// A wrapper around a type-erased trait object that implements `Read` and `Seek`. | ||
pub struct GenericReader<'a>(pub(crate) BufReader<Box<dyn ReadSeek + 'a>>); | ||
impl Read for GenericReader<'_> { | ||
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> { | ||
self.0.read(buf) | ||
} | ||
fn read_vectored(&mut self, bufs: &mut [std::io::IoSliceMut<'_>]) -> std::io::Result<usize> { | ||
self.0.read_vectored(bufs) | ||
} | ||
fn read_to_end(&mut self, buf: &mut Vec<u8>) -> std::io::Result<usize> { | ||
self.0.read_to_end(buf) | ||
} | ||
fn read_to_string(&mut self, buf: &mut String) -> std::io::Result<usize> { | ||
self.0.read_to_string(buf) | ||
} | ||
fn read_exact(&mut self, buf: &mut [u8]) -> std::io::Result<()> { | ||
self.0.read_exact(buf) | ||
} | ||
} | ||
impl BufRead for GenericReader<'_> { | ||
fn fill_buf(&mut self) -> std::io::Result<&[u8]> { | ||
self.0.fill_buf() | ||
} | ||
fn consume(&mut self, amt: usize) { | ||
self.0.consume(amt) | ||
} | ||
fn read_until(&mut self, byte: u8, buf: &mut Vec<u8>) -> std::io::Result<usize> { | ||
self.0.read_until(byte, buf) | ||
} | ||
fn read_line(&mut self, buf: &mut String) -> std::io::Result<usize> { | ||
self.0.read_line(buf) | ||
} | ||
} | ||
impl Seek for GenericReader<'_> { | ||
fn seek(&mut self, pos: std::io::SeekFrom) -> std::io::Result<u64> { | ||
self.0.seek(pos) | ||
} | ||
fn rewind(&mut self) -> std::io::Result<()> { | ||
self.0.rewind() | ||
} | ||
fn stream_position(&mut self) -> std::io::Result<u64> { | ||
self.0.stream_position() | ||
} | ||
|
||
// TODO: Add `seek_relative` once MSRV is at least 1.80.0 | ||
} | ||
|
||
/// A function to produce an `ImageDecoder` for a given image format. | ||
pub type DecodingHook = | ||
Box<dyn for<'a> Fn(GenericReader<'a>) -> ImageResult<Box<dyn ImageDecoder + 'a>> + Send + Sync>; | ||
fintelia marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/// Register a new decoding hook or returns false if one already exists for the given format. | ||
pub fn register_decoding_hook(format: ImageFormat, hook: DecodingHook) -> bool { | ||
if format.reading_enabled() { | ||
return false; | ||
} | ||
|
||
let mut hooks = DECODING_HOOKS.write().unwrap(); | ||
if hooks.is_none() { | ||
*hooks = Some(HashMap::new()); | ||
} | ||
match hooks.as_mut().unwrap().entry(format) { | ||
std::collections::hash_map::Entry::Vacant(entry) => { | ||
entry.insert(hook); | ||
true | ||
} | ||
std::collections::hash_map::Entry::Occupied(_) => false, | ||
} | ||
} | ||
|
||
/// Returns whether a decoding hook has been registered for the given format. | ||
pub fn decoding_hook_registered(format: ImageFormat) -> bool { | ||
DECODING_HOOKS | ||
.read() | ||
.unwrap() | ||
.as_ref() | ||
.map(|hooks| hooks.contains_key(&format)) | ||
.unwrap_or(false) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.