commit 3efdf1e87a21864c7c353d10f50a0256c7477b47
Author: Samuel <samuel.pitoiset@gmail.com>
Date:   Wed Apr 17 14:21:22 2013 +0200

    port noise-spread to gegl ops

diff --git a/operations/common/noise-spread.c b/operations/common/noise-spread.c
new file mode 100644
index 0000000..cc4f3cb
--- /dev/null
+++ b/operations/common/noise-spread.c
@@ -0,0 +1,188 @@
+/* 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 1997 Brian Degenhardt and Frederico Mena Quintero
+ * Copyright 2013 Samuel Pitoiset <samuel.pitoiset@gmail.com>
+ */
+
+#include "config.h"
+#include <glib/gi18n-lib.h>
+
+#ifdef GEGL_CHANT_PROPERTIES
+
+gegl_chant_double (spread_amount_x, _("Horizontal"), 0.0, 512.0, 5.0,
+                   _("Horizontal"))
+
+gegl_chant_double (spread_amount_y, _("Vertical"), 0.0, 512.0, 5.0,
+                   _("Vertical"))
+
+#else
+
+#define GEGL_CHANT_TYPE_AREA_FILTER
+#define GEGL_CHANT_C_FILE       "noise-spread.c"
+
+#include "gegl-chant.h"
+#include <math.h>
+
+typedef struct {
+  gdouble spread_amount_x;
+  gdouble spread_amount_y;
+} Ramps;
+
+static void
+prepare (GeglOperation *operation)
+{
+  GeglOperationAreaFilter *area = GEGL_OPERATION_AREA_FILTER (operation);
+  GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
+  gint amount_x, amount_y;
+
+  amount_x = (o->spread_amount_x + 1) / 2;
+  amount_y = (o->spread_amount_y + 1) / 2;
+
+  area->left   =
+  area->right  =
+  area->top    =
+  area->bottom = ceil (sqrt (amount_x * amount_x + amount_y * amount_y));
+
+  gegl_operation_set_format (operation, "input",
+                             babl_format ("RGBA float"));
+  gegl_operation_set_format (operation, "output",
+                             babl_format ("RGBA float"));
+}
+
+static void
+noise_spread(GeglOperation       *operation,
+             GeglBuffer          *src,
+             const GeglRectangle *src_rect,
+             GeglBuffer          *dst,
+             const GeglRectangle *dst_rect,
+             gdouble             spread_amount_x,
+             gdouble             spread_amount_y)
+{
+  GeglOperationAreaFilter *op_area = GEGL_OPERATION_AREA_FILTER (operation);
+  gfloat *in_pixel, *out_pixel;
+  gint total_pixels, n_pixels;
+  gdouble amount_x, amount_y;
+  gfloat *src_buf, *dst_buf;
+  GRand *gr;
+  gint x, y;
+
+  total_pixels = src_rect->width * src_rect->height;
+  n_pixels     = dst_rect->width * dst_rect->height;
+
+  src_buf = g_new0 (gfloat, total_pixels * 4);
+  dst_buf = g_new0 (gfloat, n_pixels * 4);
+
+  gr = g_rand_new ();
+  amount_x = (spread_amount_x + 1) / 2;
+  amount_y = (spread_amount_y + 1) / 2;
+
+  x = dst_rect->x;
+  y = dst_rect->y;
+
+  gegl_buffer_get (src, src_rect, 1.0, babl_format ("RGBA float"),
+                   src_buf, GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_CLAMP);
+
+  in_pixel  = src_buf + (src_rect->width + 1) * 4;
+  out_pixel = dst_buf;
+
+  while (n_pixels--)
+    {
+      gfloat *src_pixel;
+      gint xdist, ydist;
+      gdouble angle;
+      gint xi, yi;
+      gint c;
+
+      /* get random angle, x distance, and y distance */
+      xdist = (amount_x > 0)
+              ? g_rand_int_range(gr, -amount_x, amount_x) : 0;
+      ydist = (amount_y > 0)
+              ? g_rand_int_range(gr, -amount_y, amount_y) : 0;
+      angle = g_rand_double_range(gr, -G_PI, G_PI);
+
+      xi = x + floor (sin (angle) * xdist);
+      yi = y + floor (cos (angle) * ydist);
+
+      /* Only displace the pixel if it's within the bounds of the image */
+      if (xi >= 0 && xi < src_rect->width && yi >= 0 && yi < src_rect->height)
+          src_pixel = src_buf + (xi + yi * src_rect->width) * 4;
+      else
+          src_pixel = in_pixel;
+
+      for (c = 0; c < 4; c++)
+        out_pixel[c] = src_pixel[c];
+
+      if (n_pixels % dst_rect->width == 0)
+          in_pixel += (1 + op_area->left + op_area->right) * 4;
+      else
+          in_pixel += 4;
+
+      out_pixel += 4;
+
+      x++;
+      if (x >= dst_rect->x + dst_rect->width)
+        {
+          x = dst_rect->x;
+          y++;
+        }
+    }
+
+  gegl_buffer_set (dst, dst_rect, 0, babl_format ("RGBA float"),
+                   dst_buf, GEGL_AUTO_ROWSTRIDE);
+
+  g_free (src_buf);
+  g_free (dst_buf);
+  g_rand_free (gr);
+}
+
+static gboolean
+process (GeglOperation       *operation,
+         GeglBuffer          *input,
+         GeglBuffer          *output,
+         const GeglRectangle *result,
+         gint                 level)
+{
+  GeglChantO   *o = GEGL_CHANT_PROPERTIES (operation);
+  GeglRectangle compute;
+
+  compute = gegl_operation_get_required_for_output (operation, "input", result);
+
+  noise_spread (operation, input, &compute, output, result, o->spread_amount_x,
+                o->spread_amount_y);
+
+  return TRUE;
+}
+
+static void
+gegl_chant_class_init (GeglChantClass *klass)
+{
+  GeglOperationFilterClass *filter_class;
+  GeglOperationClass       *operation_class;
+
+  operation_class = GEGL_OPERATION_CLASS (klass);
+  filter_class    = GEGL_OPERATION_FILTER_CLASS (klass);
+
+  operation_class->prepare = prepare;
+  filter_class->process    = process;
+
+  gegl_operation_class_set_keys (operation_class,
+                                 "name"       , "gegl:noise-spread",
+                                 "categories" , "noise",
+                                 "description", _("Move pixels around randomly"),
+                                 NULL);
+}
+
+#endif
diff --git a/tests/compositions/Makefile.am b/tests/compositions/Makefile.am
index 9976da5..56d5180 100644
--- a/tests/compositions/Makefile.am
+++ b/tests/compositions/Makefile.am
@@ -40,6 +40,7 @@ TESTS = \
   run-gamma.xml.sh                     \
   run-hdr-color.xml.sh                 \
   run-mantiuk06.xml.sh                 \
+  run-noise-spread.xml.sh              \
   run-pixelize.xml.sh                  \
   run-reflect.xml.sh                   \
   run-reflect2.xml.sh                  \
diff --git a/tests/compositions/noise-spread.xml b/tests/compositions/noise-spread.xml
new file mode 100644
index 0000000..2b46d44
--- /dev/null
+++ b/tests/compositions/noise-spread.xml
@@ -0,0 +1,14 @@
+<?xml version='1.0' encoding='UTF-8'?>
+<gegl>
+  <node operation='gegl:noise-spread'>
+    <params>
+      <param name='spread_amount_x'>50</param>
+      <param name='spread_amount_y'>50</param>
+    </params>
+  </node>
+  <node operation='gegl:load'>
+    <params>
+      <param name='path'>data/duck.png</param>
+    </params>
+  </node>
+</gegl>
