From 8b8d2cbaf71c5d983999e329f8e0fcc94a773104 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gerg=C5=91=20S=C3=A1lyi?= Date: Wed, 16 Apr 2025 14:49:02 +0200 Subject: [PATCH] Refactor the handling of image color transform parameters --- src/image.rs | 28 ++++++++++------------------ src/main.rs | 14 ++++++++++---- src/wayland.rs | 5 ++--- 3 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/image.rs b/src/image.rs index c744906..e1840f4 100644 --- a/src/image.rs +++ b/src/image.rs @@ -18,14 +18,20 @@ use smithay_client_toolkit::reexports::client::protocol::wl_shm; use crate::wayland::WorkspaceBackground; +#[derive(Clone, Copy, PartialEq)] +pub enum ColorTransform { + // Levels { input_max: u8, input_min: u8, output_max: u8, output_min: u8 }, + Legacy { brightness: i32, contrast: f32 }, + None, +} + pub fn workspace_bgs_from_output_image_dir( dir_path: impl AsRef, slot_pool: &mut SlotPool, format: wl_shm::Format, - brightness: i32, - contrast: f32, width: u32, height: u32, + color_transform: ColorTransform, ) -> anyhow::Result> { let mut buffers = Vec::new(); let mut resizer = Resizer::new(); @@ -45,11 +51,10 @@ pub fn workspace_bgs_from_output_image_dir( entry_result, slot_pool, format, - brightness, - contrast, width, height, stride, + color_transform, &mut resizer ) { Ok(Some(workspace_bg)) => buffers.push(workspace_bg), @@ -71,11 +76,10 @@ fn workspace_bg_from_file( dir_entry_result: io::Result, slot_pool: &mut SlotPool, format: wl_shm::Format, - brightness: i32, - contrast: f32, width: u32, height: u32, stride: usize, + color_transform: ColorTransform, resizer: &mut Resizer, ) -> anyhow::Result> { let entry = dir_entry_result.context("Failed to read direectory")?; @@ -91,11 +95,6 @@ fn workspace_bg_from_file( stride.try_into().unwrap(), format, ).context("Failed to create Wayland shared memory buffer")?; - let color_transform = if brightness == 0 && contrast == 0.0 { - ColorTransform::None - } else { - ColorTransform::Legacy { brightness, contrast } - }; load_wallpaper( &path, &mut canvas[..stride * height as usize], @@ -109,13 +108,6 @@ fn workspace_bg_from_file( Ok(Some(WorkspaceBackground { workspace_name, buffer })) } -#[derive(Clone, Copy, PartialEq)] -pub enum ColorTransform { - // Levels { input_max: u8, input_min: u8, output_max: u8, output_min: u8 }, - Legacy { brightness: i32, contrast: f32 }, - None, -} - fn load_wallpaper( path: &Path, dst: &mut [u8], diff --git a/src/main.rs b/src/main.rs index 4e75ca3..f398f5c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -40,6 +40,7 @@ use smithay_client_toolkit::reexports::protocols use crate::{ cli::{Cli, PixelFormat}, compositors::{Compositor, ConnectionTask, WorkspaceVisible}, + image::ColorTransform, poll::{Poll, Waker}, signal::SignalPipe, wayland::BackgroundLayer, @@ -57,8 +58,7 @@ pub struct State { pub pixel_format: Option, pub background_layers: Vec, pub compositor_connection_task: ConnectionTask, - pub brightness: i32, - pub contrast: f32, + pub color_transform: ColorTransform, } impl State { @@ -103,6 +103,13 @@ fn run() -> anyhow::Result<()> { let cli = Cli::parse(); let wallpaper_dir = Path::new(&cli.wallpaper_dir).canonicalize().unwrap(); + let brightness = cli.brightness.unwrap_or(0); + let contrast = cli.contrast.unwrap_or(0.0); + let color_transform = if brightness == 0 && contrast == 0.0 { + ColorTransform::None + } else { + ColorTransform::Legacy { brightness, contrast } + }; // ******************************** // Initialize wayland client @@ -145,8 +152,7 @@ fn run() -> anyhow::Result<()> { compositor, tx.clone(), Arc::clone(&waker) ), - brightness: cli.brightness.unwrap_or(0), - contrast: cli.contrast.unwrap_or(0.0), + color_transform, }; event_queue.roundtrip(&mut state).unwrap(); diff --git a/src/wayland.rs b/src/wayland.rs index afffdca..4fa8d20 100644 --- a/src/wayland.rs +++ b/src/wayland.rs @@ -276,10 +276,9 @@ logical size: {}x{}, transform: {:?}", &output_wallpaper_dir, &mut shm_slot_pool, pixel_format, - self.brightness, - self.contrast, width.try_into().unwrap(), - height.try_into().unwrap() + height.try_into().unwrap(), + self.color_transform, ) { Ok(workspace_bgs) => { debug!(