[gegl] workshop: space invade and unify inpainting in one source file



commit 058f789f723a29771bc1b38fa6751fbe8825ce43
Author: Øyvind Kolås <pippin gimp org>
Date:   Thu Jul 4 15:28:54 2019 +0200

    workshop: space invade and unify inpainting in one source file

 operations/workshop/Makefile.am                    |   2 +-
 .../workshop/{pixel-duster.h => alpha-inpaint.c}   | 254 +++++++++++++++++----
 operations/workshop/inpaint.c                      | 215 -----------------
 3 files changed, 212 insertions(+), 259 deletions(-)
---
diff --git a/operations/workshop/Makefile.am b/operations/workshop/Makefile.am
index c020edc87..d6005a29f 100644
--- a/operations/workshop/Makefile.am
+++ b/operations/workshop/Makefile.am
@@ -21,7 +21,7 @@ ops = \
        gcr.la \
        hstack.la \
        aces-rrt.la \
-       inpaint.la \
+       alpha-inpaint.la \
        integral-image.la \
        rawbayer-load.la  \
        segment-kmeans.la \
diff --git a/operations/workshop/pixel-duster.h b/operations/workshop/alpha-inpaint.c
similarity index 72%
rename from operations/workshop/pixel-duster.h
rename to operations/workshop/alpha-inpaint.c
index bc0772aff..e7cf5ceb2 100644
--- a/operations/workshop/pixel-duster.h
+++ b/operations/workshop/alpha-inpaint.c
@@ -1,38 +1,94 @@
-//if 0
-/* pixel-duster
+/* This file is an image processing operation for GEGL
  *
- * the pixel duster data structures and functions are used by multiple ops,
- * but kept in one place since they share so much implementation
+ * GEGL is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
  *
- * a context aware pixel inpainting framework..
+ * GEGL 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
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with GEGL; if not, see <https://www.gnu.org/licenses/>.
+ *
+ * Copyright 2018 Øyvind Kolås <pippin gimp org>
+ *
+ */
 
-   avoid building a database of puzzle pieces - since the puzzle pieces are
-   pixels samples we condense search space by rectifying rotation - only keep a
-   cache.
+#include <stdio.h>
+#include "config.h"
+#include <glib/gi18n-lib.h>
 
-   (the puzzle pieces are stored by prefix match in a large continuous
-   memory region,)
+//retire more props after given set of completed re-runs
 
-   seed database with used spots and immediate neighbors, for faster
-   subsequent lookup
 
-   keep bloom filter - or perhaps even bitmap in GeglBuffer!! for knowing
-   contained in db or not.
+#ifdef GEGL_PROPERTIES
 
- * 2018 (c) Øyvind Kolås pippin gimp org
- */
+property_int (seek_distance, "seek radius", 11)
+  value_range (1, 512)
 
-/*
-    todo:
+property_int (min_iter, "min iter", 100)
+  value_range (1, 512)
 
-         threading
-           create list of hashtables and to hashtable list per thread
+property_int (max_iter, "max iter", 2000)
+  value_range (1, 40000)
 
-         adjust precision of matching
+property_int (improvement_iters, "improvement iters", 4)
+
+property_double (chance_try, "try chance", 0.33)
+  value_range (0.0, 1.0)
+  ui_steps    (0.01, 0.1)
+property_double (chance_retry, "retry chance", 0.8)
+  value_range (0.0, 1.0)
+  ui_steps    (0.01, 0.1)
+
+property_double (ring_twist, "ring twist", 0.0)
+  value_range (0.0, 1.0)
+  ui_steps    (0.01, 0.2)
+
+property_double (ring_gap1,    "ring gap1", 1.3)
+  value_range (0.0, 16.0)
+  ui_steps    (0.25, 0.25)
+
+property_double (ring_gap2,    "ring gap2", 2.5)
+  value_range (0.0, 16.0)
+  ui_steps    (0.25, 0.25)
+
+property_double (ring_gap3,    "ring gap3", 3.7)
+  value_range (0.0, 16.0)
+  ui_steps    (0.25, 0.25)
+
+property_double (ring_gap4,    "ring gap4", 5.5)
+  value_range (0.0, 16.0)
+  ui_steps    (0.25, 0.25)
+
+property_double (metric_dist_powk, "metric dist powk", 2.0)
+  value_range (0.0, 10.0)
+  ui_steps    (0.1, 1.0)
+
+property_double (metric_empty_hay_score, "metric empty hay score", 0.11)
+  value_range (0.01, 100.0)
+  ui_steps    (0.05, 0.1)
+
+property_double (metric_empty_needle_score, "metric empty needle score", 0.2)
+  value_range (0.01, 100.0)
+  ui_steps    (0.05, 0.1)
+
+property_double (metric_cohesion, "metric cohesion", 0.01)
+  value_range (0.0, 10.0)
+  ui_steps    (0.2, 0.2)
+
+#else
+
+#define GEGL_OP_FILTER
+#define GEGL_OP_NAME      alpha_inpaint
+#define GEGL_OP_C_SOURCE  alpha-inpaint.c
+
+#include "gegl-op.h"
+#include <stdio.h>
 
-         replace hashtables with just lists - and include coords in element - perhaps with count..
-         for identical entries - thus not losing accurate median computation capabilitiy..
- */
 #include <math.h>
 
 #define POW2(x) ((x)*(x))
