From d8298fdd670f3f6fe440a82c028ba922639803b2 Mon Sep 17 00:00:00 2001 From: Romain Vimont Date: Fri, 22 Feb 2019 22:07:18 +0100 Subject: [PATCH] Make convert_slice_2d() use raw pointers The util function convert_slice_2d() operates on a slice with stride information. It will become incompatible with PlaneSlice which will not expose a multi-rows slice anymore (see ). To keep it generic enough, we don't want to use a PlaneSlice wrapper for every call, so make the function use raw pointers (and unsafe). Note: this commit changes indentation for unsafe blocks, so the diff is more understable with "git show -b". --- src/util.rs | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/util.rs b/src/util.rs index ea4e9ade22..1c0b4b19a9 100644 --- a/src/util.rs +++ b/src/util.rs @@ -292,20 +292,19 @@ pub fn round_shift(value: i32, bit: usize) -> i32 { (value + (1 << bit >> 1)) >> bit } -pub fn convert_slice_2d( - dst: &mut [NEW], dst_stride: usize, src: &[OLD], src_stride: usize, +pub unsafe fn convert_slice_2d( + dst: *mut NEW, dst_stride: usize, src: *const OLD, src_stride: usize, width: usize, height: usize ) where NEW: CastFromPrimitive + Copy + 'static, OLD: Copy + 'static, { - for r in 0..height { - for (a, b) in dst[r * dst_stride..r * dst_stride + width] - .iter_mut() - .zip(src[r * src_stride..r * src_stride + width].iter()) - { - *a = NEW::cast_from(*b); + for y in 0..height { + for x in 0..width { + let p_dst = dst.add(y * dst_stride + x); + let p_src = src.add(y * src_stride + x); + *p_dst = NEW::cast_from(*p_src); } } }