Skip to content

Commit 7cd6b57

Browse files
committedMar 10, 2018
Implement Pixel and friends for PreAlpha
1 parent 31568d9 commit 7cd6b57

File tree

1 file changed

+78
-24
lines changed

1 file changed

+78
-24
lines changed
 

‎src/blend/pre_alpha.rs

+78-24
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
use std::ops::{Add, Sub, Mul, Div, Deref, DerefMut};
1+
use std::ops::{Add, Deref, DerefMut, Div, Mul, Sub};
22
use approx::ApproxEq;
33
use num_traits::Float;
44

5-
use {Alpha, ComponentWise, Mix, Blend, clamp};
5+
use {clamp, Alpha, Blend, ComponentWise, Mix, Pixel};
6+
use encoding::pixel::RawPixel;
67

78
///Premultiplied alpha wrapper.
89
///
@@ -26,6 +27,7 @@ use {Alpha, ComponentWise, Mix, Blend, clamp};
2627
///Note that converting to and from premultiplied alpha will cause the alpha
2728
///component to be clamped to [0.0, 1.0].
2829
#[derive(Clone, Copy, PartialEq, Debug)]
30+
#[repr(C)]
2931
pub struct PreAlpha<C, T: Float> {
3032
///The premultiplied color components (`original.color * original.alpha`).
3133
pub color: C,
@@ -35,31 +37,35 @@ pub struct PreAlpha<C, T: Float> {
3537
pub alpha: T,
3638
}
3739

38-
impl<C, T> From<Alpha<C, T>> for PreAlpha<C, T> where
39-
C: ComponentWise<Scalar=T>,
40+
impl<C, T> From<Alpha<C, T>> for PreAlpha<C, T>
41+
where
42+
C: ComponentWise<Scalar = T>,
4043
T: Float,
4144
{
4245
fn from(color: Alpha<C, T>) -> PreAlpha<C, T> {
4346
let alpha = clamp(color.alpha, T::zero(), T::one());
4447

45-
PreAlpha{
48+
PreAlpha {
4649
color: color.color.component_wise_self(|a| a * alpha),
47-
alpha: alpha
50+
alpha: alpha,
4851
}
4952
}
5053
}
5154

52-
impl<C, T> From<PreAlpha<C, T>> for Alpha<C, T> where
53-
C: ComponentWise<Scalar=T>,
55+
impl<C, T> From<PreAlpha<C, T>> for Alpha<C, T>
56+
where
57+
C: ComponentWise<Scalar = T>,
5458
T: Float,
5559
{
5660
fn from(color: PreAlpha<C, T>) -> Alpha<C, T> {
5761
let alpha = clamp(color.alpha, T::zero(), T::one());
5862

59-
let color = color.color.component_wise_self(|a| if alpha.is_normal() {
60-
a / alpha
61-
} else {
62-
T::zero()
63+
let color = color.color.component_wise_self(|a| {
64+
if alpha.is_normal() {
65+
a / alpha
66+
} else {
67+
T::zero()
68+
}
6369
});
6470

6571
Alpha {
@@ -69,8 +75,9 @@ impl<C, T> From<PreAlpha<C, T>> for Alpha<C, T> where
6975
}
7076
}
7177

72-
impl<C, T> Blend for PreAlpha<C, T> where
73-
C: Blend<Color=C> + ComponentWise<Scalar=T>,
78+
impl<C, T> Blend for PreAlpha<C, T>
79+
where
80+
C: Blend<Color = C> + ComponentWise<Scalar = T>,
7481
T: Float,
7582
{
7683
type Color = C;
@@ -95,10 +102,14 @@ impl<C: Mix> Mix for PreAlpha<C, C::Scalar> {
95102
}
96103
}
97104

98-
impl<C: ComponentWise<Scalar=T>, T: Float> ComponentWise for PreAlpha<C, T> {
105+
impl<C: ComponentWise<Scalar = T>, T: Float> ComponentWise for PreAlpha<C, T> {
99106
type Scalar = T;
100107

101-
fn component_wise<F: FnMut(T, T) -> T>(&self, other: &PreAlpha<C, T>, mut f: F) -> PreAlpha<C, T> {
108+
fn component_wise<F: FnMut(T, T) -> T>(
109+
&self,
110+
other: &PreAlpha<C, T>,
111+
mut f: F,
112+
) -> PreAlpha<C, T> {
102113
PreAlpha {
103114
alpha: f(self.alpha, other.alpha),
104115
color: self.color.component_wise(&other.color, f),
@@ -111,8 +122,24 @@ impl<C: ComponentWise<Scalar=T>, T: Float> ComponentWise for PreAlpha<C, T> {
111122
color: self.color.component_wise_self(f),
112123
}
113124
}
114-
}impl<C, T> ApproxEq for PreAlpha<C, T> where
115-
C: ApproxEq<Epsilon=T::Epsilon>,
125+
}
126+
127+
unsafe impl<T: Float, C: Pixel<T>> Pixel<T> for PreAlpha<C, T> {
128+
const CHANNELS: usize = C::CHANNELS + 1;
129+
}
130+
131+
impl<C: Default, T: Float> Default for PreAlpha<C, T> {
132+
fn default() -> PreAlpha<C, T> {
133+
PreAlpha {
134+
color: C::default(),
135+
alpha: T::one(),
136+
}
137+
}
138+
}
139+
140+
impl<C, T> ApproxEq for PreAlpha<C, T>
141+
where
142+
C: ApproxEq<Epsilon = T::Epsilon>,
116143
T: ApproxEq + Float,
117144
T::Epsilon: Copy,
118145
{
@@ -130,14 +157,19 @@ impl<C: ComponentWise<Scalar=T>, T: Float> ComponentWise for PreAlpha<C, T> {
130157
T::default_max_ulps()
131158
}
132159

133-
fn relative_eq(&self, other: &PreAlpha<C, T>, epsilon: Self::Epsilon, max_relative: Self::Epsilon) -> bool {
134-
self.color.relative_eq(&other.color, epsilon, max_relative) &&
135-
self.alpha.relative_eq(&other.alpha, epsilon, max_relative)
160+
fn relative_eq(
161+
&self,
162+
other: &PreAlpha<C, T>,
163+
epsilon: Self::Epsilon,
164+
max_relative: Self::Epsilon,
165+
) -> bool {
166+
self.color.relative_eq(&other.color, epsilon, max_relative)
167+
&& self.alpha.relative_eq(&other.alpha, epsilon, max_relative)
136168
}
137169

138-
fn ulps_eq(&self, other: &PreAlpha<C, T>, epsilon: Self::Epsilon, max_ulps: u32) -> bool{
139-
self.color.ulps_eq(&other.color, epsilon, max_ulps) &&
140-
self.alpha.ulps_eq(&other.alpha, epsilon, max_ulps)
170+
fn ulps_eq(&self, other: &PreAlpha<C, T>, epsilon: Self::Epsilon, max_ulps: u32) -> bool {
171+
self.color.ulps_eq(&other.color, epsilon, max_ulps)
172+
&& self.alpha.ulps_eq(&other.alpha, epsilon, max_ulps)
141173
}
142174
}
143175

@@ -229,6 +261,28 @@ impl<T: Float, C: Div<T>> Div<T> for PreAlpha<C, T> {
229261
}
230262
}
231263

264+
impl<C, T, P> AsRef<P> for PreAlpha<C, T>
265+
where
266+
C: Pixel<T>,
267+
T: Float,
268+
P: RawPixel<T> + ?Sized,
269+
{
270+
fn as_ref(&self) -> &P {
271+
self.as_raw()
272+
}
273+
}
274+
275+
impl<C, T, P> AsMut<P> for PreAlpha<C, T>
276+
where
277+
C: Pixel<T>,
278+
T: Float,
279+
P: RawPixel<T> + ?Sized,
280+
{
281+
fn as_mut(&mut self) -> &mut P {
282+
self.as_raw_mut()
283+
}
284+
}
285+
232286
impl<C, T: Float> Deref for PreAlpha<C, T> {
233287
type Target = C;
234288

0 commit comments

Comments
 (0)
Please sign in to comment.