From 202f76b38b9bcb04505f4e4694a13cfe3ae8455d Mon Sep 17 00:00:00 2001 From: Sweetbread Date: Mon, 7 Apr 2025 21:32:42 +0300 Subject: [PATCH] fix --- src/main.rs | 86 +++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 71 insertions(+), 15 deletions(-) diff --git a/src/main.rs b/src/main.rs index 4c52839..5cfe6b2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -45,9 +45,14 @@ fn build_ui(app: &Application) { let image_data = Rc::new(RefCell::new(None::<(PathBuf, RgbImage)>)); fn parallel_brightness(image: &mut RgbImage, value: i16) { - image.par_iter_mut().for_each(|p| { - *p = p.saturating_add_signed(value.try_into().unwrap()); - }); + let value = value.clamp(-255, 255); // Ограничиваем диапазон + + for pixel in image.pixels_mut() { + for channel in 0..3 { + let new_val = pixel[channel] as i32 + value as i32; + pixel[channel] = new_val.clamp(0, 255) as u8; + } + } } fn gray_world(image: &mut RgbImage) { @@ -89,12 +94,9 @@ fn build_ui(app: &Application) { return; } - let mut min_r = 255; - let mut max_r = 0; - let mut min_g = 255; - let mut max_g = 0; - let mut min_b = 255; - let mut max_b = 0; + let (mut min_r, mut max_r) = (255u8, 0u8); + let (mut min_g, mut max_g) = (255u8, 0u8); + let (mut min_b, mut max_b) = (255u8, 0u8); for pixel in image.pixels() { min_r = min_r.min(pixel[0]); @@ -105,14 +107,28 @@ fn build_ui(app: &Application) { max_b = max_b.max(pixel[2]); } - if min_r == max_r && min_g == max_g && min_b == max_b { + if min_r == 0 && max_r == 255 && + min_g == 0 && max_g == 255 && + min_b == 0 && max_b == 255 { return; } - for pixel in image.pixels_mut() { - pixel[0] = ((pixel[0] - min_r) as f32 * 255.0 / (max_r - min_r) as f32).round() as u8; - pixel[1] = ((pixel[1] - min_g) as f32 * 255.0 / (max_g - min_g) as f32).round() as u8; - pixel[2] = ((pixel[2] - min_b) as f32 * 255.0 / (max_b - min_b) as f32).round() as u8; + for y in 0..height { + for x in 0..width { + let mut pixel = *image.get_pixel(x, y); + + if max_r != min_r { + pixel[0] = ((pixel[0] - min_r) as f32 * 255.0 / (max_r - min_r) as f32).clamp(0.0, 255.0) as u8; + } + if max_g != min_g { + pixel[1] = ((pixel[1] - min_g) as f32 * 255.0 / (max_g - min_g) as f32).clamp(0.0, 255.0) as u8; + } + if max_b != min_b { + pixel[2] = ((pixel[2] - min_b) as f32 * 255.0 / (max_b - min_b) as f32).clamp(0.0, 255.0) as u8; + } + + image.put_pixel(x, y, pixel); + } } } @@ -158,11 +174,51 @@ fn build_ui(app: &Application) { "Gray World" => gray_world(image), "Histogram Stretch" => histogram_stretch(image), "Emboss" => emboss(image), - "Blur" => *image = imageops::blur(image, 3.0), + "Blur" => blur(image), _ => {} } } + fn blur(image: &mut RgbImage) { + let (width, height) = image.dimensions(); + let mut buffer = image.clone(); + + let kernel = [ + [1.0, 4.0, 6.0, 4.0, 1.0], + [4.0, 16.0, 24.0, 16.0, 4.0], + [6.0, 24.0, 36.0, 24.0, 6.0], + [4.0, 16.0, 24.0, 16.0, 4.0], + [1.0, 4.0, 6.0, 4.0, 1.0] + ]; + let kernel_sum: f32 = 256.0; + + for y in 2..height-2 { + for x in 2..width-2 { + let mut r = 0.0; + let mut g = 0.0; + let mut b = 0.0; + + for ky in 0..5 { + for kx in 0..5 { + let px = image.get_pixel(x + kx - 2, y + ky - 2); + let weight = kernel[ky as usize][kx as usize]; + r += px[0] as f32 * weight; + g += px[1] as f32 * weight; + b += px[2] as f32 * weight; + } + } + + let r_val = (r / kernel_sum).clamp(0.0, 255.0) as u8; + let g_val = (g / kernel_sum).clamp(0.0, 255.0) as u8; + let b_val = (b / kernel_sum).clamp(0.0, 255.0) as u8; + + buffer.put_pixel(x, y, Rgb([r_val, g_val, b_val])); + } + } + + *image = buffer; + } + { let window = window.clone(); let image_data = image_data.clone();