Split up impls for Image

This commit is contained in:
cdo committed 2026-10-27 19:07:29 +00:00
1 parent a3743dd921
commit 0bd7af6c09
1 file changed
+12 -2
+12 -2
View File
@@ -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> {
let file = std::fs::File::open(path)?;
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)
}
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 {
let mut pixels = Vec::with_capacity(extent.size());
pixels.resize(extent.size(), P::default());
Self { extent, pixels }
}
}
impl<P: Copy> Image<P> {
pub fn at(&self, x: usize, y: usize) -> P {
self.pixels[y * self.extent.x + x]
}
}
impl<P: Copy + Send + Sync> Image<P> {
pub fn for_each<F>(&mut self, f: F)
where
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>
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)));