Return kernel count from kernel generation functions

Signed-off-by: Yuxuan Shui <yshuiv7@gmail.com>
This commit is contained in:
Yuxuan Shui 2019-06-09 00:44:46 +01:00
parent 2239181551
commit f64ac97a91
No known key found for this signature in database
GPG Key ID: 37C999F617EA1A47
2 changed files with 19 additions and 12 deletions

View File

@ -280,10 +280,10 @@ default_backend_render_shadow(backend_t *backend_data, int width, int height,
return ret; return ret;
} }
static struct conv **generate_box_blur_kernel(int blur_size) { static struct conv **generate_box_blur_kernel(int blur_size, int *kernel_count) {
int r = blur_size * 2 + 1; int r = blur_size * 2 + 1;
assert(r > 0); assert(r > 0);
auto ret = ccalloc(3, struct conv *); auto ret = ccalloc(2, struct conv *);
ret[0] = cvalloc(sizeof(struct conv) + sizeof(double) * (size_t)r); ret[0] = cvalloc(sizeof(struct conv) + sizeof(double) * (size_t)r);
ret[1] = 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]->w = r;
@ -294,38 +294,41 @@ static struct conv **generate_box_blur_kernel(int blur_size) {
ret[0]->data[i] = 1; ret[0]->data[i] = 1;
ret[1]->data[i] = 1; ret[1]->data[i] = 1;
} }
*kernel_count = 2;
return ret; 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 *kernel_count) {
int r = blur_size * 2 + 1; int r = blur_size * 2 + 1;
assert(r > 0); assert(r > 0);
auto ret = ccalloc(3, struct conv *); auto ret = ccalloc(2, struct conv *);
ret[0] = cvalloc(sizeof(struct conv) + sizeof(double) * (size_t)r); ret[0] = cvalloc(sizeof(struct conv) + sizeof(double) * (size_t)r);
ret[1] = 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]->w = r;
ret[0]->h = 1; ret[0]->h = 1;
ret[1]->w = 1; ret[1]->w = 1;
ret[1]->h = r; ret[1]->h = r;
for (int i = 0; i < blur_size; i++) { for (int i = 0; i <= blur_size; i++) {
ret[0]->data[i] = ret[0]->data[r - i - 1] = ret[0]->data[i] = ret[0]->data[r - i - 1] =
1.0 / (sqrt(2.0 * M_PI) * blur_deviation) * 1.0 / (sqrt(2.0 * M_PI) * blur_deviation) *
exp(-(blur_size - i) * (blur_size - i) / exp(-(blur_size - i) * (blur_size - i) /
(2 * blur_deviation * blur_deviation)); (2 * blur_deviation * blur_deviation));
ret[1]->data[i] = ret[1]->data[r - i - 1] = ret[0]->data[0]; ret[1]->data[i] = ret[1]->data[r - i - 1] = ret[0]->data[i];
} }
*kernel_count = 2;
return ret; return ret;
} }
/// Generate blur kernels for gaussian and box blur methods. Generated kernel is not /// Generate blur kernels for gaussian and box blur methods. Generated kernel is not
/// normalized, and the center element will always be 1. /// normalized, and the center element will always be 1.
struct conv ** struct conv **generate_blur_kernel(enum blur_method method, int blur_size,
generate_blur_kernel(enum blur_method method, int blur_size, double blur_deviation) { double blur_deviation, int *kernel_count) {
assert(blur_size >= 0); assert(blur_size >= 0);
switch (method) { switch (method) {
case BLUR_METHOD_BOX: return generate_box_blur_kernel(blur_size); case BLUR_METHOD_BOX: return generate_box_blur_kernel(blur_size, kernel_count);
case BLUR_METHOD_GAUSSIAN: case BLUR_METHOD_GAUSSIAN:
return generate_gaussian_blur_kernel(blur_size, blur_deviation); return generate_gaussian_blur_kernel(blur_size, blur_deviation, kernel_count);
default: break; default: break;
} }
return NULL; return NULL;

View File

@ -7,6 +7,7 @@
#include <stdbool.h> #include <stdbool.h>
#include "config.h"
#include "region.h" #include "region.h"
typedef struct session session_t; typedef struct session session_t;
@ -22,8 +23,8 @@ bool build_shadow(xcb_connection_t *, xcb_drawable_t, double opacity, int width,
xcb_render_picture_t solid_picture(xcb_connection_t *, xcb_drawable_t, bool argb, xcb_render_picture_t solid_picture(xcb_connection_t *, xcb_drawable_t, bool argb,
double a, double r, double g, double b); double a, double r, double g, double b);
xcb_image_t *make_shadow(xcb_connection_t *c, const conv *kernel, double opacity, xcb_image_t *
int width, int height); make_shadow(xcb_connection_t *c, const conv *kernel, double opacity, int width, int height);
/// The default implementation of `is_win_transparent`, it simply looks at win::mode. So /// The default implementation of `is_win_transparent`, it simply looks at win::mode. So
/// this is not suitable for backends that alter the content of windows /// this is not suitable for backends that alter the content of windows
@ -38,3 +39,6 @@ default_backend_render_shadow(backend_t *backend_data, int width, int height,
const conv *kernel, double r, double g, double b, double a); const conv *kernel, double r, double g, double b, double a);
void init_backend_base(struct backend_base *base, session_t *ps); void init_backend_base(struct backend_base *base, session_t *ps);
struct conv **generate_blur_kernel(enum blur_method method, int blur_size,
double blur_deviation, int *kernel_count);