From 79d901c8e2fe3fe3870dc89422f8633634e7eb39 Mon Sep 17 00:00:00 2001 From: Shinyzenith Date: Thu, 12 Oct 2023 17:43:05 +0530 Subject: [PATCH] refactor(image_composition): image::imageops::{overlay -> replace} MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Overlay takes into account the underlying pixel and the overlayed pixel to interpolate the final pixel data. Eg: This takes effect for transparent regions in PNG images. * Replace performs the same operation for us but instead it does not perform the pixel interpolation and instead just replaces the pixel. * Test performed on 8-core i5 machine with only 1 `wl_output`: --- ``` Benchmark 1: ./wayshot-overlay --stdout > /dev/null Time (mean ± σ): 81.6 ms ± 14.1 ms [User: 25.1 ms, System: 17.1 ms] Range (min … max): 59.0 ms … 109.8 ms 41 runs Benchmark 2: ./wayshot-replace --stdout > /dev/null Time (mean ± σ): 75.8 ms ± 14.6 ms [User: 24.7 ms, System: 15.0 ms] Range (min … max): 58.2 ms … 105.3 ms 30 runs Summary './wayshot-replace --stdout > /dev/null' ran 1.08 ± 0.28 times faster than './wayshot-overlay --stdout > /dev/null' ``` Signed-off-by: Shinyzenith --- libwayshot/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/libwayshot/src/lib.rs b/libwayshot/src/lib.rs index 67c29a38..b751aaed 100644 --- a/libwayshot/src/lib.rs +++ b/libwayshot/src/lib.rs @@ -19,7 +19,7 @@ use std::{ thread, }; -use image::{imageops::overlay, RgbaImage}; +use image::{imageops::replace, RgbaImage}; use memmap2::MmapMut; use wayland_client::{ globals::{registry_queue_init, GlobalList}, @@ -474,7 +474,7 @@ impl WayshotConnection { if let Some(overlayed_image_or_error) = possible_overlayed_image_or_error { if let Ok(mut overlayed_image) = overlayed_image_or_error { if let Ok(image) = image { - overlay(&mut overlayed_image, &image, 0, 0); + replace(&mut overlayed_image, &image, 0, 0); Some(Ok(overlayed_image)) } else { Some(image)