diff --git a/src/image.rs b/src/image.rs index d4ac604..f6fbd42 100644 --- a/src/image.rs +++ b/src/image.rs @@ -78,7 +78,7 @@ impl Extent { } } -impl Image

{ +impl

Image

{ pub fn load>(path: &str) -> Result, Error> { let file = std::fs::File::open(path)?; F::read(&mut std::io::BufReader::new(file)) @@ -89,16 +89,26 @@ impl Image

{ F::write(&mut std::io::BufWriter::new(file), self) } + pub fn at_mut(&mut self, x: usize, y: usize) -> &mut P { + &mut self.pixels[y * self.extent.x + x] + } +} + +impl Image

{ pub fn with_extent(extent: Extent) -> Self { let mut pixels = Vec::with_capacity(extent.size()); pixels.resize(extent.size(), P::default()); Self { extent, pixels } } +} +impl Image

{ pub fn at(&self, x: usize, y: usize) -> P { self.pixels[y * self.extent.x + x] } +} +impl Image

{ pub fn for_each(&mut self, f: F) where F: Fn(usize, usize, &mut P) + Send + Sync, @@ -118,7 +128,7 @@ impl Image

{ pub fn map(&self, f: F) -> Image where F: Fn(P) -> Q + Send + Sync, - Q: Pixel, + Q: Copy + Default + Send + Sync, { let mut img = Image::with_extent(self.extent); img.for_each(|x, y, p| *p = f(self.at(x, y)));