box-blur. it's now a good deal faster, and also properly supports fractional blur widths.
fast gaussian blur. this is implemented as 3 box filters in series. it approximates a gaussian fairly closely, visually I can't tell the difference between this and the current gaussian blur, although the units don't match exactly. I've put it in a separate file to allow easy comparisons while we decide whether it's worth keeping.
pixelise. a simple pixelise filter like the one in gimp currently.
While I remember there were a couple of issues I came across whilst working on this. Firstly, if you create a transparent layer, and merge it down, then apply a gegl blur operation to the merged layer (all the blurs have the same issue, and probably other filters too), the edges of the image go transparent. I suspect this is something to do with the RaGaBaA=>RGBA conversion not happening properly for some reason, but I haven't looked in to it.
Secondly, the current gaussian blur doesn't work properly for very wide blurs. A std-dev of 200 makes the tile boundaries quite visible.
If any changes are required, just let me know.
Andy.
From be32a4bc47f86c84c406f24e79ac4db87d39ef12 Mon Sep 17 00:00:00 2001 From: Andy Gill <andyggill@xxxxxxxxx> Date: Tue, 18 Jan 2011 22:18:12 +0000 Subject: [PATCH 1/3] optimised the box blur filter. also it now supports fractional blur sizes. --- operations/common/box-blur.c | 307 +++++++++++++++++------------------------- 1 files changed, 123 insertions(+), 184 deletions(-) diff --git a/operations/common/box-blur.c b/operations/common/box-blur.c index 6cb3b2e..76a2093 100644 --- a/operations/common/box-blur.c +++ b/operations/common/box-blur.c @@ -18,11 +18,14 @@ #include "config.h" #include <glib/gi18n-lib.h> +#include <math.h> #ifdef GEGL_CHANT_PROPERTIES -gegl_chant_double (radius, _("Radius"), 0.0, 200.0, 4.0, - _("Radius of square pixel region, (width and height will be radius*2+1).")) +gegl_chant_double (xblur, _("X-Blur"), 0, 256, 0, + _("Radius of horizontal blur in pixels")) +gegl_chant_double (yblur, _("Y-Blur"), 0, 256, 0, + _("Radius of vertical blur in pixels")) #else @@ -30,224 +33,160 @@ gegl_chant_double (radius, _("Radius"), 0.0, 200.0, 4.0, #define GEGL_CHANT_C_FILE "box-blur.c" #include "gegl-chant.h" -#include <stdio.h> -#include <math.h> -#ifdef USE_DEAD_CODE -static inline float -get_mean_component (gfloat *buf, - gint buf_width, - gint buf_height, - gint x0, - gint y0, - gint width, - gint height, - gint component) +static void prepare (GeglOperation *operation) { - gint x, y; - gdouble acc=0; - gint count=0; + GeglChantO *o; + GeglOperationAreaFilter *op_area; + + op_area = GEGL_OPERATION_AREA_FILTER (operation); + o = GEGL_CHANT_PROPERTIES (operation); - gint offset = (y0 * buf_width + x0) * 4 + component; + op_area->left = + op_area->right = (gint)floor(o->xblur) + 1; /* ensure we always have an extra pixel for fractional blur sizes */ + op_area->top = + op_area->bottom = (gint)floor(o->yblur) + 1; - for (y=y0; y<y0+height; y++) - { - for (x=x0; x<x0+width; x++) - { - if (x>=0 && x<buf_width && - y>=0 && y<buf_height) - { - acc += buf [offset]; - count++; - } - offset+=4; - } - offset+= (buf_width * 4) - 4 * width; - } - if (count) - return acc/count; - return 0.0; + gegl_operation_set_format (operation, "output", + babl_format ("RaGaBaA float")); } -#endif -static inline void -get_mean_components (gfloat *buf, - gint buf_width, - gint buf_height, - gint x0, - gint y0, - gint width, - gint height, - gfloat *components) +static void +hor_blur (const gfloat* in, + gfloat* out, + const GeglRectangle *rect, + gfloat xblur, + gfloat yblur) { - gint y; - gdouble acc[4]={0,0,0,0}; - gint count[4]={0,0,0,0}; + gint y; + gint x; + gint c; - gint offset = (y0 * buf_width + x0) * 4; + gint xblur_pixels = (gint)floor(xblur) + 1; /* ensure we always have an extra pixel for fractional blur sizes */ + gint yblur_pixels = (gint)floor(yblur) + 1; - for (y=y0; y<y0+height; y++) - { - gint x; - for (x=x0; x<x0+width; x++) - { - if (x>=0 && x<buf_width && - y>=0 && y<buf_height) - { - gint c; - for (c=0;c<4;c++) - { - acc[c] += buf [offset+c]; - count[c]++; - } - } - offset+=4; - } - offset+= (buf_width * 4) - 4 * width; - } + gint width = rect->width; + gint height = rect->height; + gint total_height = height + 2*yblur_pixels; + + + gfloat inv_filter_area = 1.0f / (2.0f * xblur + 1.0f); + /* initialise weights for fractional blur widths */ + gfloat outer_weight = xblur - floor(xblur); + gfloat inner_weight = 1.0f - outer_weight; + + for(y=0; y<total_height; ++y) { - gint c; - for (c=0;c<4;c++) + gfloat sum[4] = {0.0, 0.0, 0.0, 0.0}; + /* initialise window sum */ + for (x=-xblur_pixels; x<=xblur_pixels; ++x) + { + gfloat weight = (ABS(x) == xblur_pixels) ? outer_weight : 1.0f; + for (c=0; c<4; ++c) + { + sum[c] += weight * in[(x+xblur_pixels)*4 + c]; + } + } + + /* compute horizontal blur, updating the window sum as we go */ + for(x=0; x<width; ++x) { - if (count[c]) - components[c] = acc[c]/count[c]; - else - components[c] = 0.0; + for(c=0; c<4; ++c) + { + *out++ = sum[c] * inv_filter_area; + sum[c] -= outer_weight * in[x*4 + c]; + sum[c] -= inner_weight * in[(x+1)*4 + c]; + sum[c] += inner_weight * in[(x+2*xblur_pixels)*4 + c]; + sum[c] += outer_weight * in[(x+2*xblur_pixels+1)*4 + c]; + } } + in += (width + 2*xblur_pixels) * 4; } } -/* expects src and dst buf to have the same extent */ -static void -hor_blur (GeglBuffer *src, - const GeglRectangle *src_rect, - GeglBuffer *dst, - const GeglRectangle *dst_rect, - gint radius) +static void +ver_blur (const gfloat* in, + gfloat* out, + const GeglRectangle *rect, + gfloat yblur) { - gint u,v; - gint offset; - gfloat *src_buf; - gfloat *dst_buf; - - /* src == dst for hor blur */ - src_buf = g_new0 (gfloat, src_rect->width * src_rect->height * 4); - dst_buf = g_new0 (gfloat, dst_rect->width * dst_rect->height * 4); - - gegl_buffer_get (src, 1.0, src_rect, babl_format ("RaGaBaA float"), src_buf, GEGL_AUTO_ROWSTRIDE); - - offset = 0; - for (v=0; v<dst_rect->height; v++) - for (u=0; u<dst_rect->width; u++) - { - gint i; - gfloat components[4]; - - get_mean_components (src_buf, - src_rect->width, - src_rect->height, - u - radius, - v, - 1 + radius*2, - 1, - components); - - for (i=0; i<4; i++) - dst_buf [offset++] = components[i]; - } - - gegl_buffer_set (dst, dst_rect, babl_format ("RaGaBaA float"), dst_buf, GEGL_AUTO_ROWSTRIDE); - g_free (src_buf); - g_free (dst_buf); -} - + gint width = rect->width; + gint height = rect->height; -/* expects dst buf to be radius smaller than src buf */ -static void -ver_blur (GeglBuffer *src, - const GeglRectangle *src_rect, - GeglBuffer *dst, - const GeglRectangle *dst_rect, - gint radius) -{ - gint u,v; - gint offset; - gfloat *src_buf; - gfloat *dst_buf; - - src_buf = g_new0 (gfloat, src_rect->width * src_rect->height * 4); - dst_buf = g_new0 (gfloat, dst_rect->width * dst_rect->height * 4); - - gegl_buffer_get (src, 1.0, src_rect, babl_format ("RaGaBaA float"), src_buf, GEGL_AUTO_ROWSTRIDE); - - offset=0; - for (v=0; v<dst_rect->height; v++) - for (u=0; u<dst_rect->width; u++) - { - gfloat components[4]; - gint c; - - get_mean_components (src_buf, - src_rect->width, - src_rect->height, - u + radius, /* 1x radius is the offset between the bufs */ - v - radius + radius, /* 1x radius is the offset between the bufs */ - 1, - 1 + radius * 2, - components); - - for (c=0; c<4; c++) - dst_buf [offset++] = components[c]; - } - - gegl_buffer_set (dst, dst_rect, babl_format ("RaGaBaA float"), dst_buf, GEGL_AUTO_ROWSTRIDE); - g_free (src_buf); - g_free (dst_buf); -} + gint yblur_pixels = (gint)floor(yblur) + 1; /* ensure we always have an extra pixel for fractional blur sizes */ + + gint y; + gint x; + gint c; -static void prepare (GeglOperation *operation) -{ - GeglChantO *o; - GeglOperationAreaFilter *op_area; + gfloat inv_filter_area = 1.0f / (2.0f * yblur + 1.0f); + /* initialise weights for fractional blur widths */ + gfloat outer_weight = yblur - floor(yblur); + gfloat inner_weight = 1.0f - outer_weight; - op_area = GEGL_OPERATION_AREA_FILTER (operation); - o = GEGL_CHANT_PROPERTIES (operation); + for(x=0; x<width; ++x) + { + gfloat sum[4] = {0.0, 0.0, 0.0, 0.0}; + /* initialise window sum */ + for (y=-yblur_pixels; y<=yblur_pixels; ++y) + { + gfloat weight = (ABS(y) == yblur_pixels) ? outer_weight : 1.0f; - op_area->left = - op_area->right = - op_area->top = - op_area->bottom = ceil (o->radius); + for (c=0; c<4; ++c) + { + sum[c] += weight * in[((y+yblur_pixels)*width + x)*4 + c]; + } + } - gegl_operation_set_format (operation, "output", - babl_format ("RaGaBaA float")); + /* compute vertical blur */ + for(y=0; y<height; ++y) + { + for(c=0; c<4; ++c) + { + out[(y*width + x)*4 + c] = sum[c] * inv_filter_area; + sum[c] -= outer_weight * in[(y*width + x)*4 + c]; + sum[c] -= inner_weight * in[((y+1)*width + x)*4 + c]; + sum[c] += inner_weight * in[((y+2*yblur_pixels)*width + x)*4 + c]; + sum[c] += outer_weight * in[((y+2*yblur_pixels+1)*width + x)*4 + c]; + } + } + } } + static gboolean process (GeglOperation *operation, GeglBuffer *input, GeglBuffer *output, - const GeglRectangle *result) + const GeglRectangle *roi) { - GeglRectangle rect; + GeglRectangle src_rect; GeglChantO *o = GEGL_CHANT_PROPERTIES (operation); - GeglBuffer *temp; GeglOperationAreaFilter *op_area; + gfloat* buf0; + gfloat* buf1; + op_area = GEGL_OPERATION_AREA_FILTER (operation); - rect = *result; + src_rect = *roi; + src_rect.x -= op_area->left; + src_rect.y -= op_area->top; + src_rect.width += op_area->left + op_area->right; + src_rect.height += op_area->top + op_area->bottom; + + buf0 = g_new0 (gfloat, src_rect.width * src_rect.height * 4); + buf1 = g_new0 (gfloat, roi->width * src_rect.height * 4); - rect.x-=op_area->left; - rect.y-=op_area->top; - rect.width+=op_area->left + op_area->right; - rect.height+=op_area->top + op_area->bottom; + gegl_buffer_get (input, 1.0, &src_rect, babl_format ("RaGaBaA float"), buf0, GEGL_AUTO_ROWSTRIDE); - temp = gegl_buffer_new (&rect, - babl_format ("RaGaBaA float")); + hor_blur (buf0, buf1, roi, o->xblur, o->yblur); + ver_blur (buf1, buf0, roi, o->yblur); - hor_blur (input, &rect, temp, &rect, o->radius); - ver_blur (temp, &rect, output, result, o->radius); + gegl_buffer_set (output, roi, babl_format ("RaGaBaA float"), buf0, GEGL_AUTO_ROWSTRIDE); + + g_free (buf0); + g_free (buf1); - g_object_unref (temp); return TRUE; } -- 1.7.1
From ce2e5bc69693d3cbeddd5e517b45233f2aaaeb59 Mon Sep 17 00:00:00 2001 From: Andy Gill <andyggill@xxxxxxxxx> Date: Tue, 18 Jan 2011 22:21:02 +0000 Subject: [PATCH 2/3] added fast gaussian blur operation by repeated box blur. --- operations/common/fast-gaussian-blur.c | 75 ++++++++++++++++++++++++++++++++ 1 files changed, 75 insertions(+), 0 deletions(-) create mode 100644 operations/common/fast-gaussian-blur.c diff --git a/operations/common/fast-gaussian-blur.c b/operations/common/fast-gaussian-blur.c new file mode 100644 index 0000000..cd11c6d --- /dev/null +++ b/operations/common/fast-gaussian-blur.c @@ -0,0 +1,75 @@ +/* This file is an image processing operation for GEGL + * + * GEGL is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * GEGL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with GEGL; if not, see <http://www.gnu.org/licenses/>. + * + * Copyright 2006, 2010 Ãyvind KolÃs <pippin@xxxxxxxx> + */ + +#include "config.h" +#include <glib/gi18n-lib.h> + +#ifdef GEGL_CHANT_PROPERTIES + +gegl_chant_double (xblur, _("X-Blur"), 0, 256, 0, + _("Horizontal blur size")) +gegl_chant_double (yblur, _("Y-Blur"), 0, 256, 0, + _("Vertical blur size")) + +#else + +#define GEGL_CHANT_TYPE_META +#define GEGL_CHANT_C_FILE "fast-gaussian-blur.c" + +#include "gegl-chant.h" + +static void attach (GeglOperation *operation) +{ + GeglNode *gegl, *input, *output, *blur1, *blur2, *blur3; + + gegl = operation->node; + + input = gegl_node_get_input_proxy (gegl, "input"); + output = gegl_node_get_output_proxy (gegl, "output"); + blur1 = gegl_node_new_child (gegl, "operation", "gegl:box-blur", NULL); + blur2 = gegl_node_new_child (gegl, "operation", "gegl:box-blur", NULL); + blur3 = gegl_node_new_child (gegl, "operation", "gegl:box-blur", NULL); + + /* approximate a gaussian filter with 3 box filters in series */ + gegl_node_link_many (input, blur1, blur2, blur3, output, NULL); + + gegl_operation_meta_redirect (operation, "xblur", blur1, "xblur"); + gegl_operation_meta_redirect (operation, "yblur", blur1, "yblur"); + gegl_operation_meta_redirect (operation, "xblur", blur2, "xblur"); + gegl_operation_meta_redirect (operation, "yblur", blur2, "yblur"); + gegl_operation_meta_redirect (operation, "xblur", blur3, "xblur"); + gegl_operation_meta_redirect (operation, "yblur", blur3, "yblur"); +} + +static void +gegl_chant_class_init (GeglChantClass *klass) +{ + GObjectClass *object_class; + GeglOperationClass *operation_class; + + object_class = G_OBJECT_CLASS (klass); + operation_class = GEGL_OPERATION_CLASS (klass); + operation_class->attach = attach; + + operation_class->name = "gegl:fast-gaussian-blur"; + operation_class->categories = "blur"; + operation_class->description = + _("Fast gaussian blur by repeated box filter"); +} + +#endif -- 1.7.1
From 720d0b44c8bc01ca61b46f59c1254885c513c612 Mon Sep 17 00:00:00 2001 From: Andy Gill <andyggill@xxxxxxxxx> Date: Tue, 18 Jan 2011 22:22:55 +0000 Subject: [PATCH 3/3] added pixelise filter --- operations/common/pixelise.c | 183 ++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 183 insertions(+), 0 deletions(-) create mode 100644 operations/common/pixelise.c diff --git a/operations/common/pixelise.c b/operations/common/pixelise.c new file mode 100644 index 0000000..a87fbcb --- /dev/null +++ b/operations/common/pixelise.c @@ -0,0 +1,183 @@ +/* This file is an image processing operation for GEGL + * + * GEGL is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * GEGL is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with GEGL; if not, see <http://www.gnu.org/licenses/>. + * + * Copyright 2006 Ãyvind KolÃs <pippin@xxxxxxxx> + */ + +#include "config.h" +#include <glib/gi18n-lib.h> +#include <math.h> + +#ifdef GEGL_CHANT_PROPERTIES + +gegl_chant_int (xsize, _("Block Width"), 1, 256, 8, + _("Width of blocks in pixels")) +gegl_chant_int (ysize, _("Block Height"), 1, 256, 8, + _("Height of blocks in pixels")) + +#else + +#define GEGL_CHANT_TYPE_AREA_FILTER +#define GEGL_CHANT_C_FILE "pixelise.c" + +#include "gegl-chant.h" + +#define CELL_X(px, cell_width) ((px) / (cell_width)) +#define CELL_Y(py, cell_height) ((py) / (cell_height)) + + +static void prepare (GeglOperation *operation) +{ + GeglChantO *o; + GeglOperationAreaFilter *op_area; + + op_area = GEGL_OPERATION_AREA_FILTER (operation); + o = GEGL_CHANT_PROPERTIES (operation); + + op_area->left = + op_area->right = o->xsize; + op_area->top = + op_area->bottom = o->ysize; + + gegl_operation_set_format (operation, "output", + babl_format ("RaGaBaA float")); +} + +static void +calc_block_colors (gfloat* block_colors, + const gfloat* input, + const GeglRectangle* roi, + gint xsize, + gint ysize) +{ + gint cx0 = CELL_X(roi->x, xsize); + gint cy0 = CELL_Y(roi->y, ysize); + gint cx1 = CELL_X(roi->x + roi->width - 1, xsize); + gint cy1 = CELL_Y(roi->y + roi->height - 1, ysize); + + gint cx; + gint cy; + gfloat weight = 1.0f / (xsize * ysize); + gint line_width = roi->width + 2*xsize; + /* loop over the blocks within the region of interest */ + for (cy=cy0; cy<=cy1; ++cy) + { + for (cx=cx0; cx<=cx1; ++cx) + { + gint px = (cx * xsize) - roi->x + xsize; + gint py = (cy * ysize) - roi->y + ysize; + + /* calculate the average color for this block */ + gint j,i,c; + gfloat col[4] = {0.0f, 0.0f, 0.0f, 0.0f}; + for (j=py; j<py+ysize; ++j) + { + for (i=px; i<px+xsize; ++i) + { + for (c=0; c<4; ++c) + col[c] += input[(py*line_width + px)*4 + c]; + } + } + for (c=0; c<4; ++c) + block_colors[c] = weight * col[c]; + block_colors += 4; + } + } +} + +static void +pixelise (gfloat* buf, + const GeglRectangle* roi, + gint xsize, + gint ysize) +{ + gint cx0 = CELL_X(roi->x, xsize); + gint cy0 = CELL_Y(roi->y, ysize); + gint block_count_x = CELL_X(roi->x + roi->width - 1, xsize) - cx0 + 1; + gint block_count_y = CELL_Y(roi->y + roi->height - 1, ysize) - cy0 + 1; + gfloat* block_colors = g_new0 (gfloat, block_count_x * block_count_y * 4); + gint x; + gint y; + gint c; + + /* calculate the average color of all the blocks */ + calc_block_colors(block_colors, buf, roi, xsize, ysize); + + /* set each pixel to the average color of the block it belongs to */ + for (y=0; y<roi->height; ++y) + { + gint cy = CELL_Y(y + roi->y, ysize) - cy0; + for (x=0; x<roi->width; ++x) + { + gint cx = CELL_X(x + roi->x, xsize) - cx0; + for (c=0; c<4; ++c) + *buf++ = block_colors[(cy*block_count_x + cx)*4 + c]; + } + } + + g_free (block_colors); +} + +static gboolean +process (GeglOperation *operation, + GeglBuffer *input, + GeglBuffer *output, + const GeglRectangle *roi) +{ + GeglRectangle src_rect; + GeglChantO *o = GEGL_CHANT_PROPERTIES (operation); + GeglOperationAreaFilter *op_area; + gfloat* buf; + + op_area = GEGL_OPERATION_AREA_FILTER (operation); + src_rect = *roi; + src_rect.x -= op_area->left; + src_rect.y -= op_area->top; + src_rect.width += op_area->left + op_area->right; + src_rect.height += op_area->top + op_area->bottom; + + buf = g_new0 (gfloat, src_rect.width * src_rect.height * 4); + + gegl_buffer_get (input, 1.0, &src_rect, babl_format ("RaGaBaA float"), buf, GEGL_AUTO_ROWSTRIDE); + + pixelise(buf, roi, o->xsize, o->ysize); + + gegl_buffer_set (output, roi, babl_format ("RaGaBaA float"), buf, GEGL_AUTO_ROWSTRIDE); + + g_free (buf); + + return TRUE; +} + + +static void +gegl_chant_class_init (GeglChantClass *klass) +{ + GeglOperationClass *operation_class; + GeglOperationFilterClass *filter_class; + + operation_class = GEGL_OPERATION_CLASS (klass); + filter_class = GEGL_OPERATION_FILTER_CLASS (klass); + + filter_class->process = process; + operation_class->prepare = prepare; + + operation_class->categories = "blur"; + operation_class->name = "gegl:pixelise"; + operation_class->description = + _("Pixelise filter"); +} + +#endif -- 1.7.1
_______________________________________________ Gegl-developer mailing list Gegl-developer@xxxxxxxxxxxxxxxxxxxxxx https://lists.XCF.Berkeley.EDU/mailman/listinfo/gegl-developer