[gimp] Bug 777625 - Wavelet decompose plugin



commit 17fb208e1ebac7f67949143e9fa6df4a688d7152
Author: Miroslav Talasek <miroslav talasek seznam cz>
Date:   Mon Jan 23 02:04:17 2017 +0100

    Bug 777625 - Wavelet decompose plugin
    
    Add wavelet decompose plug-in implemented from scratch.
    
    Did some cleanup and added undo group and progress (mitch).

 plug-ins/common/.gitignore          |    2 +
 plug-ins/common/Makefile.am         |   20 ++-
 plug-ins/common/gimprc.common       |    1 +
 plug-ins/common/plugin-defs.pl      |    1 +
 plug-ins/common/wavelet-decompose.c |  313 +++++++++++++++++++++++++++++++++++
 po-plug-ins/POTFILES.in             |    1 +
 6 files changed, 337 insertions(+), 1 deletions(-)
---
diff --git a/plug-ins/common/.gitignore b/plug-ins/common/.gitignore
index 61c986d..e9c7cc9 100644
--- a/plug-ins/common/.gitignore
+++ b/plug-ins/common/.gitignore
@@ -184,6 +184,8 @@
 /van-gogh-lic.exe
 /warp
 /warp.exe
+/wavelet-decompose
+/wavelet-decompose.exe
 /web-browser
 /web-browser.exe
 /web-page
diff --git a/plug-ins/common/Makefile.am b/plug-ins/common/Makefile.am
index 90b048b..f6a6d69 100644
--- a/plug-ins/common/Makefile.am
+++ b/plug-ins/common/Makefile.am
@@ -140,6 +140,7 @@ libexec_PROGRAMS = \
        unsharp-mask \
        van-gogh-lic \
        warp \
+       wavelet-decompose \
        web-browser \
        $(WEB_PAGE)
 
@@ -1608,7 +1609,6 @@ smooth_palette_LDADD = \
        $(libgimpcolor)         \
        $(libgimpbase)          \
        $(GTK_LIBS)             \
-       $(GEGL_LIBS)            \
        $(RT_LIBS)              \
        $(INTLLIBS)             \
        $(smooth_palette_RC)
@@ -1767,6 +1767,24 @@ warp_LDADD = \
        $(INTLLIBS)             \
        $(warp_RC)
 
+wavelet_decompose_SOURCES = \
+       wavelet-decompose.c
+
+wavelet_decompose_LDADD = \
+       $(libgimpui)            \
+       $(libgimpwidgets)       \
+       $(libgimpmodule)        \
+       $(libgimp)              \
+       $(libgimpmath)          \
+       $(libgimpconfig)        \
+       $(libgimpcolor)         \
+       $(libgimpbase)          \
+       $(GTK_LIBS)             \
+       $(GEGL_LIBS)            \
+       $(RT_LIBS)              \
+       $(INTLLIBS)             \
+       $(wavelet_decompose_RC)
+
 web_browser_LDFLAGS = $(framework_cocoa)
 
 web_browser_CPPFLAGS = $(AM_CPPFLAGS) $(xobjective_c)
diff --git a/plug-ins/common/gimprc.common b/plug-ins/common/gimprc.common
index 0ff328d..ad384c2 100644
--- a/plug-ins/common/gimprc.common
+++ b/plug-ins/common/gimprc.common
@@ -89,5 +89,6 @@ unit_editor_RC = unit-editor.rc.o
 unsharp_mask_RC = unsharp-mask.rc.o
 van_gogh_lic_RC = van-gogh-lic.rc.o
 warp_RC = warp.rc.o
+wavelet_decompose_RC = wavelet-decompose.rc.o
 web_browser_RC = web-browser.rc.o
 web_page_RC = web-page.rc.o
