pub mod farbfeld; pub use farbfeld::*; #[derive(Clone, Copy, Debug, PartialEq)] pub struct SRGBA { pub r: f32, pub g: f32, pub b: f32, pub a: f32, } #[derive(Clone, Copy, Debug, Eq, PartialEq)] pub struct Extent { pub x: usize, pub y: usize, } #[derive(Debug)] pub struct Image

{ pub extent: Extent, pub pixels: Vec

, } #[derive(Debug)] pub enum Error { Invalid, Io(std::io::Error), } pub trait Format

{ fn read(input: &mut I) -> Result, Error> where I: std::io::Read; fn write(output: &mut O, img: &Image

) -> std::io::Result<()> where O: std::io::Write; } impl Extent { pub fn size(&self) -> usize { self.x * self.y } } impl SRGBA { pub fn new(r: f32, g: f32, b: f32, a: f32) -> Self { Self { r, g, b, a } } } pub trait Pixel: std::ops::AddAssign + std::ops::MulAssign + std::ops::DivAssign + std::ops::Mul + std::ops::Div + Clone + Copy + Default + Sized { } impl

Pixel for P where P: std::ops::AddAssign + std::ops::MulAssign + std::ops::DivAssign + std::ops::Mul + std::ops::Div + Clone + Copy + Default + Sized { } impl std::ops::AddAssign for SRGBA { fn add_assign(&mut self, other: SRGBA) { self.r += other.r; self.g += other.g; self.b += other.b; self.a += other.a; } } impl std::ops::MulAssign for SRGBA { fn mul_assign(&mut self, scalar: f32) { self.r *= scalar; self.g *= scalar; self.b *= scalar; self.a *= scalar; } } impl std::ops::DivAssign for SRGBA { fn div_assign(&mut self, scalar: f32) { self.r /= scalar; self.g /= scalar; self.b /= scalar; self.a /= scalar; } } impl std::ops::Mul for SRGBA { type Output = Self; fn mul(mut self, scalar: f32) -> Self { self *= scalar; self } } impl std::ops::Div for SRGBA { type Output = Self; fn div(mut self, scalar: f32) -> Self { self /= scalar; self } } impl std::default::Default for SRGBA { fn default() -> Self { Self { r: 0.0, g: 0.0, b: 0.0, a: 0.0, } } } impl From for Error { fn from(error: std::io::Error) -> Self { Error::Io(error) } } impl

Image

{ pub fn load>(path: &str) -> Result, Error> { let file = std::fs::File::open(path)?; F::read(&mut std::io::BufReader::new(file)) } pub fn save>(&self, path: &str) -> std::io::Result<()> { let file = std::fs::File::create(path)?; F::write(&mut std::io::BufWriter::new(file), self) } }