On Thu, Apr 7, 2011 at 5:36 PM, Mukund Sivaraman <m...@mukund.org> wrote:

> Hi Sourav
>
> On Wed, Apr 06, 2011 at 09:44:31PM +0530, sourav de wrote:
> > I tried to implement the blur operation in GEGL. The algorithm is same as
> of
> > the blur plug-in in GIMP. The patch file for GEGL operation and the
> blur.c
> > file for the plug-in are attached below. Kindly let me know if there is
> any
> > mistake in my implementation.
> >
> > +{
> > +dst_buf[offset*4+c]=ROUND(
> > +                           ((gdouble) (src_pix[c-src_width*4 - 4] *
> src_pix[c-src_width*4 - c])
> > +                            + (gdouble) (src_pix[c-src_width*4] *
> src_pix[c-src_width*4 + 4 - c])
> > +                            + (gdouble) (src_pix[c-src_width*4 + 4] *
> src_pix[c-src_width*4 + 2*4 - c])
> > +                            + (gdouble) (src_pix[c - bytes] * src_pix[c
> - c])
> > +                            + (gdouble) (src_pix[c] * src_pix[c + 4 -
> c])
> > +                            + (gdouble) (src_pix[c + 4] * src_pix[c +
> 2*4 - c])
> > +                            + (gdouble) (src_pix[c+src_width*4 - 4] *
> src_pix[c+src_width*4 - c])
> > +                            + (gdouble) (src_pix[c+src_width*4] *
> src_pix[c+src_width*4 + 4 - c])
> > +                            + (gdouble) (src_pix[c+src_width*4 + 4] *
> src_pix[c+src_width*4 + 2*4 - c]))
> > +                           / ((gdouble) src_pix[c-src_width*4 - c]
> > +                              + (gdouble) src_pix[c-src_width*4 + 4 - c]
> > +                              + (gdouble) src_pix[c-src_width*4 + 2*4 -
> c]
> > +                              + (gdouble) src_pix[c -c]
> > +                              + (gdouble) src_pix[c + 4 - c]
> > +                              + (gdouble) src_pix[c + 2*4 - c]
> > +                              + (gdouble) src_pix[c+src_width*4 - c]
> > +                              + (gdouble) src_pix[c+src_width*4 + 4 - c]
> > +                              + (gdouble) src_pix[c+src_width*4 + 2*4 -
> c]));
> > +
> > +}
> > +
> > +dst_buf[offset*4+3]=
> ((gint)src_pix[c-4-src_width*4]+(gint)src_pix[c-src_width*4]+(gint)src_pix[c+4-src_width*4]
> +
> > +     (gint)src_pix[c-4]+(gint)src_pix[c]+(gint)src_pix[c+4]+
> > +
> (gint)src_pix[c-4+src_width*4]+(gint)src_pix[c+src_width*4]+(gint)src_pix[c+4+src_width*4]+4)/9;
>
> Did this code work for you? Were you able to use this inside GEGL to
> perform a blur?
>
> You can help me review this code if you clean it up and comment it.
>
>                Mukund
>

I've revised my code. It compiles and runs successfully. The patch is
attached below. If still there is any mistake, kindly let me know it.

-- 
Sourav De
2nd Year Student
Department of Computer Science and Engineering
IIT KHARAGPUR
From 19d005775c6b49f3dbecec17eeaa606aa2a4f7b6 Mon Sep 17 00:00:00 2001
From: Sourav De <souravde1...@gmail.com>
Date: Thu, 7 Apr 2011 20:42:21 +0530
Subject: [PATCH] first commit

---
 blur.c |  123 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 123 insertions(+), 0 deletions(-)
 create mode 100644 blur.c

