Erosion elephant foot fixer impl
This commit is contained in:
@@ -3,8 +3,9 @@ use nalgebra::Vector2;
|
||||
use crate::misc::Run;
|
||||
|
||||
/// A fast grayscale image buffer
|
||||
#[derive(Clone)]
|
||||
pub struct Image {
|
||||
size: Vector2<usize>,
|
||||
pub size: Vector2<usize>,
|
||||
data: Vec<u8>,
|
||||
idx: usize,
|
||||
}
|
||||
@@ -26,6 +27,12 @@ impl Image {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn from_decoder(width: usize, height: usize, decoder: impl Iterator<Item = Run>) -> Self {
|
||||
let mut image = Self::blank(width, height);
|
||||
decoder.for_each(|run| image.add_run(run.length as usize, run.value));
|
||||
image
|
||||
}
|
||||
|
||||
pub fn add_run(&mut self, length: usize, value: u8) {
|
||||
self.data[self.idx..self.idx + length].fill(value);
|
||||
self.idx += length;
|
||||
@@ -41,23 +48,6 @@ impl Image {
|
||||
self.data[idx] = val;
|
||||
}
|
||||
|
||||
pub fn blur(&mut self, sigma: f32) {
|
||||
let sigma = sigma as usize;
|
||||
for x in 0..self.size.x {
|
||||
for y in 0..self.size.y {
|
||||
let mut sum = 0;
|
||||
for xp in x.saturating_sub(sigma)..(x + sigma).min(self.size.x) {
|
||||
for yp in y.saturating_sub(sigma)..(y + sigma).min(self.size.y) {
|
||||
sum += self.get_pixel(xp, yp);
|
||||
}
|
||||
}
|
||||
|
||||
let avg = sum as f32 / (sigma * sigma) as f32;
|
||||
self.set_pixel(x, y, avg as u8);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn runs(&self) -> ImageRuns {
|
||||
ImageRuns {
|
||||
inner: &self.data,
|
||||
@@ -76,6 +66,25 @@ impl Image {
|
||||
}
|
||||
}
|
||||
|
||||
impl Image {
|
||||
pub fn blur(&mut self, sigma: f32) {
|
||||
let sigma = sigma as usize;
|
||||
for x in 0..self.size.x {
|
||||
for y in 0..self.size.y {
|
||||
let mut sum = 0;
|
||||
for xp in x.saturating_sub(sigma)..(x + sigma).min(self.size.x) {
|
||||
for yp in y.saturating_sub(sigma)..(y + sigma).min(self.size.y) {
|
||||
sum += self.get_pixel(xp, yp);
|
||||
}
|
||||
}
|
||||
|
||||
let avg = sum as f32 / (sigma * sigma) as f32;
|
||||
self.set_pixel(x, y, avg as u8);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for ImageRuns<'a> {
|
||||
type Item = Run;
|
||||
|
||||
|
Reference in New Issue
Block a user