Blurs pixels. This is way to slow. Need to SIMD or maybe OpenCV

master
mitchellhansen 6 years ago
parent f844982553
commit b9f51bab09

@ -32,14 +32,20 @@ fn surrounding_pixels(x: u32, y: u32, img: &DynamicImage) -> Vec<image::Rgba<u8>
if img.in_bounds(x+1, y+1) {pixels.push(img.get_pixel(x+1, y+1))} 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) {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 x > 0 {
if img.in_bounds(x, y-1) {pixels.push(img.get_pixel(x, y-1))} 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) {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-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 pixels
} }
@ -49,24 +55,35 @@ fn main() {
let mut img = image::open("test.jpg").unwrap(); let mut img = image::open("test.jpg").unwrap();
let xy = img.dimensions(); let xy = img.dimensions();
println!("Starting");
for x in 0..xy.0 { for x in 0..xy.0 {
for y in 0..xy.1 { for y in 0..xy.1 {
let mut pixel = img.get_pixel(x, y); let mut pixel = img.get_pixel(x, y);
let v = surrounding_pixels(x, y, &img); let v = surrounding_pixels(x, y, &img);
// let mut avg: Pixel; let mut avg = v.first().unwrap().clone();
//
// for p in v { for p in v {
// avg += p; 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); img.put_pixel(x, y, pixel);
} }
} }
println!("Ending");
img.save("fractal.png").unwrap(); img.save("fractal.png").unwrap();
let mut window = RenderWindow::new( let mut window = RenderWindow::new(

Loading…
Cancel
Save