From 3af9cdb7e384c15a261938a5c97452a0550afcc4 Mon Sep 17 00:00:00 2001
From: Isaac Wagner <isaacbwagner@gmail.com>
Date: Thu, 5 Apr 2012 18:26:37 -0400
Subject: [PATCH] added shotgun operation

---
 configure.ac                 |    1 +
 operations/Makefile.am       |    3 +-
 operations/isaac/Makefile.am |    4 +
 operations/isaac/shotgun.c   |  281 ++++++++++++++++++++++++++++++++++++++++++
 operations/isaac/shotgun.h   |   39 ++++++
 5 files changed, 327 insertions(+), 1 deletion(-)
 create mode 100644 operations/isaac/Makefile.am
 create mode 100644 operations/isaac/shotgun.c
 create mode 100644 operations/isaac/shotgun.h

diff --git a/configure.ac b/configure.ac
index a2871bc..4c0b8af 100644
--- a/configure.ac
+++ b/configure.ac
@@ -1088,6 +1088,7 @@ libs/rgbe/Makefile
 operations/Makefile
 operations/core/Makefile
 operations/common/Makefile
+operations/isaac/Makefile
 operations/common/perlin/Makefile
 operations/external/Makefile
 operations/generated/Makefile
diff --git a/operations/Makefile.am b/operations/Makefile.am
index b96e91e..f9bf0cf 100644
--- a/operations/Makefile.am
+++ b/operations/Makefile.am
@@ -3,7 +3,8 @@ SUBDIRS = \
 	common		\
 	generated	\
 	transform \
-	external
+	external \
+	isaac
 
 if ENABLE_WORKSHOP
 SUBDIRS+= workshop
