@@ -7,6 +7,9 @@ use image::{imageops, RgbaImage}; |
| 7 | 7 | |
| 8 | 8 | use crate::screenshot::{rgba_to_bgra, Screenshot}; |
| 9 | 9 | |
| 10 | +/// Downsample factor for fast blur (blur at 1/N resolution) |
| 11 | +const BLUR_DOWNSAMPLE_FACTOR: u32 = 4; |
| 12 | + |
| 10 | 13 | /// Processed background ready for display |
| 11 | 14 | pub struct Background { |
| 12 | 15 | /// Pixel data in BGRA format for X11 |
@@ -19,6 +22,11 @@ pub struct Background { |
| 19 | 22 | |
| 20 | 23 | impl Background { |
| 21 | 24 | /// 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 |
| 22 | 30 | pub fn from_screenshot( |
| 23 | 31 | screenshot: Screenshot, |
| 24 | 32 | blur_radius: f32, |
@@ -37,9 +45,41 @@ impl Background { |
| 37 | 45 | let img = RgbaImage::from_raw(screenshot.width, screenshot.height, rgba_data) |
| 38 | 46 | .context("Failed to create image from screenshot data")?; |
| 39 | 47 | |
| 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 | + ); |
| 43 | 83 | |
| 44 | 84 | // Apply brightness adjustment |
| 45 | 85 | tracing::debug!("Applying brightness adjustment (factor={})", brightness); |
@@ -51,8 +91,8 @@ impl Background { |
| 51 | 91 | |
| 52 | 92 | Ok(Self { |
| 53 | 93 | data: bgra_data, |
| 54 | | - width: screenshot.width, |
| 55 | | - height: screenshot.height, |
| 94 | + width: original_width, |
| 95 | + height: original_height, |
| 56 | 96 | }) |
| 57 | 97 | } |
| 58 | 98 | |