Skip to content

Commit

Permalink
fix: don't coerce images to rgba
Browse files Browse the repository at this point in the history
  • Loading branch information
vivienm committed Jan 8, 2024
1 parent 3eead85 commit 31cdddb
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 21 deletions.
29 changes: 15 additions & 14 deletions libwayshot/src/image_util.rs
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
use image::{DynamicImage, RgbaImage};
use image::{DynamicImage, GenericImageView};
use wayland_client::protocol::wl_output::Transform;

pub(crate) fn rotate_image_buffer(
image: DynamicImage,
transform: Transform,
width: u32,
height: u32,
) -> 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),
) -> DynamicImage {
let final_image = match transform {
Transform::_90 => image::imageops::rotate90(&image).into(),
Transform::_180 => image::imageops::rotate180(&image).into(),
Transform::_270 => image::imageops::rotate270(&image).into(),
Transform::Flipped => image::imageops::flip_horizontal(&image).into(),
Transform::Flipped90 => {
let flipped_buffer = image::imageops::flip_horizontal(&image);
image::imageops::rotate90(&flipped_buffer)
image::imageops::rotate90(&flipped_buffer).into()
}
Transform::Flipped180 => {
let flipped_buffer = image::imageops::flip_horizontal(&image);
image::imageops::rotate180(&flipped_buffer)
image::imageops::rotate180(&flipped_buffer).into()
}
Transform::Flipped270 => {
let flipped_buffer = image::imageops::flip_horizontal(&image);
image::imageops::rotate270(&flipped_buffer)
image::imageops::rotate270(&flipped_buffer).into()
}
_ => image.into_rgba8(),
_ => image,
};

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

image::imageops::resize(
&final_buffer,
&final_image,
width,
height,
image::imageops::FilterType::Gaussian,
)
.into()
}
13 changes: 6 additions & 7 deletions libwayshot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use std::{
thread,
};

use image::{imageops::overlay, DynamicImage, RgbaImage};
use image::{imageops::overlay, DynamicImage};
use memmap2::MmapMut;
use wayland_client::{
globals::{registry_queue_init, GlobalList},
Expand Down Expand Up @@ -444,7 +444,7 @@ impl WayshotConnection {
&self,
capture_region: CaptureRegion,
cursor_overlay: bool,
) -> Result<RgbaImage> {
) -> Result<DynamicImage> {
let (frame_copies, (width, height)) =
self.create_frame_copy(capture_region, cursor_overlay)?;

Expand Down Expand Up @@ -500,23 +500,22 @@ impl WayshotConnection {
&self,
output_info: &OutputInfo,
cursor_overlay: bool,
) -> Result<RgbaImage> {
) -> Result<DynamicImage> {
let frame_copy = self.capture_output_frame(
cursor_overlay,
&output_info.wl_output,
output_info.transform,
None,
)?;
let image = DynamicImage::try_from(frame_copy)?;
Ok(image.into_rgba8())
frame_copy.try_into()
}

/// Take a screenshot from all of the specified outputs.
pub fn screenshot_outputs(
&self,
outputs: &Vec<OutputInfo>,
cursor_overlay: bool,
) -> Result<RgbaImage> {
) -> Result<DynamicImage> {
if outputs.is_empty() {
return Err(Error::NoOutputs);
}
Expand Down Expand Up @@ -551,7 +550,7 @@ impl WayshotConnection {
}

/// Take a screenshot from all accessible outputs.
pub fn screenshot_all(&self, cursor_overlay: bool) -> Result<RgbaImage> {
pub fn screenshot_all(&self, cursor_overlay: bool) -> Result<DynamicImage> {
self.screenshot_outputs(self.get_all_outputs(), cursor_overlay)
}
}

0 comments on commit 31cdddb

Please sign in to comment.