Skip to content

Commit 20d68fb

Browse files
authored
Restructure and slightly refactor code (#13)
* separate structs out into files * no direct access to modules * fix a clippy error * lots of clippy warnings * add `into_data()` * make Yuv fields not crate-public * remove doc(hidden) and turn modules not-public * rustfmt * Allow clippy::missing_const_for_fn for into_data See rust-lang/rust-clippy#9271, this is a bug in clippy * fix legitimate clippy errors
1 parent 2287bfd commit 20d68fb

File tree

10 files changed

+694
-634
lines changed

10 files changed

+694
-634
lines changed

src/fastmath.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
#![allow(clippy::cast_possible_wrap)]
2+
#![allow(clippy::cast_precision_loss)]
3+
#![allow(clippy::cast_sign_loss)]
4+
15
use core::f32;
26

37
// The following cbrtf implementation is a port of FreeBSDs cbrtf function
@@ -23,7 +27,7 @@ use core::f32;
2327
*/
2428

2529
/// Computes the cube root of x.
26-
///
30+
///
2731
/// The argument must be normal (not NaN, +/-INF or subnormal).
2832
/// This is required for optimization purposes.
2933
#[cfg(feature = "fastmath")]
@@ -56,13 +60,13 @@ pub fn cbrtf(x: f32) -> f32 {
5660
t as f32
5761
}
5862

59-
// The following implementation of powf is based on José Fonseca's
63+
// The following implementation of powf is based on José Fonseca's
6064
// polynomial-based implementation, ported to Rust as scalar code
6165
// so that the compiler can auto-vectorize and otherwise optimize.
6266
// Original: https://jrfonseca.blogspot.com/2008/09/fast-sse2-pow-tables-or-polynomials.html
6367

6468
/// Computes x raised to the power of y.
65-
///
69+
///
6670
/// This implementation benefits a lot from FMA instructions being
6771
/// available on the target platform. Make sure to enable the relevant
6872
/// CPU feature during compilation.

src/hsl.rs

+13-41
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ impl Hsl {
5050
&mut self.data
5151
}
5252

53+
#[must_use]
54+
#[inline(always)]
55+
#[allow(clippy::missing_const_for_fn)]
56+
pub fn into_data(self) -> Vec<[f32; 3]> {
57+
self.data
58+
}
59+
5360
#[must_use]
5461
#[inline(always)]
5562
pub const fn width(&self) -> usize {
@@ -65,30 +72,17 @@ impl Hsl {
6572

6673
impl From<LinearRgb> for Hsl {
6774
fn from(lrgb: LinearRgb) -> Self {
68-
let mut data = lrgb.data;
75+
let width = lrgb.width();
76+
let height = lrgb.height();
77+
let mut data = lrgb.into_data();
6978
for pix in &mut data {
7079
*pix = lrgb_to_hsl(*pix);
7180
}
7281

73-
Hsl {
82+
Self {
7483
data,
75-
width: lrgb.width,
76-
height: lrgb.height,
77-
}
78-
}
79-
}
80-
81-
impl From<Hsl> for LinearRgb {
82-
fn from(hsl: Hsl) -> Self {
83-
let mut data = hsl.data;
84-
for pix in &mut data {
85-
*pix = hsl_to_lrgb(*pix);
86-
}
87-
88-
LinearRgb {
89-
data,
90-
width: hsl.width,
91-
height: hsl.height,
84+
width,
85+
height,
9286
}
9387
}
9488
}
@@ -117,25 +111,3 @@ fn lrgb_to_hsl(rgb: [f32; 3]) -> [f32; 3] {
117111
};
118112
[h, s, l]
119113
}
120-
121-
#[inline(always)]
122-
fn hsl_to_lrgb(hsl: [f32; 3]) -> [f32; 3] {
123-
let c = (1.0 - (2.0 * hsl[2] - 1.0).abs()) * hsl[1];
124-
let h_prime = hsl[0] / 60.0;
125-
let x = c * (1.0 - (h_prime % 2.0 - 1.0).abs());
126-
let (r1, g1, b1) = if (0.0..1.0).contains(&h_prime) {
127-
(c, x, 0.0)
128-
} else if (1.0..2.0).contains(&h_prime) {
129-
(x, c, 0.0)
130-
} else if (2.0..3.0).contains(&h_prime) {
131-
(0.0, c, x)
132-
} else if (3.0..4.0).contains(&h_prime) {
133-
(0.0, x, c)
134-
} else if (4.0..5.0).contains(&h_prime) {
135-
(x, 0.0, c)
136-
} else {
137-
(c, 0.0, x)
138-
};
139-
let m = hsl[2] - c / 2.0;
140-
[r1 + m, g1 + m, b1 + m]
141-
}

0 commit comments

Comments
 (0)