diff --git a/plug-ins/common/plugin-defs.pl b/plug-ins/common/plugin-defs.pl
index 324e7da..5bef429 100644
--- a/plug-ins/common/plugin-defs.pl
+++ b/plug-ins/common/plugin-defs.pl
@@ -90,6 +90,7 @@
     'unsharp-mask' => { ui => 1 },
     'van-gogh-lic' => { ui => 1 },
     'warp' => { ui => 1 },
+    'wavelet-decompose' => { ui => 1, gegl => 1 },
     'web-browser' => { ui => 1, ldflags => '$(framework_cocoa)', cppflags => '$(AM_CPPFLAGS) 
$(xobjective_c)' },
     'web-page' => { ui => 1, optional => 1, libs => 'WEBKIT_LIBS', cflags => 'WEBKIT_CFLAGS' }
 );
diff --git a/plug-ins/common/wavelet-decompose.c b/plug-ins/common/wavelet-decompose.c
new file mode 100644
index 0000000..d3b4685
--- /dev/null
+++ b/plug-ins/common/wavelet-decompose.c
@@ -0,0 +1,313 @@
+/*
+ *  Wavelet decompose plug-in by Miroslav Talasek, miroslav talasek seznam cz
+ */
+
+/*
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "config.h"
+
+#include <libgimp/gimp.h>
+#include <libgimp/gimpui.h>
+
+#include "libgimp/stdplugins-intl.h"
+
+
+#define PLUG_IN_PROC   "plug-in-wavelet-decompose"
+#define PLUG_IN_ROLE   "gimp-wavelet-decompose"
+#define PLUG_IN_BINARY "wavelet-decompose"
+
+#define SCALE_WIDTH   120
+#define ENTRY_WIDTH     5
+
+
+typedef struct
+{
+  gint     scales;
+} WaveletDecomposeParams;
+
+
+/* Declare local functions.
+ */
+static void      query                    (void);
+static void      run                      (const gchar      *name,
+                                           gint              nparams,
+                                           const GimpParam  *param,
+                                           gint             *nreturn_vals,
+                                           GimpParam       **return_vals);
+
+static void      wavelet_blur             (gint32            drawable_id,
+                                           gint              radius);
+static gboolean  wavelet_decompose_dialog (void);
+
+
+/* create a few globals, set default values */
+static WaveletDecomposeParams wavelet_params =
+{
+  5 /*default scales*/
+};
+
+const GimpPlugInInfo PLUG_IN_INFO =
+{
+  NULL,  /* init_proc  */
+  NULL,  /* quit_proc  */
+  query, /* query_proc */
+  run,   /* run_proc   */
+};
+
+
+MAIN ()
+
+
+static void
+query (void)
+{
+  static const GimpParamDef args[] =
+  {
+    { GIMP_PDB_INT32,    "run-mode", "The run mode { RUN-INTERACTIVE (0), RUN-NONINTERACTIVE (1) }" },
+    { GIMP_PDB_IMAGE,    "image",    "Input image (unused)"   },
+    { GIMP_PDB_DRAWABLE, "drawable", "Input drawable"         },
+    { GIMP_PDB_INT32,    "scales",   "Number of scales (1-7)" }
+  };
+
+  gimp_install_procedure (PLUG_IN_PROC,
+                          N_("Wavelet decompose"),
+                          "compute wavelet scales",
+                          "Miroslav Talasek <miroslav talasek seznam cz>",
+                          "Miroslav Talasek <miroslav talasek seznam cz>",
+                          "19january 2017",
+                          N_("Wavelet-decompose"),
+                          "RGB*",
+                          GIMP_PLUGIN,
+                          G_N_ELEMENTS (args), 0,
+                          args, NULL);
+
+  gimp_plugin_menu_register (PLUG_IN_PROC, "<Image>/Filters/Enhance");
+}
+
+static void
+run (const gchar      *name,
+     gint              nparams,
+     const GimpParam  *param,
+     gint             *nreturn_vals,
+     GimpParam       **return_vals)
+{
+  static GimpParam   values[1];
+  GimpPDBStatusType  status = GIMP_PDB_SUCCESS;
+  gint32             image_id;
+  gint32             drawable_id;
+  GimpRunMode        run_mode;
+
+  run_mode = param[0].data.d_int32;
+
+  INIT_I18N();
+  gegl_init (NULL, NULL);
+
+  *nreturn_vals = 1;
+  *return_vals  = values;
+
+  values[0].type          = GIMP_PDB_STATUS;
+  values[0].data.d_status = status;
+
+  image_id    = param[1].data.d_image;
+  drawable_id = param[2].data.d_drawable;
+
+  switch (run_mode)
+    {
+    case GIMP_RUN_INTERACTIVE:
+      gimp_get_data (PLUG_IN_PROC, &wavelet_params);
+
+      if (! wavelet_decompose_dialog ())
+        return;
+      break;
+
+    case GIMP_RUN_NONINTERACTIVE:
+      if (nparams != 4)
+        {
+          status = GIMP_PDB_CALLING_ERROR;
+        }
+      else
+        {
+          wavelet_params.scales = param[3].data.d_int32;
+        }
+      break;
+
+    case GIMP_RUN_WITH_LAST_VALS:
+      gimp_get_data (PLUG_IN_PROC, &wavelet_params);
+      break;
+
+    default:
+      break;
+    }
+
+  if (status == GIMP_PDB_SUCCESS)
+    {
+      gint32 *scale_ids;
+      gint32  original_id;
+      gint    id;
+
+      gimp_progress_init (_("Wavelet-Decompose"));
+
+      gimp_image_undo_group_start (image_id);
+
+      scale_ids = g_new (gint32, wavelet_params.scales);
+      original_id = gimp_layer_copy (drawable_id);
+      gimp_image_insert_layer (image_id, original_id, -1, 0);
+
+      for (id = 0 ; id < wavelet_params.scales; ++id)
+        {
+          gint32 blur_id;
+          gchar  scale_name[20];
+
+          gimp_progress_update ((gdouble) id / (gdouble) wavelet_params.scales);
+
+          /* blur */
+          blur_id = gimp_layer_copy (original_id);
+          gimp_image_insert_layer (image_id, blur_id, 0,
+                                   gimp_image_get_item_position (image_id,
+                                                                 original_id));
+          wavelet_blur (blur_id, 1 << id);
+
+          /* grain extract */
+          gimp_layer_set_mode (blur_id,GIMP_LAYER_MODE_GRAIN_EXTRACT);
+
+          /* new from visible */
+          g_snprintf (scale_name, sizeof (scale_name), "Scale %d", id + 1);
+          scale_ids[id] = gimp_layer_new_from_visible (image_id, image_id,
+                                                       scale_name);
+
+          gimp_image_insert_layer (image_id, scale_ids[id], 0,
+                                   gimp_image_get_item_position (image_id,
+                                                                 blur_id) );
+          gimp_layer_set_mode (scale_ids[id], GIMP_LAYER_MODE_GRAIN_MERGE);
+          gimp_layer_set_mode (blur_id, GIMP_LAYER_MODE_NORMAL);
+          gimp_item_set_visible (scale_ids[id], FALSE);
+
+          gimp_image_remove_layer (image_id, original_id);
+
+          original_id = blur_id;
+        }
+
+      gimp_item_set_name (original_id, "Residual");
+
+      for (id = 0 ; id < wavelet_params.scales; ++id)
+        {
+          gimp_item_set_visible (scale_ids[id], TRUE);
+        }
+
+      g_free (scale_ids);
+
+      gimp_image_undo_group_end (image_id);
+
+      gimp_progress_update (1.0);
+
+      values[0].data.d_status = status;
+      gimp_displays_flush ();
+
+      /* set data for next use of filter */
+      if (run_mode == GIMP_RUN_INTERACTIVE)
+        gimp_set_data (PLUG_IN_PROC,
+                       &wavelet_params, sizeof (WaveletDecomposeParams));
+    }
+
+  gegl_exit ();
+}
+
+static void
+wavelet_blur (gint32 drawable_id,
+              gint   radius)
+{
+  gint x, y, width, height;
+
+  if (gimp_drawable_mask_intersect (drawable_id, &x, &y, &width, &height))
+    {
+      GeglBuffer *buffer;
+      GeglBuffer *shadow_buffer;
+
+      buffer        = gimp_drawable_get_buffer (drawable_id);
+      shadow_buffer = gimp_drawable_get_shadow_buffer (drawable_id);
+
+      gegl_render_op (buffer, shadow_buffer,
+                      "gegl:wavelet-blur",
+                      "radius", (gdouble) radius,
+                      NULL);
+
+      g_object_unref (shadow_buffer); /* flushes the shadow blur */
+      g_object_unref (buffer);
+
+      gimp_drawable_merge_shadow (drawable_id, TRUE);
+      gimp_drawable_update (drawable_id, x, y, width, height);
+      gimp_displays_flush ();
+    }
+}
+
+static gboolean
+wavelet_decompose_dialog (void)
+{
+  GtkWidget *dialog;
+  GtkWidget *main_vbox;
+  GtkWidget *table;
+  GtkObject *adj;
+  gboolean   run;
+
+  gimp_ui_init (PLUG_IN_BINARY, TRUE);
+
+  dialog = gimp_dialog_new (_("Wavelet decompose"), PLUG_IN_ROLE,
+                            NULL, 0,
+                            gimp_standard_help_func, PLUG_IN_PROC,
+
+                            GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
+                            GTK_STOCK_OK,     GTK_RESPONSE_OK,
+
+                            NULL);
+
+  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
+                                           GTK_RESPONSE_OK,
+                                           GTK_RESPONSE_CANCEL,
+                                           -1);
+
+  gimp_window_set_transient (GTK_WINDOW (dialog));
+
+  main_vbox = gtk_box_new (GTK_ORIENTATION_VERTICAL, 12);
+  gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
+  gtk_box_pack_start (GTK_BOX (gtk_dialog_get_content_area (GTK_DIALOG (dialog))),
+                      main_vbox, TRUE, TRUE, 0);
+  gtk_widget_show (main_vbox);
+
+  table = gtk_table_new (3, 1, FALSE);
+  gtk_table_set_col_spacings (GTK_TABLE (table), 6);
+  gtk_table_set_row_spacings (GTK_TABLE (table), 6);
+  gtk_box_pack_start (GTK_BOX (main_vbox), table, FALSE, FALSE, 0);
+  gtk_widget_show (table);
+
+  adj = gimp_scale_entry_new (GTK_TABLE (table), 0, 0,
+                              _("Scales:"), SCALE_WIDTH, ENTRY_WIDTH,
+                              wavelet_params.scales,
+                              1.0, 7.0, 1.0, 1.0, 0,
+                              TRUE, 0, 0,
+                              NULL, NULL);
+
+  g_signal_connect (adj, "value-changed",
+                    G_CALLBACK (gimp_int_adjustment_update),
+                    &wavelet_params.scales);
+
+  gtk_widget_show (dialog);
+
+  run = (gimp_dialog_run (GIMP_DIALOG (dialog)) == GTK_RESPONSE_OK);
+
+  gtk_widget_destroy (dialog);
+
+  return run;
+}
diff --git a/po-plug-ins/POTFILES.in b/po-plug-ins/POTFILES.in
index 2d3c511..8af2873 100644
--- a/po-plug-ins/POTFILES.in
+++ b/po-plug-ins/POTFILES.in
@@ -94,6 +94,7 @@ plug-ins/common/unit-editor.c
 plug-ins/common/unsharp-mask.c
 plug-ins/common/van-gogh-lic.c
 plug-ins/common/warp.c
+plug-ins/common/wavelet-decompose.c
 plug-ins/common/web-browser.c
 plug-ins/common/web-page.c
 plug-ins/file-bmp/bmp-load.c


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