diff --git a/blur.c b/blur.c
new file mode 100644
index 0000000..b991310
--- /dev/null
+++ b/blur.c
@@ -0,0 +1,123 @@
+#include "config.h"
+#include <string.h>
+
+#ifdef GEGL_CHANT_PROPERTIES
+
+#else
+
+#define GEGL_CHANT_TYPE_AREA_FILTER
+#define GEGL_CHANT_C_FILE  "blur.c"
+
+#include <gegl-chant.h>
+#include <math.h>
+#include <stdio.h>
+
+#define BLUR_RADIUS 1;
+
+static void blur(GeglBuffer *src,const GeglRectangle *src_rect,GeglBuffer *dst,const GeglRectangle *dst_rect);
+
+static inline void  prepare(GeglOperation *operation)
+{
+GeglOperationAreaFilter *area = GEGL_OPERATION_AREA_FILTER (operation);
+area->left = area->right = area->top = area->bottom=BLUR_RADIUS;
+gegl_operation_set_format (operation, "input", babl_format ("RGBA float"));
+gegl_operation_set_format (operation, "output", babl_format ("RGBA float"));
+}
+
+static gboolean process (GeglOperation *operation, GeglBuffer *input, GeglBuffer *output, const GeglRectangle *result)
+{
+GeglChantO   *o = GEGL_CHANT_PROPERTIES (operation);
+GeglRectangle compute;
+compute = gegl_operation_get_required_for_output (operation, "input",result);
+blur(input, &compute, output, result);
+return TRUE;
+}
+
+
+static void blur (GeglBuffer *src,const GeglRectangle *src_rect,GeglBuffer *dst,const GeglRectangle *dst_rect)
+{
+gint x,y;
+gint i,j;
+gfloat *src_buf,*dst_buf;
+gint offset;
+
+
+src_buf = g_new0 (gfloat, src_rect->width * src_rect->height * 4);
+dst_buf = g_new0 (gfloat, dst_rect->width * dst_rect->height * 4);
+
+gint src_width=src_rect->width;
+
+gegl_buffer_get (src, 1.0, src_rect, babl_format ("RGBA float"), src_buf, GEGL_AUTO_ROWSTRIDE);
+
+offset=0;
+for(y=0; y<dst_rect->height; y++)
+{
+for (x=0; x<dst_rect->width; x++)
+{
+
+gint c;
+i=x+BLUR_RADIUS;
+ j=y+BLUR_RADIUS;
+gfloat *src_pix = src_buf + (i + j * src_width) * 4;
+
+for (c=0;c<3;c++)
+{
+dst_buf[offset*4+c]=lround(
+                           ((gdouble) (src_pix[c-src_width*4 - 4] * src_pix[c-src_width*4 - c])
+                            + (gdouble) (src_pix[c-src_width*4] * src_pix[c-src_width*4 + 4 - c])
+                            + (gdouble) (src_pix[c-src_width*4 + 4] * src_pix[c-src_width*4 + 2*4 - c])
+                            + (gdouble) (src_pix[c - 4] * src_pix[c - c])
+                            + (gdouble) (src_pix[c] * src_pix[c + 4 - c])
+                            + (gdouble) (src_pix[c + 4] * src_pix[c + 2*4 - c])
+                            + (gdouble) (src_pix[c+src_width*4 - 4] * src_pix[c+src_width*4 - c])
+                            + (gdouble) (src_pix[c+src_width*4] * src_pix[c+src_width*4 + 4 - c])
+                            + (gdouble) (src_pix[c+src_width*4 + 4] * src_pix[c+src_width*4 + 2*4 - c]))
+                           / ((gdouble) src_pix[c-src_width*4 - c]
+                              + (gdouble) src_pix[c-src_width*4 + 4 - c]
+                              + (gdouble) src_pix[c-src_width*4 + 2*4 - c]
+                              + (gdouble) src_pix[c -c]
+                              + (gdouble) src_pix[c + 4 - c]
+                              + (gdouble) src_pix[c + 2*4 - c]
+                              + (gdouble) src_pix[c+src_width*4 - c]
+                              + (gdouble) src_pix[c+src_width*4 + 4 - c]
+                              + (gdouble) src_pix[c+src_width*4 + 2*4 - c]));
+
+}
+
+dst_buf[offset*4+3]= ((gint)src_pix[c-4-src_width*4]+(gint)src_pix[c-src_width*4]+(gint)src_pix[c+4-src_width*4] +
+	(gint)src_pix[c-4]+(gint)src_pix[c]+(gint)src_pix[c+4]+
+	(gint)src_pix[c-4+src_width*4]+(gint)src_pix[c+src_width*4]+(gint)src_pix[c+4+src_width*4]+4)/9;
+
+
+
+offset++;
+}
+
+}
+
+gegl_buffer_set (dst, dst_rect, babl_format ("RGBA float"), dst_buf,GEGL_AUTO_ROWSTRIDE);
+
+g_free (src_buf);
+g_free (dst_buf);
+
+}
+
+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:blur";
+  operation_class->description =
+       _("Performs an averaging of pixels.");
+}
+
+#endif
-- 
1.7.0.4

_______________________________________________
Gimp-developer mailing list
Gimp-developer@lists.XCF.Berkeley.EDU
https://lists.XCF.Berkeley.EDU/mailman/listinfo/gimp-developer

Reply via email to