From 223918155186e27a4c58d90e120e912d97fb7215 Mon Sep 17 00:00:00 2001 From: Yuxuan Shui Date: Sat, 8 Jun 2019 23:56:47 +0100 Subject: [PATCH] Properly implement gaussian blur kernel Signed-off-by: Yuxuan Shui --- src/backend/backend_common.c | 33 ++++++++++++++++++--------------- 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/src/backend/backend_common.c b/src/backend/backend_common.c index e80aa79..2e41de7 100644 --- a/src/backend/backend_common.c +++ b/src/backend/backend_common.c @@ -280,15 +280,16 @@ default_backend_render_shadow(backend_t *backend_data, int width, int height, return ret; } -static struct conv ** -generate_box_blur_kernel(int blur_size) { +static struct conv **generate_box_blur_kernel(int blur_size) { int r = blur_size * 2 + 1; assert(r > 0); auto ret = ccalloc(3, struct conv *); ret[0] = cvalloc(sizeof(struct conv) + sizeof(double) * (size_t)r); ret[1] = cvalloc(sizeof(struct conv) + sizeof(double) * (size_t)r); - ret[0]->w = r; ret[0]->h = 1; - ret[1]->w = 1; ret[1]->h = r; + ret[0]->w = r; + ret[0]->h = 1; + ret[1]->w = 1; + ret[1]->h = r; for (int i = 0; i < r; i++) { ret[0]->data[i] = 1; ret[1]->data[i] = 1; @@ -296,18 +297,22 @@ generate_box_blur_kernel(int blur_size) { return ret; } -static struct conv ** -generate_gaussian_blur_kernel(int blur_size, double blur_deviation) { +static struct conv **generate_gaussian_blur_kernel(int blur_size, double blur_deviation) { int r = blur_size * 2 + 1; assert(r > 0); auto ret = ccalloc(3, struct conv *); ret[0] = cvalloc(sizeof(struct conv) + sizeof(double) * (size_t)r); ret[1] = cvalloc(sizeof(struct conv) + sizeof(double) * (size_t)r); - ret[0]->w = r; ret[0]->h = 1; - ret[1]->w = 1; ret[1]->h = r; - for (int i = 0; i < r; i++) { - ret[0]->data[i] = 1; - ret[1]->data[i] = 1; + ret[0]->w = r; + ret[0]->h = 1; + ret[1]->w = 1; + ret[1]->h = r; + for (int i = 0; i < blur_size; i++) { + ret[0]->data[i] = ret[0]->data[r - i - 1] = + 1.0 / (sqrt(2.0 * M_PI) * blur_deviation) * + exp(-(blur_size - i) * (blur_size - i) / + (2 * blur_deviation * blur_deviation)); + ret[1]->data[i] = ret[1]->data[r - i - 1] = ret[0]->data[0]; } return ret; } @@ -318,12 +323,10 @@ struct conv ** generate_blur_kernel(enum blur_method method, int blur_size, double blur_deviation) { assert(blur_size >= 0); switch (method) { - case BLUR_METHOD_BOX: - return generate_box_blur_kernel(blur_size); + case BLUR_METHOD_BOX: return generate_box_blur_kernel(blur_size); case BLUR_METHOD_GAUSSIAN: return generate_gaussian_blur_kernel(blur_size, blur_deviation); - default: - break; + default: break; } return NULL; }