[gegl] color-to-alpha: merge back color-to-alpha-plus changes



commit ecbc4f935f7467dbb96741debf10c0fa4e87734e
Author: Ell <ell_se yahoo com>
Date:   Thu Feb 15 08:38:39 2018 -0500

    color-to-alpha: merge back color-to-alpha-plus changes
    
    Merge the color-to-alpha-plus changes back into color-to-alpha, and
    remove it from the workshop.
    
    Remove the compress-threshold-range property of
    color-to-alpha-plus: keep only the compress-threshold-range ==
    FALSE behavior.
    
    Disable the OpenCL implementation, since it doesn't support the new
    properties.  It should be re-enabled once the OpenCL version
    implements them.

 operations/common-gpl3+/color-to-alpha.c  |  107 ++++++++-----
 operations/workshop/Makefile.am           |    1 -
 operations/workshop/color-to-alpha-plus.c |  241 -----------------------------
 po/POTFILES.in                            |    1 -
 4 files changed, 69 insertions(+), 281 deletions(-)
---
diff --git a/operations/common-gpl3+/color-to-alpha.c b/operations/common-gpl3+/color-to-alpha.c
index d99038e..a8e676d 100644
--- a/operations/common-gpl3+/color-to-alpha.c
+++ b/operations/common-gpl3+/color-to-alpha.c
@@ -18,6 +18,7 @@
  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  * Copyright (C) 2011 Robert Sasu <sasu robert gmail com>
  * Copyright (C) 2012 Øyvind Kolås <pippin gimp org>
+ * Copyright (C) 2017 Ell
  */
 
 #include "config.h"
@@ -28,6 +29,14 @@
 property_color (color, _("Color"), "white")
     description(_("The color to make transparent."))
 
+property_double (transparency_threshold, _("Transparency threshold"), 0.0)
+    description(_("The limit below which colors become transparent."))
+    value_range (0.0, 1.0)
+
+property_double (opacity_threshold, _("Opacity threshold"), 1.0)
+    description(_("The limit above which colors remain opaque."))
+    value_range (0.0, 1.0)
+
 #else
 
 #define GEGL_OP_POINT_FILTER
@@ -38,6 +47,8 @@ property_color (color, _("Color"), "white")
 #include <stdio.h>
 #include <math.h>
 
+#define EPSILON 0.00001
+
 static void
 prepare (GeglOperation *operation)
 {
@@ -83,53 +94,65 @@ prepare (GeglOperation *operation)
 static void
 color_to_alpha (const gfloat *color,
                 const gfloat *src,
-                gfloat       *dst)
+                gfloat       *dst,
+                gfloat        transparency_threshold,
+                gfloat        opacity_threshold)
 {
-  gint i;
-  gfloat alpha[4];
+  gint   i;
+  gfloat dist  = 0.0f;
+  gfloat alpha = 0.0f;
 
-  for (i=0; i<4; i++)
+  for (i = 0; i < 4; i++)
     dst[i] = src[i];
 
-  alpha[3] = dst[3];
-
-  for (i=0; i<3; i++)
+  for (i = 0; i < 3; i++)
     {
-      if (color[i] < 0.00001)
-        alpha[i] = dst[i];
-      else if (dst[i] > color[i] + 0.00001)
-        alpha[i] = (dst[i] - color[i]) / (1.0f - color[i]);
-      else if (dst[i] < color[i] - 0.00001)
-        alpha[i] = (color[i] - dst[i]) / (color[i]);
-      else
-        alpha[i] = 0.0f;
-    }
+      gfloat d;
+      gfloat a;
 
-  if (alpha[0] > alpha[1])
-    {
-      if (alpha[0] > alpha[2])
-        dst[3] = alpha[0];
+      d = fabsf (dst[i] - color[i]);
+
+      if (d < transparency_threshold + EPSILON)
+        a = 0.0f;
+      else if (d > opacity_threshold - EPSILON)
+        a = 1.0f;
+      else if (dst[i] < color[i])
+        a = (d - transparency_threshold) / (MIN (opacity_threshold,        color[i]) - 
transparency_threshold);
       else
-        dst[3] = alpha[2];
-    }
-  else if (alpha[1] > alpha[2])
-    {
-      dst[3] = alpha[1];
+        a = (d - transparency_threshold) / (MIN (opacity_threshold, 1.0f - color[i]) - 
transparency_threshold);
+
+      if (a > alpha)
+        {
+          alpha = a;
+          dist  = d;
+        }
     }
-  else
+
+  if (alpha > EPSILON)
     {
-      dst[3] = alpha[2];
-    }
+      gfloat ratio     = transparency_threshold / dist;
+      gfloat alpha_inv = 1.0f / alpha;
+
+      for (i = 0; i < 3; i++)
+        {
+          gfloat c;
 
-  if (dst[3] < 0.00001)
-    return;
+          c = color[i] + (dst[i] - color[i]) * ratio;
 
-  for (i=0; i<3; i++)
-    dst[i] = (dst[i] - color[i]) / dst[3] + color[i];
+          dst[i] = c + (dst[i] - c) * alpha_inv;
+        }
+    }
 
