Skip to content

Commit

Permalink
BUG Fix bounds in sum_rect()
Browse files Browse the repository at this point in the history
Without this fix, it was possible to trigger a crash from the
interpreter, which, even if it was an edge case, is always a serious
bug.

closes #58
  • Loading branch information
luispedro committed Apr 6, 2015
1 parent 387e0ff commit cf3463f
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 2 additions & 2 deletions mahotas/features/_surf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ template <typename T>
double sum_rect(numpy::aligned_array<T> integral, int y0, int x0, int y1, int x1) {
y0 = std::max<int>(y0-1, 0);
x0 = std::max<int>(x0-1, 0);
y1 = std::min<int>(y1-1, integral.dim(0));
x1 = std::min<int>(x1-1, integral.dim(1));
y1 = std::min<int>(y1-1, integral.dim(0) - 1);
x1 = std::min<int>(x1-1, integral.dim(1) - 1);

const T A = integral.at(y0,x0);
const T B = integral.at(y0,x1);
Expand Down
9 changes: 8 additions & 1 deletion mahotas/tests/test_surf.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def test_integral2():

def test_sum_rect():
f = np.arange(800*160).reshape((800,160)) % 7
fi = surf.integral(f.copy())
fi = surf.integral(f.copy())

np.random.seed(22)
for i in range(100):
Expand All @@ -32,6 +32,13 @@ def test_sum_rect():
x1 = np.random.randint(x0+1, 159)
assert _surf.sum_rect(fi, y0, x0, y1, x1) == f[y0:y1, x0:x1].sum()

def test_sum_rect_edge():
# regression
# https://github.com/luispedro/mahotas/issues/58
f = np.arange(80*60).reshape((80,60)) % 7
fi = surf.integral(f.copy())
_surf.sum_rect(fi, 0, 0, 81, 61)

def test_surf_guassians():
f = np.zeros((1024,1024))
Y,X = np.indices(f.shape)
Expand Down

0 comments on commit cf3463f

Please sign in to comment.