From b9f51bab09a8d9341ced3c606866c9ff1bda4f0a Mon Sep 17 00:00:00 2001 From: mitchellhansen Date: Thu, 6 Jun 2019 23:33:08 -0700 Subject: [PATCH] Blurs pixels. This is way to slow. Need to SIMD or maybe OpenCV --- src/main.rs | 43 ++++++++++++++++++++++++++++++------------- 1 file changed, 30 insertions(+), 13 deletions(-) diff --git a/src/main.rs b/src/main.rs index ec1dd37c..2c77bf4e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,14 +32,20 @@ fn surrounding_pixels(x: u32, y: u32, img: &DynamicImage) -> Vec if img.in_bounds(x+1, y+1) {pixels.push(img.get_pixel(x+1, y+1))} if img.in_bounds(x+1, y) {pixels.push(img.get_pixel(x+1, y))} - if img.in_bounds(x+1, y-1) {pixels.push(img.get_pixel(x+1, y-1))} + if img.in_bounds(x, y+1) {pixels.push(img.get_pixel(x, y+1))} - if img.in_bounds(x, y+1) {pixels.push(img.get_pixel(x, y+1))} - if img.in_bounds(x, y-1) {pixels.push(img.get_pixel(x, y-1))} + if x > 0 { + if img.in_bounds(x-1, y+1) {pixels.push(img.get_pixel(x-1, y+1))} + if img.in_bounds(x-1, y) {pixels.push(img.get_pixel(x-1, y))} + } - if img.in_bounds(x-1, y+1) {pixels.push(img.get_pixel(x-1, y+1))} - if img.in_bounds(x-1, y) {pixels.push(img.get_pixel(x-1, y))} - if img.in_bounds(x-1, y-1) {pixels.push(img.get_pixel(x-1, y-1))} + if y > 0 { + if img.in_bounds(x+1, y-1) {pixels.push(img.get_pixel(x+1, y-1))} + if img.in_bounds(x, y-1) {pixels.push(img.get_pixel(x, y-1))} + if x > 0 { + if img.in_bounds(x - 1, y - 1) { pixels.push(img.get_pixel(x - 1, y - 1)) } + } + } pixels } @@ -49,24 +55,35 @@ fn main() { let mut img = image::open("test.jpg").unwrap(); let xy = img.dimensions(); + println!("Starting"); for x in 0..xy.0 { for y in 0..xy.1 { let mut pixel = img.get_pixel(x, y); let v = surrounding_pixels(x, y, &img); -// let mut avg: Pixel; -// -// for p in v { -// avg += p; -// } + let mut avg = v.first().unwrap().clone(); + + for p in v { + let r: u16 = (avg.data[0] as u16 + p.data[0] as u16); + let g: u16 = (avg.data[1] as u16 + p.data[1] as u16); + let b: u16 = (avg.data[2] as u16 + p.data[2] as u16); + let a: u16 = (avg.data[3] as u16 + p.data[3] as u16); + avg.data[0] = (r/2) as u8; + avg.data[1] = (g/2) as u8; + avg.data[2] = (b/2) as u8; + avg.data[3] = (a/2) as u8; + } - pixel.data[0] = 1; + pixel.data[0] = avg.data[0]; + pixel.data[1] = avg.data[1]; + pixel.data[2] = avg.data[2]; + pixel.data[3] = avg.data[3]; img.put_pixel(x, y, pixel); } } - + println!("Ending"); img.save("fractal.png").unwrap(); let mut window = RenderWindow::new(