Skip to content

Commit

Permalink
XYZ with alpha, unaligned memory fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Jun 15, 2024
1 parent 2e7364a commit 7074604
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
10 changes: 5 additions & 5 deletions src/image_to_linear.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,10 +93,10 @@ fn channels_to_linear<const CHANNELS_CONFIGURATION: u8, const USE_ALPHA: bool>(
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())
};
Expand All @@ -111,9 +111,9 @@ fn channels_to_linear<const CHANNELS_CONFIGURATION: u8, const USE_ALPHA: bool>(
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() {
Expand All @@ -122,7 +122,7 @@ fn channels_to_linear<const CHANNELS_CONFIGURATION: u8, const USE_ALPHA: bool>(
};
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);
}
}
}
Expand Down
18 changes: 13 additions & 5 deletions src/linear_to_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,25 @@ fn linear_to_gamma_channels<const CHANNELS_CONFIGURATION: u8, const USE_ALPHA: b
let src_ptr = unsafe { (src.as_ptr() as *const u8).add(src_offset) as *const f32 };
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 src_slice = unsafe { src_ptr.add(px) };
let r = unsafe {
*src_slice.get_unchecked(px + image_configuration.get_r_channel_offset())
src_slice
.add(image_configuration.get_r_channel_offset())
.read_unaligned()
};
let g = unsafe {
*src_slice.get_unchecked(px + image_configuration.get_g_channel_offset())
src_slice
.add(image_configuration.get_g_channel_offset())
.read_unaligned()
};
let b = unsafe {
*src_slice.get_unchecked(px + image_configuration.get_b_channel_offset())
src_slice
.add(image_configuration.get_b_channel_offset())
.read_unaligned()
};

let rgb = Rgb::<f32>::new(r, g, b);
Expand All @@ -117,7 +123,9 @@ fn linear_to_gamma_channels<const CHANNELS_CONFIGURATION: u8, const USE_ALPHA: b

if USE_ALPHA && image_configuration.has_alpha() {
let a = unsafe {
*src_slice.get_unchecked(px + image_configuration.get_a_channel_offset())
src_slice
.add(image_configuration.get_a_channel_offset())
.read_unaligned()
};
let a_lin = (a * 255f32) as u8;
unsafe {
Expand Down

0 comments on commit 7074604

Please sign in to comment.