Refactor allowing multiple pixel formats
This commit is contained in:
1 parent
cef82a2e14
commit
6cd059f401
3 files changed
+138
-102
No files matched your search
+53
-27
@@ -3,7 +3,7 @@ pub mod farbfeld;
|
||||
pub use farbfeld::*;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
pub struct Pixel {
|
||||
pub struct SRGBA {
|
||||
pub r: f32,
|
||||
pub g: f32,
|
||||
pub b: f32,
|
||||
@@ -12,14 +12,14 @@ pub struct Pixel {
|
||||
|
||||
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
||||
pub struct Extent {
|
||||
pub x: u32,
|
||||
pub y: u32,
|
||||
pub x: usize,
|
||||
pub y: usize,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Image {
|
||||
pub struct Image<P> {
|
||||
pub extent: Extent,
|
||||
pub pixels: Vec<Pixel>,
|
||||
pub pixels: Vec<P>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
@@ -28,30 +28,56 @@ pub enum Error {
|
||||
Io(std::io::Error),
|
||||
}
|
||||
|
||||
pub trait Format {
|
||||
fn read<I>(input: &mut I) -> Result<Image, Error>
|
||||
pub trait Format<P> {
|
||||
fn read<I>(input: &mut I) -> Result<Image<P>, Error>
|
||||
where
|
||||
I: std::io::Read;
|
||||
|
||||
fn write<O>(output: &mut O, img: &Image) -> std::io::Result<()>
|
||||
fn write<O>(output: &mut O, img: &Image<P>) -> std::io::Result<()>
|
||||
where
|
||||
O: std::io::Write;
|
||||
}
|
||||
|
||||
impl Extent {
|
||||
pub fn size(&self) -> u32 {
|
||||
pub fn size(&self) -> usize {
|
||||
self.x * self.y
|
||||
}
|
||||
}
|
||||
|
||||
impl Pixel {
|
||||
pub fn rgba(r: f32, g: f32, b: f32, a: f32) -> Self {
|
||||
impl SRGBA {
|
||||
pub fn new(r: f32, g: f32, b: f32, a: f32) -> Self {
|
||||
Self { r, g, b, a }
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::AddAssign for Pixel {
|
||||
fn add_assign(&mut self, other: Pixel) {
|
||||
pub trait Pixel:
|
||||
std::ops::AddAssign
|
||||
+ std::ops::MulAssign<f32>
|
||||
+ std::ops::DivAssign<f32>
|
||||
+ std::ops::Mul<f32, Output = Self>
|
||||
+ std::ops::Div<f32, Output = Self>
|
||||
+ Clone
|
||||
+ Copy
|
||||
+ Default
|
||||
+ Sized
|
||||
{
|
||||
}
|
||||
|
||||
impl<P> Pixel for P where
|
||||
P: std::ops::AddAssign
|
||||
+ std::ops::MulAssign<f32>
|
||||
+ std::ops::DivAssign<f32>
|
||||
+ std::ops::Mul<f32, Output = Self>
|
||||
+ std::ops::Div<f32, Output = Self>
|
||||
+ 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;
|
||||
@@ -59,7 +85,7 @@ impl std::ops::AddAssign for Pixel {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::MulAssign<f32> for Pixel {
|
||||
impl std::ops::MulAssign<f32> for SRGBA {
|
||||
fn mul_assign(&mut self, scalar: f32) {
|
||||
self.r *= scalar;
|
||||
self.g *= scalar;
|
||||
@@ -68,7 +94,7 @@ impl std::ops::MulAssign<f32> for Pixel {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::DivAssign<f32> for Pixel {
|
||||
impl std::ops::DivAssign<f32> for SRGBA {
|
||||
fn div_assign(&mut self, scalar: f32) {
|
||||
self.r /= scalar;
|
||||
self.g /= scalar;
|
||||
@@ -77,7 +103,7 @@ impl std::ops::DivAssign<f32> for Pixel {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Mul<f32> for Pixel {
|
||||
impl std::ops::Mul<f32> for SRGBA {
|
||||
type Output = Self;
|
||||
|
||||
fn mul(mut self, scalar: f32) -> Self {
|
||||
@@ -86,7 +112,7 @@ impl std::ops::Mul<f32> for Pixel {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::Div<f32> for Pixel {
|
||||
impl std::ops::Div<f32> for SRGBA {
|
||||
type Output = Self;
|
||||
|
||||
fn div(mut self, scalar: f32) -> Self {
|
||||
@@ -95,7 +121,7 @@ impl std::ops::Div<f32> for Pixel {
|
||||
}
|
||||
}
|
||||
|
||||
impl std::default::Default for Pixel {
|
||||
impl std::default::Default for SRGBA {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
r: 0.0,
|
||||
@@ -112,14 +138,14 @@ impl From<std::io::Error> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl Image {
|
||||
pub fn load<F: Format>(path: &str) -> Result<Image, Error> {
|
||||
let file = std::fs::File::open(path)?;
|
||||
F::read(&mut std::io::BufReader::new(file))
|
||||
}
|
||||
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))
|
||||
}
|
||||
|
||||
pub fn save<F: Format>(&self, path: &str) -> std::io::Result<()> {
|
||||
let file = std::fs::File::create(path)?;
|
||||
F::write(&mut std::io::BufWriter::new(file), self)
|
||||
}
|
||||
pub fn save<F: Format<P>>(&self, path: &str) -> std::io::Result<()> {
|
||||
let file = std::fs::File::create(path)?;
|
||||
F::write(&mut std::io::BufWriter::new(file), self)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user