diff --git a/src/hsv_to_image.rs b/src/hsv_to_image.rs index e6fb7ab..853c162 100644 --- a/src/hsv_to_image.rs +++ b/src/hsv_to_image.rs @@ -98,14 +98,14 @@ fn hsv_u16_to_channels< let src_ptr = unsafe { (src.as_ptr() as *const u8).add(src_offset) as *const u16 }; let dst_ptr = unsafe { dst.as_mut_ptr().add(dst_offset) }; - 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 h = unsafe { *src_slice.get_unchecked(px) }; - let s = unsafe { *src_slice.get_unchecked(px + 1) }; - let v = unsafe { *src_slice.get_unchecked(px + 2) }; + let src = unsafe { src_ptr.add(px) }; + let h = unsafe { src.read_unaligned() }; + let s = unsafe { src.add(1).read_unaligned() }; + let v = unsafe { src.add(2).read_unaligned() }; let s_f = s as f32 * scale; let v_f = v as f32 * scale; @@ -133,7 +133,7 @@ fn hsv_u16_to_channels< } if image_configuration.has_alpha() { - let a = unsafe { *src_slice.get_unchecked(hx + 3) }; + let a = unsafe { src.add(3).read_unaligned() }; unsafe { *dst_slice.get_unchecked_mut(hx + image_configuration.get_a_channel_offset()) = a as u8; diff --git a/src/image_to_hsv.rs b/src/image_to_hsv.rs index 494d870..cf8bdc3 100644 --- a/src/image_to_hsv.rs +++ b/src/image_to_hsv.rs @@ -97,7 +97,6 @@ fn channels_to_hsv_u16< let dst_ptr = unsafe { (dst.as_mut_ptr() as *mut u8).add(dst_offset) as *mut u16 }; 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; @@ -113,21 +112,22 @@ fn channels_to_hsv_u16< let rgb = Rgb::::new(r, g, b); let hx = x * channels; + let dst = unsafe { dst_ptr.add(hx) }; match target { HsvTarget::HSV => { let hsv = rgb.to_hsv(); unsafe { - *dst_slice.get_unchecked_mut(hx) = hsv.h as u16; - *dst_slice.get_unchecked_mut(hx + 1) = (hsv.s * scale).round() as u16; - *dst_slice.get_unchecked_mut(hx + 2) = (hsv.v * scale).round() as u16; + dst.write_unaligned(hsv.h as u16); + dst.add(1).write_unaligned((hsv.s * scale).round() as u16); + dst.add(2).write_unaligned((hsv.v * scale).round() as u16); } } HsvTarget::HSL => { let hsl = rgb.to_hsl(); unsafe { - *dst_slice.get_unchecked_mut(hx) = hsl.h as u16; - *dst_slice.get_unchecked_mut(hx + 1) = (hsl.s * scale).round() as u16; - *dst_slice.get_unchecked_mut(hx + 2) = (hsl.l * scale).round() as u16; + dst.write_unaligned(hsl.h as u16); + dst.add(1).write_unaligned((hsl.s * scale).round() as u16); + dst.add(2).write_unaligned((hsl.l * scale).round() as u16); } } } @@ -137,7 +137,7 @@ fn channels_to_hsv_u16< *src_slice.get_unchecked(hx + image_configuration.get_a_channel_offset()) }; unsafe { - *dst_slice.get_unchecked_mut(hx + 3) = a as u16; + dst.add(3).write_unaligned(a as u16); } } } diff --git a/src/image_to_xyz_lab.rs b/src/image_to_xyz_lab.rs index d683893..2386415 100644 --- a/src/image_to_xyz_lab.rs +++ b/src/image_to_xyz_lab.rs @@ -206,7 +206,6 @@ fn channels_to_xyz { let lab = rgb.to_lab(); unsafe { - *dst_slice.get_unchecked_mut(x * 3) = lab.l; - *dst_slice.get_unchecked_mut(x * 3 + 1) = lab.a; - *dst_slice.get_unchecked_mut(x * 3 + 2) = lab.b; + let ptr = dst_ptr.add(x * 3); + ptr.write_unaligned(lab.l); + ptr.add(1).write_unaligned(lab.a); + ptr.add(2).write_unaligned(lab.b); } } XYZ => { let xyz = Xyz::from_rgb(&rgb, &matrix, transfer_function); unsafe { - *dst_slice.get_unchecked_mut(x * 3) = xyz.x; - *dst_slice.get_unchecked_mut(x * 3 + 1) = xyz.y; - *dst_slice.get_unchecked_mut(x * 3 + 2) = xyz.z; + let ptr = dst_ptr.add(x * 3); + ptr.write_unaligned(xyz.x); + ptr.add(1).write_unaligned(xyz.y); + ptr.add(2).write_unaligned(xyz.z); } } XyzTarget::LUV => { let luv = rgb.to_luv(); unsafe { - *dst_slice.get_unchecked_mut(x * 3) = luv.l; - *dst_slice.get_unchecked_mut(x * 3 + 1) = luv.u; - *dst_slice.get_unchecked_mut(x * 3 + 2) = luv.v; + let ptr = dst_ptr.add(x * 3); + ptr.write_unaligned(luv.l); + ptr.add(1).write_unaligned(luv.u); + ptr.add(2).write_unaligned(luv.v); } } } @@ -255,9 +257,8 @@ fn channels_to_xyz::new(r, g, b); let px = x * CHANNELS; + let dst_store = unsafe { dst_ptr.add(px) }; match target { LAB => { let lab = rgb.to_lab(); unsafe { - *dst_slice.get_unchecked_mut(px) = lab.l; - *dst_slice.get_unchecked_mut(px + 1) = lab.a; - *dst_slice.get_unchecked_mut(px + 2) = lab.b; + dst_store.write_unaligned(lab.l); + dst_store.add(1).write_unaligned(lab.a); + dst_store.add(2).write_unaligned(lab.b); } } XYZ => { let xyz = Xyz::from_rgb(&rgb, &matrix, transfer_function); unsafe { - *dst_slice.get_unchecked_mut(px) = xyz.x; - *dst_slice.get_unchecked_mut(px + 1) = xyz.y; - *dst_slice.get_unchecked_mut(px + 2) = xyz.z; + dst_store.write_unaligned(xyz.x); + dst_store.add(1).write_unaligned(xyz.y); + dst_store.add(2).write_unaligned(xyz.z); } } XyzTarget::LUV => { let luv = rgb.to_luv(); unsafe { - *dst_slice.get_unchecked_mut(px) = luv.l; - *dst_slice.get_unchecked_mut(px + 1) = luv.u; - *dst_slice.get_unchecked_mut(px + 2) = luv.v; + dst_store.write_unaligned(luv.l); + dst_store.add(1).write_unaligned(luv.u); + dst_store.add(2).write_unaligned(luv.v); } } } @@ -146,7 +146,7 @@ fn channels_to_xyz_with_alpha { @@ -211,16 +211,27 @@ fn xyz_to_channels { @@ -156,7 +155,7 @@ fn xyz_with_alpha_to_channels