Skip to content

Commit

Permalink
Restructure and slightly refactor code (#13)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
FreezyLemon authored Dec 8, 2022
1 parent 2287bfd commit 20d68fb
Show file tree
Hide file tree
Showing 10 changed files with 694 additions and 634 deletions.
10 changes: 7 additions & 3 deletions src/fastmath.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#![allow(clippy::cast_possible_wrap)]
#![allow(clippy::cast_precision_loss)]
#![allow(clippy::cast_sign_loss)]

use core::f32;

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

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

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

/// Computes x raised to the power of y.
///
///
/// This implementation benefits a lot from FMA instructions being
/// available on the target platform. Make sure to enable the relevant
/// CPU feature during compilation.
Expand Down
54 changes: 13 additions & 41 deletions src/hsl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,13 @@ impl Hsl {
&mut self.data
}

#[must_use]
#[inline(always)]
#[allow(clippy::missing_const_for_fn)]
pub fn into_data(self) -> Vec<[f32; 3]> {
self.data
}

#[must_use]
#[inline(always)]
pub const fn width(&self) -> usize {
Expand All @@ -65,30 +72,17 @@ impl Hsl {

impl From<LinearRgb> for Hsl {
fn from(lrgb: LinearRgb) -> Self {
let mut data = lrgb.data;
let width = lrgb.width();
let height = lrgb.height();
let mut data = lrgb.into_data();
for pix in &mut data {
*pix = lrgb_to_hsl(*pix);
}

Hsl {
Self {
data,
width: lrgb.width,
height: lrgb.height,
}
}
}

impl From<Hsl> for LinearRgb {
fn from(hsl: Hsl) -> Self {
let mut data = hsl.data;
for pix in &mut data {
*pix = hsl_to_lrgb(*pix);
}

LinearRgb {
data,
width: hsl.width,
height: hsl.height,
width,
height,
}
}
}
Expand Down Expand Up @@ -117,25 +111,3 @@ fn lrgb_to_hsl(rgb: [f32; 3]) -> [f32; 3] {
};
[h, s, l]
}

#[inline(always)]
fn hsl_to_lrgb(hsl: [f32; 3]) -> [f32; 3] {
let c = (1.0 - (2.0 * hsl[2] - 1.0).abs()) * hsl[1];
let h_prime = hsl[0] / 60.0;
let x = c * (1.0 - (h_prime % 2.0 - 1.0).abs());
let (r1, g1, b1) = if (0.0..1.0).contains(&h_prime) {
(c, x, 0.0)
} else if (1.0..2.0).contains(&h_prime) {
(x, c, 0.0)
} else if (2.0..3.0).contains(&h_prime) {
(0.0, c, x)
} else if (3.0..4.0).contains(&h_prime) {
(0.0, x, c)
} else if (4.0..5.0).contains(&h_prime) {
(x, 0.0, c)
} else {
(c, 0.0, x)
};
let m = hsl[2] - c / 2.0;
[r1 + m, g1 + m, b1 + m]
}
Loading

0 comments on commit 20d68fb

Please sign in to comment.