diff --git a/pipi/filter/median.c b/pipi/filter/median.c index f74191e..0be5898 100644 --- a/pipi/filter/median.c +++ b/pipi/filter/median.c @@ -27,6 +27,11 @@ #include "pipi.h" #include "pipi_internals.h" +static int cmpint(void const *i1, void const *i2) +{ + return *(int const *)i1 > *(int const *)i2; +} + pipi_image_t *pipi_median(pipi_image_t *src, int radius) { return pipi_median_ext(src, radius, radius); @@ -45,7 +50,7 @@ pipi_image_t *pipi_median_ext(pipi_image_t *src, int rx, int ry) pipi_pixels_t *srcp, *dstp; float *srcdata, *dstdata; double *list; - int x, y, w, h, i, j, k, size, gray; + int x, y, w, h, i, j, size, gray; w = src->w; h = src->h; @@ -68,7 +73,6 @@ pipi_image_t *pipi_median_ext(pipi_image_t *src, int rx, int ry) { for(x = 0; x < w; x++) { - double tmp; double *parser = list; /* Make a list of neighbours */ @@ -90,41 +94,22 @@ pipi_image_t *pipi_median_ext(pipi_image_t *src, int rx, int ry) } else { - *parser++ = srcdata[4 * (y2 * w + x2)]; - *parser++ = srcdata[4 * (y2 * w + x2) + 1]; - *parser++ = srcdata[4 * (y2 * w + x2) + 2]; - *parser++ = srcdata[4 * (y2 * w + x2) + 3]; + parser[0] = srcdata[4 * (y2 * w + x2)]; + parser[size * 1] = srcdata[4 * (y2 * w + x2) + 1]; + parser[size * 2] = srcdata[4 * (y2 * w + x2) + 2]; + parser[size * 3] = srcdata[4 * (y2 * w + x2) + 3]; + parser++; } } } /* Sort the list */ - for(i = 0; i < size; i++) + qsort(list, size, sizeof(int), cmpint); + if(!gray) { - for(j = 0; j < i; j++) - { - if(gray) - { - if(list[i] < list[j]) - { - tmp = list[i]; - list[i] = list[j]; - list[j] = tmp; - } - } - else - { - for(k = 0; k < 4; k++) - { - if(list[4 * i + k] < list[4 * j + k]) - { - tmp = list[4 * i + k]; - list[4 * i + k] = list[4 * j + k]; - list[4 * j + k] = tmp; - } - } - } - } + qsort(list + size, size, sizeof(int), cmpint); + qsort(list + 2 * size, size, sizeof(int), cmpint); + qsort(list + 3 * size, size, sizeof(int), cmpint); } /* Store the median value */ @@ -134,10 +119,10 @@ pipi_image_t *pipi_median_ext(pipi_image_t *src, int rx, int ry) } else { - dstdata[4 * (y * w + x)] = list[size / 2 * 4]; - dstdata[4 * (y * w + x) + 1] = list[size / 2 * 4 + 1]; - dstdata[4 * (y * w + x) + 2] = list[size / 2 * 4 + 2]; - dstdata[4 * (y * w + x) + 3] = list[size / 2 * 4 + 3]; + dstdata[4 * (y * w + x)] = list[size / 2]; + dstdata[4 * (y * w + x) + 1] = list[size / 2 + size * 1]; + dstdata[4 * (y * w + x) + 2] = list[size / 2 + size * 2]; + dstdata[4 * (y * w + x) + 3] = list[size / 2 + size * 3]; } } }