gardesk/garlock / 1eace39

Browse files

Optimize blur with downsample-blur-upsample

Significantly faster blur processing by:
1. Downsampling image to 1/4 resolution
2. Applying blur at reduced size (16x fewer pixels)
3. Upsampling back to original resolution

Especially beneficial on ARM where image processing is slower.
Authored by mfwolffe <wolffemf@dukes.jmu.edu>
SHA
1eace39daff3946f2b7838776263d8f3bf86148f
Parents
4c3739e
Tree
f262d2e

1 changed file

StatusFile+-
M garlock/src/background.rs 45 5
garlock/src/background.rsmodified
@@ -7,6 +7,9 @@ use image::{imageops, RgbaImage};
77
 
88
 use crate::screenshot::{rgba_to_bgra, Screenshot};
99
 
10
+/// Downsample factor for fast blur (blur at 1/N resolution)
11
+const BLUR_DOWNSAMPLE_FACTOR: u32 = 4;
12
+
1013
 /// Processed background ready for display
1114
 pub struct Background {
1215
     /// Pixel data in BGRA format for X11
@@ -19,6 +22,11 @@ pub struct Background {
1922
 
2023
 impl Background {
2124
     /// Create a blurred background from a screenshot
25
+    ///
26
+    /// Uses downsample-blur-upsample for fast processing:
27
+    /// 1. Shrink image to 1/4 size
28
+    /// 2. Apply blur at reduced resolution (much faster)
29
+    /// 3. Scale back to original size
2230
     pub fn from_screenshot(
2331
         screenshot: Screenshot,
2432
         blur_radius: f32,
@@ -37,9 +45,41 @@ impl Background {
3745
         let img = RgbaImage::from_raw(screenshot.width, screenshot.height, rgba_data)
3846
             .context("Failed to create image from screenshot data")?;
3947
 
40
-        // Apply gaussian blur
41
-        tracing::debug!("Applying blur (radius={})", blur_radius);
42
-        let blurred = imageops::blur(&img, blur_radius);
48
+        let original_width = screenshot.width;
49
+        let original_height = screenshot.height;
50
+
51
+        // Downsample for faster blur
52
+        let small_width = original_width / BLUR_DOWNSAMPLE_FACTOR;
53
+        let small_height = original_height / BLUR_DOWNSAMPLE_FACTOR;
54
+
55
+        tracing::debug!(
56
+            original_width,
57
+            original_height,
58
+            small_width,
59
+            small_height,
60
+            "Downsampling for fast blur"
61
+        );
62
+
63
+        let small_img = imageops::resize(
64
+            &img,
65
+            small_width,
66
+            small_height,
67
+            imageops::FilterType::Triangle,
68
+        );
69
+
70
+        // Apply blur at reduced resolution (scale radius proportionally)
71
+        let scaled_radius = blur_radius / BLUR_DOWNSAMPLE_FACTOR as f32;
72
+        tracing::debug!(scaled_radius, "Applying blur at reduced resolution");
73
+        let blurred_small = imageops::blur(&small_img, scaled_radius);
74
+
75
+        // Upsample back to original size
76
+        tracing::debug!("Upsampling to original resolution");
77
+        let blurred = imageops::resize(
78
+            &blurred_small,
79
+            original_width,
80
+            original_height,
81
+            imageops::FilterType::Triangle,
82
+        );
4383
 
4484
         // Apply brightness adjustment
4585
         tracing::debug!("Applying brightness adjustment (factor={})", brightness);
@@ -51,8 +91,8 @@ impl Background {
5191
 
5292
         Ok(Self {
5393
             data: bgra_data,
54
-            width: screenshot.width,
55
-            height: screenshot.height,
94
+            width: original_width,
95
+            height: original_height,
5696
         })
5797
     }
5898