From d581b9e7033419ad6222aa1351757e220f0deb8e Mon Sep 17 00:00:00 2001 From: rexept Date: Fri, 25 Oct 2024 19:03:53 -0600 Subject: [PATCH 1/4] added resize-stretch feature --- client/src/cli.rs | 2 ++ client/src/imgproc.rs | 42 ++++++++++++++++++++++++++++++++++++++++++ client/src/main.rs | 3 +++ 3 files changed, 47 insertions(+) diff --git a/client/src/cli.rs b/client/src/cli.rs index 67b93b2..8cc8c58 100644 --- a/client/src/cli.rs +++ b/client/src/cli.rs @@ -212,6 +212,8 @@ pub enum ResizeStrategy { Crop, /// Resize the image to fit inside the screen, preserving the original aspect ratio Fit, + /// Resize the image to stretch across the screen + Stretch, } #[derive(Parser)] diff --git a/client/src/imgproc.rs b/client/src/imgproc.rs index 86ef875..f2082d7 100644 --- a/client/src/imgproc.rs +++ b/client/src/imgproc.rs @@ -205,6 +205,7 @@ pub fn compress_frames( ResizeStrategy::No => img_pad(&first_img, dim, color)?, ResizeStrategy::Crop => img_resize_crop(&first_img, dim, filter)?, ResizeStrategy::Fit => img_resize_fit(&first_img, dim, filter, color)?, + ResizeStrategy::Stretch => img_resize_stretch(&first_img, dim, filter)?, }; let mut canvas: Option> = None; @@ -217,6 +218,7 @@ pub fn compress_frames( ResizeStrategy::No => img_pad(&img, dim, color)?, ResizeStrategy::Crop => img_resize_crop(&img, dim, filter)?, ResizeStrategy::Fit => img_resize_fit(&img, dim, filter, color)?, + ResizeStrategy::Stretch => img_resize_crop(&img, dim, filter)?, }; if let Some(canvas) = canvas.as_ref() { @@ -382,6 +384,46 @@ pub fn img_resize_fit( } } +pub fn img_resize_stretch( + img: &Image, + dimensions: (u32, u32), + filter: FilterType, +) -> Result, String> { + let (width, height) = dimensions; + let resized_img = if (img.width, img.height) != (width, height) { + let pixel_type = if img.format.channels() == 3 { + PixelType::U8x3 + } else { + PixelType::U8x4 + }; + + let src = match fast_image_resize::images::ImageRef::new( + img.width, + img.height, + img.bytes.as_ref(), + pixel_type, + ) { + Ok(i) => i, + Err(e) => return Err(e.to_string()), + }; + + let mut dst = fast_image_resize::images::Image::new(width, height, pixel_type); + let mut resizer = Resizer::new(); + let options = ResizeOptions::new() + .resize_alg(ResizeAlg::Convolution(filter)); + + if let Err(e) = resizer.resize(&src, &mut dst, Some(&options)) { + return Err(e.to_string()); + } + + dst.into_vec().into_boxed_slice() + } else { + img.bytes.clone() + }; + + Ok(resized_img) +} + pub fn img_resize_crop( img: &Image, dimensions: (u32, u32), diff --git a/client/src/main.rs b/client/src/main.rs index a284cba..f4d7712 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -184,6 +184,9 @@ fn make_img_request( ResizeStrategy::Fit => { img_resize_fit(&img_raw, dim, make_filter(&img.filter), &img.fill_color)? } + ResizeStrategy::Stretch => { + img_resize_stretch(&img_raw, dim, make_filter(&img.filter))? + } }; img_req_builder.push( From 8c72051ccbb6126897e98984a6e1802484cee1f5 Mon Sep 17 00:00:00 2001 From: rexept Date: Fri, 25 Oct 2024 21:26:41 -0600 Subject: [PATCH 2/4] added to docs --- doc/swww-img.1.scd | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/doc/swww-img.1.scd b/doc/swww-img.1.scd index 8df9f35..c9e40eb 100644 --- a/doc/swww-img.1.scd +++ b/doc/swww-img.1.scd @@ -40,9 +40,10 @@ swww-img Whether to resize the image and the method by which to resize it. Possible values: - - _no_: Do not resize the image - - _crop_: Resize the image to fill the whole screen, cropping out parts that don't fit - - _fit_: Resize the image to fit inside the screen, preserving the original aspect ratio + - _no_: Do not resize the image + - _crop_: Resize the image to fill the whole screen, cropping out parts that don't fit + - _fit_: Resize the image to fit inside the screen, preserving the original aspect ratio + - _stretch_: Resize the image to fit inside the screen, without preserving the original aspect ratio Default is _crop_. From 3cdf15bae9249b938cca77ee6eb2ac203cbcc395 Mon Sep 17 00:00:00 2001 From: rexept Date: Fri, 25 Oct 2024 21:29:40 -0600 Subject: [PATCH 3/4] more concise definition --- client/src/cli.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/client/src/cli.rs b/client/src/cli.rs index 8cc8c58..22d97f7 100644 --- a/client/src/cli.rs +++ b/client/src/cli.rs @@ -212,7 +212,7 @@ pub enum ResizeStrategy { Crop, /// Resize the image to fit inside the screen, preserving the original aspect ratio Fit, - /// Resize the image to stretch across the screen + /// Resize the image to fit inside the screen, without preserving the original aspect ratio Stretch, } From 741ca7821ef8f437e56b8c341ce41d0aa5d96534 Mon Sep 17 00:00:00 2001 From: rexept Date: Sun, 27 Oct 2024 08:47:11 -0600 Subject: [PATCH 4/4] formatted + typo fix --- client/src/imgproc.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/client/src/imgproc.rs b/client/src/imgproc.rs index f2082d7..68c4333 100644 --- a/client/src/imgproc.rs +++ b/client/src/imgproc.rs @@ -218,7 +218,7 @@ pub fn compress_frames( ResizeStrategy::No => img_pad(&img, dim, color)?, ResizeStrategy::Crop => img_resize_crop(&img, dim, filter)?, ResizeStrategy::Fit => img_resize_fit(&img, dim, filter, color)?, - ResizeStrategy::Stretch => img_resize_crop(&img, dim, filter)?, + ResizeStrategy::Stretch => img_resize_stretch(&img, dim, filter)?, }; if let Some(canvas) = canvas.as_ref() { @@ -409,8 +409,7 @@ pub fn img_resize_stretch( let mut dst = fast_image_resize::images::Image::new(width, height, pixel_type); let mut resizer = Resizer::new(); - let options = ResizeOptions::new() - .resize_alg(ResizeAlg::Convolution(filter)); + let options = ResizeOptions::new().resize_alg(ResizeAlg::Convolution(filter)); if let Err(e) = resizer.resize(&src, &mut dst, Some(&options)) { return Err(e.to_string());