[gegl/soc-2012-ops: 33/58] Noise-Slur Operation



commit 3b379c3109fffe3500615c75cd7f22554a0930bd
Author: Maxime Nicco <maxime nicco gmail fr>
Date:   Tue Aug 7 12:25:08 2012 +0200

    Noise-Slur Operation
    
    First version, wait for gegl_chant_seed propertie

 operations/common/noise-slur.c |  184 ++++++++++++++++++++++++++++++++++++++++
 1 files changed, 184 insertions(+), 0 deletions(-)
---
diff --git a/operations/common/noise-slur.c b/operations/common/noise-slur.c
new file mode 100644
index 0000000..1d9c93c
--- /dev/null
+++ b/operations/common/noise-slur.c
@@ -0,0 +1,184 @@
+/* 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 Miles O'Neal <meo rru com>  http://www.rru.com/~meo/
+ * Copyright 2012 Maxime Nicco <maxime nicco gmail com>
+ */
+
+/*
+ *  SLUR Operation
+ *  We replace the current pixel by:
+ *      80% chance it's from directly above,
+ *      10% from above left,
+ *      10% from above right.
+ */
+
+#include "config.h"
+#include <glib/gi18n-lib.h>
+
+#ifdef GEGL_CHANT_PROPERTIES
+
+//gegl_chant_int (random_seed, _("Random seed"),   1, 8, 2, _("Random seed"))
+
+gegl_chant_double (pct_random, _("Randomization (%)"),   0.0, 100.0, 3.0, _("Radomization"))
+
+gegl_chant_int (repeat, _("Repeat"),   1, 100, 1, _("Repeat"))
+
+
+#else
+
+#define GEGL_CHANT_TYPE_AREA_FILTER
+#define GEGL_CHANT_C_FILE       "noise-slur.c"
+
+#include "gegl-chant.h"
+#include <stdio.h>
+#include <math.h>
+#include <stdlib.h>
+
+static void prepare (GeglOperation *operation)
+{
+  GeglOperationAreaFilter *op_area;
+  op_area = GEGL_OPERATION_AREA_FILTER (operation);
+
+  op_area->left   =
+    op_area->right  =
+    op_area->top    =
+    op_area->bottom = 1;
+
+  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,
+         gint                 level)
+{
+  GeglChantO *o                    = GEGL_CHANT_PROPERTIES (operation);
+  GeglOperationAreaFilter *op_area = GEGL_OPERATION_AREA_FILTER (operation);
+
+  GeglBuffer *tmp;
+
+  gfloat *src_buf;
+  gfloat *dst_buf;
+  gfloat *out_pixel, *in_pixel;
+  gint n_pixels = result->width * result->height;
+  gint width  = result->width;
+  gint height = result->height;
+  GeglRectangle src_rect;
+  GRand    *gr;
+  gint k, b, i;
+  gint total_pixels;
+
+  gr = g_rand_new ();
+  tmp = gegl_buffer_new(result, babl_format ("RGBA float"));
+
+  src_rect.x      = result->x - op_area->left;
+  src_rect.width  = result->width + op_area->left + op_area->right;
+  src_rect.y      = result->y - op_area->top;
+  src_rect.height = result->height + op_area->top + op_area->bottom;
+
+  total_pixels = src_rect.height * src_rect.width;
+
+  src_buf = g_slice_alloc (4 * total_pixels * sizeof (gfloat));
+  dst_buf = g_slice_alloc (4 * n_pixels * sizeof (gfloat));
+
+
+  gegl_buffer_copy(input, NULL, tmp, NULL);
+
+  for (i = 0; i < o->repeat; i++)
+  {
+    n_pixels = result->width * result->height;
+
+    gegl_buffer_get (tmp, &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--)
+      {
+        if (g_rand_double_range (gr, 0.0, 100.0) <= o->pct_random)
+        {
+          k = g_rand_int_range (gr, 0, 10);
+
+          for (b = 0; b < 4; b++)
+          {
+            switch (k )
+            {
+              case 0:
+                out_pixel[b] = in_pixel[b - src_rect.width*4 - 4];
+                break;
+              case 9:
+                  out_pixel[b] = in_pixel[b - src_rect.width*4 + 4];
+                break;
+              default:
+                  out_pixel[b] = in_pixel[b - src_rect.width*4];
+                break;
+            }
+          }
+        }
+        else
+        {
+          for (b = 0; b < 4; b++)
+          {
+            out_pixel[b] = in_pixel[b];
+          }
+        }
+
+        if (n_pixels % width == 0)
+          in_pixel += 12;
+        else
+          in_pixel  += 4;
+        out_pixel += 4;
+      }
+
+    gegl_buffer_set (tmp, result, 0, babl_format ("RGBA float"), dst_buf, GEGL_AUTO_ROWSTRIDE);
+
+  }
+
+   gegl_buffer_copy(tmp, NULL, output, NULL);
+
+  g_slice_free1 (4 * total_pixels * sizeof (gfloat), src_buf);
+  g_slice_free1 (4 * n_pixels * sizeof (gfloat), dst_buf);
+
+  return TRUE;
+}
+
+static void
+gegl_chant_class_init (GeglChantClass *klass)
+{
+  GObjectClass             *object_class;
+  GeglOperationClass       *operation_class;
+  GeglOperationFilterClass *filter_class;
+
+
+  object_class    = G_OBJECT_CLASS (klass);
+  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-Slur",
+      "categories", "noise",
+      "description", _("Randomly slide some pixels downward (similar to melting)"),
+      NULL);
+}
+
+#endif



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]