@@ -50,6 +106,7 @@ typedef struct
   GeglSampler   *in_sampler_f;
   GeglSampler   *ref_sampler_f;
   GeglSampler   *out_sampler_f;
+  const Babl    *format; /* RGBA float in right space */
   int            seek_radius;
   int            minimum_iterations;
   int            maximum_iterations;
@@ -194,7 +251,7 @@ static PixelDuster * pixel_duster_new (GeglBuffer *reference,
   ret->min_y = 10000;
   ret->max_age = improvement_iterations;
   ret->ring_twist = ring_twist;
-
+  ret->format = babl_format_with_space ("RGBA float", gegl_buffer_get_format (ret->input));
   ret->in_rect  = *in_rect;
   ret->out_rect = *out_rect;
   ret->metric_dist_powk = metric_dist_powk;
@@ -203,15 +260,15 @@ static PixelDuster * pixel_duster_new (GeglBuffer *reference,
   ret->metric_cohesion = metric_cohesion;
 
   ret->in_sampler_f = gegl_buffer_sampler_new (input,
-                                               babl_format ("RGBA float"),
+                                               ret->format,
                                                GEGL_SAMPLER_CUBIC);
 
   ret->ref_sampler_f = gegl_buffer_sampler_new (reference,
-                                               babl_format ("RGBA float"),
+                                               ret->format,
                                                GEGL_SAMPLER_CUBIC);
 
   ret->out_sampler_f = gegl_buffer_sampler_new (output,
-                                               babl_format ("RGBA float"),
+                                               ret->format,
                                                GEGL_SAMPLER_CUBIC);
 
   for (int i = 0; i < 1; i++)
@@ -253,7 +310,6 @@ void gegl_sampler_prepare (GeglSampler *sampler);
  */
 static void extract_site (PixelDuster *duster, GeglBuffer *buffer, double x, double y, float scale, gfloat 
*dst)
 {
-  static const Babl *format = NULL;
   GeglSampler *sampler_f;
   if (buffer == duster->output)
   {
@@ -269,10 +325,6 @@ static void extract_site (PixelDuster *duster, GeglBuffer *buffer, double x, dou
     sampler_f   = duster->in_sampler_f;
   }
 
-  if (!format){
-    format = babl_format ("RGBA float");
-  }
-
   for (int i = 0; i < NEIGHBORHOOD; i++)
   {
     float dx, dy;
@@ -549,9 +601,6 @@ probe_prep (PixelDuster *duster,
 static void
 probe_post_search (PixelDuster *duster, Probe *probe)
 {
-  static const Babl *format = NULL;
-  if (!format)
-    format = babl_format ("RGBA float");
   probe->age++;
 
   if (probe->score != probe->old_score)
@@ -564,7 +613,7 @@ probe_post_search (PixelDuster *duster, Probe *probe)
 
     gegl_buffer_set (duster->output,
                      GEGL_RECTANGLE(probe->target_x, probe->target_y, 1, 1),
-                     0, format, &rgba[0], 0);
+                     0, duster->format, &rgba[0], 0);
   }
 }
 
@@ -602,7 +651,6 @@ static int probe_improve (PixelDuster *duster,
 {
   needles_t needles;
   //void *ptr[2] = {duster, probe};
-  static const Babl *format = NULL;
 
   if (probe->age >= duster->max_age)
     {
@@ -611,9 +659,6 @@ static int probe_improve (PixelDuster *duster,
       return -1;
     }
 
-  if (!format)
-    format = babl_format ("RGBA float");
-
   probe_prep (duster, probe, needles);
 
   {
@@ -662,11 +707,10 @@ static inline int probes_improve (PixelDuster *duster,
 
 static inline void pixel_duster_add_probes_for_transparent (PixelDuster *duster)
 {
-  const Babl *format = babl_format ("RGBA float");
   GeglBufferIterator *i = gegl_buffer_iterator_new (duster->output,
                                                     &duster->out_rect,
                                                     0,
-                                                    format,
+                                                    duster->format,
                                                     GEGL_ACCESS_WRITE,
                                                     GEGL_ABYSS_NONE, 1);
   while (gegl_buffer_iterator_next (i))
@@ -756,3 +800,127 @@ static inline void pixel_duster_fill (PixelDuster *duster)
   fprintf (stderr, "\n");
 #endif
 }
+
+static GeglRectangle
+get_required_for_output (GeglOperation       *operation,
+                         const gchar         *input_pad,
+                         const GeglRectangle *roi)
+{
+  GeglRectangle result = *gegl_operation_source_get_bounding_box (operation, "input");
+  if (gegl_rectangle_is_infinite_plane (&result))
+    return *roi;
+  return result;
+}
+
+static void
+prepare (GeglOperation *operation)
+{
+  const Babl *space = gegl_operation_get_source_space (operation, "input");
+  const Babl *format = babl_format_with_space ("RGBA float", space);
+
+  gegl_operation_set_format (operation, "input",  format);
+  gegl_operation_set_format (operation, "output", format);
+}
+
+static gboolean
+process (GeglOperation       *operation,
+         GeglBuffer          *input,
+         GeglBuffer          *output,
+         const GeglRectangle *result,
+         gint                 level)
+{
+  GeglProperties *o      = GEGL_PROPERTIES (operation);
+  GeglRectangle in_rect = *gegl_buffer_get_extent (input);
+  GeglRectangle out_rect = *gegl_buffer_get_extent (output);
+  PixelDuster    *duster = pixel_duster_new (input, input, output, &in_rect, &out_rect,
+                                             o->seek_distance,
+                                             o->min_iter,
+                                             o->max_iter,
+                                             o->chance_try,
+                                             o->chance_retry,
+                                             o->improvement_iters,
+                                             o->ring_gap1,
+                                             o->ring_gap2,
+                                             o->ring_gap3,
+                                             o->ring_gap4,
+                                             o->ring_twist,
+                                             o->metric_dist_powk,
+                                             o->metric_empty_hay_score,
+                                             o->metric_empty_needle_score,
+                                             o->metric_cohesion/1000.0,
+                                             operation);
+
+  gegl_buffer_copy (input, NULL, GEGL_ABYSS_NONE, output, NULL);
+
+  pixel_duster_add_probes_for_transparent (duster);
+
+  pixel_duster_fill (duster);
+  pixel_duster_destroy (duster);
+
+  return TRUE;
+}
+
+static GeglRectangle
+get_cached_region (GeglOperation       *operation,
+                   const GeglRectangle *roi)
+{
+  GeglRectangle result = *gegl_operation_source_get_bounding_box (operation, "input");
+
+  if (gegl_rectangle_is_infinite_plane (&result))
+    return *roi;
+
+  return result;
+}
+
+static gboolean
+operation_process (GeglOperation        *operation,
+                   GeglOperationContext *context,
+                   const gchar          *output_prop,
+                   const GeglRectangle  *result,
+                   gint                  level)
+{
+  GeglOperationClass  *operation_class;
+
+  const GeglRectangle *in_rect =
+    gegl_operation_source_get_bounding_box (operation, "input");
+
+  operation_class = GEGL_OPERATION_CLASS (gegl_op_parent_class);
+
+  if ((in_rect && gegl_rectangle_is_infinite_plane (in_rect)))
+    {
+      gpointer in = gegl_operation_context_get_object (context, "input");
+      gegl_operation_context_take_object (context, "output",
+                                          g_object_ref (G_OBJECT (in)));
+      return TRUE;
+    }
+
+  return operation_class->process (operation, context, output_prop, result,
+                                   gegl_operation_context_get_level (context));
+}
+
+static void
+gegl_op_class_init (GeglOpClass *klass)
+{
+  GeglOperationClass       *operation_class;
+  GeglOperationFilterClass *filter_class;
+
+  operation_class = GEGL_OPERATION_CLASS (klass);
+  filter_class    = GEGL_OPERATION_FILTER_CLASS (klass);
+
+  filter_class->process                    = process;
+  operation_class->prepare                 = prepare;
+  operation_class->process                 = operation_process;
+  operation_class->get_required_for_output = get_required_for_output;
+  operation_class->get_cached_region       = get_cached_region;
+  operation_class->opencl_support          = FALSE;
+  operation_class->threaded                = FALSE;
+
+  gegl_operation_class_set_keys (operation_class,
+      "name",        "gegl:alpha-inpaint",
+      "title",       "Heal transparent",
+      "categories",  "heal",
+      "description", "Replaces fully transparent pixels with good candidate pixels found in the whole image",
+      NULL);
+}
+
+#endif 


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