[gegl] linear-gradient: Clean up and move out of workshop



commit e6da899f8f385bffa6d2e039cb589740ecabf106
Author: Daniel Sabo <DanielSabo gmail com>
Date:   Sat Dec 14 03:50:16 2013 -0800

    linear-gradient: Clean up and move out of workshop

 operations/common/Makefile.am                     |    1 +
 operations/{workshop => common}/linear-gradient.c |   89 +++++++++-----------
 operations/workshop/Makefile.am                   |    1 -
 po/POTFILES.in                                    |    2 +-
 4 files changed, 42 insertions(+), 51 deletions(-)
---
diff --git a/operations/common/Makefile.am b/operations/common/Makefile.am
index 13a072d..995e9dd 100644
--- a/operations/common/Makefile.am
+++ b/operations/common/Makefile.am
@@ -54,6 +54,7 @@ op_LTLIBRARIES = \
        layer.la \
        lens-distortion.la \
        levels.la \
+       linear-gradient.la \
        load.la \
        magick-load.la \
        mantiuk06.la \
diff --git a/operations/workshop/linear-gradient.c b/operations/common/linear-gradient.c
similarity index 57%
rename from operations/workshop/linear-gradient.c
rename to operations/common/linear-gradient.c
index 28ea820..97072dd 100644
--- a/operations/workshop/linear-gradient.c
+++ b/operations/common/linear-gradient.c
@@ -14,6 +14,7 @@
  * License along with GEGL; if not, see <http://www.gnu.org/licenses/>.
  *
  * Copyright 2008 Øyvind Kolås <pippin gimp org>
+ *           2013 Daniel Sabo
  */
 
 #include "config.h"
@@ -21,19 +22,19 @@
 
 #ifdef GEGL_CHANT_PROPERTIES
 
-gegl_chant_double (x1,        _("X1"),  -1000.0, 1000.0, 25.0, "")
-gegl_chant_double (y1,        _("Y1"),  -1000.0, 1000.0, 25.0, "")
-gegl_chant_double (x2,        _("X2"),  -1000.0, 1000.0, 150.0, "")
-gegl_chant_double (y2,        _("Y2"),  -1000.0, 1000.0, 150.0, "")
-gegl_chant_color (color1,   _("Color"), "black",
-                  _("One end of a agradient"))
-gegl_chant_color (color2,   _("Other color"), "white",
-                  _("The other end of a gradient"))
+gegl_chant_double (start_x,      _("X1"), G_MININT, G_MAXINT, 25.0, "")
+gegl_chant_double (start_y,      _("Y1"), G_MININT, G_MAXINT, 25.0, "")
+gegl_chant_double (end_x,        _("X2"), G_MININT, G_MAXINT, 150.0, "")
+gegl_chant_double (end_y,        _("Y2"), G_MININT, G_MAXINT, 150.0, "")
+gegl_chant_color  (start_color,  _("Start Color"), "black",
+                                 _("The color at (x1, y1)"))
+gegl_chant_color  (end_color,    _("End Color"), "white",
+                                 _("The color at (x2, y2)"))
 
 #else
 
 #define GEGL_CHANT_TYPE_POINT_RENDER
-#define GEGL_CHANT_C_FILE       "linear-gradient.c"
+#define GEGL_CHANT_C_FILE "linear-gradient.c"
 
 #include "gegl-chant.h"
 
@@ -42,7 +43,7 @@ gegl_chant_color (color2,   _("Other color"), "white",
 static void
 prepare (GeglOperation *operation)
 {
-  gegl_operation_set_format (operation, "output", babl_format ("RGBA float"));
+  gegl_operation_set_format (operation, "output", babl_format ("R'G'B'A float"));
 }
 
 static GeglRectangle
@@ -51,11 +52,6 @@ get_bounding_box (GeglOperation *operation)
   return gegl_rectangle_infinite_plane ();
 }
 
