[gimp] app: add gimp_fill_options_create_buffer()



commit fc2a4cd589dcbb9767d508fcab9dbf7799bc780f
Author: Michael Natterer <mitch gimp org>
Date:   Sun May 29 18:25:14 2016 +0200

    app: add gimp_fill_options_create_buffer()
    
    Which creates a buffer from GimpFillOptions that can be applied to a
    drawable. Eliminates three slightly different copies of the same
    code. Also adds to the color history for each color fill, we missed
    two places before.

 app/core/gimp-edit.c                |   54 +++----------------------------
 app/core/gimpdrawable-bucket-fill.c |   39 ++---------------------
 app/core/gimpdrawable-fill.c        |   41 +++--------------------
 app/core/gimpfilloptions.c          |   59 ++++++++++++++++++++++++++++++++++-
 app/core/gimpfilloptions.h          |   48 +++++++++++++++-------------
 5 files changed, 99 insertions(+), 142 deletions(-)
---
diff --git a/app/core/gimp-edit.c b/app/core/gimp-edit.c
index 0cf1fde..14b4923 100644
--- a/app/core/gimp-edit.c
+++ b/app/core/gimp-edit.c
@@ -44,7 +44,6 @@
 #include "gimplayer-floating-selection.h"
 #include "gimplayer-new.h"
 #include "gimplist.h"
-#include "gimppattern.h"
 #include "gimppickable.h"
 #include "gimpselection.h"
 #include "gimptempbuf.h"
