[gimp] app: add gimp_drawable_filter_set_add_alpha()



commit 1f81c3140584473991dbb4a9be058ae87b9289ce
Author: Ell <ell_se yahoo com>
Date:   Wed Jan 15 23:20:38 2020 +0200

    app: add gimp_drawable_filter_set_add_alpha()
    
    Add an optional "format" parameter to gimp_drawable_merge_filter(),
    which specifies the format to use for the output, possibly changing
    the drawable's format.
    
    In GimpDrawableFilter, add a gimp_drawable_filter_set_add_alpha()
    function, which allows the filter to add an alpha channel to the
    drawable when committed (by passing an appropriate format to
    gimp_drawable_merge_filter()).

 app/core/gimpdrawable-filters.c         | 37 ++++++++++++++++++++++++++-------
 app/core/gimpdrawable-filters.h         |  1 +
 app/core/gimpdrawablefilter.c           | 34 +++++++++++++++++++++++++++---
 app/core/gimpdrawablefilter.h           |  2 ++
 app/core/gimplayer-floating-selection.c |  2 +-
 5 files changed, 65 insertions(+), 11 deletions(-)
---
diff --git a/app/core/gimpdrawable-filters.c b/app/core/gimpdrawable-filters.c
index 3cdb218050..5e1dc8bb51 100644
--- a/app/core/gimpdrawable-filters.c
+++ b/app/core/gimpdrawable-filters.c
@@ -111,6 +111,7 @@ gimp_drawable_merge_filter (GimpDrawable *drawable,
                             GimpFilter   *filter,
                             GimpProgress *progress,
                             const gchar  *undo_desc,
+                            const Babl   *format,
                             gboolean      clip,
                             gboolean      cancellable,
                             gboolean      update)
