[gnome-photos/wip/rishi/hefe: 1/5] Add radial-gradient



commit 2c0778ae2159771a1b7775a90bee335f242ee280
Author: Debarshi Ray <debarshir gnome org>
Date:   Tue Jan 12 18:34:51 2016 +0100

    Add radial-gradient

 src/Makefile.am                        |    2 +
 src/photos-operation-radial-gradient.c |  340 ++++++++++++++++++++++++++++++++
 src/photos-operation-radial-gradient.h |   45 +++++
 src/photos-utils.c                     |    2 +
 4 files changed, 389 insertions(+), 0 deletions(-)
---
diff --git a/src/Makefile.am b/src/Makefile.am
index e53b0f3..8731021 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -136,6 +136,8 @@ gnome_photos_SOURCES = \
        photos-operation-jpg-guess-sizes.h \
        photos-operation-png-guess-sizes.c \
        photos-operation-png-guess-sizes.h \
+       photos-operation-radial-gradient.c \
+       photos-operation-radial-gradient.h \
        photos-operation-saturation.c \
        photos-operation-saturation.h \
        photos-organize-collection-dialog.c \
diff --git a/src/photos-operation-radial-gradient.c b/src/photos-operation-radial-gradient.c
new file mode 100644
index 0000000..d477623
--- /dev/null
+++ b/src/photos-operation-radial-gradient.c
@@ -0,0 +1,340 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2016 Red Hat, Inc.
+ *
+ * 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+
+#include "config.h"
+
+#include <math.h>
+
+#include <babl/babl.h>
+#include <gegl.h>
+#include <gegl-plugin.h>
+
+#include "photos-operation-radial-gradient.h"
+
+
+struct _PhotosOperationRadialGradient
+{
+  GeglOperationPointRender parent_instance;
+  GeglColor *color_end;
+  GeglColor *color_start;
+  gfloat r0;
+  gfloat r1;
+  gfloat x;
+  gfloat y;
+};
+
+struct _PhotosOperationRadialGradientClass
+{
+  GeglOperationPointRenderClass parent_class;
+};
+
+enum
+{
+  PROP_0,
+  PROP_COLOR_END,
+  PROP_R0,
+  PROP_R1,
+  PROP_COLOR_START,
+  PROP_X,
+  PROP_Y
+};
+
+
+G_DEFINE_TYPE (PhotosOperationRadialGradient, photos_operation_radial_gradient, 
GEGL_TYPE_OPERATION_POINT_RENDER);
+
+
+static gfloat
+photos_operation_radial_gradient_calculate_distance (gfloat x1, gfloat y1, gfloat x2, gfloat y2)
+{
+  const gfloat dx = x1 - x2;
+  const gfloat dx2 = dx * dx;
+  const gfloat dy = y1 - y2;
+  const gfloat dy2 = dy * dy;
+  gfloat distance;
+
+  distance = sqrtf (dx2 + dy2);
+  return distance;
+}
+
+
+static GeglRectangle
+photos_operation_radial_gradient_get_bounding_box (GeglOperation *operation)
+{
+  GeglRectangle bbox;
+
+  bbox = gegl_rectangle_infinite_plane ();
+  return bbox;
+}
+
+
+static void
+photos_operation_radial_gradient_prepare (GeglOperation *operation)
+{
+  const Babl* format;
+
+  format = babl_format ("R'G'B'A float");
+  gegl_operation_set_format (operation, "output", format);
+}
+
+
+static gboolean
+photos_operation_radial_gradient_process (GeglOperation *operation,
+                                          void *out_buf,
+                                          glong n_pixels,
+                                          const GeglRectangle *roi,
+                                          gint level)
+{
+  PhotosOperationRadialGradient *self = PHOTOS_OPERATION_RADIAL_GRADIENT (operation);
+  const Babl *format;
+  GeglColor *transparent;
+  gfloat pixel_end[4];
+  gfloat pixel_start[4];
+  gfloat pixel_transparent[4];
+  gfloat *out = out_buf;
+  gfloat rdiff;
+  gint bpp;
+
+  format = babl_format ("R'G'B'A float");
+  bpp = babl_format_get_bytes_per_pixel (format);
+
+  transparent = gegl_color_new ("#00000000");
+  gegl_color_get_pixel (transparent, format, pixel_transparent);
+  gegl_color_get_pixel (self->color_end, format, pixel_end);
+  gegl_color_get_pixel (self->color_start, format, pixel_start);
+
+  rdiff = self->r1 - self->r0;
+
+  if (GEGL_FLOAT_IS_ZERO (rdiff))
+    {
+      gegl_memset_pattern (out_buf, pixel_transparent, bpp, n_pixels);
+    }
+  else
+    {
+      const gint x1 = roi->x + roi->width;
+      const gint y1 = roi->y + roi->height;
+      gint x;
+      gint y;
+
+      for (y = roi->y; y < y1; y++)
+        {
+          for (x = roi->x; x < x1; x++)
+            {
+              gfloat distance;
+              gfloat v;
+              gint c;
+
+              distance = photos_operation_radial_gradient_calculate_distance (x, y, self->x, self->y);
+              if (distance < self->r0 + GEGL_FLOAT_EPSILON)
+                v = 0.0f;
+              else if (distance > self->r1 - GEGL_FLOAT_EPSILON)
+                v = 1.0f;
+              else
+                v = (distance - self->r0) / rdiff;
+
+              for (c = 0; c < 4; c++)
+                out[c] = pixel_start[c] * (1.0f - v) + pixel_end[c] * v;
+
+              out += 4;
+            }
+        }
+    }
+
+  g_object_unref (transparent);
+  return TRUE;
+}
+
+
+static void
+photos_operation_radial_gradient_dispose (GObject *object)
+{
+  PhotosOperationRadialGradient *self = PHOTOS_OPERATION_RADIAL_GRADIENT (object);
+
+  g_clear_object (&self->color_end);
+  g_clear_object (&self->color_start);
+
+  G_OBJECT_CLASS (photos_operation_radial_gradient_parent_class)->dispose (object);
+}
+
+
+static void
+photos_operation_radial_gradient_get_property (GObject *object, guint prop_id, GValue *value, GParamSpec 
*pspec)
+{
+  PhotosOperationRadialGradient *self = PHOTOS_OPERATION_RADIAL_GRADIENT (object);
+
+  switch (prop_id)
+    {
+    case PROP_COLOR_END:
+      g_value_set_object (value, self->color_end);
+      break;
+
+    case PROP_COLOR_START:
+      g_value_set_object (value, self->color_start);
+      break;
+
+    case PROP_R0:
+      g_value_set_double (value, (gdouble) self->r0);
+      break;
+
+    case PROP_R1:
+      g_value_set_double (value, (gdouble) self->r1);
+      break;
+
+    case PROP_X:
+      g_value_set_double (value, (gdouble) self->x);
+      break;
+
+    case PROP_Y:
+      g_value_set_double (value, (gdouble) self->y);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+
+static void
+photos_operation_radial_gradient_set_property (GObject *object, guint prop_id, const GValue *value, 
GParamSpec *pspec)
+{
+  PhotosOperationRadialGradient *self = PHOTOS_OPERATION_RADIAL_GRADIENT (object);
+
+  switch (prop_id)
+    {
+    case PROP_COLOR_END:
+      g_clear_object (&self->color_end);
+      self->color_end = GEGL_COLOR (g_value_dup_object (value));
+      break;
+
+    case PROP_COLOR_START:
+      g_clear_object (&self->color_start);
+      self->color_start = GEGL_COLOR (g_value_dup_object (value));
+      break;
+
+    case PROP_R0:
+      self->r0 = (gfloat) g_value_get_double (value);
+      break;
+
+    case PROP_R1:
+      self->r1 = (gfloat) g_value_get_double (value);
+      break;
+
+    case PROP_X:
+      self->x = (gfloat) g_value_get_double (value);
+      break;
+
+    case PROP_Y:
+      self->y = (gfloat) g_value_get_double (value);
+      break;
+
+    default:
+      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
+      break;
+    }
+}
+
+
+static void
+photos_operation_radial_gradient_init (PhotosOperationRadialGradient *self)
+{
+}
+
+
+static void
+photos_operation_radial_gradient_class_init (PhotosOperationRadialGradientClass *class)
+{
+  GObjectClass *object_class = G_OBJECT_CLASS (class);
+  GeglOperationClass *operation_class = GEGL_OPERATION_CLASS (class);
+  GeglOperationPointRenderClass *point_render_class = GEGL_OPERATION_POINT_RENDER_CLASS (class);
+
+  operation_class->no_cache = TRUE;
+  operation_class->opencl_support = FALSE;
+
+  object_class->dispose = photos_operation_radial_gradient_dispose;
+  object_class->get_property = photos_operation_radial_gradient_get_property;
+  object_class->set_property = photos_operation_radial_gradient_set_property;
+  operation_class->get_bounding_box = photos_operation_radial_gradient_get_bounding_box;
+  operation_class->prepare = photos_operation_radial_gradient_prepare;
+  point_render_class->process = photos_operation_radial_gradient_process;
+
+  g_object_class_install_property (object_class,
+                                   PROP_COLOR_END,
+                                   gegl_param_spec_color_from_string ("color-end",
+                                                                      "Outer Color",
+                                                                      "The color at the edge of the end 
circle",
+                                                                      "black",
+                                                                      G_PARAM_CONSTRUCT | 
G_PARAM_READWRITE));
+
+  g_object_class_install_property (object_class,
+                                   PROP_COLOR_START,
+                                   gegl_param_spec_color_from_string ("color-start",
+                                                                      "Inner Color",
+                                                                      "The color at the edge of the start 
circle",
+                                                                      "white",
+                                                                      G_PARAM_CONSTRUCT | 
G_PARAM_READWRITE));
+
+  g_object_class_install_property (object_class,
+                                   PROP_R0,
+                                   g_param_spec_double ("r0",
+                                                        "Inner radius",
+                                                        "The radius of the start circle",
+                                                        0.0,
+                                                        G_MAXDOUBLE,
+                                                        0.0,
+                                                        G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+
+  g_object_class_install_property (object_class,
+                                   PROP_R1,
+                                   g_param_spec_double ("r1",
+                                                        "Outer radius",
+                                                        "The radius of the end circle",
+                                                        0.0,
+                                                        G_MAXDOUBLE,
+                                                        25.0,
+                                                        G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+
+  g_object_class_install_property (object_class,
+                                   PROP_X,
+                                   g_param_spec_double ("x",
+                                                        "X",
+                                                        "The x-coordinate of the circles' center",
+                                                        0.0,
+                                                        G_MAXDOUBLE,
+                                                        25.0,
+                                                        G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+
+  g_object_class_install_property (object_class,
+                                   PROP_Y,
+                                   g_param_spec_double ("y",
+                                                        "Y",
+                                                        "The y-coordinate of the circles' center",
+                                                        0.0,
+                                                        G_MAXDOUBLE,
+                                                        25.0,
+                                                        G_PARAM_CONSTRUCT | G_PARAM_READWRITE));
+
+  gegl_operation_class_set_keys (operation_class,
+                                 "name", "photos:radial-gradient",
+                                 "title", "Radial Gradient",
+                                 "description", "Radial gradient renderer",
+                                 "categories", "render:gradient",
+                                 NULL);
+}
diff --git a/src/photos-operation-radial-gradient.h b/src/photos-operation-radial-gradient.h
new file mode 100644
index 0000000..5cd4fd9
--- /dev/null
+++ b/src/photos-operation-radial-gradient.h
@@ -0,0 +1,45 @@
+/*
+ * Photos - access, organize and share your photos on GNOME
+ * Copyright © 2016 Red Hat, Inc.
+ *
+ * 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 2
+ * 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, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
+ * 02110-1301, USA.
+ */
+
+#ifndef PHOTOS_OPERATION_RADIAL_GRADIENT_H
+#define PHOTOS_OPERATION_RADIAL_GRADIENT_H
+
+#include <glib-object.h>
+
+G_BEGIN_DECLS
+
+#define PHOTOS_TYPE_OPERATION_RADIAL_GRADIENT (photos_operation_radial_gradient_get_type ())
+
+#define PHOTOS_OPERATION_RADIAL_GRADIENT(obj) \
+  (G_TYPE_CHECK_INSTANCE_CAST ((obj), \
+   PHOTOS_TYPE_OPERATION_RADIAL_GRADIENT, PhotosOperationRadialGradient))
+
+#define PHOTOS_IS_OPERATION_RADIAL_GRADIENT(obj) \
+  (G_TYPE_CHECK_INSTANCE_TYPE ((obj), \
+   PHOTOS_TYPE_OPERATION_RADIAL_GRADIENT))
+
+typedef struct _PhotosOperationRadialGradient      PhotosOperationRadialGradient;
+typedef struct _PhotosOperationRadialGradientClass PhotosOperationRadialGradientClass;
+
+GType            photos_operation_radial_gradient_get_type       (void) G_GNUC_CONST;
+
+G_END_DECLS
+
+#endif /* PHOTOS_OPERATION_RADIAL_GRADIENT_H */
diff --git a/src/photos-utils.c b/src/photos-utils.c
index 47ca3e1..6afa818 100644
--- a/src/photos-utils.c
+++ b/src/photos-utils.c
@@ -46,6 +46,7 @@
 #include "photos-operation-insta-filter.h"
 #include "photos-operation-jpg-guess-sizes.h"
 #include "photos-operation-png-guess-sizes.h"
+#include "photos-operation-radial-gradient.h"
 #include "photos-operation-saturation.h"
 #include "photos-query.h"
 #include "photos-source.h"
@@ -815,6 +816,7 @@ photos_utils_ensure_builtins (void)
       g_type_ensure (PHOTOS_TYPE_OPERATION_INSTA_FILTER);
       g_type_ensure (PHOTOS_TYPE_OPERATION_JPG_GUESS_SIZES);
       g_type_ensure (PHOTOS_TYPE_OPERATION_PNG_GUESS_SIZES);
+      g_type_ensure (PHOTOS_TYPE_OPERATION_RADIAL_GRADIENT);
       g_type_ensure (PHOTOS_TYPE_OPERATION_SATURATION);
 
       g_type_ensure (PHOTOS_TYPE_TOOL_COLORS);


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