[gimp/blend-tool-fun: 3/3] DON'T READ THIS COMMIT IT'S UGLY



commit a8ed949b1669795645009020f94e8b186ac165f9
Author: Michael Henning <drawoc darkrefraction com>
Date:   Thu Jun 12 20:57:26 2014 -0400

    DON'T READ THIS COMMIT IT'S UGLY

 app/core/gimpgradient.c             |    3 +-
 app/operations/Makefile.am          |    1 +
 app/operations/gimp-operations.c    |    2 +
 app/operations/gimpoperationblend.c | 1062 +++++++++++++++++++++++++++++++++++
 app/operations/gimpoperationblend.h |   56 ++
 5 files changed, 1123 insertions(+), 1 deletions(-)
---
diff --git a/app/core/gimpgradient.c b/app/core/gimpgradient.c
index 85884eb..0faa0e9 100644
--- a/app/core/gimpgradient.c
+++ b/app/core/gimpgradient.c
@@ -395,7 +395,8 @@ gimp_gradient_get_color_at (GimpGradient        *gradient,
   GimpRGB  rgb;
 
   g_return_val_if_fail (GIMP_IS_GRADIENT (gradient), NULL);
-  g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
+//XXX
+  //g_return_val_if_fail (GIMP_IS_CONTEXT (context), NULL);
   g_return_val_if_fail (color != NULL, NULL);
 
   pos = CLAMP (pos, 0.0, 1.0);
diff --git a/app/operations/Makefile.am b/app/operations/Makefile.am
index d5998fd..4ee2529 100644
--- a/app/operations/Makefile.am
+++ b/app/operations/Makefile.am
@@ -68,6 +68,7 @@ libappoperations_generic_a_sources = \
        gimpoperationshrink.h                   \
        gimpoperationthresholdalpha.c           \
        gimpoperationthresholdalpha.h           \
+gimpoperationblend.c gimpoperationblend.h \
        \
        gimpoperationpointfilter.c              \
        gimpoperationpointfilter.h              \
diff --git a/app/operations/gimp-operations.c b/app/operations/gimp-operations.c
index 6b35ec9..ed6eed9 100644
--- a/app/operations/gimp-operations.c
+++ b/app/operations/gimp-operations.c
@@ -27,6 +27,7 @@
 
 #include "gimp-operations.h"
 
+#include "gimpoperationblend.h"
 #include "gimpoperationborder.h"
 #include "gimpoperationcagecoefcalc.h"
 #include "gimpoperationcagetransform.h"
@@ -94,6 +95,7 @@ gimp_operations_init (void)
   g_type_class_ref (GIMP_TYPE_OPERATION_SHAPEBURST);
   g_type_class_ref (GIMP_TYPE_OPERATION_SHRINK);
   g_type_class_ref (GIMP_TYPE_OPERATION_THRESHOLD_ALPHA);
+g_type_class_ref (GIMP_TYPE_OPERATION_BLEND);
 
   g_type_class_ref (GIMP_TYPE_OPERATION_BRIGHTNESS_CONTRAST);
   g_type_class_ref (GIMP_TYPE_OPERATION_COLOR_BALANCE);
diff --git a/app/operations/gimpoperationblend.c b/app/operations/gimpoperationblend.c
new file mode 100644
index 0000000..4237f0e
--- /dev/null
+++ b/app/operations/gimpoperationblend.c
@@ -0,0 +1,1062 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * gimpoperationblend.c
+ * Copyright (C) XXX foobar
+ *
+ * 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 <cairo.h>
+#include <gegl.h>
+#include <gdk-pixbuf/gdk-pixbuf.h>
+
+#include "libgimpcolor/gimpcolor.h"
+#include "libgimpmath/gimpmath.h"
+
+#include "operations-types.h"
+
+#include "core/gimp.h"
+#include "core/gimpgradient.h"
+
+#include "gimpoperationblend.h"
+
+#include "gimp-intl.h"
+
+enum
+{
+  PROP_0,
+  PROP_START_X,
+  PROP_START_Y,
+  PROP_END_X,
+  PROP_END_Y,
+  PROP_GRADIENT_TYPE
+};
+
+//#define USE_GRADIENT_CACHE 1
+
+
+typedef struct
+{
+  GimpGradient     *gradient;
+  GimpContext      *context;
+  gboolean          reverse;
+#ifdef USE_GRADIENT_CACHE
+  GimpRGB          *gradient_cache;
+  gint              gradient_cache_size;
+#endif
+  gdouble           offset;
+  gdouble           sx, sy;
+  GimpGradientType  gradient_type;
+  gdouble           dist;
+  gdouble           vec[2];
+  GimpRepeatMode    repeat;
+  GRand            *seed;
+  GeglBuffer       *dist_buffer;
+} RenderBlendData;
+
+
+typedef struct
+{
+  GeglBuffer    *buffer;
+  gfloat        *row_data;
+  gint           width;
+  GRand         *dither_rand;
+} PutPixelData;
+
+
+/*  local function prototypes  */
+
+static void     gimp_operation_blend_get_property (GObject      *object,
+                                                    guint         property_id,
+                                                    GValue       *value,
+                                                    GParamSpec   *pspec);
+static void     gimp_operation_blend_set_property (GObject      *object,
+                                                    guint         property_id,
+                                                    const GValue *value,
+                                                    GParamSpec   *pspec);
+
+/*
+static GeglRectangle
+gimp_operation_blend_get_required_for_output (GeglOperation       *self,
+                                               const gchar         *input_pad,
+                                               const GeglRectangle *roi);
+static GeglRectangle
+      gimp_operation_blend_get_cached_region (GeglOperation       *self,
+                                               const GeglRectangle *roi);*/
+static void     gimp_operation_blend_prepare (GeglOperation       *operation);
+/*static gboolean gimp_operation_blend_process (GeglOperation       *operation,
+                                               GeglBuffer          *input,
+                                               GeglBuffer          *output,
+                                               const GeglRectangle *roi,
+                                               gint                 level);
+*/
+static gdouble  gradient_calc_conical_sym_factor  (gdouble   dist,
+                                                   gdouble  *axis,
+                                                   gdouble   offset,
+                                                   gdouble   x,
+                                                   gdouble   y);
+static gdouble  gradient_calc_conical_asym_factor (gdouble   dist,
+                                                   gdouble  *axis,
+                                                   gdouble   offset,
+                                                   gdouble   x,
+                                                   gdouble   y);
+static gdouble  gradient_calc_square_factor       (gdouble   dist,
+                                                   gdouble   offset,
+                                                   gdouble   x,
+                                                   gdouble   y);
+static gdouble  gradient_calc_radial_factor       (gdouble   dist,
+                                                   gdouble   offset,
+                                                   gdouble   x,
+                                                   gdouble   y);
+static gdouble  gradient_calc_linear_factor       (gdouble   dist,
+                                                   gdouble  *vec,
+                                                   gdouble   offset,
+                                                   gdouble   x,
+                                                   gdouble   y);
+static gdouble  gradient_calc_bilinear_factor     (gdouble   dist,
+                                                   gdouble  *vec,
+                                                   gdouble   offset,
+                                                   gdouble   x,
+                                                   gdouble   y);
+static gdouble  gradient_calc_spiral_factor       (gdouble   dist,
+                                                   gdouble  *axis,
+                                                   gdouble   offset,
+                                                   gdouble   x,
+                                                   gdouble   y,
+                                                   gboolean  clockwise);
+
+static gdouble  gradient_calc_shapeburst_angular_factor   (GeglBuffer *dist_buffer,
+                                                           gdouble     x,
+                                                           gdouble     y);
+static gdouble  gradient_calc_shapeburst_spherical_factor (GeglBuffer *dist_buffer,
+                                                           gdouble     x,
+                                                           gdouble     y);
+static gdouble  gradient_calc_shapeburst_dimpled_factor   (GeglBuffer *dist_buffer,
+                                                           gdouble     x,
+                                                           gdouble     y);
+
+static void     gradient_render_pixel       (gdouble              x,
+                                             gdouble              y,
+                                             GimpRGB             *color,
+                                             gpointer             render_data);
+
+static void     gradient_put_pixel          (gint                 x,
+                                             gint                 y,
+                                             GimpRGB             *color,
+                                             gpointer             put_pixel_data);
+
+////// XXX: Reorder stuff
+
+static gboolean
+gimp_operation_blend_process (GeglOperation       *operation,
+         GeglBuffer          *output,
+         const GeglRectangle *result,
+         gint                 level);
+
+
+
+
+G_DEFINE_TYPE (GimpOperationBlend, gimp_operation_blend,
+               GEGL_TYPE_OPERATION_SOURCE)
+
+#define parent_class gimp_operation_blend_parent_class
+
+
+static GeglRectangle
+get_bounding_box (GeglOperation *operation)
+{
+  return gegl_rectangle_infinite_plane ();
+}
+
+static void
+gimp_operation_blend_class_init (GimpOperationBlendClass *klass)
+{
+  GObjectClass             *object_class    = G_OBJECT_CLASS (klass);
+  GeglOperationClass       *operation_class = GEGL_OPERATION_CLASS (klass);
+  GeglOperationSourceClass *source_class    = GEGL_OPERATION_SOURCE_CLASS (klass);
+
+  object_class->set_property   = gimp_operation_blend_set_property;
+  object_class->get_property   = gimp_operation_blend_get_property;
+
+  gegl_operation_class_set_keys (operation_class,
+                                 "name",        "gegl:blend", // XXX
+                                 "categories",  "gegl",//XXX
+                                 "description", "GIMP Blend operation",
+                                 NULL);
+
+
+  operation_class->prepare                 = gimp_operation_blend_prepare;
+operation_class->get_bounding_box  =get_bounding_box;
+  /*operation_class->get_required_for_output = gimp_operation_blend_get_required_for_output;
+  operation_class->get_cached_region       = gimp_operation_blend_get_cached_region;
+*/
+  //filter_class->process                    = gimp_operation_blend_process;
+  source_class->process                    = gimp_operation_blend_process;
+
+  g_object_class_install_property (object_class, PROP_START_X,
+                                   g_param_spec_double ("start_x",
+                                                        "Start X",
+                                                        "X coordinate of the first point",
+                                                        -G_MAXDOUBLE, G_MAXDOUBLE, 0,
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_CONSTRUCT));
+
+  g_object_class_install_property (object_class, PROP_START_Y,
+                                   g_param_spec_double ("start_y",
+                                                        "Start Y",
+                                                        "Y coordinate of the first point",
+                                                        -G_MAXDOUBLE, G_MAXDOUBLE, 0,
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_CONSTRUCT));
+
+  g_object_class_install_property (object_class, PROP_END_X,
+                                   g_param_spec_double ("end_x",
+                                                        "End X",
+                                                        "X coordinate of the second point",
+                                                        -G_MAXDOUBLE, G_MAXDOUBLE, 200,
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_CONSTRUCT));
+
+  g_object_class_install_property (object_class, PROP_END_Y,
+                                   g_param_spec_double ("end_y",
+                                                        "End Y",
+                                                        "Y coordinate of the second point",
+                                                        -G_MAXDOUBLE, G_MAXDOUBLE, 200,
+                                                        G_PARAM_READWRITE |
+                                                        G_PARAM_CONSTRUCT));
+
+  g_object_class_install_property (object_class, PROP_GRADIENT_TYPE,
+                                   g_param_spec_enum ("gradient_type",
+                                                      "Gradient Type",
+                                                      "The type of gradient to render",
+                                                      GIMP_TYPE_GRADIENT_TYPE,
+                                                      GIMP_GRADIENT_LINEAR,
+                                                      G_PARAM_READWRITE |
+                                                      G_PARAM_CONSTRUCT));
+}
+
+static void
+gimp_operation_blend_init (GimpOperationBlend *self)
+{
+}
+
+static void
+gimp_operation_blend_get_property (GObject    *object,
+                                   guint       property_id,
+                                   GValue     *value,
+                                   GParamSpec *pspec)
+{
+ GimpOperationBlend *self = GIMP_OPERATION_BLEND (object);
+
+  switch (property_id)
+    {
+    case PROP_START_X:
+      g_value_set_double (value, self->start_x);
+      break;
+
+    case PROP_START_Y:
+      g_value_set_double (value, self->start_y);
+      break;
+
+    case PROP_END_X:
+      g_value_set_double (value, self->end_x);
+      break;
+
+    case PROP_END_Y:
+      g_value_set_double (value, self->end_y);
+      break;
+
+    case PROP_GRADIENT_TYPE:
+      g_value_set_enum (value, self->gradient_type);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gimp_operation_blend_set_property (GObject      *object,
+                                   guint         property_id,
+                                   const GValue *value,
+                                   GParamSpec   *pspec)
+{
+  GimpOperationBlend *self = GIMP_OPERATION_BLEND (object);
+
+  switch (property_id)
+    {
+    case PROP_START_X:
+      self->start_x = g_value_get_double (value);
+      break;
+
+    case PROP_START_Y:
+      self->start_y = g_value_get_double (value);
+      break;
+
+    case PROP_END_X:
+      self->end_x = g_value_get_double (value);
+      break;
+
+    case PROP_END_Y:
+      self->end_y = g_value_get_double (value);
+      break;
+
+    case PROP_GRADIENT_TYPE:
+      self->gradient_type = g_value_get_enum (value);
+      break;
+
+   default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
+      break;
+    }
+}
+
+static void
+gimp_operation_blend_prepare (GeglOperation *operation)
+{
+  gegl_operation_set_format (operation, "output", babl_format ("R'G'B'A float"));
+}
+/*
+static GeglRectangle
+gimp_operation_blend_get_required_for_output (GeglOperation       *self,
+                                               const gchar         *input_pad,
+                                               const GeglRectangle *roi)
+{
+  return *gegl_operation_source_get_bounding_box (self, "input");
+}
+
+static GeglRectangle
+gimp_operation_blend_get_cached_region (GeglOperation       *self,
+                                         const GeglRectangle *roi)
+{
+  return *gegl_operation_source_get_bounding_box (self, "input");
+}
+*/
+
+
+
+
+
+
+static gboolean
+gimp_operation_blend_process (GeglOperation       *operation,
+         GeglBuffer          *output,
+         const GeglRectangle *result,
+         gint                 level)
+{
+  GimpOperationBlend *self          = GIMP_OPERATION_BLEND (operation);
+
+gdouble sx = self->start_x;
+gdouble sy = self->start_y;
+gdouble ex = self->end_x;
+gdouble ey = self->end_y;
+
+GimpContext         *context = NULL;//gimp_get_user_context ();
+
+GimpGradient        *gradient = GIMP_GRADIENT (gimp_gradient_get_standard (context)); // XXX
+GimpGradientType     gradient_type = self->gradient_type;
+
+gdouble              offset = 0.0;
+GimpRepeatMode       repeat = GIMP_REPEAT_TRUNCATE;
+gboolean             reverse = FALSE;
+gboolean             supersample = FALSE;
+gint                 max_depth = 0;
+gdouble              threshold = 0.0;
+gboolean             dither = FALSE;
+
+/*static void
+gradient_fill_region (GimpImage           *image,
+                      GimpDrawable        *drawable,
+                      GimpContext         *context,
+                      GeglBuffer          *buffer,
+                      const GeglRectangle *buffer_region,
+                      GimpGradient        *gradient,
+                      GimpGradientType     gradient_type,
+                      gdouble              offset,
+                      GimpRepeatMode       repeat,
+                      gboolean             reverse,
+                      gboolean             supersample,
+                      gint                 max_depth,
+                      gdouble              threshold,
+                      gboolean             dither,
+                      gdouble              sx,
+                      gdouble              sy,
+                      gdouble              ex,
+                      gdouble              ey,
+                      GimpProgress        *progress)*/
+{
+  RenderBlendData rbd = { 0, };
+
+  rbd.gradient = gradient;
+  rbd.context  = context;
+  rbd.reverse  = reverse;
+
+#ifdef USE_GRADIENT_CACHE
+  {
+    gint i;
+
+    rbd.gradient_cache_size = ceil (sqrt (SQR (sx - ex) + SQR (sy - ey)));
+    rbd.gradient_cache      = g_new0 (GimpRGB, rbd.gradient_cache_size);
+
+    for (i = 0; i < rbd.gradient_cache_size; i++)
+      {
+        gdouble factor = (gdouble) i / (gdouble) (rbd.gradient_cache_size - 1);
+
+        gimp_gradient_get_color_at (rbd.gradient, rbd.context, NULL,
+                                    factor, rbd.reverse,
+                                    rbd.gradient_cache + i);
+      }
+  }
+#endif
+
+  if (gimp_gradient_has_fg_bg_segments (rbd.gradient))
+    rbd.gradient = gimp_gradient_flatten (rbd.gradient, context);
+  else
+    rbd.gradient = g_object_ref (rbd.gradient);
+
+  /* Calculate type-specific parameters */
+
+  switch (gradient_type)
+    {
+    case GIMP_GRADIENT_RADIAL:
+      rbd.dist = sqrt (SQR (ex - sx) + SQR (ey - sy));
+      break;
+
+    case GIMP_GRADIENT_SQUARE:
+      rbd.dist = MAX (fabs (ex - sx), fabs (ey - sy));
+      break;
+
+    case GIMP_GRADIENT_CONICAL_SYMMETRIC:
+    case GIMP_GRADIENT_CONICAL_ASYMMETRIC:
+    case GIMP_GRADIENT_SPIRAL_CLOCKWISE:
+    case GIMP_GRADIENT_SPIRAL_ANTICLOCKWISE:
+    case GIMP_GRADIENT_LINEAR:
+    case GIMP_GRADIENT_BILINEAR:
+      rbd.dist = sqrt (SQR (ex - sx) + SQR (ey - sy));
+
+      if (rbd.dist > 0.0)
+        {
+          rbd.vec[0] = (ex - sx) / rbd.dist;
+          rbd.vec[1] = (ey - sy) / rbd.dist;
+        }
+
+      break;
+
+    case GIMP_GRADIENT_SHAPEBURST_ANGULAR:
+    case GIMP_GRADIENT_SHAPEBURST_SPHERICAL:
+    case GIMP_GRADIENT_SHAPEBURST_DIMPLED:
+      rbd.dist = sqrt (SQR (ex - sx) + SQR (ey - sy));
+      /*rbd.dist_buffer = gradient_precalc_shapeburst (image, drawable,
+                                                     buffer_region,
+                                                     rbd.dist, progress);*/
+      /* FIXME */
+      g_assert_not_reached ();
+      break;
+
+    default:
+      g_assert_not_reached ();
+      break;
+    }
+
+  /* Initialize render data */
+
+  rbd.offset        = offset;
+  rbd.sx            = sx;
+  rbd.sy            = sy;
+  rbd.gradient_type = gradient_type;
+  rbd.repeat        = repeat;
+
+  /* Render the gradient! */
+
+  if (supersample)
+    {
+      PutPixelData  ppd;
+
+      ppd.buffer      = output;
+      ppd.row_data    = g_malloc (sizeof (float) * 4 * result->width);
+      ppd.width       = result->width;
+      ppd.dither_rand = g_rand_new ();
+
+      gimp_adaptive_supersample_area (0, 0,
+                                      (result->width  - 1),
+                                      (result->height - 1),
+                                      max_depth, threshold,
+                                      gradient_render_pixel, &rbd,
+                                      gradient_put_pixel, &ppd,
+                                      NULL,
+                                      NULL);
+
+      g_rand_free (ppd.dither_rand);
+      g_free (ppd.row_data);
+    }
+  else
+    {
+      GeglBufferIterator *iter;
+      GeglRectangle      *roi;
+
+      iter = gegl_buffer_iterator_new (output, result, 0,
+                                       babl_format ("R'G'B'A float"),
+                                       GEGL_BUFFER_WRITE, GEGL_ABYSS_NONE);
+      roi = &iter->roi[0];
+
+      if (dither)
+        rbd.seed = g_rand_new ();
+
+      while (gegl_buffer_iterator_next (iter))
+        {
+          gfloat *dest = iter->data[0];
+          gint    endx  = roi->x + roi->width;
+          gint    endy  = roi->y + roi->height;
+          gint    x, y;
+
+          if (rbd.seed)
+            {
+              GRand *dither_rand = g_rand_new_with_seed (g_rand_int (rbd.seed));
+
+              for (y = roi->y; y < endy; y++)
+                for (x = roi->x; x < endx; x++)
+                  {
+                    GimpRGB  color = { 0.0, 0.0, 0.0, 1.0 };
+                    gint     i = g_rand_int (dither_rand);
+
+                    gradient_render_pixel (x, y, &color, &rbd);
+
+                    *dest++ = color.r + (gdouble) (i & 0xff) / 256.0 / 256.0; i >>= 8;
+                    *dest++ = color.g + (gdouble) (i & 0xff) / 256.0 / 256.0; i >>= 8;
+                    *dest++ = color.b + (gdouble) (i & 0xff) / 256.0 / 256.0; i >>= 8;
+                    *dest++ = color.a + (gdouble) (i & 0xff) / 256.0 / 256.0;
+                  }
+
+              g_rand_free (dither_rand);
+            }
+          else
+            {
+              for (y = roi->y; y < endy; y++)
+                for (x = roi->x; x < endx; x++)
+                  {
+                    GimpRGB  color = { 0.0, 0.0, 0.0, 1.0 };
+
+                    gradient_render_pixel (x, y, &color, &rbd);
+
+                    *dest++ = color.r;
+                    *dest++ = color.g;
+                    *dest++ = color.b;
+                    *dest++ = color.a;
+                  }
+            }
+        }
+
+      if (dither)
+        g_rand_free (rbd.seed);
+    }
+
+#ifdef USE_GRADIENT_CACHE
+  g_free (rbd.gradient_cache);
+#endif
+
+  g_object_unref (rbd.gradient);
+
+  if (rbd.dist_buffer)
+    g_object_unref (rbd.dist_buffer);
+}
+
+
+
+
+  return TRUE;
+}
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+static gdouble
+gradient_calc_conical_sym_factor (gdouble  dist,
+                                  gdouble *axis,
+                                  gdouble  offset,
+                                  gdouble  x,
+                                  gdouble  y)
+{
+  if (dist == 0.0)
+    {
+      return 0.0;
+    }
+  else if ((x != 0) || (y != 0))
+    {
+      gdouble vec[2];
+      gdouble r;
+      gdouble rat;
+
+      /* Calculate offset from the start in pixels */
+
+      r = sqrt (SQR (x) + SQR (y));
+
+      vec[0] = x / r;
+      vec[1] = y / r;
+
+      rat = axis[0] * vec[0] + axis[1] * vec[1]; /* Dot product */
+
+      if (rat > 1.0)
+        rat = 1.0;
+      else if (rat < -1.0)
+        rat = -1.0;
+
+      /* This cool idea is courtesy Josh MacDonald,
+       * Ali Rahimi --- two more XCF losers.  */
+
+      rat = acos (rat) / G_PI;
+      rat = pow (rat, (offset / 10.0) + 1.0);
+
+      return CLAMP (rat, 0.0, 1.0);
+    }
+  else
+    {
+      return 0.5;
+    }
+}
+
+static gdouble
+gradient_calc_conical_asym_factor (gdouble  dist,
+                                   gdouble *axis,
+                                   gdouble  offset,
+                                   gdouble  x,
+                                   gdouble  y)
+{
+  if (dist == 0.0)
+    {
+      return 0.0;
+    }
+  else if (x != 0 || y != 0)
+    {
+      gdouble ang0, ang1;
+      gdouble ang;
+      gdouble rat;
+
+      ang0 = atan2 (axis[0], axis[1]) + G_PI;
+
+      ang1 = atan2 (x, y) + G_PI;
+
+      ang = ang1 - ang0;
+
+      if (ang < 0.0)
+        ang += (2.0 * G_PI);
+
+      rat = ang / (2.0 * G_PI);
+      rat = pow (rat, (offset / 10.0) + 1.0);
+
+      return CLAMP (rat, 0.0, 1.0);
+    }
+  else
+    {
+      return 0.5; /* We are on middle point */
+    }
+}
+
+static gdouble
+gradient_calc_square_factor (gdouble dist,
+                             gdouble offset,
+                             gdouble x,
+                             gdouble y)
+{
+  if (dist == 0.0)
+    {
+      return 0.0;
+    }
+  else
+    {
+      gdouble r;
+      gdouble rat;
+
+      /* Calculate offset from start as a value in [0, 1] */
+
+      offset = offset / 100.0;
+
+      r   = MAX (abs (x), abs (y));
+      rat = r / dist;
+
+      if (rat < offset)
+        return 0.0;
+      else if (offset == 1.0)
+        return (rat >= 1.0) ? 1.0 : 0.0;
+      else
+        return (rat - offset) / (1.0 - offset);
+    }
+}
+
+static gdouble
+gradient_calc_radial_factor (gdouble dist,
+                             gdouble offset,
+                             gdouble x,
+                             gdouble y)
+{
+  if (dist == 0.0)
+    {
+      return 0.0;
+    }
+  else
+    {
+      gdouble r;
+      gdouble rat;
+
+      /* Calculate radial offset from start as a value in [0, 1] */
+
+      offset = offset / 100.0;
+
+      r   = sqrt (SQR (x) + SQR (y));
+      rat = r / dist;
+
+      if (rat < offset)
+        return 0.0;
+      else if (offset == 1.0)
+        return (rat >= 1.0) ? 1.0 : 0.0;
+      else
+        return (rat - offset) / (1.0 - offset);
+    }
+}
+
+static gdouble
+gradient_calc_linear_factor (gdouble  dist,
+                             gdouble *vec,
+                             gdouble  offset,
+                             gdouble  x,
+                             gdouble  y)
+{
+  if (dist == 0.0)
+    {
+      return 0.0;
+    }
+  else
+    {
+      gdouble r;
+      gdouble rat;
+
+      offset = offset / 100.0;
+
+      r   = vec[0] * x + vec[1] * y;
+      rat = r / dist;
+
+      if (rat >= 0.0 && rat < offset)
+        return 0.0;
+      else if (offset == 1.0)
+        return (rat >= 1.0) ? 1.0 : 0.0;
+      else if (rat < 0.0)
+        return rat / (1.0 - offset);
+      else
+        return (rat - offset) / (1.0 - offset);
+    }
+}
+
+static gdouble
+gradient_calc_bilinear_factor (gdouble  dist,
+                               gdouble *vec,
+                               gdouble  offset,
+                               gdouble  x,
+                               gdouble  y)
+{
+  if (dist == 0.0)
+    {
+      return 0.0;
+    }
+  else
+    {
+      gdouble r;
+      gdouble rat;
+
+      /* Calculate linear offset from the start line outward */
+
+      offset = offset / 100.0;
+
+      r   = vec[0] * x + vec[1] * y;
+      rat = r / dist;
+
+      if (fabs (rat) < offset)
+        return 0.0;
+      else if (offset == 1.0)
+        return (rat == 1.0) ? 1.0 : 0.0;
+      else
+        return (fabs (rat) - offset) / (1.0 - offset);
+    }
+}
+
+static gdouble
+gradient_calc_spiral_factor (gdouble   dist,
+                             gdouble  *axis,
+                             gdouble   offset,
+                             gdouble   x,
+                             gdouble   y,
+                             gboolean  clockwise)
+{
+  if (dist == 0.0)
+    {
+      return 0.0;
+    }
+  else if (x != 0.0 || y != 0.0)
+    {
+      gdouble ang0, ang1;
+      gdouble ang;
+      double  r;
+
+      ang0 = atan2 (axis[0], axis[1]) + G_PI;
+      ang1 = atan2 (x, y) + G_PI;
+
+      if (clockwise)
+        ang = ang1 - ang0;
+      else
+        ang = ang0 - ang1;
+
+      if (ang < 0.0)
+        ang += (2.0 * G_PI);
+
+      r = sqrt (SQR (x) + SQR (y)) / dist;
+
+      return fmod (ang / (2.0 * G_PI) + r + offset, 1.0);
+    }
+  else
+    {
+      return 0.5 ; /* We are on the middle point */
+    }
+}
+
+static gdouble
+gradient_calc_shapeburst_angular_factor (GeglBuffer *dist_buffer,
+                                         gdouble     x,
+                                         gdouble     y)
+{
+  gint   ix = CLAMP (x, 0.0, gegl_buffer_get_width  (dist_buffer) - 0.7);
+  gint   iy = CLAMP (y, 0.0, gegl_buffer_get_height (dist_buffer) - 0.7);
+  gfloat value;
+
+  gegl_buffer_get (dist_buffer, GEGL_RECTANGLE (ix, iy, 1, 1), 1.0,
+                   NULL, &value,
+                   GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
+
+  value = 1.0 - value;
+
+  return value;
+}
+
+
+static gdouble
+gradient_calc_shapeburst_spherical_factor (GeglBuffer *dist_buffer,
+                                           gdouble     x,
+                                           gdouble     y)
+{
+  gint   ix = CLAMP (x, 0.0, gegl_buffer_get_width  (dist_buffer) - 0.7);
+  gint   iy = CLAMP (y, 0.0, gegl_buffer_get_height (dist_buffer) - 0.7);
+  gfloat value;
+
+  gegl_buffer_get (dist_buffer, GEGL_RECTANGLE (ix, iy, 1, 1), 1.0,
+                   NULL, &value,
+                   GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
+
+  value = 1.0 - sin (0.5 * G_PI * value);
+
+  return value;
+}
+
+
+static gdouble
+gradient_calc_shapeburst_dimpled_factor (GeglBuffer *dist_buffer,
+                                         gdouble     x,
+                                         gdouble     y)
+{
+  gint   ix = CLAMP (x, 0.0, gegl_buffer_get_width  (dist_buffer) - 0.7);
+  gint   iy = CLAMP (y, 0.0, gegl_buffer_get_height (dist_buffer) - 0.7);
+  gfloat value;
+
+  gegl_buffer_get (dist_buffer, GEGL_RECTANGLE (ix, iy, 1, 1), 1.0,
+                   NULL, &value,
+                   GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
+
+  value = cos (0.5 * G_PI * value);
+
+  return value;
+}
+
+static void
+gradient_render_pixel (gdouble   x,
+                       gdouble   y,
+                       GimpRGB  *color,
+                       gpointer  render_data)
+{
+  RenderBlendData *rbd = render_data;
+  gdouble          factor;
+
+  /* Calculate blending factor */
+
+  switch (rbd->gradient_type)
+    {
+    case GIMP_GRADIENT_LINEAR:
+      factor = gradient_calc_linear_factor (rbd->dist,
+                                            rbd->vec, rbd->offset,
+                                            x - rbd->sx, y - rbd->sy);
+      break;
+
+    case GIMP_GRADIENT_BILINEAR:
+      factor = gradient_calc_bilinear_factor (rbd->dist,
+                                              rbd->vec, rbd->offset,
+                                              x - rbd->sx, y - rbd->sy);
+      break;
+
+    case GIMP_GRADIENT_RADIAL:
+      factor = gradient_calc_radial_factor (rbd->dist,
+                                            rbd->offset,
+                                            x - rbd->sx, y - rbd->sy);
+      break;
+
+    case GIMP_GRADIENT_SQUARE:
+      factor = gradient_calc_square_factor (rbd->dist, rbd->offset,
+                                            x - rbd->sx, y - rbd->sy);
+      break;
+
+    case GIMP_GRADIENT_CONICAL_SYMMETRIC:
+      factor = gradient_calc_conical_sym_factor (rbd->dist,
+                                                 rbd->vec, rbd->offset,
+                                                 x - rbd->sx, y - rbd->sy);
+      break;
+
+    case GIMP_GRADIENT_CONICAL_ASYMMETRIC:
+      factor = gradient_calc_conical_asym_factor (rbd->dist,
+                                                  rbd->vec, rbd->offset,
+                                                  x - rbd->sx, y - rbd->sy);
+      break;
+
+    case GIMP_GRADIENT_SHAPEBURST_ANGULAR:
+      factor = gradient_calc_shapeburst_angular_factor (rbd->dist_buffer, x, y);
+      break;
+
+    case GIMP_GRADIENT_SHAPEBURST_SPHERICAL:
+      factor = gradient_calc_shapeburst_spherical_factor (rbd->dist_buffer, x, y);
+      break;
+
+    case GIMP_GRADIENT_SHAPEBURST_DIMPLED:
+      factor = gradient_calc_shapeburst_dimpled_factor (rbd->dist_buffer, x, y);
+      break;
+
+    case GIMP_GRADIENT_SPIRAL_CLOCKWISE:
+      factor = gradient_calc_spiral_factor (rbd->dist,
+                                            rbd->vec, rbd->offset,
+                                            x - rbd->sx, y - rbd->sy, TRUE);
+      break;
+
+    case GIMP_GRADIENT_SPIRAL_ANTICLOCKWISE:
+      factor = gradient_calc_spiral_factor (rbd->dist,
+                                            rbd->vec, rbd->offset,
+                                            x - rbd->sx, y - rbd->sy, FALSE);
+      break;
+
+    default:
+      g_assert_not_reached ();
+      return;
+    }
+
+  /* Adjust for repeat */
+
+  switch (rbd->repeat)
+    {
+    case GIMP_REPEAT_TRUNCATE:
+      break;
+    case GIMP_REPEAT_NONE:
+      factor = CLAMP (factor, 0.0, 1.0);
+      break;
+
+    case GIMP_REPEAT_SAWTOOTH:
+      factor = factor - floor (factor);
+      break;
+
+    case GIMP_REPEAT_TRIANGULAR:
+      {
+        guint ifactor;
+
+        if (factor < 0.0)
+          factor = -factor;
+
+        ifactor = (guint) factor;
+        factor = factor - floor (factor);
+
+        if (ifactor & 1)
+          factor = 1.0 - factor;
+      }
+      break;
+    }
+
+  /* Blend the colors */
+
+  if (factor < 0.0 || factor > 1.0)
+    {
+      color->r = color->g = color->b = 0;
+      color->a = GIMP_OPACITY_TRANSPARENT;
+    }
+  else
+    {
+#ifdef USE_GRADIENT_CACHE
+      *color = rbd->gradient_cache[(gint) (factor * (rbd->gradient_cache_size - 1))];
+#else
+      gimp_gradient_get_color_at (rbd->gradient, rbd->context, NULL,
+                                  factor, rbd->reverse, color);
+#endif
+    }
+}
+
+static void
+gradient_put_pixel (gint      x,
+                    gint      y,
+                    GimpRGB  *color,
+                    gpointer  put_pixel_data)
+{
+  PutPixelData *ppd  = put_pixel_data;
+  gfloat       *dest = ppd->row_data + 4 * x;
+
+  if (ppd->dither_rand)
+    {
+      gint i = g_rand_int (ppd->dither_rand);
+
+      *dest++ = color->r + (gdouble) (i & 0xff) / 256.0 / 256.0; i >>= 8;
+      *dest++ = color->g + (gdouble) (i & 0xff) / 256.0 / 256.0; i >>= 8;
+      *dest++ = color->b + (gdouble) (i & 0xff) / 256.0 / 256.0; i >>= 8;
+      *dest++ = color->a + (gdouble) (i & 0xff) / 256.0 / 256.0;
+    }
+  else
+    {
+      *dest++ = color->r;
+      *dest++ = color->g;
+      *dest++ = color->b;
+      *dest++ = color->a;
+    }
+
+  /* Paint whole row if we are on the rightmost pixel */
+
+  if (x == (ppd->width - 1))
+    gegl_buffer_set (ppd->buffer, GEGL_RECTANGLE (0, y, ppd->width, 1),
+                     0, babl_format ("R'G'B'A float"), ppd->row_data,
+                     GEGL_AUTO_ROWSTRIDE);
+}
diff --git a/app/operations/gimpoperationblend.h b/app/operations/gimpoperationblend.h
new file mode 100644
index 0000000..a7b3f2c
--- /dev/null
+++ b/app/operations/gimpoperationblend.h
@@ -0,0 +1,56 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * gimpoperationblend.h
+ * Copyright (C) foobar
+ *
+ * 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_BLEND_H__
+#define __GIMP_OPERATION_BLEND_H__
+
+
+#include <gegl-plugin.h>
+
+
+#define GIMP_TYPE_OPERATION_BLEND            (gimp_operation_blend_get_type ())
+#define GIMP_OPERATION_BLEND(obj)            (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_OPERATION_BLEND, 
GimpOperationBlend))
+#define GIMP_OPERATION_BLEND_CLASS(klass)    (G_TYPE_CHECK_CLASS_CAST ((klass),  GIMP_TYPE_OPERATION_BLEND, 
GimpOperationBlendClass))
+#define GIMP_IS_OPERATION_BLEND(obj)         (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_OPERATION_BLEND))
+#define GIMP_IS_OPERATION_BLEND_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass),  GIMP_TYPE_OPERATION_BLEND))
+#define GIMP_OPERATION_BLEND_GET_CLASS(obj)  (G_TYPE_INSTANCE_GET_CLASS ((obj),  GIMP_TYPE_OPERATION_BLEND, 
GimpOperationBlendClass))
+
+
+typedef struct _GimpOperationBlend      GimpOperationBlend;
+typedef struct _GimpOperationBlendClass GimpOperationBlendClass;
+
+struct _GimpOperationBlend
+{
+  GeglOperationSource  parent_instance;
+
+  gdouble              start_x, start_y, end_x, end_y;
+  GimpGradientType     gradient_type;
+};
+
+struct _GimpOperationBlendClass
+{
+  GeglOperationSourceClass  parent_class;
+};
+
+
+GType   gimp_operation_blend_get_type (void) G_GNUC_CONST;
+
+
+#endif /* __GIMP_OPERATION_BLEND_H__ */



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