[gimp/soc-2010-cage] add a gegl operator that compute the coeffcient for a cage transform



commit 7699fec754924dc5bc2e65c484bffef36bc790ca
Author: Michael Muré <batolettre gmail com>
Date:   Fri Jul 23 21:31:28 2010 +0200

    add a gegl operator that compute the coeffcient for a cage transform

 app/gegl/Makefile.am                 |    2 +
 app/gegl/gimp-gegl-types.h           |    1 +
 app/gegl/gimpoperationcagecoefcalc.c |  227 ++++++++++++++++++++++++++++++++++
 app/gegl/gimpoperationcagecoefcalc.h |   59 +++++++++
 app/gegl/makefile.msc                |    1 +
 5 files changed, 290 insertions(+), 0 deletions(-)
---
diff --git a/app/gegl/Makefile.am b/app/gegl/Makefile.am
index 2fee4f9..515d202 100644
--- a/app/gegl/Makefile.am
+++ b/app/gegl/Makefile.am
@@ -55,6 +55,8 @@ libappgegl_a_SOURCES = \
 	gimpoperationcolorize.h		\
 	gimpoperationcage.c			\
 	gimpoperationcage.h			\
+	gimpoperationcagecoefcalc.c	\
+	gimpoperationcagecoefcalc.h	\
 	gimpoperationcurves.c		\
 	gimpoperationcurves.h		\
 	gimpoperationdesaturate.c	\
diff --git a/app/gegl/gimp-gegl-types.h b/app/gegl/gimp-gegl-types.h
index d2bfe2c..b28ff60 100644
--- a/app/gegl/gimp-gegl-types.h
+++ b/app/gegl/gimp-gegl-types.h
@@ -32,6 +32,7 @@ typedef struct _GimpOperationPointFilter      GimpOperationPointFilter;
 typedef struct _GimpOperationColorBalance     GimpOperationColorBalance;
 typedef struct _GimpOperationColorize         GimpOperationColorize;
 typedef struct _GimpOperationCage             GimpOperationCage;
+typedef struct _GimpOperationCageCoefCalc     GimpOperationCageCoefCalc;
 typedef struct _GimpOperationCurves           GimpOperationCurves;
 typedef struct _GimpOperationDesaturate       GimpOperationDesaturate;
 typedef struct _GimpOperationHueSaturation    GimpOperationHueSaturation;
