Skip to content

Instantly share code, notes, and snippets.

@jbclements
Created March 30, 2013 00:34
Show Gist options
  • Save jbclements/5274680 to your computer and use it in GitHub Desktop.
Save jbclements/5274680 to your computer and use it in GitHub Desktop.
Blur code in Rust. Written by Brian Anderson.
fn blur_rust(width: uint, height: uint, data: &[u8]) -> ~[u8] {
let filter = [[0.011, 0.084, 0.011],
[0.084, 0.620, 0.084],
[0.011, 0.084, 0.011]];
let mut newdata = ~[];
for uint::range(0, height) |y| {
for uint::range(0, width) |x| {
let mut new_value = 0.0;
for uint::range(0, filter.len()) |yy| {
for uint::range(0, filter.len()) |xx| {
let x_sample = x - (filter.len() - 1) / 2 + xx;
let y_sample = y - (filter.len() - 1) / 2 + yy;
let sample_value = data[width * (y_sample % height) + (x_sample % width)];
let sample_value = sample_value as float;
let weight = filter[yy][xx];
new_value += sample_value * weight;
}
}
newdata.push(new_value as u8);
}
}
return newdata;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment