Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add stretch to resize options #373

Merged
merged 4 commits into from
Oct 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions client/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 fit inside the screen, without preserving the original aspect ratio
Stretch,
}

#[derive(Parser)]
Expand Down
41 changes: 41 additions & 0 deletions client/src/imgproc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Box<[u8]>> = None;
Expand All @@ -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_stretch(&img, dim, filter)?,
};

if let Some(canvas) = canvas.as_ref() {
Expand Down Expand Up @@ -382,6 +384,45 @@ pub fn img_resize_fit(
}
}

pub fn img_resize_stretch(
img: &Image,
dimensions: (u32, u32),
filter: FilterType,
) -> Result<Box<[u8]>, 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),
Expand Down
3 changes: 3 additions & 0 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 4 additions & 3 deletions doc/swww-img.1.scd
Original file line number Diff line number Diff line change
Expand Up @@ -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_.

Expand Down
Loading