diff --git a/app/gegl/gimpoperationcagecoefcalc.c b/app/gegl/gimpoperationcagecoefcalc.c
new file mode 100644
index 0000000..3b9059f
--- /dev/null
+++ b/app/gegl/gimpoperationcagecoefcalc.c
@@ -0,0 +1,227 @@
+/* GIMP - The GNU Image Manipulation Program
+ *
+ * gimpoperationcagecoefcalc.c
+ * Copyright (C) 2010 Michael Muré <batolettre gmail com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <gegl.h>
+#include "gimp-gegl-types.h"
+#include <gegl-buffer-iterator.h>
+
+#include "libgimpmath/gimpmath.h"
+
+#include "gimpoperationcagecoefcalc.h"
+#include "gimpcageconfig.h"
+
+
+static void       gimp_operation_cage_coef_calc_prepare         (GeglOperation        *operation);
+static void       gimp_operation_cage_coef_calc_finalize        (GObject              *object);
+static void       gimp_operation_cage_coef_calc_get_property    (GObject              *object,
+                                                                 guint                 property_id,
+                                                                 GValue               *value,
+                                                                 GParamSpec           *pspec);
+static void       gimp_operation_cage_coef_calc_set_property    (GObject              *object,
+                                                                 guint                 property_id,
+                                                                 const GValue         *value,
+                                                                 GParamSpec           *pspec);
+static gboolean   gimp_operation_cage_coef_calc_process         (GeglOperation        *operation,
+                                                                 GeglBuffer           *output,
+                                                                 const GeglRectangle  *roi);
+
+                                        
+G_DEFINE_TYPE (GimpOperationCageCoefCalc, gimp_operation_cage_coef_calc,
+               GEGL_TYPE_OPERATION_SOURCE)
+
+#define parent_class gimp_operation_cage_coef_calc_parent_class
+
+
+static void
+gimp_operation_cage_coef_calc_class_init (GimpOperationCageCoefCalcClass *klass)
+{
+  GObjectClass             *object_class    = G_OBJECT_CLASS (klass);
+  GeglOperationSourceClass *source_class    = GEGL_OPERATION_SOURCE_CLASS (klass);
+  GeglOperationClass       *operation_class = GEGL_OPERATION_CLASS (klass);
+  
+  /* FIXME: wrong categories and name, to appears in the gegl tool */
+  operation_class->name               = "gegl:cage_coef_calc";
+  operation_class->categories         = "color";
+  operation_class->description        = "GIMP cage transform";
+
+  operation_class->prepare            = gimp_operation_cage_coef_calc_prepare;
+  operation_class->no_cache           = FALSE;
+  operation_class->get_cached_region  = NULL;
+  
+  source_class->process               = gimp_operation_cage_coef_calc_process;
+
+  object_class->get_property          = gimp_operation_cage_coef_calc_get_property;
+  object_class->set_property          = gimp_operation_cage_coef_calc_set_property;
+  object_class->finalize              = gimp_operation_cage_coef_calc_finalize;
+  
+  g_object_class_install_property (object_class,
+                                   GIMP_OPERATION_CAGE_COEF_CALC_PROP_CONFIG,
+                                   g_param_spec_object ("config", NULL, NULL,
+                                                        GIMP_TYPE_CAGE_CONFIG,
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_CONSTRUCT));
+}
+
+static void
+gimp_operation_cage_coef_calc_init (GimpOperationCageCoefCalc *self)
+{
+
+}
+
+static void
+gimp_operation_cage_coef_calc_prepare (GeglOperation *operation)
+{
+  GimpOperationCageCoefCalc   *occc   = GIMP_OPERATION_CAGE_COEF_CALC (operation);
+  GimpCageConfig              *config = GIMP_CAGE_CONFIG (occc->config);
+  
+  gegl_operation_set_format (operation, "output", babl_format_n (babl_type ("float"), 2 * config->cage_vertice_number));
+}
+
+static void
+gimp_operation_cage_coef_calc_finalize (GObject *object)
+{
+  GimpOperationCageCoefCalc *self = GIMP_OPERATION_CAGE_COEF_CALC (object);
+
+  if (self->config)
+    {
+      g_object_unref (self->config);
+      self->config = NULL;
+    }
+
+  G_OBJECT_CLASS (parent_class)->finalize (object);
+}
+
+static void
+gimp_operation_cage_coef_calc_get_property  (GObject    *object,
+                                             guint       property_id,
+                                             GValue     *value,
+                                             GParamSpec *pspec)
+{
+  GimpOperationCageCoefCalc   *self   = GIMP_OPERATION_CAGE_COEF_CALC (object);
+
+  switch (property_id)
+    {
+    case GIMP_OPERATION_CAGE_COEF_CALC_PROP_CONFIG:
+      g_value_set_object (value, self->config);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gimp_operation_cage_coef_calc_set_property  (GObject      *object,
+                                             guint         property_id,
+                                             const GValue *value,
+                                             GParamSpec   *pspec)
+{
+  GimpOperationCageCoefCalc   *self   = GIMP_OPERATION_CAGE_COEF_CALC (object);
+
+  switch (property_id)
+    {
+    case GIMP_OPERATION_CAGE_COEF_CALC_PROP_CONFIG:
+      if (self->config)
+        g_object_unref (self->config);
+      self->config = g_value_dup_object (value);
+      break;
+
+   default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+
+static gboolean
+gimp_operation_cage_coef_calc_process (GeglOperation       *operation,
+                                       GeglBuffer          *output,
+                                       const GeglRectangle *roi)
+{
+  GimpOperationCageCoefCalc   *occc   = GIMP_OPERATION_CAGE_COEF_CALC (operation);
+  GimpCageConfig              *config = GIMP_CAGE_CONFIG (occc->config);
+  
+  Babl *format = babl_format_n (babl_type ("float"), 2 * config->cage_vertice_number);
+  
+  GeglBufferIterator *it;
+  
+  if (! config)
+    return FALSE;
+  
+  it = gegl_buffer_iterator_new (output, roi, format, GEGL_BUFFER_READWRITE);
+  
+  while (gegl_buffer_iterator_next (it))
+  {
+    /* iterate inside the roi */
+    gint  n_pixels = it->length;
+    gint  x = it->roi->x; /* initial x                   */
+    gint  y = it->roi->y; /*           and y coordinates */
+    gint  j;
+    
+    gfloat      *coef = it->data[0];
+  
+    while(n_pixels--)
+    {
+      for( j = 0; j < config->cage_vertice_number; j++)
+      {
+        GimpVector2 v1,v2,a,b;
+        gfloat Q,S,R,BA,SRT,L0,L1,A0,A1,A10,L10;
+      
+        v1 = config->cage_vertices[j];
+        v2 = config->cage_vertices[(j+1)%config->cage_vertice_number];
+      
+        a.x = v2.x - v1.x;
+        a.y = v2.y - v1.y;
+        b.x = v1.x - x;
+        b.y = v1.y - y;
+        Q = a.x * a.x + a.y * a.y;
+        S = b.x * b.x + b.y * b.y;
+        R = 2.0 * (a.x * b.x + a.y * b.y);
+        BA = b.x * a.y - b.y * a.x;
+        SRT = sqrt(4.0 * S * Q - R * R);
+        L0 = log(S);
+        L1 = log(S + Q + R);
+        A0 = atan2(R, SRT) / SRT;
+        A1 = atan2(2.0 * Q + R, SRT) / SRT;
+        A10 = A1 - A0;
+        L10 = L1 - L0;
+        
+        coef[j + config->cage_vertice_number] = 1.0 / (4.0 * M_PI) * ((4.0*S-R*R/Q) * A10 + R / (2.0 * Q) * L10 + L1 - 2.0);
+        
+        coef[j] += BA / (2.0 * M_PI) * (L10 /(2.0*Q) - A10 * (2.0 + R / Q));
+        coef[(j+1)%config->cage_vertice_number] -= BA / (2.0 * M_PI) * (L10 / (2.0 * Q) - A10 * R / Q);
+      }
+      
+      coef += 2 * config->cage_vertice_number;
+
+      /* update x and y coordinates */
+      x++;
+      if (x >= (it->roi->x + it->roi->width))
+      {
+        x = it->roi->x;
+        y++;
+      }
+    }
+  }
+  
+  return TRUE;
+}
diff --git a/app/gegl/gimpoperationcagecoefcalc.h b/app/gegl/gimpoperationcagecoefcalc.h
new file mode 100644
index 0000000..f652f51
--- /dev/null
+++ b/app/gegl/gimpoperationcagecoefcalc.h
@@ -0,0 +1,59 @@
+/* GIMP - The GNU Image Manipulation Program
+ *
+ * gimpoperationcagecoefcalc.h
+ * Copyright (C) 2010 Michael Muré <batolettre gmail com>
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef __GIMP_OPERATION_CAGE_COEF_CALC_H__
+#define __GIMP_OPERATION_CAGE_COEF_CALC_H__
+
+#include <gegl-plugin.h>
+#include <operation/gegl-operation-source.h>
+
+enum
+{
+  GIMP_OPERATION_CAGE_COEF_CALC_PROP_0,
+  GIMP_OPERATION_CAGE_COEF_CALC_PROP_CONFIG
+};
+
+
+#define GIMP_TYPE_OPERATION_CAGE_COEF_CALC            (gimp_operation_cage_coef_calc_get_type ())
+#define GIMP_OPERATION_CAGE_COEF_CALC(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_OPERATION_CAGE_COEF_CALC, GimpOperationCageCoefCalc))
+#define GIMP_OPERATION_CAGE_COEF_CALC_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass),  GIMP_TYPE_OPERATION_CAGE_COEF_CALC, GimpOperationCageCoefCalcClass))
+#define GIMP_IS_OPERATION_CAGE_COEF_CALC(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_OPERATION_CAGE_COEF_CALC))
+#define GIMP_IS_OPERATION_CAGE_COEF_CALC_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),  GIMP_TYPE_OPERATION_CAGE_COEF_CALC))
+#define GIMP_OPERATION_CAGE_COEF_CALC_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),  GIMP_TYPE_OPERATION_CAGE_COEF_CALC, GimpOperationCageCoefCalcClass))
+
+
+typedef struct _GimpOperationCageCoefCalcClass GimpOperationCageCoefCalcClass;
+
+struct _GimpOperationCageCoefCalc
+{
+  GeglOperationSource   parent_instance;
+  
+  GimpCageConfig       *config;
+};
+
+struct _GimpOperationCageCoefCalcClass
+{
+  GeglOperationSourceClass  parent_class;
+};
+
+
+GType   gimp_operation_cage_coef_calc_get_type (void) G_GNUC_CONST;
+
+
+#endif /* __GIMP_OPERATION_CAGE_COEF_CALC_H__ */
diff --git a/app/gegl/makefile.msc b/app/gegl/makefile.msc
index ab5b2dd..1b08861 100644
--- a/app/gegl/makefile.msc
+++ b/app/gegl/makefile.msc
@@ -38,6 +38,7 @@ OBJECTS = \
 	gimplevelsconfig.obj \
 	gimpoperationcolorbalance.obj \
 	gimpoperationcage.obj  \
+	gimpoperationcagecoefcalc.obj	\
 	gimpoperationcurves.obj \
 	gimpoperationhuesaturation.obj \
 	gimpoperationlevels.obj \



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