-  dst[3] *= alpha[3];
+  dst[3] *= alpha;
 }
 
+
+/* FIXME:  the transparency-threshold and opacity-threshold properties are not
+ * handled by the opencl version atm.  re-enable the opencl version once
+ * they're implemented.
+ */
+#if 0
+
 #include "opencl/gegl-cl.h"
 #include "opencl/color-to-alpha.cl.h"
 
@@ -183,6 +206,7 @@ cl_process (GeglOperation       *operation,
   return TRUE;
 }
 
+#endif
 
 static gboolean
 process (GeglOperation       *operation,
@@ -192,10 +216,12 @@ process (GeglOperation       *operation,
          const GeglRectangle *roi,
          gint                 level)
 {
-  GeglProperties *o      = GEGL_PROPERTIES (operation);
-  const Babl *format = babl_format ("R'G'B'A float");
-  gfloat      color[4];
-  gint        x;
+  GeglProperties *o                      = GEGL_PROPERTIES (operation);
+  const Babl     *format                 = babl_format ("R'G'B'A float");
+  gfloat          color[4];
+  gfloat          transparency_threshold = o->transparency_threshold;
+  gfloat          opacity_threshold      = o->opacity_threshold;
+  gint            x;
 
   gfloat *in_buff = in_buf;
   gfloat *out_buff = out_buf;
@@ -204,7 +230,8 @@ process (GeglOperation       *operation,
 
   for (x = 0; x < n_pixels; x++)
     {
-      color_to_alpha (color, in_buff, out_buff);
+      color_to_alpha (color, in_buff, out_buff,
+                      transparency_threshold, opacity_threshold);
       in_buff  += 4;
       out_buff += 4;
     }
@@ -244,10 +271,14 @@ gegl_op_class_init (GeglOpClass *klass)
   filter_class    = GEGL_OPERATION_POINT_FILTER_CLASS (klass);
 
   filter_class->process    = process;
+#if 0 /* see opencl comment above */
   filter_class->cl_process = cl_process;
+#endif
 
   operation_class->prepare = prepare;
+#if 0 /* see opencl comment above */
   operation_class->opencl_support = TRUE;
+#endif
 
   gegl_operation_class_set_keys (operation_class,
     "name",        "gegl:color-to-alpha",
diff --git a/operations/workshop/Makefile.am b/operations/workshop/Makefile.am
index e7f97df..9ec396e 100644
--- a/operations/workshop/Makefile.am
+++ b/operations/workshop/Makefile.am
@@ -12,7 +12,6 @@ opdir = $(ext_dir)
 op_LTLIBRARIES =    \
        bayer-matrix.la \
        bilateral-filter-fast.la \
-       color-to-alpha-plus.la \
        demosaic-bimedian.la \
        demosaic-simple.la \
        ditto.la \
diff --git a/po/POTFILES.in b/po/POTFILES.in
index efd7bde..5686468 100644
--- a/po/POTFILES.in
+++ b/po/POTFILES.in
@@ -236,7 +236,6 @@ operations/transform/transform-core.c
 operations/transform/translate.c
 operations/workshop/bayer-matrix.c
 operations/workshop/bilateral-filter-fast.c
-operations/workshop/color-to-alpha-plus.c
 operations/workshop/demosaic-bimedian.c
 operations/workshop/demosaic-simple.c
 operations/workshop/ditto.c


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