Skip to content

Commit d9380e4

Browse files
committed
Move convert_space fns to N channel
Remove `convert_space_alpha`
1 parent a2b1913 commit d9380e4

File tree

2 files changed

+36
-15
lines changed

2 files changed

+36
-15
lines changed

src/lib.rs

+15-11
Original file line numberDiff line numberDiff line change
@@ -761,8 +761,8 @@ macro_rules! op_chunk {
761761

762762
macro_rules! op_inter {
763763
($func:ident, $data:expr) => {
764-
$data.chunks_exact_mut(3).for_each(|pixel| {
765-
let pixel: &mut [T; 3] = pixel.try_into().unwrap();
764+
$data.chunks_exact_mut(N).for_each(|pixel| {
765+
let pixel: &mut [T; N] = pixel.try_into().unwrap();
766766
$func(pixel);
767767
})
768768
};
@@ -821,23 +821,32 @@ macro_rules! graph {
821821

822822
/// Runs conversion functions to convert `pixel` from one `Space` to another
823823
/// in the least possible moves.
824-
pub fn convert_space<T: DType>(from: Space, to: Space, pixel: &mut [T; 3]) {
824+
pub fn convert_space<T: DType, const N: usize>(from: Space, to: Space, pixel: &mut [T; N])
825+
where
826+
Channels<N>: ValidChannels,
827+
{
825828
graph!(convert_space, pixel, from, to, op_single);
826829
}
827830

828831
/// Runs conversion functions to convert `pixel` from one `Space` to another
829832
/// in the least possible moves.
830833
///
831834
/// Caches conversion graph for faster iteration.
832-
pub fn convert_space_chunked<T: DType>(from: Space, to: Space, pixels: &mut [[T; 3]]) {
835+
pub fn convert_space_chunked<T: DType, const N: usize>(from: Space, to: Space, pixels: &mut [[T; N]])
836+
where
837+
Channels<N>: ValidChannels,
838+
{
833839
graph!(convert_space_chunked, pixels, from, to, op_chunk);
834840
}
835841

836842
/// Runs conversion functions to convert `pixel` from one `Space` to another
837843
/// in the least possible moves.
838844
///
839845
/// Caches conversion graph for faster iteration and ignores remainder values in slice.
840-
pub fn convert_space_sliced<T: DType>(from: Space, to: Space, pixels: &mut [T]) {
846+
pub fn convert_space_sliced<T: DType, const N: usize>(from: Space, to: Space, pixels: &mut [T])
847+
where
848+
Channels<N>: ValidChannels,
849+
{
841850
graph!(convert_space_sliced, pixels, from, to, op_inter);
842851
}
843852

@@ -885,15 +894,10 @@ pub extern "C" fn convert_space_ffi(from: *const c_char, to: *const c_char, pixe
885894
core::slice::from_raw_parts_mut(pixels, len)
886895
}
887896
};
888-
convert_space_sliced(from, to, pixels);
897+
convert_space_sliced::<_, 3>(from, to, pixels);
889898
0
890899
}
891900

892-
/// Same as `convert_space`, ignores the 4th value in `pixel`.
893-
pub fn convert_space_alpha(from: Space, to: Space, pixel: &mut [f32; 4]) {
894-
unsafe { convert_space(from, to, pixel.get_unchecked_mut(0..3).try_into().unwrap_unchecked()) }
895-
}
896-
897901
// ### Convert Space ### }}}
898902

899903
// ### Str2Col ### {{{

src/tests.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -369,8 +369,25 @@ fn alpha_untouch() {
369369
lch_to_lab,
370370
] {
371371
f(&mut pixel);
372-
assert_eq!(pixel[3].to_bits(), 4.0_f64.to_bits());
372+
assert_eq!(pixel[3].to_bits(), 4.0_f64.to_bits(), "{:?}", f);
373373
}
374+
convert_space(Space::SRGB, Space::CIELCH, &mut pixel);
375+
assert_eq!(pixel[3].to_bits(), 4.0_f64.to_bits());
376+
let mut chunks = [pixel, pixel, pixel];
377+
convert_space_chunked(Space::CIELCH, Space::SRGB, &mut chunks);
378+
chunks
379+
.iter()
380+
.for_each(|c| assert_eq!(c[3].to_bits(), 4.0_f64.to_bits(), "alpha_untouch_chunked"));
381+
let mut slice = [pixel, pixel, pixel].iter().fold(Vec::<f64>::new(), |mut acc, it| {
382+
acc.extend_from_slice(it.as_slice());
383+
acc
384+
});
385+
convert_space_sliced::<_, 4>(Space::CIELCH, Space::SRGB, &mut slice);
386+
slice
387+
.iter()
388+
.skip(3)
389+
.step_by(4)
390+
.for_each(|n| assert_eq!(n.to_bits(), 4.0_f64.to_bits(), "alpha_untouch_sliced"));
374391
}
375392

376393
#[test]
@@ -379,7 +396,7 @@ fn sliced() {
379396
acc.extend_from_slice(it);
380397
acc
381398
});
382-
convert_space_sliced(Space::SRGB, Space::CIELCH, &mut pixel);
399+
convert_space_sliced::<_, 3>(Space::SRGB, Space::CIELCH, &mut pixel);
383400
pix_cmp(
384401
&pixel
385402
.chunks_exact(3)
@@ -398,7 +415,7 @@ fn sliced_odd() {
398415
acc
399416
});
400417
pixel.push(1234.5678);
401-
convert_space_sliced(Space::SRGB, Space::CIELCH, &mut pixel);
418+
convert_space_sliced::<_, 3>(Space::SRGB, Space::CIELCH, &mut pixel);
402419
pix_cmp(
403420
&pixel
404421
.chunks_exact(3)
@@ -415,7 +432,7 @@ fn sliced_odd() {
415432
fn sliced_smol() {
416433
let pixels = [1.0, 0.0];
417434
let mut smol = pixels.clone();
418-
convert_space_sliced(Space::SRGB, Space::CIELCH, &mut smol);
435+
convert_space_sliced::<_, 3>(Space::SRGB, Space::CIELCH, &mut smol);
419436
assert_eq!(pixels, smol);
420437
}
421438

0 commit comments

Comments
 (0)