From 46b8d97901bad379ebeb2e2f132ca4c935e56963 Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 4 Mar 2014 01:57:35 +0200 Subject: [PATCH] Fix blobs and array bugs --- src/array.c | 3 +-- src/img/blob.c | 41 +++++++++++++++++++++++++---------------- 2 files changed, 26 insertions(+), 18 deletions(-) diff --git a/src/array.c b/src/array.c index 425c81657..f7f2bef11 100644 --- a/src/array.c +++ b/src/array.c @@ -59,7 +59,7 @@ void *array_at(struct array *array, int idx) void array_push_back(struct array *array, void *element) { - if (array->index == array->length-1) { + if (array->index == array->length) { array->length += ARRAY_INIT_SIZE; array->data = xrealloc(array->data, array->length * sizeof(void*)); } @@ -71,7 +71,6 @@ void *array_pop_back(struct array *array) void *el=NULL; if (array->index) { el = array->data[--array->index]; - } return el; } diff --git a/src/img/blob.c b/src/img/blob.c index a5c7107d6..d2ebd3a28 100644 --- a/src/img/blob.c +++ b/src/img/blob.c @@ -12,8 +12,8 @@ array_t *imlib_count_blobs(struct image *image) array_alloc_init(&points, xfree, 500); uint16_t *pixels = (uint16_t*) image->pixels; - for (int y=1; yh-1; y++) { - for (int x=1; xw-1; x++) { + for (int y=0; yh; y++) { + for (int x=0; xw; x++) { int i=y*image->w+x; if (pixels[i]) { /* new blob */ @@ -21,9 +21,8 @@ array_t *imlib_count_blobs(struct image *image) array_push_back(blobs, blob); /* flood fill */ - array_push_back(points, point_alloc(x, y)); - point_t *p; - while((p = array_pop_back(points))!= NULL) { + point_t *p=point_alloc(x, y); + while(p != NULL) { /* add point to blob */ if (p->x < blob->x) { blob->x = p->x; @@ -40,12 +39,22 @@ array_t *imlib_count_blobs(struct image *image) } if (pixels[p->y*image->w+p->x]) { - pixels[p->y*image->w+p->x]=0x0000; - array_push_back(points, point_alloc(p->x-1, p->y)); - array_push_back(points, point_alloc(p->x+1, p->y)); - array_push_back(points, point_alloc(p->x, p->y-1)); - array_push_back(points, point_alloc(p->x, p->y+1)); + pixels[p->y*image->w+p->x]=0; + if ((p->x-1) > 0) { + array_push_back(points, point_alloc(p->x-1, p->y)); + } + if ((p->x+1) < image->w) { + array_push_back(points, point_alloc(p->x+1, p->y)); + } + if ((p->y-1) > 0) { + array_push_back(points, point_alloc(p->x, p->y-1)); + } + if ((p->y+1) < image->h) { + array_push_back(points, point_alloc(p->x, p->y+1)); + } } + xfree(p); + p = array_pop_back(points); } } } @@ -53,14 +62,14 @@ array_t *imlib_count_blobs(struct image *image) for (int i=0; iw < 10) { /* blob too small */ - //array_erase(blobs, i); - i--; - } else { +// if (blob->w < 10) { /* blob too small */ + //array_erase(blobs, i); i--; + // } else { blob->w = blob->w - blob->x; blob->h = blob->h - blob->y; - } + // } } - //array_free(points); + + array_free(points); return blobs; }