Skip to content

Commit 206a9a7

Browse files
committed
Use custom traits instead of Into<f32>
1 parent e96880b commit 206a9a7

File tree

1 file changed

+45
-12
lines changed

1 file changed

+45
-12
lines changed

src/lib.rs

+45-12
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,38 @@ fn spowf(n: f32, power: f32) -> f32 {
1717
n.abs().powf(power).copysign(n)
1818
}
1919

20-
trait DT:
20+
// DT {{{
21+
22+
trait FromF32: Sized {
23+
fn from_float(float: f32) -> Self;
24+
}
25+
26+
impl FromF32 for f32 {
27+
fn from_float(float: f32) -> Self {
28+
float
29+
}
30+
}
31+
32+
impl FromF32 for f64 {
33+
fn from_float(float: f32) -> Self {
34+
float.into()
35+
}
36+
}
37+
38+
trait ToDType<T>: Sized {
39+
fn to_dt(self) -> T;
40+
}
41+
42+
impl<U> ToDType<U> for f32
43+
where
44+
U: FromF32 + Sized,
45+
{
46+
fn to_dt(self) -> U {
47+
FromF32::from_float(self)
48+
}
49+
}
50+
51+
trait DType:
2152
Sized
2253
+ Copy
2354
+ Add<Output = Self>
@@ -26,7 +57,7 @@ trait DT:
2657
+ Sub<Output = Self>
2758
+ Rem<Output = Self>
2859
+ PartialOrd
29-
+ From<f32>
60+
+ FromF32
3061
{
3162
fn _fma(self, mul: Self, add: Self) -> Self;
3263
fn powf(self, rhs: Self) -> Self;
@@ -43,7 +74,7 @@ trait DT:
4374

4475
macro_rules! impl_float {
4576
($type:ident) => {
46-
impl DT for $type {
77+
impl DType for $type {
4778
fn _fma(self, mul: Self, add: Self) -> Self {
4879
self.mul_add(mul, add)
4980
}
@@ -57,6 +88,8 @@ macro_rules! impl_float {
5788
impl_float!(f32);
5889
impl_float!(f64);
5990

91+
// }}}
92+
6093
/// Create an array of separate channel buffers from a single interwoven buffer.
6194
/// Copies the data.
6295
pub fn unweave<const N: usize>(slice: &[f32]) -> [Box<[f32]>; N] {
@@ -222,11 +255,11 @@ fn matmul3t(pixel: [f32; 3], matrix: [[f32; 3]; 3]) -> [f32; 3] {
222255
}
223256

224257
/// Transposed 3 * 3x3 matrix multiply, ie matrix @ pixel
225-
fn matmul3<T: DT>(m: [[f32; 3]; 3], p: [T; 3]) -> [T; 3] {
258+
fn matmul3<T: DType>(m: [[f32; 3]; 3], p: [T; 3]) -> [T; 3] {
226259
[
227-
p[0].fma(m[0][0].into(), p[1].fma(m[0][1].into(), p[2] * m[0][2].into())),
228-
p[0].fma(m[1][0].into(), p[1].fma(m[1][1].into(), p[2] * m[1][2].into())),
229-
p[0].fma(m[2][0].into(), p[1].fma(m[2][1].into(), p[2] * m[2][2].into())),
260+
p[0].fma(m[0][0].to_dt(), p[1].fma(m[0][1].to_dt(), p[2] * m[0][2].to_dt())),
261+
p[0].fma(m[1][0].to_dt(), p[1].fma(m[1][1].to_dt(), p[2] * m[1][2].to_dt())),
262+
p[0].fma(m[2][0].to_dt(), p[1].fma(m[2][1].to_dt(), p[2] * m[2][2].to_dt())),
230263
]
231264
}
232265
// ### MATRICES ### }}}
@@ -236,11 +269,11 @@ fn matmul3<T: DT>(m: [[f32; 3]; 3], p: [T; 3]) -> [T; 3] {
236269
/// sRGB Electro-Optical Transfer Function
237270
///
238271
/// <https://en.wikipedia.org/wiki/SRGB#Computing_the_transfer_function>
239-
pub fn srgb_eotf<T: DT>(n: T) -> T {
240-
if n <= SRGBEOTF_CHI.into() {
241-
n / SRGBEOTF_PHI.into()
272+
pub fn srgb_eotf<T: DType>(n: T) -> T {
273+
if n <= SRGBEOTF_CHI.to_dt() {
274+
n / SRGBEOTF_PHI.to_dt()
242275
} else {
243-
((n + SRGBEOTF_ALPHA.into()) / (SRGBEOTF_ALPHA + 1.0).into()).powf(SRGBEOTF_GAMMA.into())
276+
((n + SRGBEOTF_ALPHA.to_dt()) / (SRGBEOTF_ALPHA + 1.0).to_dt()).powf(SRGBEOTF_GAMMA.to_dt())
244277
}
245278
}
246279

@@ -964,7 +997,7 @@ pub extern "C" fn srgb_to_lrgb(pixel: &mut [f32; 3]) {
964997
/// Convert from Linear Light RGB to CIE XYZ, D65 standard illuminant
965998
///
966999
/// <https://en.wikipedia.org/wiki/SRGB#From_sRGB_to_CIE_XYZ>
967-
pub fn lrgb_to_xyz<T: DT>(pixel: &mut [T; 3]) {
1000+
pub fn lrgb_to_xyz<T: DType>(pixel: &mut [T; 3]) {
9681001
*pixel = matmul3(XYZ65_MAT, *pixel)
9691002
}
9701003

0 commit comments

Comments
 (0)