diff --git a/operations/isaac/Makefile.am b/operations/isaac/Makefile.am
new file mode 100644
index 0000000..af85a56
--- /dev/null
+++ b/operations/isaac/Makefile.am
@@ -0,0 +1,4 @@
+include $(top_srcdir)/operations/Makefile-operations.am
+
+AM_CPPFLAGS += \
+	-I$(top_srcdir)/operations/isaac
\ No newline at end of file
diff --git a/operations/isaac/shotgun.c b/operations/isaac/shotgun.c
new file mode 100644
index 0000000..311b50c
--- /dev/null
+++ b/operations/isaac/shotgun.c
@@ -0,0 +1,281 @@
+#include "shotgun.h"
+#include <math.h>
+
+//process function prototype
+static gboolean shotgun_process (GeglOperation *self, void *in_buf, void *out_buf, glong samples, const GeglRectangle *roi, gint level);
+
+//gobject magic to register Shotgun class
+//G_DEFINE_TYPE (Shotgun, shotgun, GEGL_TYPE_OPERATION_POINT_FILTER);
+
+static void shotgun_init (Shotgun *self); 
+static void shotgun_class_init (ShotgunClass *klass); 
+
+static gpointer shotgun_parent_class = ((void *)0); 
+
+static void shotgun_class_intern_init (gpointer klass) { 
+       shotgun_parent_class = g_type_class_peek_parent (klass); 
+       shotgun_class_init ((ShotgunClass*) klass); 
+} 
+
+GType shotgun_get_type (void) { 
+  /*  static volatile gsize g_define_type_id__volatile = 0;
+	      	       
+  if (g_once_init_enter (&g_define_type_id__volatile)) { 
+    GType g_define_type_id = g_type_register_static_simple ((gegl_operation_point_filter_get_type ()), 
+							    "Shotgun",
+							    sizeof (ShotgunClass), 
+							    (GClassInitFunc) shotgun_class_intern_init, 
+							    sizeof (Shotgun), 
+							    (GInstanceInitFunc) shotgun_init, 
+							    (GTypeFlags) 0);
+    g_once_init_leave (&g_define_type_id__volatile, g_define_type_id);
+    } return g_define_type_id__volatile;*/
+  return type_id;
+};
+
+enum {
+  PROP_0,
+  PROP_SEED,
+  PROP_SCATTER_RADIUS,
+  PROP_X,
+  PROP_Y,
+  N_PROPERTIES
+};
+
+static GParamSpec *obj_properties[N_PROPERTIES] = {NULL, };
+
+static void shotgun_set_property ( GObject      *object,
+				  guint         property_id,
+				  const GValue *value,
+				  GParamSpec   *pspec) {
+  Shotgun *self = SHOTGUN(object);
+  switch(property_id) {
+  case PROP_SEED:
+    self->seed = g_value_get_uint(value);
+    g_print("shotgun: seed=%d\n", self->seed);
+    break;
+case PROP_SCATTER_RADIUS:
+    self->radius = g_value_get_uint(value);
+    g_print("shotgun: scatter-radius=%d\n", self->radius);
+    break;
+  case PROP_X:
+    self->x = g_value_get_uint(value);
+    g_print("shotgun: x=%d\n", self->x);
+    break;
+  case PROP_Y:
+    self->y = g_value_get_uint(value);
+    g_print("shotgun: y=%d\n", self->y);
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+    break;
+  }
+}
+
+static void shotgun_get_property ( GObject    *object,
+				   guint       property_id,
+				   GValue     *value,
+				   GParamSpec *pspec) {
+  Shotgun *self = SHOTGUN(object);
+  switch(property_id) {
+  case PROP_SEED:
+    g_value_set_uint(value, self->seed);
+    break;
+  case PROP_SCATTER_RADIUS:
+    g_value_set_uint(value, self->radius);
+    break;
+  case PROP_X:
+    g_value_set_uint(value, self->x);
+    break;
+  case PROP_Y:
+    g_value_set_uint(value, self->y);
+    break;
+  default:
+    G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+    break;
+  }
+}
+
+static void shotgun_class_finalize(ShotgunClass* klass) { }
+
+static void shotgun_class_init (ShotgunClass* klass) {
+  GObjectClass			*object_class	    = G_OBJECT_CLASS(klass);
+  GeglOperationClass		*operation_class    = GEGL_OPERATION_CLASS(klass);
+  GeglOperationPointFilterClass *point_filter_class = GEGL_OPERATION_POINT_FILTER_CLASS(klass);
+
+  object_class->set_property = shotgun_set_property;
+  object_class->get_property = shotgun_get_property;
+
+  //???
+  operation_class->name = "gegl:shotgun";
+  operation_class->categories = "filter";
+  operation_class->description = "Shoot an image full of holes";
+  
+  point_filter_class->process = shotgun_process;
+
+  obj_properties[PROP_SEED] =
+    g_param_spec_uint ("seed",
+		       "Seed property",
+		       "Set the random seed",
+		       0,
+		       G_MAXUINT,
+		       57473,
+		       G_PARAM_READWRITE);
+
+  obj_properties[PROP_SCATTER_RADIUS] =
+    g_param_spec_uint ("scatter-radius",
+		       "Scatter radius property",
+		       "Set the radius within which holes will be drawn",
+		       0,
+		       G_MAXUINT,
+		       200,
+		       G_PARAM_READWRITE);
+
+  obj_properties[PROP_X] =
+    g_param_spec_uint ("x",
+		       "x property",
+		       "Center of shotgun blast",
+		       0,
+		       G_MAXUINT,
+		       0,
+		       G_PARAM_READWRITE);
+
+  obj_properties[PROP_Y] =
+    g_param_spec_uint ("y",
+		       "y property",
+		       "Center of shotgun blast",
+		       0,
+		       G_MAXUINT,
+		       0,
+		       G_PARAM_READWRITE);
+
+  g_object_class_install_properties (object_class,
+				     N_PROPERTIES,
+				     obj_properties);
+				  
+}				//shotgun_init
+
+static void shotgun_init(Shotgun* self) { }
+
+
+//overriding of GeglOperationPointFilter's process function
+static gboolean shotgun_process (GeglOperation *self, void *in_buf, void *out_buf, glong samples, const GeglRectangle *roi, gint level) {
+  GeglOperationPointFilter *point = GEGL_OPERATION_POINT_FILTER (self);
+  Shotgun *shotgun = SHOTGUN(self);
+
+  gfloat *src = in_buf;
+  gfloat *dest = out_buf;
+
+  guint seed = shotgun->seed;
+  guint radius = shotgun->radius;
+  guint center_x = shotgun->x;
+  guint center_y = shotgun->y;
+
+  int count = radius/10;
+  GSList	*centers_x = NULL;
+  GSList	*centers_y = NULL;
+  GSList	*radii	   = NULL;
+  srand(seed);
+
+  int i;
+  for(i = 0; i < count; i++) {
+    gfloat dist = (float)rand()/(float)RAND_MAX*(float)radius;
+    gfloat angle = (float)rand()/(float)RAND_MAX*3.14159*2.0;
+    /*    centers_x = g_slist_append(centers_x, GINT_TO_POINTER(rand()%500));
+    centers_y = g_slist_append(centers_y, GINT_TO_POINTER(rand()%500));*/
+
+    centers_x = g_slist_append(centers_x, GINT_TO_POINTER(center_x+dist*cos(angle)));
+    centers_y = g_slist_append(centers_y, GINT_TO_POINTER(center_y+dist*sin(angle)));
+
+    radii = g_slist_append(radii, GINT_TO_POINTER(rand()%10+5));
+  }
+  
+
+  int x = roi->x;
+  int y = roi->y;
+  while (samples--) {
+
+
+    gboolean in_circle = FALSE;
+
+    GSList *cx_e = centers_x;
+    GSList *cy_e = centers_y;
+    GSList *r_e = radii;
+
+    int i;
+    for(i = 0; i < count && !in_circle; i++) {
+      gint cx = GPOINTER_TO_INT(cx_e->data);
+      gint cy = GPOINTER_TO_INT(cy_e->data);
+      gint r = GPOINTER_TO_INT(r_e->data);
+
+      if(((x-cx)*(x-cx)+(y-cy)*(y-cy))<=r*r)
+	//if(x == cx && y == cy)
+	in_circle = TRUE;
+
+      cx_e = cx_e->next;
+      cy_e = cy_e->next;
+      r_e = r_e->next;
+    }
+
+    if(in_circle && roi->width > 0 && roi->height > 0) {
+      dest[0] = 1;
+      dest[1] = 1;
+      dest[2] = 1;
+      dest[3] = 1;
+    } else {
+      dest[0] = src[0];
+      dest[1] = src[1];
+      dest[2] = src[2];
+      dest[3] = src[3];
+    }
+
+    src += 4;
+    dest += 4;
+
+    x++;
+
+    if(x >= roi->x+roi->width) {
+      x = roi->x;
+      y++;
+    }
+  }
+
+  return TRUE;
+}
+
+G_MODULE_EXPORT const GeglModuleInfo *
+gegl_module_query (GTypeModule *module)
+{
+  return &modinfo;
+}
+
+static gboolean registered = FALSE;
+G_MODULE_EXPORT gboolean
+gegl_module_register (GTypeModule *module)
+{
+  if(registered)
+    return;
+  registered = TRUE;
+  g_print("shotgun: gegl_module_register()\n");
+  const GTypeInfo g_type_info =
+    {
+      sizeof(ShotgunClass),
+      (GBaseInitFunc) NULL,
+      (GBaseFinalizeFunc) NULL,
+      (GClassInitFunc) shotgun_class_intern_init,
+      (GClassFinalizeFunc) shotgun_class_finalize,
+      NULL,
+      sizeof(Shotgun),
+      0,
+      (GInstanceInitFunc) shotgun_init,
+      NULL
+    };
+
+  //  g_type_module_register_type(module,
+  type_id = gegl_module_register_type(module,
+			      GEGL_TYPE_OPERATION_POINT_FILTER,
+			      "Shotgun",
+			      &g_type_info,
+			      0);
+  return TRUE;
+}
diff --git a/operations/isaac/shotgun.h b/operations/isaac/shotgun.h
new file mode 100644
index 0000000..c97da83
--- /dev/null
+++ b/operations/isaac/shotgun.h
@@ -0,0 +1,39 @@
+#ifndef __SHOTGUN_H__
+#define __SHOTGUN_H__
+
+#include <gegl.h>
+#include <glib-object.h> //maybe already included
+#include <gegl-plugin.h>
+
+#define TYPE_SHOTGUN (shotgun_get_type())
+#define SHOTGUN(obj)                  (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_SHOTGUN, Shotgun))
+#define IS_SHOTGUN(obj)               (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_SHOTGUN))
+#define SHOTGUN_CLASS(klass)          (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_SHOTGUN, ShotgunClass))
+#define IS_SHOTGUN_CLASS(klass)       (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_SHOTGUN))
+#define SHOTGUN_GET_CLASS(obj)        (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_SHOTGUN, ShotgunClass))
+
+typedef struct _Shotgun Shotgun;
+typedef struct _ShotgunClass ShotgunClass;
+
+static GType type_id = 0;
+
+struct _Shotgun {
+  GeglOperationPointFilter parent_instance;
+  guint seed, radius, x, y;
+};
+
+struct _ShotgunClass {
+  GeglOperationPointFilterClass parent_class;
+};
+
+GType shotgun_get_type(void);// G_GNUC_CONST;
+
+gboolean                gegl_module_register (GTypeModule *module);
+const GeglModuleInfo  * gegl_module_query    (GTypeModule *module);
+
+static const GeglModuleInfo modinfo =
+{
+  GEGL_MODULE_ABI_VERSION
+};
+
+#endif
-- 
1.7.9.5