@@ -121,7 +122,7 @@ gimp_drawable_merge_filter (GimpDrawable *drawable,
   const Babl     *applicator_output_format = NULL;
   GeglBuffer     *buffer                   = NULL;
   GeglBuffer     *dest_buffer;
-  GeglBuffer     *undo_buffer;
+  GeglBuffer     *undo_buffer              = NULL;
   GeglRectangle   undo_rect;
   GeglBuffer     *cache                    = NULL;
   GeglRectangle  *rects                    = NULL;
@@ -137,6 +138,9 @@ gimp_drawable_merge_filter (GimpDrawable *drawable,
   applicator  = gimp_filter_get_applicator (filter);
   dest_buffer = gimp_drawable_get_buffer (drawable);
 
+  if (format == gimp_drawable_get_format (drawable))
+    format = NULL;
+
   rect = gegl_node_get_bounding_box (gimp_filter_get_node (filter));
 
   if (! clip && gegl_rectangle_equal (&rect,
@@ -153,11 +157,19 @@ gimp_drawable_merge_filter (GimpDrawable *drawable,
         {
           return TRUE;
         }
+
+      if (format)
+        {
+          buffer = gegl_buffer_new (gegl_buffer_get_extent (dest_buffer),
+                                    format);
+
+          dest_buffer = buffer;
+        }
     }
   else
     {
       buffer = gegl_buffer_new (GEGL_RECTANGLE (0, 0, rect.width, rect.height),
-                                gimp_drawable_get_format (drawable));
+                                format);
 
       dest_buffer = g_object_new (GEGL_TYPE_BUFFER,
                                   "source",  buffer,
@@ -193,7 +205,7 @@ gimp_drawable_merge_filter (GimpDrawable *drawable,
         gimp_applicator_set_output_format (applicator, NULL);
     }
 
-  if (clip)
+  if (! buffer)
     {
       gegl_rectangle_align_to_buffer (
         &undo_rect,
@@ -231,9 +243,19 @@ gimp_drawable_merge_filter (GimpDrawable *drawable,
 
       if (clip)
         {
-          gimp_drawable_push_undo (drawable, undo_desc, undo_buffer,
-                                   undo_rect.x, undo_rect.y,
-                                   undo_rect.width, undo_rect.height);
+          if (buffer)
+            {
+              gimp_drawable_set_buffer_full (drawable,
+                                             TRUE, undo_desc,
+                                             buffer, NULL,
+                                             FALSE);
+            }
+          else
+            {
+              gimp_drawable_push_undo (drawable, undo_desc, undo_buffer,
+                                       undo_rect.x, undo_rect.y,
+                                       undo_rect.width, undo_rect.height);
+            }
         }
       else
         {
@@ -289,7 +311,8 @@ gimp_drawable_merge_filter (GimpDrawable *drawable,
 
   if (clip)
     {
-      g_object_unref (undo_buffer);
+      g_clear_object (&undo_buffer);
+      g_clear_object (&buffer);
     }
   else
     {
diff --git a/app/core/gimpdrawable-filters.h b/app/core/gimpdrawable-filters.h
index 389c9c00a1..504d402199 100644
--- a/app/core/gimpdrawable-filters.h
+++ b/app/core/gimpdrawable-filters.h
@@ -37,6 +37,7 @@ gboolean        gimp_drawable_merge_filter  (GimpDrawable *drawable,
                                              GimpFilter   *filter,
                                              GimpProgress *progress,
                                              const gchar  *undo_desc,
+                                             const Babl   *format,
                                              gboolean      clip,
                                              gboolean      cancellable,
                                              gboolean      update);
diff --git a/app/core/gimpdrawablefilter.c b/app/core/gimpdrawablefilter.c
index 691f0f0adf..e4461a635a 100644
--- a/app/core/gimpdrawablefilter.c
+++ b/app/core/gimpdrawablefilter.c
@@ -77,6 +77,7 @@ struct _GimpDrawableFilter
   GimpLayerColorSpace     blend_space;
   GimpLayerColorSpace     composite_space;
   GimpLayerCompositeMode  composite_mode;
+  gboolean                add_alpha;
   gboolean                color_managed;
   gboolean                gamma_hack;
 
@@ -419,6 +420,23 @@ gimp_drawable_filter_set_mode (GimpDrawableFilter     *filter,
     }
 }
 
+void
+gimp_drawable_filter_set_add_alpha (GimpDrawableFilter *filter,
+                                    gboolean            add_alpha)
+{
+  g_return_if_fail (GIMP_IS_DRAWABLE_FILTER (filter));
+
+  if (add_alpha != filter->add_alpha)
+    {
+      filter->add_alpha = add_alpha;
+
+      gimp_drawable_filter_sync_format (filter);
+
+      if (gimp_drawable_filter_is_filtering (filter))
+        gimp_drawable_filter_update_drawable (filter, NULL);
+    }
+}
+
 void
 gimp_drawable_filter_set_gamma_hack (GimpDrawableFilter *filter,
                                      gboolean            gamma_hack)
@@ -466,6 +484,10 @@ gimp_drawable_filter_commit (GimpDrawableFilter *filter,
 
   if (gimp_drawable_filter_is_filtering (filter))
     {
+      const Babl *format;
+
+      format = gimp_applicator_get_output_format (filter->applicator);
+
       gimp_drawable_filter_set_preview (filter, FALSE,
                                         filter->preview_alignment,
                                         filter->preview_position);
@@ -474,6 +496,7 @@ gimp_drawable_filter_commit (GimpDrawableFilter *filter,
                                             GIMP_FILTER (filter),
                                             progress,
                                             gimp_object_get_name (filter),
+                                            format,
                                             filter->filter_clip,
                                             cancellable,
                                             FALSE);
@@ -770,9 +793,14 @@ gimp_drawable_filter_sync_affect (GimpDrawableFilter *filter)
 static void
 gimp_drawable_filter_sync_format (GimpDrawableFilter *filter)
 {
-  gimp_applicator_set_output_format (
-    filter->applicator,
-    gimp_drawable_get_format (filter->drawable));
+  const Babl *format;
+
+  if (filter->add_alpha && GIMP_IS_LAYER (filter->drawable))
+    format = gimp_drawable_get_format_with_alpha (filter->drawable);
+  else
+    format = gimp_drawable_get_format (filter->drawable);
+
+  gimp_applicator_set_output_format (filter->applicator, format);
 }
 
 static void
diff --git a/app/core/gimpdrawablefilter.h b/app/core/gimpdrawablefilter.h
index 53dd214955..14bb699359 100644
--- a/app/core/gimpdrawablefilter.h
+++ b/app/core/gimpdrawablefilter.h
@@ -73,6 +73,8 @@ void       gimp_drawable_filter_set_mode       (GimpDrawableFilter  *filter,
                                                 GimpLayerColorSpace  blend_space,
                                                 GimpLayerColorSpace  composite_space,
                                                 GimpLayerCompositeMode composite_mode);
+void       gimp_drawable_filter_set_add_alpha  (GimpDrawableFilter  *filter,
+                                                gboolean             add_alpha);
 
 void       gimp_drawable_filter_set_gamma_hack (GimpDrawableFilter  *filter,
                                                 gboolean             gamma_hack);
diff --git a/app/core/gimplayer-floating-selection.c b/app/core/gimplayer-floating-selection.c
index aa6cc6bc2a..80222a2b0c 100644
--- a/app/core/gimplayer-floating-selection.c
+++ b/app/core/gimplayer-floating-selection.c
@@ -142,7 +142,7 @@ floating_sel_anchor (GimpLayer *layer)
   if (filter)
     {
       gimp_drawable_merge_filter (drawable, filter, NULL, NULL,
-                                  FALSE, FALSE, FALSE);
+                                  NULL, FALSE, FALSE, FALSE);
     }
 
   gimp_image_remove_layer (image, layer, TRUE, NULL);


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