[gegl] operations: add preserve luminocity option to mono-mixer op and disable broken CL code



commit 12b0aa46ff9683d2f1c65258da763ceedc8993d3
Author: Alexia Death <alexiadeath gmail com>
Date:   Sat Dec 6 23:37:46 2014 +0200

    operations: add preserve luminocity option to mono-mixer op and disable broken CL code

 opencl/mono-mixer.cl           |   16 ++++++++++++++--
 opencl/mono-mixer.cl.h         |   16 ++++++++++++++--
 operations/common/mono-mixer.c |   36 +++++++++++++++++++++++++++++-------
 3 files changed, 57 insertions(+), 11 deletions(-)
---
diff --git a/opencl/mono-mixer.cl b/opencl/mono-mixer.cl
index 88b337b..0a17cac 100644
--- a/opencl/mono-mixer.cl
+++ b/opencl/mono-mixer.cl
@@ -2,10 +2,22 @@ __kernel void gegl_mono_mixer (__global const float4 *src_buf,
                                __global       float2 *dst_buf,
                                float                  red,
                                float                  green,
-                               float                  blue)
+                               float                  blue,
+                               const int              preserve_luminocity)
 {
   int gid = get_global_id(0);
   float4 in_v = src_buf[gid];
-  dst_buf[gid].x = in_v.x * red + in_v.y * green + in_v.z * blue;
+  float norm_factor = 1.0f;
+
+  if (preserve_luminocity)
+    {
+      float sum = red + green + blue;
+      if (sum == 0.0)
+        norm_factor = 1.0f;
+      else
+        norm_factor = fabs (1.0f / sum);
+    }
+
+  dst_buf[gid].x = (in_v.x * red + in_v.y * green + in_v.z * blue) * norm_factor;
   dst_buf[gid].y = in_v.w;
 }
diff --git a/opencl/mono-mixer.cl.h b/opencl/mono-mixer.cl.h
index 7b2fbc8..7a655ac 100644
--- a/opencl/mono-mixer.cl.h
+++ b/opencl/mono-mixer.cl.h
@@ -3,11 +3,23 @@ static const char* mono_mixer_cl_source =
 "                               __global       float2 *dst_buf,                \n"
 "                               float                  red,                    \n"
 "                               float                  green,                  \n"
-"                               float                  blue)                   \n"
+"                               float                  blue,                   \n"
+"                               const int              preserve_luminocity)    \n"
 "{                                                                             \n"
 "  int gid = get_global_id(0);                                                 \n"
 "  float4 in_v = src_buf[gid];                                                 \n"
-"  dst_buf[gid].x = in_v.x * red + in_v.y * green + in_v.z * blue;             \n"
+"  float norm_factor = 1.0f;                                                   \n"
+"                                                                              \n"
+"  if (preserve_luminocity)                                                    \n"
+"    {                                                                         \n"
+"      float sum = red + green + blue;                                         \n"
+"      if (sum == 0.0)                                                         \n"
+"        norm_factor = 1.0f;                                                   \n"
+"      else                                                                    \n"
+"        norm_factor = fabs (1.0f / sum);                                      \n"
+"    }                                                                         \n"
+"                                                                              \n"
+"  dst_buf[gid].x = (in_v.x * red + in_v.y * green + in_v.z * blue) * norm_factor;\n"
 "  dst_buf[gid].y = in_v.w;                                                    \n"
 "}                                                                             \n"
 ;
diff --git a/operations/common/mono-mixer.c b/operations/common/mono-mixer.c
index 4b71bab..be02770 100644
--- a/operations/common/mono-mixer.c
+++ b/operations/common/mono-mixer.c
@@ -19,9 +19,11 @@
 
 #include "config.h"
 #include <glib/gi18n-lib.h>
+#include <math.h>
 
 
 #ifdef GEGL_PROPERTIES
+property_boolean (preserve_luminosity, _("Preserve luminosity"), FALSE)
 
 property_double (red, _("Amount of red"), 0.5)
     value_range (-10.0, 10.0)
@@ -57,10 +59,12 @@ process (GeglOperation       *op,
          const GeglRectangle *roi,
          gint                 level)
 {
-  GeglProperties *o = GEGL_PROPERTIES (op);
-  gfloat      red   = o->red;
-  gfloat      green = o->green;
-  gfloat      blue  = o->blue;
+  GeglProperties *o       = GEGL_PROPERTIES (op);
+  gfloat      red         = o->red;
+  gfloat      green       = o->green;
+  gfloat      blue        = o->blue;
+  gboolean    normalize   = o->preserve_luminosity;
+  gfloat      norm_factor = 1.0;
   gfloat     * GEGL_ALIGNED in_pixel;
   gfloat     * GEGL_ALIGNED out_pixel;
   glong       i;
@@ -68,17 +72,35 @@ process (GeglOperation       *op,
   in_pixel   = in_buf;
   out_pixel  = out_buf;
 
+  if (normalize)
+   {
+     gdouble sum = red + green + blue;
+
+     if (sum == 0.0)
+       norm_factor = 1.0;
+     else
+       norm_factor = fabs (1 / sum);
+   }
+
   for (i=0; i<n_pixels; i++)
     {
-      out_pixel[0] = in_pixel[0] * red + in_pixel[1] * green + in_pixel[2] * blue;
+      out_pixel[0] = (in_pixel[0] * red +
+                      in_pixel[1] * green +
+                      in_pixel[2] * blue) * norm_factor;
       out_pixel[1] = in_pixel[3];
       in_pixel  += 4;
       out_pixel += 2;
     }
   return TRUE;
 }
-
+/* FIXME!
+ * CL variant of the operation gives a different
+ * result than the non-cl code even without the luminoscity preservation code
+ * that seems to have no effect, as if toggle parameter never changes.
+ * Disabling for now. */
+/*
 #include "opencl/mono-mixer.cl.h"
+ */
 
 static void
 gegl_op_class_init (GeglOpClass *klass)
@@ -97,7 +119,7 @@ gegl_op_class_init (GeglOpClass *klass)
     "title",       _("Mono Mixer"),
     "categories",  "color",
     "description", _("Monochrome channel mixer"),
-    "cl-source",   mono_mixer_cl_source,
+/*  "cl-source",   mono_mixer_cl_source, */
     NULL);
 }
 


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