Skip to content

Commit d46d05e

Browse files
committed
fix: use image feature of jxl to fix image decoding error
1 parent b3b2c2a commit d46d05e

3 files changed

Lines changed: 14 additions & 72 deletions

File tree

Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ dirs = "6.0.0"
1212
eyre = "0.6.12"
1313
fast_image_resize = { version = "4.2.3", features = ["image"] }
1414
image = { workspace = true, features = ["hdr", "jpeg", "png", "rayon", "webp"] }
15-
jxl-oxide = "0.12.4"
15+
jxl-oxide = { version = "0.12.4", features = ["image"] }
1616
notify = "6.1.1"
1717
rand = "0.9.2"
1818
ron = { workspace = true }

src/wallpaper.rs

Lines changed: 11 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ use crate::{CosmicBg, CosmicBgLayer};
44

55
use std::{
66
collections::VecDeque,
7-
fs,
7+
fs::{self, File},
88
path::PathBuf,
99
time::{Duration, Instant},
1010
};
1111

1212
use cosmic_bg_config::{Color, Entry, SamplingMethod, ScalingMode, Source, state::State};
1313
use cosmic_config::CosmicConfigEntry;
14-
use eyre::{OptionExt, eyre};
15-
use image::{DynamicImage, GrayAlphaImage, GrayImage, ImageReader, RgbImage, RgbaImage};
16-
use jxl_oxide::{EnumColourEncoding, JxlImage, PixelFormat};
14+
use eyre::eyre;
15+
use image::{DynamicImage, ImageReader};
16+
use jxl_oxide::integration::JxlDecoder;
1717
use notify::{RecommendedWatcher, RecursiveMode, Watcher};
1818
use rand::{rng, seq::SliceRandom};
1919
use sctk::reexports::{
@@ -405,71 +405,11 @@ fn current_image(output: &str) -> Option<Source> {
405405

406406
/// Decodes JPEG XL image files into `image::DynamicImage` via `jxl-oxide`.
407407
fn decode_jpegxl(path: &std::path::Path) -> eyre::Result<DynamicImage> {
408-
let mut image = JxlImage::builder()
409-
.open(path)
410-
.map_err(|why| eyre!("failed to read image header: {why}"))?;
411-
412-
image.request_color_encoding(EnumColourEncoding::srgb(
413-
jxl_oxide::RenderingIntent::Relative,
414-
));
415-
416-
let render = image
417-
.render_frame(0)
418-
.map_err(|why| eyre!("failed to render image frame: {why}"))?;
419-
420-
let framebuffer = render.image_all_channels();
421-
422-
match image.pixel_format() {
423-
PixelFormat::Graya => GrayAlphaImage::from_raw(
424-
framebuffer.width() as u32,
425-
framebuffer.height() as u32,
426-
framebuffer
427-
.buf()
428-
.iter()
429-
.map(|x| x * 255. + 0.5)
430-
.map(|x| x as u8)
431-
.collect::<Vec<_>>(),
432-
)
433-
.map(DynamicImage::ImageLumaA8)
434-
.ok_or_eyre("Can't decode gray alpha buffer"),
435-
PixelFormat::Gray => GrayImage::from_raw(
436-
framebuffer.width() as u32,
437-
framebuffer.height() as u32,
438-
framebuffer
439-
.buf()
440-
.iter()
441-
.map(|x| x * 255. + 0.5)
442-
.map(|x| x as u8)
443-
.collect::<Vec<_>>(),
444-
)
445-
.map(DynamicImage::ImageLuma8)
446-
.ok_or_eyre("Can't decode gray buffer"),
447-
PixelFormat::Rgba => RgbaImage::from_raw(
448-
framebuffer.width() as u32,
449-
framebuffer.height() as u32,
450-
framebuffer
451-
.buf()
452-
.iter()
453-
.map(|x| x * 255. + 0.5)
454-
.map(|x| x as u8)
455-
.collect::<Vec<_>>(),
456-
)
457-
.map(DynamicImage::ImageRgba8)
458-
.ok_or_eyre("Can't decode rgba buffer"),
459-
PixelFormat::Rgb => RgbImage::from_raw(
460-
framebuffer.width() as u32,
461-
framebuffer.height() as u32,
462-
framebuffer
463-
.buf()
464-
.iter()
465-
.map(|x| x * 255. + 0.5)
466-
.map(|x| x as u8)
467-
.collect::<Vec<_>>(),
468-
)
469-
.map(DynamicImage::ImageRgb8)
470-
.ok_or_eyre("Can't decode rgb buffer"),
471-
//TODO: handle this
472-
PixelFormat::Cmyk => Err(eyre!("unsupported pixel format: CMYK")),
473-
PixelFormat::Cmyka => Err(eyre!("unsupported pixel format: CMYKA")),
474-
}
408+
let file = File::open(path).map_err(|why| eyre!("failed to open jxl image file: {why}"))?;
409+
410+
let decoder =
411+
JxlDecoder::new(file).map_err(|why| eyre!("failed to read jxl image header: {why}"))?;
412+
413+
image::DynamicImage::from_decoder(decoder)
414+
.map_err(|why| eyre!("failed to decode jxl image: {why}"))
475415
}

0 commit comments

Comments
 (0)