From 354b4c77c2e3c679f9fa47806169645da4f7dc39 Mon Sep 17 00:00:00 2001 From: Justus Spitzmueller Date: Mon, 24 Mar 2025 22:15:10 +0100 Subject: [PATCH] Fix neighbour lookup to use correct 4-connectivity in fillminima The previous implementation incorrectly added only diagonal neighbours to the queue, ignoring directly adjacent pixels (up, down, left, right). This could lead to checkerboard-like artifacts. The fix replaces the nested loop with a simple 4-directional lookup using static offset arrays. --- c_src/fillminima.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/c_src/fillminima.c b/c_src/fillminima.c index 294a5f7..9516edf 100644 --- a/c_src/fillminima.c +++ b/c_src/fillminima.c @@ -182,24 +182,23 @@ static PQel *PQ_first(PixelQueue *pixQ, int h) { } return current; } + +static int di[4] = { 0, 0, -1, 1 }; +static int dj[4] = { -1, 1, 0, 0 }; /* Return a list of neighbouring pixels to given pixel p. */ static PQel *neighbours(PQel *p, int nRows, int nCols) { - int ii, jj, i, j; + int n, i, j; PQel *pl, *pNew; pl = NULL; - for (ii=-1; ii<=1; ii++) { - for (jj=-1; jj<=1; jj++) { - if ((ii != 0) && (jj != 0)) { - i = p->i + ii; - j = p->j + jj; - if ((i >= 0) && (i < nRows) && (j >= 0) && (j < nCols)) { - pNew = newPix(i, j); - pNew->next = pl; - pl = pNew; - } - } + for (n=0; n<4; n++) { + i = p->i + di[n]; + j = p->j + dj[n]; + if ((i >= 0) && (i < nRows) && (j >= 0) && (j < nCols)) { + pNew = newPix(i, j); + pNew->next = pl; + pl = pNew; } } return pl;