This commit is contained in:
Sweetbread 2025-04-07 21:32:42 +03:00
parent c7441349a1
commit 202f76b38b

View File

@ -45,9 +45,14 @@ fn build_ui(app: &Application) {
let image_data = Rc::new(RefCell::new(None::<(PathBuf, RgbImage)>)); let image_data = Rc::new(RefCell::new(None::<(PathBuf, RgbImage)>));
fn parallel_brightness(image: &mut RgbImage, value: i16) { fn parallel_brightness(image: &mut RgbImage, value: i16) {
image.par_iter_mut().for_each(|p| { let value = value.clamp(-255, 255); // Ограничиваем диапазон
*p = p.saturating_add_signed(value.try_into().unwrap());
}); 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) { fn gray_world(image: &mut RgbImage) {
@ -89,12 +94,9 @@ fn build_ui(app: &Application) {
return; return;
} }
let mut min_r = 255; let (mut min_r, mut max_r) = (255u8, 0u8);
let mut max_r = 0; let (mut min_g, mut max_g) = (255u8, 0u8);
let mut min_g = 255; let (mut min_b, mut max_b) = (255u8, 0u8);
let mut max_g = 0;
let mut min_b = 255;
let mut max_b = 0;
for pixel in image.pixels() { for pixel in image.pixels() {
min_r = min_r.min(pixel[0]); min_r = min_r.min(pixel[0]);
@ -105,14 +107,28 @@ fn build_ui(app: &Application) {
max_b = max_b.max(pixel[2]); 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; return;
} }
for pixel in image.pixels_mut() { for y in 0..height {
pixel[0] = ((pixel[0] - min_r) as f32 * 255.0 / (max_r - min_r) as f32).round() as u8; for x in 0..width {
pixel[1] = ((pixel[1] - min_g) as f32 * 255.0 / (max_g - min_g) as f32).round() as u8; let mut pixel = *image.get_pixel(x, y);
pixel[2] = ((pixel[2] - min_b) as f32 * 255.0 / (max_b - min_b) as f32).round() as u8;
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), "Gray World" => gray_world(image),
"Histogram Stretch" => histogram_stretch(image), "Histogram Stretch" => histogram_stretch(image),
"Emboss" => emboss(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 window = window.clone();
let image_data = image_data.clone(); let image_data = image_data.clone();