@@ -427,10 +426,7 @@ gimp_edit_fill (GimpImage       *image,
                 GimpFillOptions *options,
                 const gchar     *undo_desc)
 {
-  GeglBuffer  *dest_buffer;
-  GimpPattern *pattern = NULL;
-  GimpRGB      color;
-  const Babl  *format;
+  GeglBuffer  *buffer;
   gint         x, y, width, height;
 
   g_return_if_fail (GIMP_IS_IMAGE (image));
@@ -441,59 +437,21 @@ gimp_edit_fill (GimpImage       *image,
   if (! gimp_item_mask_intersect (GIMP_ITEM (drawable), &x, &y, &width, &height))
     return;  /*  nothing to do, but the fill succeeded  */
 
-  switch (gimp_fill_options_get_style (options))
-    {
-    case GIMP_FILL_STYLE_SOLID:
-      gimp_context_get_foreground (GIMP_CONTEXT (options), &color);
-      gimp_pickable_srgb_to_image_color (GIMP_PICKABLE (drawable),
-                                         &color, &color);
-      break;
-
-    case GIMP_FILL_STYLE_PATTERN:
-      pattern = gimp_context_get_pattern (GIMP_CONTEXT (options));
-      break;
-    }
-
-  if (pattern &&
-      babl_format_has_alpha (gimp_temp_buf_get_format (pattern->mask)) &&
-      ! gimp_drawable_has_alpha (drawable))
-    {
-      format = gimp_drawable_get_format_with_alpha (drawable);
-    }
-  else
-    {
-      format = gimp_drawable_get_format (drawable);
-    }
-
-  dest_buffer = gegl_buffer_new (GEGL_RECTANGLE (0, 0, width, height),
-                                 format);
-
-  if (pattern)
-    {
-      GeglBuffer *src_buffer = gimp_pattern_create_buffer (pattern);
-
-      gegl_buffer_set_pattern (dest_buffer, NULL, src_buffer, 0, 0);
-      g_object_unref (src_buffer);
-    }
-  else
-    {
-      GeglColor *gegl_color = gimp_gegl_color_new (&color);
-
-      gegl_buffer_set_color (dest_buffer, NULL, gegl_color);
-      g_object_unref (gegl_color);
-    }
+  buffer = gimp_fill_options_create_buffer (options, drawable,
+                                            GEGL_RECTANGLE (0, 0,
+                                                            width, height));
 
   if (! undo_desc)
     undo_desc = gimp_fill_options_get_undo_desc (options);
 
-  gimp_drawable_apply_buffer (drawable, dest_buffer,
+  gimp_drawable_apply_buffer (drawable, buffer,
                               GEGL_RECTANGLE (0, 0, width, height),
                               TRUE, undo_desc,
                               gimp_context_get_opacity (GIMP_CONTEXT (options)),
                               gimp_context_get_paint_mode (GIMP_CONTEXT (options)),
                               NULL, x, y);
 
-  g_object_unref (dest_buffer);
+  g_object_unref (buffer);
 
   gimp_drawable_update (drawable, x, y, width, height);
 }
diff --git a/app/core/gimpdrawable-bucket-fill.c b/app/core/gimpdrawable-bucket-fill.c
index 70a3926..b2b4a08 100644
--- a/app/core/gimpdrawable-bucket-fill.c
+++ b/app/core/gimpdrawable-bucket-fill.c
@@ -31,12 +31,10 @@
 #include "gegl/gimp-gegl-utils.h"
 
 #include "gimp.h"
-#include "gimp-palettes.h"
 #include "gimpdrawable.h"
 #include "gimpdrawable-bucket-fill.h"
 #include "gimpfilloptions.h"
 #include "gimpimage.h"
-#include "gimppattern.h"
 #include "gimppickable.h"
 #include "gimppickable-contiguous-region.h"
 
@@ -60,8 +58,6 @@ gimp_drawable_bucket_fill (GimpDrawable         *drawable,
   GimpPickable *pickable;
   GeglBuffer   *buffer;
   GeglBuffer   *mask_buffer;
-  GimpPattern  *pattern = NULL;
-  GimpRGB       color;
   gint          x1, y1, x2, y2;
   gint          mask_offset_x = 0;
   gint          mask_offset_y = 0;
@@ -146,38 +142,9 @@ gimp_drawable_bucket_fill (GimpDrawable         *drawable,
       mask_offset_y = y1;
     }
 
-  buffer = gegl_buffer_new (GEGL_RECTANGLE (0, 0, x2 - x1, y2 - y1),
-                            gimp_drawable_get_format_with_alpha (drawable));
-
-  switch (gimp_fill_options_get_style (options))
-    {
-    case GIMP_FILL_STYLE_SOLID:
-      gimp_context_get_foreground (GIMP_CONTEXT (options), &color);
-      gimp_pickable_srgb_to_image_color (GIMP_PICKABLE (drawable),
-                                         &color, &color);
-      break;
-
-    case GIMP_FILL_STYLE_PATTERN:
-      pattern = gimp_context_get_pattern (GIMP_CONTEXT (options));
-      break;
-    }
-
-  if (pattern)
-    {
-      GeglBuffer *pattern_buffer = gimp_pattern_create_buffer (pattern);
-
-      gegl_buffer_set_pattern (buffer, NULL, pattern_buffer, -x1, -y1);
-      g_object_unref (pattern_buffer);
-    }
-  else
-    {
-      GeglColor *gegl_color = gimp_gegl_color_new (&color);
-
-      gegl_buffer_set_color (buffer, NULL, gegl_color);
-      g_object_unref (gegl_color);
-
-      gimp_palettes_add_color_history (image->gimp, &color);
-    }
+  buffer = gimp_fill_options_create_buffer (options, drawable,
+                                            GEGL_RECTANGLE (0, 0,
+                                                            x2 - x1, y2 - y1));
 
   gimp_gegl_apply_opacity (buffer, NULL, NULL, buffer,
                            mask_buffer,
diff --git a/app/core/gimpdrawable-fill.c b/app/core/gimpdrawable-fill.c
index 1f436c3..29e919a 100644
--- a/app/core/gimpdrawable-fill.c
+++ b/app/core/gimpdrawable-fill.c
@@ -168,7 +168,7 @@ gimp_drawable_fill_scan_convert (GimpDrawable    *drawable,
 {
   GimpContext *context;
   GimpImage   *image;
-  GeglBuffer  *base_buffer;
+  GeglBuffer  *buffer;
   GeglBuffer  *mask_buffer;
   gint         x, y, w, h;
   gint         off_x;
@@ -214,51 +214,22 @@ gimp_drawable_fill_scan_convert (GimpDrawable    *drawable,
                             x + off_x, y + off_y,
                             gimp_fill_options_get_antialias (options));
 
-  base_buffer = gegl_buffer_new (GEGL_RECTANGLE (0, 0, w, h),
-                                 gimp_drawable_get_format_with_alpha (drawable));
+  buffer = gimp_fill_options_create_buffer (options, drawable,
+                                            GEGL_RECTANGLE (0, 0, w, h));
 
-  switch (gimp_fill_options_get_style (options))
-    {
-    case GIMP_FILL_STYLE_SOLID:
-      {
-        GimpRGB    fg;
-        GeglColor *color;
-
-        gimp_context_get_foreground (context, &fg);
-        gimp_pickable_srgb_to_image_color (GIMP_PICKABLE (drawable),
-                                           &fg, &fg);
-
-        color = gimp_gegl_color_new (&fg);
-        gegl_buffer_set_color (base_buffer, NULL, color);
-        g_object_unref (color);
-      }
-      break;
-
-    case GIMP_FILL_STYLE_PATTERN:
-      {
-        GimpPattern *pattern = gimp_context_get_pattern (context);
-        GeglBuffer  *pattern_buffer;
-
-        pattern_buffer = gimp_pattern_create_buffer (pattern);
-        gegl_buffer_set_pattern (base_buffer, NULL, pattern_buffer, 0, 0);
-        g_object_unref (pattern_buffer);
-      }
-      break;
-    }
-
-  gimp_gegl_apply_opacity (base_buffer, NULL, NULL, base_buffer,
+  gimp_gegl_apply_opacity (buffer, NULL, NULL, buffer,
                            mask_buffer, 0, 0, 1.0);
   g_object_unref (mask_buffer);
 
   /* Apply to drawable */
-  gimp_drawable_apply_buffer (drawable, base_buffer,
+  gimp_drawable_apply_buffer (drawable, buffer,
                               GEGL_RECTANGLE (0, 0, w, h),
                               push_undo, C_("undo-type", "Render Stroke"),
                               gimp_context_get_opacity (context),
                               gimp_context_get_paint_mode (context),
                               NULL, x, y);
 
-  g_object_unref (base_buffer);
+  g_object_unref (buffer);
 
   gimp_drawable_update (drawable, x, y, w, h);
 }
diff --git a/app/core/gimpfilloptions.c b/app/core/gimpfilloptions.c
index 25e9c10..dff7592 100644
--- a/app/core/gimpfilloptions.c
+++ b/app/core/gimpfilloptions.c
@@ -30,10 +30,15 @@
 
 #include "core-types.h"
 
+#include "gegl/gimp-gegl-utils.h"
+
 #include "gimp.h"
+#include "gimp-palettes.h"
+#include "gimpdrawable.h"
 #include "gimperror.h"
 #include "gimpfilloptions.h"
-#include "gimpviewable.h"
+#include "gimppattern.h"
+#include "gimppickable.h"
 
 #include "gimp-intl.h"
 
@@ -381,3 +386,55 @@ gimp_fill_options_get_undo_desc (GimpFillOptions *options)
 
   g_return_val_if_reached (NULL);
 }
+
+GeglBuffer *
+gimp_fill_options_create_buffer (GimpFillOptions     *options,
+                                 GimpDrawable        *drawable,
+                                 const GeglRectangle *rect)
+{
+  GeglBuffer  *buffer;
+  GimpPattern *pattern = NULL;
+  GimpRGB      color;
+
+  g_return_val_if_fail (GIMP_IS_FILL_OPTIONS (options), NULL);
+  g_return_val_if_fail (gimp_fill_options_get_style (options) !=
+                        GIMP_FILL_STYLE_PATTERN ||
+                        gimp_context_get_pattern (GIMP_CONTEXT (options)) != NULL,
+                        NULL);
+  g_return_val_if_fail (GIMP_IS_DRAWABLE (drawable), NULL);
+  g_return_val_if_fail (rect != NULL, NULL);
+
+  switch (gimp_fill_options_get_style (options))
+    {
+    case GIMP_FILL_STYLE_SOLID:
+      gimp_context_get_foreground (GIMP_CONTEXT (options), &color);
+      gimp_palettes_add_color_history (GIMP_CONTEXT (options)->gimp, &color);
+      gimp_pickable_srgb_to_image_color (GIMP_PICKABLE (drawable),
+                                         &color, &color);
+      break;
+
+    case GIMP_FILL_STYLE_PATTERN:
+      pattern = gimp_context_get_pattern (GIMP_CONTEXT (options));
+      break;
+    }
+
+  buffer = gegl_buffer_new (rect,
+                            gimp_drawable_get_format_with_alpha (drawable));
+
+  if (pattern)
+    {
+      GeglBuffer *pattern_buffer = gimp_pattern_create_buffer (pattern);
+
+      gegl_buffer_set_pattern (buffer, NULL, pattern_buffer, 0, 0);
+      g_object_unref (pattern_buffer);
+    }
+  else
+    {
+      GeglColor *gegl_color = gimp_gegl_color_new (&color);
+
+      gegl_buffer_set_color (buffer, NULL, gegl_color);
+      g_object_unref (gegl_color);
+    }
+
+  return buffer;
+}
diff --git a/app/core/gimpfilloptions.h b/app/core/gimpfilloptions.h
index 29374bf..62ff629 100644
--- a/app/core/gimpfilloptions.h
+++ b/app/core/gimpfilloptions.h
@@ -48,28 +48,32 @@ struct _GimpFillOptionsClass
 
 GType             gimp_fill_options_get_type         (void) G_GNUC_CONST;
 
-GimpFillOptions * gimp_fill_options_new              (Gimp               *gimp,
-                                                      GimpContext        *context,
-                                                      gboolean            use_context_color);
-
-GimpFillStyle     gimp_fill_options_get_style        (GimpFillOptions    *options);
-void              gimp_fill_options_set_style        (GimpFillOptions    *options,
-                                                      GimpFillStyle       style);
-
-gboolean          gimp_fill_options_get_antialias    (GimpFillOptions    *options);
-void              gimp_fill_options_set_antialias    (GimpFillOptions    *options,
-                                                      gboolean            antialias);
-
-gboolean          gimp_fill_options_set_by_fill_type (GimpFillOptions    *options,
-                                                      GimpContext        *context,
-                                                      GimpFillType        fill_type,
-                                                      GError            **error);
-gboolean          gimp_fill_options_set_by_fill_mode (GimpFillOptions    *options,
-                                                      GimpContext        *context,
-                                                      GimpBucketFillMode  fill_mode,
-                                                      GError            **error);
-
-const gchar     * gimp_fill_options_get_undo_desc    (GimpFillOptions    *options);
+GimpFillOptions * gimp_fill_options_new              (Gimp                *gimp,
+                                                      GimpContext         *context,
+                                                      gboolean             use_context_color);
+
+GimpFillStyle     gimp_fill_options_get_style        (GimpFillOptions     *options);
+void              gimp_fill_options_set_style        (GimpFillOptions     *options,
+                                                      GimpFillStyle        style);
+
+gboolean          gimp_fill_options_get_antialias    (GimpFillOptions     *options);
+void              gimp_fill_options_set_antialias    (GimpFillOptions     *options,
+                                                      gboolean             antialias);
+
+gboolean          gimp_fill_options_set_by_fill_type (GimpFillOptions     *options,
+                                                      GimpContext         *context,
+                                                      GimpFillType         fill_type,
+                                                      GError             **error);
+gboolean          gimp_fill_options_set_by_fill_mode (GimpFillOptions     *options,
+                                                      GimpContext         *context,
+                                                      GimpBucketFillMode   fill_mode,
+                                                      GError             **error);
+
+const gchar     * gimp_fill_options_get_undo_desc    (GimpFillOptions     *options);
+
+GeglBuffer      * gimp_fill_options_create_buffer    (GimpFillOptions     *options,
+                                                      GimpDrawable        *drawable,
+                                                      const GeglRectangle *rect);
 
 
 #endif /* __GIMP_FILL_OPTIONS_H__ */


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