From f64365f5bd4991c49ccd934dda34348b5eda207e Mon Sep 17 00:00:00 2001 From: Vinzenz Schroeter Date: Fri, 28 Feb 2025 12:12:39 +0100 Subject: [PATCH] implement sharpen --- src/ledwand_dither.rs | 24 ++++++++++++++++++++++++ src/stream_window.rs | 10 ++++++++-- 2 files changed, 32 insertions(+), 2 deletions(-) diff --git a/src/ledwand_dither.rs b/src/ledwand_dither.rs index ce92b03..135515c 100644 --- a/src/ledwand_dither.rs +++ b/src/ledwand_dither.rs @@ -127,6 +127,13 @@ impl LedwandDither { Self::blur_inner_pixels(source, destination); } + pub fn sharpen(source: &GrayImage, destination: &mut GrayImage) { + assert_eq!(source.len(), destination.len()); + + Self::copy_border(source, destination); + Self::sharpen_inner_pixels(source, destination); + } + fn copy_border(source: &GrayImage, destination: &mut GrayImage) { let last_row = source.height() -1; for x in 0..source.width() { @@ -157,4 +164,21 @@ impl LedwandDither { } } } + + fn sharpen_inner_pixels(source: &GrayImage, destination: &mut GrayImage) { + for y in 1..source.height() - 2 { + for x in 1..source.width() - 2 { + let weighted_sum = -(source.get_pixel(x - 1, y - 1).0[0] as i32) + - source.get_pixel(x, y - 1).0[0] as i32 + - source.get_pixel(x + 1, y - 1).0[0] as i32 + - source.get_pixel(x - 1, y).0[0] as i32 + + 9 * source.get_pixel(x, y).0[0] as i32 + - source.get_pixel(x + 1, y).0[0] as i32 + - source.get_pixel(x - 1, y + 1).0[0] as i32 + - source.get_pixel(x, y + 1).0[0] as i32 + - source.get_pixel(x + 1, y + 1).0[0] as i32; + destination.get_pixel_mut(x, y).0[0] = weighted_sum.clamp(u8::MIN as i32, u8::MAX as i32) as u8; + } + } + } } diff --git a/src/stream_window.rs b/src/stream_window.rs index 5ab7c3d..0358c61 100644 --- a/src/stream_window.rs +++ b/src/stream_window.rs @@ -29,11 +29,17 @@ pub fn stream_window(connection: &Connection, options: StreamScreenOptions) { loop { let mut frame = get_next_frame(&capturer); + LedwandDither::histogram_correction(&mut frame); + + let mut orig = frame.clone(); + LedwandDither::blur(&orig, &mut frame); + + std::mem::swap(&mut frame, &mut orig); + LedwandDither::sharpen(&orig, &mut frame); + let cutoff = if options.no_dither { LedwandDither::median_brightness(&frame) } else { - LedwandDither::histogram_correction(&mut frame); - LedwandDither::blur(&frame.clone(), &mut frame); dither(&mut frame, &BiLevel); u8::MAX / 2 };