fast image operations
Diffstat (limited to 'src/pixels/convert.rs')
-rw-r--r--src/pixels/convert.rs97
1 files changed, 97 insertions, 0 deletions
diff --git a/src/pixels/convert.rs b/src/pixels/convert.rs
new file mode 100644
index 0000000..55c9338
--- /dev/null
+++ b/src/pixels/convert.rs
@@ -0,0 +1,97 @@
+//! provides From's for pixels.
+
+use super::{Push, Trunc};
+
+/// Converts a pixel to another pixel.
+pub trait PFrom<const N: usize> {
+ /// Convert a pixel to this pixel.
+ fn pfrom(f: [u8; N]) -> Self;
+}
+
+impl<const N: usize> PFrom<N> for [u8; N] {
+ fn pfrom(f: [u8; N]) -> Self {
+ f
+ }
+}
+
+/// Y pixel
+pub type Y = [u8; 1];
+impl PFrom<2> for Y {
+ fn pfrom(f: YA) -> Self {
+ f.trunc()
+ }
+}
+
+impl PFrom<3> for Y {
+ fn pfrom([r, g, b]: RGB) -> Self {
+ [((2126 * r as u16 + 7152 * g as u16 + 722 * b as u16) / 10000) as u8]
+ }
+}
+
+impl PFrom<4> for Y {
+ fn pfrom(f: RGBA) -> Self {
+ PFrom::pfrom(f.trunc())
+ }
+}
+
+/// YA pixel
+pub type YA = [u8; 2];
+impl PFrom<1> for YA {
+ fn pfrom(f: Y) -> Self {
+ f.and(255)
+ }
+}
+
+impl PFrom<3> for YA {
+ fn pfrom(f: RGB) -> Self {
+ Y::pfrom(f).and(255)
+ }
+}
+
+impl PFrom<4> for YA {
+ fn pfrom(f: RGBA) -> Self {
+ Y::pfrom(f.trunc()).and(255)
+ }
+}
+
+/// RGB pixel
+pub type RGB = [u8; 3];
+
+impl PFrom<1> for RGB {
+ fn pfrom([y]: Y) -> Self {
+ [y; 3]
+ }
+}
+
+impl PFrom<2> for RGB {
+ fn pfrom([y, _]: YA) -> Self {
+ [y; 3]
+ }
+}
+
+impl PFrom<4> for RGB {
+ fn pfrom(f: RGBA) -> Self {
+ f.trunc()
+ }
+}
+
+/// RGBA pixel
+pub type RGBA = [u8; 4];
+
+impl PFrom<1> for RGBA {
+ fn pfrom([y]: Y) -> Self {
+ [y; 3].and(255)
+ }
+}
+
+impl PFrom<2> for RGBA {
+ fn pfrom([y, a]: YA) -> Self {
+ [y; 3].and(a)
+ }
+}
+
+impl PFrom<3> for RGBA {
+ fn pfrom(f: [u8; 3]) -> Self {
+ f.and(255)
+ }
+}