Skip to content

Commit c76dc2c

Browse files
committed
Change call signatures of c extensions to use full widths
1 parent 8626148 commit c76dc2c

4 files changed

Lines changed: 20 additions & 22 deletions

File tree

‎src/crtools/fmedian/__init__.py‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,9 @@ def fmedian(input_array, xsize: int, ysize: int, exclude_center: int):
8686
if ysize <= 0:
8787
raise ValueError(f"ysize must be positive, got {ysize}")
8888

89-
# Convert from full window size to half-size for the C extension
90-
# For a window of size 3, we need half-size of 1
91-
xhalf = xsize // 2
92-
yhalf = ysize // 2
93-
9489
arr = _np.asarray(input_array, dtype=_np.float64)
9590
out = _np.empty_like(arr, dtype=_np.float64)
96-
_c_fmedian(arr, out, xhalf, yhalf, int(exclude_center))
91+
_c_fmedian(arr, out, xsize, ysize, int(exclude_center))
9792
return out
9893

9994

‎src/crtools/fmedian/fmedian_ext.c‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ static PyObject *fmedian(PyObject *self, PyObject *args)
9595
return NULL;
9696
}
9797

98+
/* Convert from full window size to half-size for internal use */
99+
int xsize_half = xsize / 2;
100+
int ysize_half = ysize / 2;
101+
98102
/* Check input arguments */
99103
if (check_inputs(input_array, output_array, &height, &width) != 0)
100104
{
@@ -110,7 +114,7 @@ static PyObject *fmedian(PyObject *self, PyObject *args)
110114
npy_intp *output_strides = PyArray_STRIDES(output_array);
111115

112116
/* Allocate buffer for neighborhood values */
113-
int max_neighbors = (2 * xsize + 1) * (2 * ysize + 1);
117+
int max_neighbors = (2 * xsize_half + 1) * (2 * ysize_half + 1);
114118
double *neighbors = (double *)malloc(max_neighbors * sizeof(double));
115119
if (neighbors == NULL)
116120
{
@@ -129,9 +133,9 @@ static PyObject *fmedian(PyObject *self, PyObject *args)
129133
double center_value = *(double *)(((char *)input_data) + y * input_strides[0] + x * input_strides[1]);
130134

131135
/* Collect neighborhood values (conditionally include center) */
132-
for (int dy = -ysize; dy <= ysize; dy++)
136+
for (int dy = -ysize_half; dy <= ysize_half; dy++)
133137
{
134-
for (int dx = -xsize; dx <= xsize; dx++)
138+
for (int dx = -xsize_half; dx <= xsize_half; dx++)
135139
{
136140
int ny = y + dy;
137141
int nx = x + dx;
@@ -197,9 +201,9 @@ static PyMethodDef FmedianMethods[] = {
197201
" output_array : numpy.ndarray (float64, 2D)\n"
198202
" Output array (same size as input)\n"
199203
" xsize : int\n"
200-
" Half-width of window in x direction\n"
204+
" Full width of window in x direction\n"
201205
" ysize : int\n"
202-
" Half-width of window in y direction\n"
206+
" Full height of window in y direction\n"
203207
" exclude_center : int\n"
204208
" If non-zero, exclude the center pixel from the median calculation\n"},
205209
{NULL, NULL, 0, NULL}};

‎src/crtools/fsigma/__init__.py‎

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -75,14 +75,9 @@ def fsigma(input_array, xsize: int, ysize: int, exclude_center: int):
7575
if ysize <= 0:
7676
raise ValueError(f"ysize must be positive, got {ysize}")
7777

78-
# Convert from full window size to half-size for the C extension
79-
# For a window of size 3, we need half-size of 1
80-
xhalf = xsize // 2
81-
yhalf = ysize // 2
82-
8378
arr = _np.asarray(input_array, dtype=_np.float64)
8479
out = _np.empty_like(arr, dtype=_np.float64)
85-
_c_fsigma(arr, out, xhalf, yhalf, int(exclude_center))
80+
_c_fsigma(arr, out, xsize, ysize, int(exclude_center))
8681
return out
8782

8883

‎src/crtools/fsigma/fsigma_ext.c‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ static PyObject *fsigma(PyObject *self, PyObject *args)
9090
return NULL;
9191
}
9292

93+
/* Convert from full window size to half-size for internal use */
94+
int xsize_half = xsize / 2;
95+
int ysize_half = ysize / 2;
96+
9397
/* Check input arguments */
9498
if (check_inputs(input_array, output_array, &height, &width) != 0)
9599
{
@@ -105,7 +109,7 @@ static PyObject *fsigma(PyObject *self, PyObject *args)
105109
npy_intp *output_strides = PyArray_STRIDES(output_array);
106110

107111
/* Allocate buffer for neighborhood values */
108-
int max_neighbors = (2 * xsize + 1) * (2 * ysize + 1);
112+
int max_neighbors = (2 * xsize_half + 1) * (2 * ysize_half + 1);
109113
double *neighbors = (double *)malloc(max_neighbors * sizeof(double));
110114
if (neighbors == NULL)
111115
{
@@ -121,9 +125,9 @@ static PyObject *fsigma(PyObject *self, PyObject *args)
121125
int count = 0;
122126

123127
/* Collect neighborhood values (conditionally include center) */
124-
for (int dy = -ysize; dy <= ysize; dy++)
128+
for (int dy = -ysize_half; dy <= ysize_half; dy++)
125129
{
126-
for (int dx = -xsize; dx <= xsize; dx++)
130+
for (int dx = -xsize_half; dx <= xsize_half; dx++)
127131
{
128132
int ny = y + dy;
129133
int nx = x + dx;
@@ -169,9 +173,9 @@ static PyMethodDef FsigmaMethods[] = {
169173
" output_array : numpy.ndarray (float64, 2D)\n"
170174
" Output array (same size as input)\n"
171175
" xsize : int\n"
172-
" Half-width of window in x direction\n"
176+
" Full width of window in x direction\n"
173177
" ysize : int\n"
174-
" Half-width of window in y direction\n"
178+
" Full height of window in y direction\n"
175179
" exclude_center : int\n"
176180
" If non-zero, exclude the center pixel from the computation.\n"
177181
"\n"

0 commit comments

Comments
 (0)