@@ -17,7 +17,38 @@ fn spowf(n: f32, power: f32) -> f32 {
17
17
n. abs ( ) . powf ( power) . copysign ( n)
18
18
}
19
19
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 :
21
52
Sized
22
53
+ Copy
23
54
+ Add < Output = Self >
@@ -26,7 +57,7 @@ trait DT:
26
57
+ Sub < Output = Self >
27
58
+ Rem < Output = Self >
28
59
+ PartialOrd
29
- + From < f32 >
60
+ + FromF32
30
61
{
31
62
fn _fma ( self , mul : Self , add : Self ) -> Self ;
32
63
fn powf ( self , rhs : Self ) -> Self ;
@@ -43,7 +74,7 @@ trait DT:
43
74
44
75
macro_rules! impl_float {
45
76
( $type: ident) => {
46
- impl DT for $type {
77
+ impl DType for $type {
47
78
fn _fma( self , mul: Self , add: Self ) -> Self {
48
79
self . mul_add( mul, add)
49
80
}
@@ -57,6 +88,8 @@ macro_rules! impl_float {
57
88
impl_float ! ( f32 ) ;
58
89
impl_float ! ( f64 ) ;
59
90
91
+ // }}}
92
+
60
93
/// Create an array of separate channel buffers from a single interwoven buffer.
61
94
/// Copies the data.
62
95
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] {
222
255
}
223
256
224
257
/// 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 ] {
226
259
[
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 ( ) ) ) ,
230
263
]
231
264
}
232
265
// ### MATRICES ### }}}
@@ -236,11 +269,11 @@ fn matmul3<T: DT>(m: [[f32; 3]; 3], p: [T; 3]) -> [T; 3] {
236
269
/// sRGB Electro-Optical Transfer Function
237
270
///
238
271
/// <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 ( )
242
275
} 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 ( ) )
244
277
}
245
278
}
246
279
@@ -964,7 +997,7 @@ pub extern "C" fn srgb_to_lrgb(pixel: &mut [f32; 3]) {
964
997
/// Convert from Linear Light RGB to CIE XYZ, D65 standard illuminant
965
998
///
966
999
/// <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 ] ) {
968
1001
* pixel = matmul3 ( XYZ65_MAT , * pixel)
969
1002
}
970
1003
0 commit comments