Skip to content

Commit

Permalink
feat: support jpegxl images
Browse files Browse the repository at this point in the history
  • Loading branch information
mmstick committed Oct 9, 2024
1 parent 584f6b3 commit 7bbbe02
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 22 deletions.
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"] }
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
}
}
}

0 comments on commit 7bbbe02

Please sign in to comment.