Skip to content
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

feat: support jpegxl images #63

Merged
merged 6 commits into from
Oct 9, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 96 additions & 6 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dirs = "5.0.1"
eyre = "0.6.12"
fast_image_resize = { version = "4.2.1", features = ["image"] }
image = { workspace = true, features = ["hdr", "jpeg", "png", "rayon", "webp"] }
jpegxl-rs = { version = "0.11.1", features = ["vendored"] }
mmstick marked this conversation as resolved.
Show resolved Hide resolved
notify = "6.1.1"
rand = "0.8"
ron = { workspace = true }
Expand Down
73 changes: 57 additions & 16 deletions src/wallpaper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,27 +123,35 @@ impl Wallpaper {
tracing::info!("No source for wallpaper");
continue;
};

cur_resized_img = match source {
Source::Path(ref path) => {
if self.current_image.is_none() {
self.current_image = Some(match ImageReader::open(&path) {
Ok(img) => {
match img
.with_guessed_format()
.ok()
.and_then(|f| f.decode().ok())
{
Some(img) => img,
None => {
tracing::warn!(
"Could not decode image: {}",
path.display()
);
continue;
self.current_image = Some(match path.extension() {
Some(ext) if ext == "jxl" => match decode_jpegxl(&path) {
Some(image) => image,
None => continue,
},

_ => match ImageReader::open(&path) {
Ok(img) => {
match img
.with_guessed_format()
.ok()
.and_then(|f| f.decode().ok())
{
Some(img) => img,
None => {
tracing::warn!(
"could not decode image: {}",
path.display()
);
continue;
}
}
}
}
Err(_) => continue,
Err(_) => continue,
},
});
}
let img = self.current_image.as_ref().unwrap();
Expand Down Expand Up @@ -380,3 +388,36 @@ fn current_image(output: &str) -> Option<Source> {

wallpaper.map(|(_name, path)| path)
}

/// Decodes JPEG XL image files into `image::DynamicImage` via `jpegxl-rs`.
fn decode_jpegxl(path: &std::path::Path) -> Option<DynamicImage> {
use jpegxl_rs::image::ToDynamic;

let jxl_file = match std::fs::read(path) {
Ok(file) => file,
Err(why) => {
tracing::warn!(?why, "could not read image: {}", path.display());
return None;
}
};

let jxl_decode_result = jpegxl_rs::decoder_builder()
.parallel_runner(&jpegxl_rs::ThreadsRunner::default())
.build()
.and_then(move |decoder| decoder.decode_to_image(&jxl_file));

match jxl_decode_result {
Ok(Some(image)) => Some(image),
Ok(None) => {
tracing::warn!(
"decoded image could not be represented as a DynamicImage: {}",
path.display()
);
None
}
Err(why) => {
tracing::warn!(?why, "could not decode image: {}", path.display());
None
}
}
}
Loading