From 8eaa4fafb50e4f0a83ace47ad8348cde15b6dd42 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 4 Mar 2014 20:02:35 +0200 Subject: [PATCH] Improve flood algorithm --- src/img/blob.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/src/img/blob.c b/src/img/blob.c index d2ebd3a28..5da78d645 100644 --- a/src/img/blob.c +++ b/src/img/blob.c @@ -38,19 +38,28 @@ array_t *imlib_count_blobs(struct image *image) blob->h = p->y; } - if (pixels[p->y*image->w+p->x]) { - pixels[p->y*image->w+p->x]=0; - if ((p->x-1) > 0) { - array_push_back(points, point_alloc(p->x-1, p->y)); + /* move west */ + for (int n=p->x-1; n>=0 && pixels[p->y*image->w+n]; n--) { + pixels[p->y*image->w+n]=0; + if ((p->y-1) >= 0 && pixels[(p->y-1)*image->w+n]) { + array_push_back(points, point_alloc(n, p->y-1)); + } - if ((p->x+1) < image->w) { - array_push_back(points, point_alloc(p->x+1, p->y)); + if ((p->y+1) < image->h && pixels[(p->y+1)*image->w+n]) { + array_push_back(points, point_alloc(n, p->y+1)); + } - if ((p->y-1) > 0) { - array_push_back(points, point_alloc(p->x, p->y-1)); + } + /* move east */ + for (int n=p->x; nw && pixels[p->y*image->w+n]; n++) { + pixels[p->y*image->w+n]=0; + if ((p->y-1) > 0 && pixels[(p->y-1)*image->w+n]) { + array_push_back(points, point_alloc(n, p->y-1)); + } - if ((p->y+1) < image->h) { - array_push_back(points, point_alloc(p->x, p->y+1)); + if ((p->y+1) < image->h && pixels[(p->y+1)*image->w+n]) { + array_push_back(points, point_alloc(n, p->y+1)); + } } xfree(p);