summaryrefslogtreecommitdiff
path: root/src/image.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/image.rs')
-rw-r--r--src/image.rs63
1 files changed, 58 insertions, 5 deletions
diff --git a/src/image.rs b/src/image.rs
index ab5dc99..9b42b27 100644
--- a/src/image.rs
+++ b/src/image.rs
@@ -2,18 +2,31 @@ use std::ops;
use crate::{reader::FileReader, writer::FileWriter};
+type FullColorDepth = u32;
pub trait ColorDepth: Clone
{
const DEFAULT: Self;
+ fn from_full_color(x: FullColorDepth) -> Self;
+ fn to_full_color(&self) -> FullColorDepth;
+ fn convert<T: ColorDepth>(&self) -> T
+ {
+ T::from_full_color(self.to_full_color())
+ }
}
impl ColorDepth for u8 {
const DEFAULT: Self = 0;
+ fn from_full_color(x: FullColorDepth) -> Self {(x>>24) as Self}
+ fn to_full_color(&self) -> FullColorDepth { (*self as FullColorDepth) << 24 }
}
impl ColorDepth for u16 {
const DEFAULT: Self = 0;
+ fn from_full_color(x: FullColorDepth) -> Self {(x>>16) as Self}
+ fn to_full_color(&self) -> FullColorDepth { (*self as FullColorDepth) << 16 }
}
impl ColorDepth for u32 {
const DEFAULT: Self = 0;
+ fn from_full_color(x: FullColorDepth) -> Self {x}
+ fn to_full_color(&self) -> FullColorDepth { *self }
}
#[derive(Debug, Clone, Copy)]
@@ -32,17 +45,57 @@ pub struct RGB<T: ColorDepth>
pub b: T,
}
+type FullColorPixel = RGBA<FullColorDepth>;
pub trait ColorType: Clone
{
const DEFAULT: Self;
+ fn from_full_color(x: FullColorPixel) -> Self;
+ fn to_full_color(&self) -> FullColorPixel;
+ fn convert<T: ColorType>(&self) -> T
+ {
+ T::from_full_color(self.to_full_color())
+ }
}
impl<T: ColorDepth> ColorType for RGBA<T>
{
const DEFAULT: Self = Self { r: T::DEFAULT, g: T::DEFAULT, b: T::DEFAULT, a: T::DEFAULT };
+ fn from_full_color(x: FullColorPixel) -> Self {
+ Self
+ {
+ r: T::from_full_color(x.r),
+ g: T::from_full_color(x.g),
+ b: T::from_full_color(x.b),
+ a: T::from_full_color(x.a),
+ }
+ }
+ fn to_full_color(&self) -> FullColorPixel {
+ RGBA {
+ r: T::to_full_color(&self.r),
+ g: T::to_full_color(&self.g),
+ b: T::to_full_color(&self.b),
+ a: T::to_full_color(&self.a),
+ }
+ }
}
impl<T: ColorDepth> ColorType for RGB<T>
{
const DEFAULT: Self = Self { r: T::DEFAULT, g: T::DEFAULT, b: T::DEFAULT};
+ fn from_full_color(x: FullColorPixel) -> Self {
+ Self
+ {
+ r: T::from_full_color(x.r),
+ g: T::from_full_color(x.g),
+ b: T::from_full_color(x.b)
+ }
+ }
+ fn to_full_color(&self) -> FullColorPixel {
+ RGBA {
+ r: T::to_full_color(&self.r),
+ g: T::to_full_color(&self.g),
+ b: T::to_full_color(&self.b),
+ a: u32::max_value()
+ }
+ }
}
#[derive(Debug)]
@@ -76,14 +129,14 @@ impl<T: ColorType> ops::IndexMut<(usize, usize)> for PixelArr<T>
}
#[derive(Debug)]
-pub struct ImageBase
+pub struct ImageBase<T: ColorType>
{
pub bit_depth: u8,
- pub pixels: PixelArr<RGB<u8>>
+ pub pixels: PixelArr<T>
}
-pub trait Image
+pub trait Image<T: ColorType>
{
- fn read_image(reader: &mut FileReader) -> Result<ImageBase, &'static str>;
- fn write_image(image: &ImageBase, writer: &mut FileWriter)-> ();
+ fn read_image(reader: &mut FileReader) -> Result<ImageBase<T>, &'static str>;
+ fn write_image(image: &ImageBase<T>, writer: &mut FileWriter)-> ();
}