Skip to content

Commit

Permalink
fix(blur): use rendered surface after commit
Browse files Browse the repository at this point in the history
- Fix blur logic
- Reused rendered surface to optimize future render on comitted blur
- Include gaussian kernel function

Closes #20
Closes #22
  • Loading branch information
jtheoof committed Jun 11, 2020
1 parent d7c7d60 commit b5360b1
Show file tree
Hide file tree
Showing 10 changed files with 226 additions and 119 deletions.
13 changes: 13 additions & 0 deletions include/algebra.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#pragma once

#include <glib.h>

struct gaussian_kernel {
gdouble *kernel;
gint size;
gdouble sigma;
gdouble sum;
};

struct gaussian_kernel *gaussian_kernel(gint width, gdouble sigma);
void gaussian_kernel_free(gpointer data);
3 changes: 2 additions & 1 deletion include/swappy.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,10 @@ struct swappy_paint_brush {
};

struct swappy_paint_blur {
double bluriness;
double blur_level;
struct swappy_point from;
struct swappy_point to;
cairo_surface_t *surface;
};

struct swappy_paint {
Expand Down
3 changes: 2 additions & 1 deletion include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
#include <glib.h>

void string_remove_at(char *str, size_t pos);
gchar *string_insert_chars_at(gchar *str, gchar *chars, size_t pos);
gchar *string_insert_chars_at(gchar *str, gchar *chars, size_t pos);
void pixel_data_print(guint32 pixel);
1 change: 1 addition & 0 deletions meson.build
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ executable(
swappy_resources,
files([
'src/main.c',
'src/algebra.c',
'src/application.c',
'src/buffer.c',
'src/box.c',
Expand Down
37 changes: 37 additions & 0 deletions src/algebra.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "algebra.h"

#include <glib.h>
#include <math.h>

struct gaussian_kernel *gaussian_kernel(int width, double sigma) {
double sum = 0;
gint size = width * width + 1;
double *kernel = g_new(double, size);
struct gaussian_kernel *gaussian = g_new(struct gaussian_kernel, 1);
for (gint y = 0; y < width; y++) {
for (gint x = 0; x < width; x++) {
double j = y - width;
double i = x - width;
double cell = ((1.0 / (2.0 * G_PI * sigma)) *
exp((-(i * i + j * j)) / (2.0 * sigma * sigma))) *
0xff;
kernel[y * width + x] = cell;
sum += cell;
}
}

gaussian->kernel = kernel;
gaussian->size = size;
gaussian->sigma = sigma;
gaussian->sum = sum;

return gaussian;
}

void gaussian_kernel_free(gpointer data) {
struct gaussian_kernel *gaussian = (struct gaussian_kernel *)data;
if (gaussian != NULL) {
g_free(gaussian->kernel);
g_free(gaussian);
}
}
11 changes: 9 additions & 2 deletions src/application.c
Original file line number Diff line number Diff line change
Expand Up @@ -406,11 +406,18 @@ gboolean draw_area_configure_handler(GtkWidget *widget,
g_debug("received configure_event handler");
cairo_surface_destroy(state->cairo_surface);

state->cairo_surface = gdk_window_create_similar_surface(
gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR,
cairo_surface_t *surface = gdk_window_create_similar_surface(
gtk_widget_get_window(widget), CAIRO_CONTENT_COLOR_ALPHA,
gtk_widget_get_allocated_width(widget),
gtk_widget_get_allocated_height(widget));

g_info("size of cairo_surface: %ux%u with type: %d",
cairo_image_surface_get_width(surface),
cairo_image_surface_get_height(surface),
cairo_image_surface_get_format(surface));

state->cairo_surface = surface;

render_state(state);

return TRUE;
Expand Down
16 changes: 14 additions & 2 deletions src/paint.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@ void paint_free(gpointer data) {

switch (paint->type) {
case SWAPPY_PAINT_MODE_BLUR:
if (paint->content.blur.surface) {
cairo_surface_destroy(paint->content.blur.surface);
}
break;
case SWAPPY_PAINT_MODE_BRUSH:
g_list_free_full(paint->content.brush.points, g_free);
Expand Down Expand Up @@ -70,7 +73,7 @@ void paint_add_temporary(struct swappy_state *state, double x, double y,
if (type == SWAPPY_PAINT_MODE_TEXT) {
paint_commit_temporary(state);
} else {
g_free(state->temp_paint);
paint_free(state->temp_paint);
state->temp_paint = NULL;
}
}
Expand All @@ -79,9 +82,10 @@ void paint_add_temporary(struct swappy_state *state, double x, double y,
case SWAPPY_PAINT_MODE_BLUR:
paint->can_draw = false;

paint->content.blur.bluriness = state->settings.blur_level;
paint->content.blur.blur_level = state->settings.blur_level;
paint->content.blur.from.x = x;
paint->content.blur.from.y = y;
paint->content.blur.surface = NULL;
break;
case SWAPPY_PAINT_MODE_BRUSH:
paint->can_draw = true;
Expand Down Expand Up @@ -147,6 +151,14 @@ void paint_update_temporary_shape(struct swappy_state *state, double x,
return;
}

int32_t width = state->window->width;
int32_t height = state->window->height;

// Bounding x and y to the window dimensions to avoid side effects in
// rendering.
x = fmin(fmax(x, 0), width);
y = fmin(fmax(y, 0), height);

switch (paint->type) {
case SWAPPY_PAINT_MODE_BLUR:
paint->can_draw = true;
Expand Down
Loading

0 comments on commit b5360b1

Please sign in to comment.