Skip to content

Commit 85c64cb

Browse files
committed
4 Channel function variants proof-of-concept
I probably need to refactor the benchmark functions to be less copy paste hell
1 parent f6dd7ef commit 85c64cb

File tree

2 files changed

+17
-6
lines changed

2 files changed

+17
-6
lines changed

benches/conversions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ pub fn conversions(c: &mut Criterion) {
1717
let pixels = pixels();
1818

1919
c.bench_function("srgb_to_lrgb", |b| b.iter(|| {
20-
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| colcon::srgb_to_lrgb(pixel.try_into().unwrap())));
20+
black_box(pixels.clone().chunks_exact_mut(3).for_each(|pixel| colcon::srgb_to_lrgb::<_, 3>(pixel.try_into().unwrap())));
2121
} ));
2222

2323
c.bench_function("lrgb_to_xyz", |b| b.iter(|| {

src/lib.rs

+16-5
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@ use core::ops::{Add, Div, Mul, Neg, Rem, Sub};
1717

1818
// DType {{{
1919

20+
/// 3 channels, or 4 with alpha. Alpha ignored.
21+
pub struct Channels<const N: usize>;
22+
/// 3 channels, or 4 with alpha. Alpha ignored.
23+
pub trait ValidChannels {}
24+
impl ValidChannels for Channels<3> {}
25+
impl ValidChannels for Channels<4> {}
26+
2027
#[allow(missing_docs)]
2128
/// Convert an F32 ito any supported DType
2229
pub trait FromF32: Sized {
@@ -754,9 +761,10 @@ macro_rules! op_chunk {
754761

755762
macro_rules! op_inter {
756763
($func:ident, $data:expr) => {
757-
$data
758-
.chunks_exact_mut(3)
759-
.for_each(|pixel| $func(pixel.try_into().unwrap()))
764+
$data.chunks_exact_mut(3).for_each(|pixel| {
765+
let pixel: &mut [T; 3] = unsafe { pixel.try_into().unwrap_unchecked() };
766+
$func(pixel);
767+
})
760768
};
761769
}
762770

@@ -1037,8 +1045,11 @@ pub fn srgb_to_hsv<T: DType>(pixel: &mut [T; 3]) {
10371045
/// Convert from sRGB to Linear RGB by applying the sRGB EOTF
10381046
///
10391047
/// <https://www.color.org/chardata/rgb/srgb.xalter>
1040-
pub fn srgb_to_lrgb<T: DType>(pixel: &mut [T; 3]) {
1041-
pixel.iter_mut().for_each(|c| *c = srgb_eotf(*c));
1048+
pub fn srgb_to_lrgb<T: DType, const N: usize>(pixel: &mut [T; N])
1049+
where
1050+
Channels<N>: ValidChannels,
1051+
{
1052+
pixel.iter_mut().take(3).for_each(|c| *c = srgb_eotf(*c));
10421053
}
10431054

10441055
/// Convert from Linear Light RGB to CIE XYZ, D65 standard illuminant

0 commit comments

Comments
 (0)