From cec7b6055d21ae0a9ac65b10d9fbc73feac70979 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Sat, 6 Oct 2012 08:32:08 +0200 Subject: [PATCH] Restructure cdef declarations --- skimage/filter/_denoise.pyx | 85 +++++++++++++++++++++---------------- 1 file changed, 48 insertions(+), 37 deletions(-) diff --git a/skimage/filter/_denoise.pyx b/skimage/filter/_denoise.pyx index bf6c372a..b0da5ddb 100644 --- a/skimage/filter/_denoise.pyx +++ b/skimage/filter/_denoise.pyx @@ -15,22 +15,30 @@ cdef inline double _gaussian_weight(double sigma, double value): cdef double* _compute_color_lut(int bins, double sigma, double max_value): - cdef double* color_lut = malloc(bins * sizeof(double)) - cdef Py_ssize_t b + + cdef: + double* color_lut = malloc(bins * sizeof(double)) + Py_ssize_t b + for b in range(bins): color_lut[b] = _gaussian_weight(sigma, b * max_value / bins) + return color_lut cdef double* _compute_range_lut(int win_size, double sigma): - cdef double* range_lut = malloc(win_size**2 * sizeof(double)) - cdef Py_ssize_t kr, kc - cdef Py_ssize_t window_ext = (win_size - 1) / 2 - cdef double dist + + cdef: + double* range_lut = malloc(win_size**2 * sizeof(double)) + Py_ssize_t kr, kc + Py_ssize_t window_ext = (win_size - 1) / 2 + double dist + for kr in range(win_size): for kc in range(win_size): dist = sqrt((kr - window_ext)**2 + (kc - window_ext)**2) range_lut[kr * win_size + kc] = _gaussian_weight(sigma, dist) + return range_lut @@ -39,24 +47,25 @@ def _denoise_bilateral2d(cnp.ndarray[dtype=cnp.double_t, ndim=2, mode='c'] image double sigma_range, int bins, char mode, double cval): - cdef Py_ssize_t rows = image.shape[0] - cdef Py_ssize_t cols = image.shape[1] - cdef Py_ssize_t window_ext = (win_size - 1) / 2 - cdef double max_value = image.max() + cdef: + Py_ssize_t rows = image.shape[0] + Py_ssize_t cols = image.shape[1] + Py_ssize_t window_ext = (win_size - 1) / 2 + double max_value = image.max() - cdef cnp.ndarray[dtype=cnp.double_t, ndim=2, mode='c'] out = \ - np.zeros((rows, cols), dtype=np.double) + cnp.ndarray[dtype=cnp.double_t, ndim=2, mode='c'] out = \ + np.zeros((rows, cols), dtype=np.double) - cdef double* image_data = image.data - cdef double* out_data = out.data + double* image_data = image.data + double* out_data = out.data - cdef double* color_lut = _compute_color_lut(bins, sigma_color, max_value) - cdef double* range_lut = _compute_range_lut(win_size, sigma_range) + double* color_lut = _compute_color_lut(bins, sigma_color, max_value) + double* range_lut = _compute_range_lut(win_size, sigma_range) - cdef Py_ssize_t r, c, wr, wc, kr, kc, rr, cc, pixel_addr - cdef double centre, value, weight, total_value, total_weight, \ - color_weight, range_weight, diff - cdef double dist_scale = bins / max_value + Py_ssize_t r, c, wr, wc, kr, kc, rr, cc, pixel_addr + double centre, value, weight, total_value, total_weight, \ + color_weight, range_weight, diff + double dist_scale = bins / max_value for r in range(rows): for c in range(cols): @@ -95,27 +104,29 @@ def _denoise_bilateral3d(cnp.ndarray[dtype=cnp.double_t, ndim=3, mode='c'] image double sigma_range, int bins, char mode, double cval): - cdef Py_ssize_t rows = image.shape[0] - cdef Py_ssize_t cols = image.shape[1] - cdef Py_ssize_t dims = image.shape[2] - cdef Py_ssize_t window_ext = (win_size - 1) / 2 - cdef double max_value = image.max() + cdef: + Py_ssize_t rows = image.shape[0] + Py_ssize_t cols = image.shape[1] + Py_ssize_t dims = image.shape[2] + Py_ssize_t window_ext = (win_size - 1) / 2 - cdef cnp.ndarray[dtype=cnp.double_t, ndim=3, mode='c'] out = \ - np.zeros((rows, cols, dims), dtype=np.double) + double max_value = image.max() - cdef double* image_data = image.data - cdef double* out_data = out.data + cnp.ndarray[dtype=cnp.double_t, ndim=3, mode='c'] out = \ + np.zeros((rows, cols, dims), dtype=np.double) - cdef double* color_lut = _compute_color_lut(bins, sigma_color, max_value) - cdef double* range_lut = _compute_range_lut(win_size, sigma_range) + double* image_data = image.data + double* out_data = out.data - cdef Py_ssize_t r, c, d, wr, wc, kr, kc, rr, cc, pixel_addr - cdef double value, weight, dist, total_weight, color_weight, range_weight - cdef double dist_scale = bins / dims / max_value - cdef double* values = malloc(dims * sizeof(double)) - cdef double* centres = malloc(dims * sizeof(double)) - cdef double* total_values = malloc(dims * sizeof(double)) + double* color_lut = _compute_color_lut(bins, sigma_color, max_value) + double* range_lut = _compute_range_lut(win_size, sigma_range) + + Py_ssize_t r, c, d, wr, wc, kr, kc, rr, cc, pixel_addr + double value, weight, dist, total_weight, color_weight, range_weight + double dist_scale = bins / dims / max_value + double* values = malloc(dims * sizeof(double)) + double* centres = malloc(dims * sizeof(double)) + double* total_values = malloc(dims * sizeof(double)) for r in range(rows): for c in range(cols):