Split up impls for Image
This commit is contained in:
1 parent
a3743dd921
commit
0bd7af6c09
1 file changed
+12
-2
+12
-2
@@ -78,7 +78,7 @@ impl Extent {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<P: Pixel> Image<P> {
|
impl<P> Image<P> {
|
||||||
pub fn load<F: Format<P>>(path: &str) -> Result<Image<P>, Error> {
|
pub fn load<F: Format<P>>(path: &str) -> Result<Image<P>, Error> {
|
||||||
let file = std::fs::File::open(path)?;
|
let file = std::fs::File::open(path)?;
|
||||||
F::read(&mut std::io::BufReader::new(file))
|
F::read(&mut std::io::BufReader::new(file))
|
||||||
@@ -89,16 +89,26 @@ impl<P: Pixel> Image<P> {
|
|||||||
F::write(&mut std::io::BufWriter::new(file), self)
|
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<P: Clone + Default> Image<P> {
|
||||||
pub fn with_extent(extent: Extent) -> Self {
|
pub fn with_extent(extent: Extent) -> Self {
|
||||||
let mut pixels = Vec::with_capacity(extent.size());
|
let mut pixels = Vec::with_capacity(extent.size());
|
||||||
pixels.resize(extent.size(), P::default());
|
pixels.resize(extent.size(), P::default());
|
||||||
Self { extent, pixels }
|
Self { extent, pixels }
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<P: Copy> Image<P> {
|
||||||
pub fn at(&self, x: usize, y: usize) -> P {
|
pub fn at(&self, x: usize, y: usize) -> P {
|
||||||
self.pixels[y * self.extent.x + x]
|
self.pixels[y * self.extent.x + x]
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<P: Copy + Send + Sync> Image<P> {
|
||||||
pub fn for_each<F>(&mut self, f: F)
|
pub fn for_each<F>(&mut self, f: F)
|
||||||
where
|
where
|
||||||
F: Fn(usize, usize, &mut P) + Send + Sync,
|
F: Fn(usize, usize, &mut P) + Send + Sync,
|
||||||
@@ -118,7 +128,7 @@ impl<P: Pixel> Image<P> {
|
|||||||
pub fn map<F, Q>(&self, f: F) -> Image<Q>
|
pub fn map<F, Q>(&self, f: F) -> Image<Q>
|
||||||
where
|
where
|
||||||
F: Fn(P) -> Q + Send + Sync,
|
F: Fn(P) -> Q + Send + Sync,
|
||||||
Q: Pixel,
|
Q: Copy + Default + Send + Sync,
|
||||||
{
|
{
|
||||||
let mut img = Image::with_extent(self.extent);
|
let mut img = Image::with_extent(self.extent);
|
||||||
img.for_each(|x, y, p| *p = f(self.at(x, y)));
|
img.for_each(|x, y, p| *p = f(self.at(x, y)));
|
||||||
|
|||||||
Reference in new issue
Block a user