From 7074604adb123eae069f554039085ce3dd214503 Mon Sep 17 00:00:00 2001 From: awxkee Date: Sat, 15 Jun 2024 23:24:43 +0100 Subject: [PATCH] XYZ with alpha, unaligned memory fixes --- src/image_to_linear.rs | 10 +++++----- src/linear_to_image.rs | 18 +++++++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/src/image_to_linear.rs b/src/image_to_linear.rs index 2937f12..2fad1cc 100644 --- a/src/image_to_linear.rs +++ b/src/image_to_linear.rs @@ -93,10 +93,10 @@ fn channels_to_linear( let dst_ptr = unsafe { (dst.as_mut_ptr() as *mut u8).add(dst_offset) as *mut f32 }; let src_slice = unsafe { slice::from_raw_parts(src_ptr, width as usize * channels) }; - let dst_slice = unsafe { slice::from_raw_parts_mut(dst_ptr, width as usize * channels) }; for x in _cx..width as usize { let px = x * channels; + let dst = unsafe { dst_ptr.add(px) }; let r = unsafe { *src_slice.get_unchecked(px + image_configuration.get_r_channel_offset()) }; @@ -111,9 +111,9 @@ fn channels_to_linear( let rgb_f32 = rgb.to_rgb_f32(); unsafe { - *dst_slice.get_unchecked_mut(px) = transfer(rgb_f32.r); - *dst_slice.get_unchecked_mut(px + 1) = transfer(rgb_f32.g); - *dst_slice.get_unchecked_mut(px + 2) = transfer(rgb_f32.b); + dst.write_unaligned(transfer(rgb_f32.r)); + dst.add(1).write_unaligned(transfer(rgb_f32.g)); + dst.add(2).write_unaligned(transfer(rgb_f32.b)); } if USE_ALPHA && image_configuration.has_alpha() { @@ -122,7 +122,7 @@ fn channels_to_linear( }; let a_lin = a as f32 * (1f32 / 255f32); unsafe { - *dst_slice.get_unchecked_mut(px + 3) = a_lin; + dst.add(3).write_unaligned(a_lin); } } } diff --git a/src/linear_to_image.rs b/src/linear_to_image.rs index af46ad3..9e43290 100644 --- a/src/linear_to_image.rs +++ b/src/linear_to_image.rs @@ -92,19 +92,25 @@ fn linear_to_gamma_channels::new(r, g, b); @@ -117,7 +123,9 @@ fn linear_to_gamma_channels