-static gfloat dist(gfloat x1, gfloat y1, gfloat x2, gfloat y2)
-{
-  return sqrt(  (x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
-}
-
 static gboolean
 process (GeglOperation       *operation,
          void                *out_buf,
@@ -67,55 +63,50 @@ process (GeglOperation       *operation,
   gfloat     *out_pixel = out_buf;
   gfloat      color1[4];
   gfloat      color2[4];
-  gint        x, y;
+  gfloat      length;
   gfloat      dx, dy;
 
-  gfloat length = dist (o->x1, o->y1, o->x2, o->y2);
-
-  gegl_color_get_pixel (o->color1, babl_format ("RGBA float"), color1);
-  gegl_color_get_pixel (o->color2, babl_format ("RGBA float"), color2);
+  dx = o->end_x - o->start_x;
+  dy = o->end_y - o->start_y;
 
+  length = sqrtf (dx * dx + dy * dy);
 
-  x= roi->x;
-  y= roi->y;
-
-  dx = (o->x2-o->x1)/length;
-  dy = (o->y2-o->y1)/length;
-
-  while (n_pixels--)
+  if (GEGL_FLOAT_IS_ZERO (length))
+    {
+      memset (out_buf, 0, n_pixels * sizeof(float) * 4);
+    }
+  else
     {
-      gfloat v;
-      gint c;
+      gfloat vec0 = dx / length;
+      gfloat vec1 = dy / length;
+      gint x, y;
 
-      if (length == 0.0)
-        v = 0.5;
-      else
-        {
-          v = (dx * x + dy * y) / length;
+      gegl_color_get_pixel (o->start_color, babl_format ("R'G'B'A float"), color1);
+      gegl_color_get_pixel (o->end_color, babl_format ("R'G'B'A float"), color2);
 
-          if (v < 0.0)
-            v = 0.0;
-          else if (v>1.0)
-            v = 1.0;
-        }
+      for (y = roi->y; y < roi->y + roi->height; ++y)
+        {
+          for (x = roi->x; x < roi->x + roi->width; ++x)
+            {
+              gint c;
+              gfloat v = (vec0 * (x - o->start_x) + vec1 * (y - o->start_y)) / length;
 
-      for (c=0;c<4;c++)
-        out_pixel[c]=color1[c] * v + color2[c] * (1.0-v);
+              if (v > 1.0f - GEGL_FLOAT_EPSILON)
+                v = 1.0f;
+              if (v < 0.0f + GEGL_FLOAT_EPSILON)
+                v = 0.0f;
 
-      out_pixel += 4;
+              for (c = 0; c < 4; c++)
+                out_pixel[c] = color1[c] * v + color2[c] * (1.0f - v);
 
-      /* update x and y coordinates */
-      if (++x>=roi->x + roi->width)
-        {
-          x=roi->x;
-          y++;
-      }
+              out_pixel += 4;
+            }
+        }
     }
 
   return  TRUE;
 }
 
-
 static void
 gegl_chant_class_init (GeglChantClass *klass)
 {
diff --git a/operations/workshop/Makefile.am b/operations/workshop/Makefile.am
index 1f1c70b..4b46ba6 100644
--- a/operations/workshop/Makefile.am
+++ b/operations/workshop/Makefile.am
@@ -24,7 +24,6 @@ op_LTLIBRARIES =    \
        gblur-1d.la \
        hstack.la \
        kuwahara.la \
-       linear-gradient.la \
        mandelbrot.la \
        radial-gradient.la \
        rawbayer-load.la \
diff --git a/po/POTFILES.in b/po/POTFILES.in
index b7dacc1..e3704e9 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -47,6 +47,7 @@ operations/common/invert-linear.c
 operations/common/layer.c
 operations/common/lens-distortion.c
 operations/common/levels.c
+operations/common/linear-gradient.c
 operations/common/load.c
 operations/common/magick-load.c
 operations/common/mantiuk06.c
@@ -197,7 +198,6 @@ operations/workshop/generated/soft-dodge.c
 operations/workshop/generated/subtractive.c
 operations/workshop/hstack.c
 operations/workshop/kuwahara.c
-operations/workshop/linear-gradient.c
 operations/workshop/mandelbrot.c
 operations/workshop/radial-gradient.c
 operations/workshop/rawbayer-load.c


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