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

Performance Tuning #65

Merged
merged 1 commit into from
Oct 6, 2023
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
45 changes: 17 additions & 28 deletions libwayshot/src/image_util.rs
Original file line number Diff line number Diff line change
@@ -1,47 +1,36 @@
use image::{GenericImageView, ImageBuffer, Pixel};
use image::RgbaImage;
use wayland_client::protocol::wl_output::Transform;

pub(crate) fn rotate_image_buffer<T: GenericImageView>(
image: &T,
pub(crate) fn rotate_image_buffer(
image: RgbaImage,
transform: Transform,
width: u32,
height: u32,
) -> ImageBuffer<T::Pixel, Vec<<T::Pixel as Pixel>::Subpixel>>
where
T::Pixel: 'static,
<T::Pixel as Pixel>::Subpixel: 'static,
{
let final_buffer: ImageBuffer<
<T as GenericImageView>::Pixel,
Vec<<<T as GenericImageView>::Pixel as Pixel>::Subpixel>,
> = match transform {
Transform::_90 => image::imageops::rotate90(image),
Transform::_180 => image::imageops::rotate180(image),
Transform::_270 => image::imageops::rotate270(image),
Transform::Flipped => image::imageops::flip_horizontal(image),
) -> RgbaImage {
let final_buffer = match transform {
Transform::_90 => image::imageops::rotate90(&image),
Transform::_180 => image::imageops::rotate180(&image),
Transform::_270 => image::imageops::rotate270(&image),
Transform::Flipped => image::imageops::flip_horizontal(&image),
Transform::Flipped90 => {
let flipped_buffer = image::imageops::flip_horizontal(image);
let flipped_buffer = image::imageops::flip_horizontal(&image);
image::imageops::rotate90(&flipped_buffer)
}
Transform::Flipped180 => {
let flipped_buffer = image::imageops::flip_horizontal(image);
let flipped_buffer = image::imageops::flip_horizontal(&image);
image::imageops::rotate180(&flipped_buffer)
}
Transform::Flipped270 => {
let flipped_buffer = image::imageops::flip_horizontal(image);
let flipped_buffer = image::imageops::flip_horizontal(&image);
image::imageops::rotate270(&flipped_buffer)
}
_ => {
let final_buffer = image::imageops::resize(
image,
width,
height,
image::imageops::FilterType::Gaussian,
);
return final_buffer;
}
_ => image,
};

if final_buffer.dimensions() == (width, height) {
return final_buffer;
}

image::imageops::resize(
&final_buffer,
width,
Expand Down
13 changes: 6 additions & 7 deletions libwayshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use std::{
sync::atomic::{AtomicBool, Ordering},
};

use image::{imageops::overlay, DynamicImage, RgbaImage};
use image::{imageops::overlay, RgbaImage};
use memmap2::MmapMut;
use wayland_client::{
globals::{registry_queue_init, GlobalList},
Expand Down Expand Up @@ -414,9 +414,9 @@ impl WayshotConnection {
let (width, height) = frame_copy.1.unwrap();
let frame_copy = &frame_copy.0[0];

let image: DynamicImage = frame_copy.try_into()?;
let image = frame_copy.try_into()?;
composited_image = image_util::rotate_image_buffer(
&image,
image,
frame_copy.transform,
width as u32,
height as u32,
Expand All @@ -426,9 +426,9 @@ impl WayshotConnection {
let (frame_copy, region) = frame_copy;
let (width, height) = region.unwrap();
for frame_copy in frame_copy {
let image: DynamicImage = (&frame_copy).try_into()?;
let image = (&frame_copy).try_into()?;
let image = image_util::rotate_image_buffer(
&image,
image,
frame_copy.transform,
width as u32,
height as u32,
Expand Down Expand Up @@ -456,8 +456,7 @@ impl WayshotConnection {
output_info.transform,
None,
)?;
let dynamic_image: DynamicImage = (&frame_copy).try_into()?;
Ok(dynamic_image.into_rgba8())
Ok((&frame_copy).try_into()?)
}

/// Take a screenshot from all of the specified outputs.
Expand Down
15 changes: 5 additions & 10 deletions libwayshot/src/screencopy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::{
time::{SystemTime, UNIX_EPOCH},
};

use image::{ColorType, DynamicImage, ImageBuffer, Pixel};
use image::{ColorType, ImageBuffer, Pixel, RgbaImage};
use memmap2::MmapMut;
use nix::{
fcntl,
Expand Down Expand Up @@ -46,19 +46,14 @@ pub struct FrameCopy {
pub transform: wl_output::Transform,
}

impl TryFrom<&FrameCopy> for DynamicImage {
impl TryFrom<&FrameCopy> for RgbaImage {
type Error = Error;

fn try_from(value: &FrameCopy) -> Result<Self> {
Ok(match value.frame_color_type {
ColorType::Rgb8 => DynamicImage::ImageRgb8(create_image_buffer(
&value.frame_format,
&value.frame_mmap,
)?),
ColorType::Rgba8 => DynamicImage::ImageRgba8(create_image_buffer(
&value.frame_format,
&value.frame_mmap,
)?),
ColorType::Rgb8 | ColorType::Rgba8 => {
create_image_buffer(&value.frame_format, &value.frame_mmap)?.into()
}
_ => return Err(Error::InvalidColor),
})
}
Expand Down
3 changes: 2 additions & 1 deletion wayshot/src/wayshot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ where
.with_prompt("Choose Screen")
.default(0)
.items(ouputs)
.interact() else {
.interact()
else {
return None;
};
Some(selection)
Expand Down
Loading