Change type to ssize_t for all index and size variables

This commit is contained in:
Johannes Schönberger
2013-01-22 19:14:49 +01:00
parent 6026ddd9a2
commit 96c5f894ec
2 changed files with 113 additions and 115 deletions
+97 -99
View File
@@ -84,9 +84,9 @@ cdef struct PixelCount:
# relative offsets from the octagon center
#
cdef struct SCoord:
np.int32_t stride # add the stride to the memory location
np.int32_t x
np.int32_t y
ssize_t stride # add the stride to the memory location
ssize_t x
ssize_t y
cdef struct Histograms:
void *memory # pointer to the allocated memory
@@ -95,16 +95,16 @@ cdef struct Histograms:
np.uint8_t *data # pointer to the image data
np.uint8_t *mask # pointer to the image mask
np.uint8_t *output # pointer to the output array
np.int32_t column_count # number of columns represented by this
ssize_t column_count # number of columns represented by this
# structure
np.int32_t stripe_length # number of columns including "radius" before
ssize_t stripe_length # number of columns including "radius" before
# and after
np.int32_t row_count # number of rows available in image
np.int32_t current_column # the column being processed
np.int32_t current_row # the row being processed
np.int32_t current_stride # offset in data and mask to current location
np.int32_t radius # the "radius" of the octagon
np.int32_t a_2 # 1/2 of the length of a side of the octagon
ssize_t row_count # number of rows available in image
ssize_t current_column # the column being processed
ssize_t current_row # the row being processed
ssize_t current_stride # offset in data and mask to current location
ssize_t radius # the "radius" of the octagon
ssize_t a_2 # 1/2 of the length of a side of the octagon
#
#
# The strides are the offsets in the array to the points that need to
@@ -138,50 +138,50 @@ cdef struct Histograms:
SCoord last_bottom_left # (-) trailing edge bottom - 1 col
SCoord bottom_left # (+) left side of octagon's bottom - 1 col
np.int32_t row_stride # stride between one row and the next
np.int32_t col_stride # stride between one column and the next
ssize_t row_stride # stride between one row and the next
ssize_t col_stride # stride between one column and the next
# The accumulator holds the running histogram
#
HistogramPiece accumulator
#
# The running count of pixels in the accumulator
#
np.uint32_t accumulator_count
ssize_t accumulator_count
#
# The percent of pixels within the octagon whose value is
# less than or equal to the median-filtered value (e.g. for
# median, this is 50, for lower quartile it's 25)
#
np.int32_t percent
ssize_t percent
#
# last_update_column keeps track of the column # of the last update
# to the fine histogram accumulator. Short-term, the median
# stays in one coarse block so only one fine histogram might
# need to be updated
#
np.int32_t last_update_column[16]
ssize_t last_update_column[16]
############################################################################
#
# allocate_histograms - allocates the Histograms structure for the run
#
############################################################################
cdef Histograms *allocate_histograms(np.int32_t rows,
np.int32_t columns,
np.int32_t row_stride,
np.int32_t col_stride,
np.int32_t radius,
np.int32_t percent,
cdef Histograms *allocate_histograms(ssize_t rows,
ssize_t columns,
ssize_t row_stride,
ssize_t col_stride,
ssize_t radius,
ssize_t percent,
np.uint8_t *data,
np.uint8_t *mask,
np.uint8_t *output):
cdef:
unsigned int adjusted_stripe_length = columns + 2*radius + 1
unsigned int memory_size
ssize_t adjusted_stripe_length = columns + 2*radius + 1
ssize_t memory_size
void *ptr
Histograms *ph
size_t roundoff
int a
ssize_t a
SCoord *psc
memory_size = (adjusted_stripe_length *
@@ -199,7 +199,7 @@ cdef Histograms *allocate_histograms(np.int32_t rows,
#
# Align histogram memory to a 32-byte boundary
#
roundoff = <size_t> ptr
roundoff = <ssize_t>ptr
roundoff += 31
roundoff -= roundoff % 32
ptr = <void *> roundoff
@@ -232,7 +232,7 @@ cdef Histograms *allocate_histograms(np.int32_t rows,
# a_2 is the offset from the center to each of the octagon
# corners
#
a = <int> (<np.float64_t> radius * 2.0 / 2.414213)
a = <ssize_t>(<np.float64_t>radius * 2.0 / 2.414213)
a_2 = a / 2
if a_2 == 0:
a_2 = 1
@@ -326,19 +326,18 @@ cdef void set_stride(Histograms *ph, SCoord *psc):
# a column that is "radius" to the left.
#
############################################################################
cdef inline np.int32_t tl_br_colidx(Histograms *ph, np.int32_t colidx):
return <np.int32_t> (colidx + 3 * ph.radius + ph.current_row) % \
ph.stripe_length
cdef inline ssize_t tl_br_colidx(Histograms *ph, ssize_t colidx):
return (colidx + 3*ph.radius + ph.current_row) % ph.stripe_length
cdef inline np.int32_t tr_bl_colidx(Histograms *ph, np.int32_t colidx):
return <np.int32_t> (colidx + 3 * ph.radius + ph.row_count - ph.current_row) % \
ph.stripe_length
cdef inline ssize_t tr_bl_colidx(Histograms *ph, ssize_t colidx):
return (colidx + 3*ph.radius + ph.row_count-ph.current_row) % \
ph.stripe_length
cdef inline np.int32_t leading_edge_colidx(Histograms *ph, np.int32_t colidx):
return <np.int32_t> (colidx + 5 * ph.radius) % ph.stripe_length
cdef inline ssize_t leading_edge_colidx(Histograms *ph, ssize_t colidx):
return (colidx + 5*ph.radius) % ph.stripe_length
cdef inline np.int32_t trailing_edge_colidx(Histograms *ph, np.int32_t colidx):
return <np.int32_t> (colidx + 3 * ph.radius - 1) % ph.stripe_length
cdef inline ssize_t trailing_edge_colidx(Histograms *ph, ssize_t colidx):
return (colidx + 3*ph.radius - 1) % ph.stripe_length
############################################################################
#
@@ -349,9 +348,8 @@ cdef inline np.int32_t trailing_edge_colidx(Histograms *ph, np.int32_t colidx):
# colidx - the index of the column to add
#
############################################################################
cdef inline void accumulate_coarse_histogram(Histograms *ph, np.int32_t colidx):
cdef:
int offset
cdef inline void accumulate_coarse_histogram(Histograms *ph, ssize_t colidx):
cdef ssize_t offset
offset = tr_bl_colidx(ph, colidx)
if ph.pixel_count[offset].top_right > 0:
@@ -372,9 +370,8 @@ cdef inline void accumulate_coarse_histogram(Histograms *ph, np.int32_t colidx):
# for a given column
#
############################################################################
cdef inline void deaccumulate_coarse_histogram(Histograms *ph, np.int32_t colidx):
cdef:
int offset
cdef inline void deaccumulate_coarse_histogram(Histograms *ph, ssize_t colidx):
cdef ssize_t offset
#
# The trailing diagonals don't appear until here
#
@@ -403,11 +400,11 @@ cdef inline void deaccumulate_coarse_histogram(Histograms *ph, np.int32_t colidx
#
############################################################################
cdef inline void accumulate_fine_histogram(Histograms *ph,
np.int32_t colidx,
np.uint32_t fineidx):
ssize_t colidx,
ssize_t fineidx):
cdef:
int fineoffset = fineidx * 16
int offset
ssize_t fineoffset = fineidx * 16
ssize_t offset
offset = tr_bl_colidx(ph, colidx)
add16(ph.accumulator.fine + fineoffset,
@@ -427,11 +424,11 @@ cdef inline void accumulate_fine_histogram(Histograms *ph,
#
############################################################################
cdef inline void deaccumulate_fine_histogram(Histograms *ph,
np.int32_t colidx,
np.uint32_t fineidx):
ssize_t colidx,
ssize_t fineidx):
cdef:
int fineoffset = fineidx * 16
int offset
ssize_t fineoffset = fineidx * 16
ssize_t offset
#
# The trailing diagonals don't appear until here
@@ -459,10 +456,7 @@ cdef inline void deaccumulate_fine_histogram(Histograms *ph,
############################################################################
cdef inline void accumulate(Histograms *ph):
cdef:
int i
int j
np.int32_t accumulator
cdef np.int32_t accumulator
accumulate_coarse_histogram(ph, ph.current_column)
deaccumulate_coarse_histogram(ph, ph.current_column)
@@ -486,11 +480,11 @@ cdef inline void accumulate(Histograms *ph):
# to choose remains to be done.
############################################################################
cdef inline void update_fine(Histograms *ph, int fineidx):
cdef inline void update_fine(Histograms *ph, ssize_t fineidx):
cdef:
int first_update_column = ph.last_update_column[fineidx]+1
int update_limit = ph.current_column+1
int i
ssize_t first_update_column = ph.last_update_column[fineidx]+1
ssize_t update_limit = ph.current_column+1
ssize_t i
for i in range(first_update_column, update_limit):
accumulate_fine_histogram(ph, i, fineidx)
@@ -515,15 +509,15 @@ cdef inline void update_histogram(Histograms *ph,
SCoord *last_coord,
SCoord *coord):
cdef:
np.int32_t current_column = ph.current_column
np.int32_t current_row = ph.current_row
np.int32_t current_stride = ph.current_stride
np.int32_t column_count = ph.column_count
np.int32_t row_count = ph.row_count
ssize_t current_column = ph.current_column
ssize_t current_row = ph.current_row
ssize_t current_stride = ph.current_stride
ssize_t column_count = ph.column_count
ssize_t row_count = ph.row_count
np.uint8_t value
np.int32_t stride
np.int32_t x
np.int32_t y
ssize_t stride
ssize_t x
ssize_t y
x = last_coord.x + current_column
y = last_coord.y + current_row
@@ -556,21 +550,21 @@ cdef inline void update_histogram(Histograms *ph,
############################################################################
cdef inline void update_current_location(Histograms *ph):
cdef:
np.int32_t current_column = ph.current_column
np.int32_t radius = ph.radius
np.int32_t top_left_off = tl_br_colidx(ph, current_column)
np.int32_t top_right_off = tr_bl_colidx(ph, current_column)
np.int32_t bottom_left_off = tr_bl_colidx(ph, current_column)
np.int32_t bottom_right_off = tl_br_colidx(ph, current_column)
np.int32_t leading_edge_off = leading_edge_colidx(ph, current_column)
ssize_t current_column = ph.current_column
ssize_t radius = ph.radius
ssize_t top_left_off = tl_br_colidx(ph, current_column)
ssize_t top_right_off = tr_bl_colidx(ph, current_column)
ssize_t bottom_left_off = tr_bl_colidx(ph, current_column)
ssize_t bottom_right_off = tl_br_colidx(ph, current_column)
ssize_t leading_edge_off = leading_edge_colidx(ph, current_column)
np.int32_t *coarse_histogram
np.int32_t *fine_histogram
np.int32_t last_xoff
np.int32_t last_yoff
np.int32_t last_stride
np.int32_t xoff
np.int32_t yoff
np.int32_t stride
ssize_t last_xoff
ssize_t last_yoff
ssize_t last_stride
ssize_t xoff
ssize_t yoff
ssize_t stride
update_histogram(ph, &ph.histogram[top_left_off].top_left,
&ph.pixel_count[top_left_off].top_left,
@@ -605,16 +599,18 @@ cdef inline void update_current_location(Histograms *ph):
cdef inline np.uint8_t find_median(Histograms *ph):
cdef:
np.uint32_t pixels_below # of pixels below the median
int i
int j
int k
ssize_t pixels_below # of pixels below the median
ssize_t i
ssize_t j
ssize_t k
np.uint32_t accumulator
if ph.accumulator_count == 0:
return 0
pixels_below = <np.uint32_t> ((ph.accumulator_count * ph.percent + 50)
/ 100) # +50 for roundoff
# +50 for roundoff
pixels_below = (ph.accumulator_count * ph.percent + 50) / 100
if pixels_below > 0:
pixels_below -= 1
@@ -626,10 +622,10 @@ cdef inline np.uint8_t find_median(Histograms *ph):
accumulator -= ph.accumulator.coarse[i]
update_fine(ph, i)
for j in range(i * 16, (i + 1) * 16):
for j in range(i*16, (i + 1)*16):
accumulator += ph.accumulator.fine[j]
if accumulator > pixels_below:
return <np.uint8_t> j
return <np.uint8_t>j
return 0
@@ -648,23 +644,25 @@ cdef inline np.uint8_t find_median(Histograms *ph):
# output - array to be filled with filtered pixels
#
############################################################################
cdef int c_median_filter(np.int32_t rows,
np.int32_t columns,
np.int32_t row_stride,
np.int32_t col_stride,
np.int32_t radius,
np.int32_t percent,
cdef int c_median_filter(ssize_t rows,
ssize_t columns,
ssize_t row_stride,
ssize_t col_stride,
ssize_t radius,
ssize_t percent,
np.uint8_t *data,
np.uint8_t *mask,
np.uint8_t *output):
cdef:
Histograms *ph
Histogram *phistogram
int row, col, i
np.int32_t top_left_off
np.int32_t top_right_off
np.int32_t bottom_left_off
np.int32_t bottom_right_off
ssize_t row
ssize_t col
ssize_t i
ssize_t top_left_off
ssize_t top_right_off
ssize_t bottom_left_off
ssize_t bottom_right_off
ph = allocate_histograms(rows, columns, row_stride, col_stride,
radius, percent, data, mask, output)
+16 -16
View File
@@ -17,7 +17,7 @@ cdef inline double _gaussian_weight(double sigma, double value):
return exp(-0.5 * (value / sigma)**2)
cdef double* _compute_color_lut(int bins, double sigma, double max_value):
cdef double* _compute_color_lut(ssize_t bins, double sigma, double max_value):
cdef:
double* color_lut = <double*>malloc(bins * sizeof(double))
@@ -29,7 +29,7 @@ cdef double* _compute_color_lut(int bins, double sigma, double max_value):
return color_lut
cdef double* _compute_range_lut(int win_size, double sigma):
cdef double* _compute_range_lut(ssize_t win_size, double sigma):
cdef:
double* range_lut = <double*>malloc(win_size**2 * sizeof(double))
@@ -45,9 +45,9 @@ cdef double* _compute_range_lut(int win_size, double sigma):
return range_lut
def denoise_bilateral(image, int win_size=5, sigma_range=None,
double sigma_spatial=1, int bins=10000, mode='constant',
double cval=0):
def denoise_bilateral(image, ssize_t win_size=5, sigma_range=None,
double sigma_spatial=1, ssize_t bins=10000,
mode='constant', double cval=0):
"""Denoise image using bilateral filter.
This is an edge-preserving and noise reducing denoising filter. It averages
@@ -100,10 +100,10 @@ def denoise_bilateral(image, int win_size=5, sigma_range=None,
image = np.atleast_3d(img_as_float(image))
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
ssize_t rows = image.shape[0]
ssize_t cols = image.shape[1]
ssize_t dims = image.shape[2]
ssize_t window_ext = (win_size - 1) / 2
double max_value = image.max()
@@ -224,14 +224,14 @@ def denoise_tv_bregman(image, double weight, int max_iter=100, double eps=1e-3):
image = np.atleast_3d(img_as_float(image))
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 rows2 = rows + 2
Py_ssize_t cols2 = cols + 2
Py_ssize_t r, c, k
ssize_t rows = image.shape[0]
ssize_t cols = image.shape[1]
ssize_t dims = image.shape[2]
ssize_t rows2 = rows + 2
ssize_t cols2 = cols + 2
ssize_t r, c, k
Py_ssize_t total = rows * cols * dims
ssize_t total = rows * cols * dims
shape_ext = (rows2, cols2, dims)