Skip to content
This repository was archived by the owner on Apr 24, 2023. It is now read-only.
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ exclude = ['.idea']
blend-srgb = ['dep:blend-srgb']
rayon = ['dep:rayon', 'dep:unique']
simd = ['imgref-iter/simd']
rgb = ['dep:rgb']
rgb_argb = ['rgb', 'rgb/argb']

[dependencies]
imgref = '^1.9.2'
imgref-iter = '~0.3.3'
blend-srgb = { version = '~0.1.1', optional = true }
rayon = { version = '^1.5.3', optional = true }
unique = { version = '~0.9.1', optional = true }
rgb = { version = '^0.8.33', optional = true }

[dev-dependencies]
stackblur = { git = 'https://github.com/LoganDark/stackblur', branch = 'larger-radius' }
Expand Down
58 changes: 30 additions & 28 deletions src/color/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use serial::StackBlurrableU32;
use simd::StackBlurrableU32xN;

#[repr(transparent)]
#[derive(Copy, Clone, Eq, PartialEq, Debug, Default)]
pub struct Argb<T: StackBlurrable>([T; 4]);
#[derive(Copy, Clone, Eq, PartialEq, Debug)]
pub struct Argb<T: StackBlurrable, const N: usize>(pub(crate) [T; N]);

impl Argb<StackBlurrableU32> {
impl Argb<StackBlurrableU32, 4> {
pub fn from_u32(argb: u32) -> Self {
let [a, r, g, b] = argb.to_be_bytes();
let cvt = |i| StackBlurrableU32(i as u32);
Expand All @@ -32,7 +32,7 @@ impl Argb<StackBlurrableU32> {

let [a, r, g, b] = argb.to_be_bytes();
let cvt = |i| StackBlurrableU32(srgb8_to_rgb12(i) as u32);
Self([cvt(a), cvt(r), cvt(g), cvt(b)])
Self([StackBlurrableU32(a as u32), cvt(r), cvt(g), cvt(b)])
}

#[cfg(feature = "blend-srgb")]
Expand All @@ -42,13 +42,13 @@ impl Argb<StackBlurrableU32> {
let [a, r, g, b] = self.0;
let [a, r, g, b] = [a.0 as u16, r.0 as u16, g.0 as u16, b.0 as u16];
let cvt = |i| rgb12_to_srgb8(i) as u8;
u32::from_be_bytes([cvt(a), cvt(r), cvt(g), cvt(b)])
u32::from_be_bytes([a as u8, cvt(r), cvt(g), cvt(b)])
}
}

#[allow(non_snake_case)]
#[cfg(feature = "simd")]
impl<const N: usize> Argb<StackBlurrableU32xN<N>> where simd::LaneCount<N>: simd::SupportedLaneCount {
impl<const N: usize> Argb<StackBlurrableU32xN<N>, 4> where simd::LaneCount<N>: simd::SupportedLaneCount {
pub fn from_u32xN(pixels: [u32; N]) -> Self {
let arrs: [[u8; 4]; N] = pixels.map(u32::to_be_bytes);
let a = simd::Simd::<u32, N>::from_array(arrs.map(|a| a[0] as u32));
Expand All @@ -73,7 +73,7 @@ impl<const N: usize> Argb<StackBlurrableU32xN<N>> where simd::LaneCount<N>: simd
pub fn from_u32xN_srgb(pixels: [u32; N]) -> Self {
use blend_srgb::convert::srgb8_to_rgb12;
let arrs: [[u8; 4]; N] = pixels.map(u32::to_be_bytes);
let a = simd::Simd::<u32, N>::from_array(arrs.map(|a| srgb8_to_rgb12(a[0]) as u32));
let a = simd::Simd::<u32, N>::from_array(arrs.map(|a| a[0] as u32));
let r = simd::Simd::<u32, N>::from_array(arrs.map(|a| srgb8_to_rgb12(a[1]) as u32));
let g = simd::Simd::<u32, N>::from_array(arrs.map(|a| srgb8_to_rgb12(a[2]) as u32));
let b = simd::Simd::<u32, N>::from_array(arrs.map(|a| srgb8_to_rgb12(a[3]) as u32));
Expand All @@ -90,7 +90,7 @@ impl<const N: usize> Argb<StackBlurrableU32xN<N>> where simd::LaneCount<N>: simd
[(); N].map(move |_| {
let i = countup.next().unwrap();
u32::from_be_bytes([
rgb12_to_srgb8(a[i] as u16),
a[i] as u8,
rgb12_to_srgb8(r[i] as u16),
rgb12_to_srgb8(g[i] as u16),
rgb12_to_srgb8(b[i] as u16)
Expand All @@ -99,7 +99,13 @@ impl<const N: usize> Argb<StackBlurrableU32xN<N>> where simd::LaneCount<N>: simd
}
}

impl<T: StackBlurrable> Add for Argb<T> {
impl<T: StackBlurrable, const N: usize> Default for Argb<T, N> {
fn default() -> Self {
Self([(); N].map(|_| T::default()))
}
}

impl<T: StackBlurrable, const N: usize> Add for Argb<T, N> {
type Output = Self;

fn add(mut self, rhs: Self) -> Self::Output {
Expand All @@ -108,7 +114,7 @@ impl<T: StackBlurrable> Add for Argb<T> {
}
}

impl<T: StackBlurrable> Sub for Argb<T> {
impl<T: StackBlurrable, const N: usize> Sub for Argb<T, N> {
type Output = Self;

fn sub(mut self, rhs: Self) -> Self::Output {
Expand All @@ -117,40 +123,36 @@ impl<T: StackBlurrable> Sub for Argb<T> {
}
}

impl<T: StackBlurrable> AddAssign for Argb<T> {
impl<T: StackBlurrable, const N: usize> AddAssign for Argb<T, N> {
fn add_assign(&mut self, rhs: Self) {
let [a, r, g, b] = rhs.0;
self.0[0] += a;
self.0[1] += r;
self.0[2] += g;
self.0[3] += b;
for (lhs, rhs) in self.0.iter_mut().zip(rhs.0.into_iter()) {
*lhs += rhs;
}
}
}

impl<T: StackBlurrable> SubAssign for Argb<T> {
impl<T: StackBlurrable, const N: usize> SubAssign for Argb<T, N> {
fn sub_assign(&mut self, rhs: Self) {
let [a, r, g, b] = rhs.0;
self.0[0] -= a;
self.0[1] -= r;
self.0[2] -= g;
self.0[3] -= b;
for (lhs, rhs) in self.0.iter_mut().zip(rhs.0.into_iter()) {
*lhs -= rhs;
}
}
}

impl<T: StackBlurrable> Mul<usize> for Argb<T> {
impl<T: StackBlurrable, const N: usize> Mul<usize> for Argb<T, N> {
type Output = Self;

fn mul(self, rhs: usize) -> Self::Output {
let [a, r, g, b] = self.0;
Self([a * rhs, r * rhs, g * rhs, b * rhs])
let mut iter = self.0.into_iter();
Self([(); N].map(|_| iter.next().unwrap() * rhs))
}
}

impl<T: StackBlurrable> Div<usize> for Argb<T> {
impl<T: StackBlurrable, const N: usize> Div<usize> for Argb<T, N> {
type Output = Self;

fn div(self, rhs: usize) -> Self::Output {
let [a, r, g, b] = self.0;
Self([a / rhs, r / rhs, g / rhs, b / rhs])
let mut iter = self.0.into_iter();
Self([(); N].map(|_| iter.next().unwrap() / rhs))
}
}
Loading