[gimp/goat-invasion: 70/401] app: add a flatten meta-op and use it for layers and channels



commit 1ec35706ac13129aaa188f6f233d9bf0a0467bfe
Author: Michael Natterer <mitch gimp org>
Date:   Fri Mar 16 12:49:34 2012 +0100

    app: add a flatten meta-op and use it for layers and channels

 app/core/gimpchannel.c     |   26 +++++++---------
 app/core/gimplayer.c       |   42 ++++++++------------------
 app/gegl/Makefile.am       |    2 +
 app/gegl/gimp-gegl-nodes.c |   69 ++++++++++++++++++++++++++++++++++++++++++++
 app/gegl/gimp-gegl-nodes.h |   28 ++++++++++++++++++
 app/gegl/gimp-gegl-utils.c |   10 ++++++
 app/gegl/gimp-gegl-utils.h |    3 ++
 7 files changed, 136 insertions(+), 44 deletions(-)
---
diff --git a/app/core/gimpchannel.c b/app/core/gimpchannel.c
index 2e204ab..16fd6d7 100644
--- a/app/core/gimpchannel.c
+++ b/app/core/gimpchannel.c
@@ -37,6 +37,8 @@
 #include "paint/gimppaintcore-stroke.h"
 #include "paint/gimppaintoptions.h"
 
+#include "gegl/gimp-gegl-nodes.h"
+
 #include "gimp.h"
 #include "gimp-utils.h"
 #include "gimpcontainer.h"
@@ -456,27 +458,21 @@ gimp_channel_convert (GimpItem  *item,
 
   if (gimp_drawable_has_alpha (drawable))
     {
+      GeglNode    *flatten;
       TileManager *new_tiles;
-      PixelRegion  srcPR;
-      PixelRegion  destPR;
-      guchar       bg[1] = { 0 };
+      GimpRGB      background;
 
       new_tiles = tile_manager_new (gimp_item_get_width (item),
                                     gimp_item_get_height (item),
                                     GIMP_IMAGE_TYPE_BYTES (GIMP_GRAY_IMAGE));
 
-      pixel_region_init (&srcPR, gimp_drawable_get_tiles (drawable),
-                         0, 0,
-                         gimp_item_get_width  (item),
-                         gimp_item_get_height (item),
-                         FALSE);
-      pixel_region_init (&destPR, new_tiles,
-                         0, 0,
-                         gimp_item_get_width (item),
-                         gimp_item_get_height (item),
-                         TRUE);
-
-      flatten_region (&srcPR, &destPR, bg);
+      gimp_rgba_set (&background, 0.0, 0.0, 0.0, 0.0);
+      flatten = gimp_gegl_create_flatten_node (&background);
+
+      gimp_drawable_apply_operation_to_tiles (drawable, NULL, NULL,
+                                              flatten, TRUE, new_tiles);
+
+      g_object_unref (flatten);
 
       gimp_drawable_set_tiles_full (drawable, FALSE, NULL,
                                     new_tiles, GIMP_GRAY_IMAGE,
diff --git a/app/core/gimplayer.c b/app/core/gimplayer.c
index 3b464b2..d72819b 100644
--- a/app/core/gimplayer.c
+++ b/app/core/gimplayer.c
@@ -34,12 +34,14 @@
 
 #include "paint-funcs/paint-funcs.h"
 
+#include "gegl/gimp-gegl-nodes.h"
 #include "gegl/gimp-gegl-utils.h"
 
 #include "gimpchannel-select.h"
 #include "gimpcontext.h"
 #include "gimpcontainer.h"
 #include "gimpdrawable-convert.h"
+#include "gimpdrawable-operation.h"
 #include "gimperror.h"
 #include "gimpimage-undo-push.h"
 #include "gimpimage-undo.h"
@@ -1914,12 +1916,10 @@ void
 gimp_layer_flatten (GimpLayer   *layer,
                     GimpContext *context)
 {
-  GimpItem      *item;
-  GimpDrawable  *drawable;
-  PixelRegion    srcPR, destPR;
+  GeglNode      *flatten;
   TileManager   *new_tiles;
   GimpImageType  new_type;
-  guchar         bg[4];
+  GimpRGB        background;
 
   g_return_if_fail (GIMP_IS_LAYER (layer));
   g_return_if_fail (GIMP_IS_CONTEXT (context));
@@ -1927,36 +1927,20 @@ gimp_layer_flatten (GimpLayer   *layer,
   if (! gimp_drawable_has_alpha (GIMP_DRAWABLE (layer)))
     return;
 
-  item     = GIMP_ITEM (layer);
-  drawable = GIMP_DRAWABLE (layer);
+  new_type = gimp_drawable_type_without_alpha (GIMP_DRAWABLE (layer));
 
-  new_type = gimp_drawable_type_without_alpha (drawable);
-
-  gimp_image_get_background (gimp_item_get_image (item), context,
-                             gimp_drawable_type (drawable),
-                             bg);
-
-  /*  Allocate the new tiles  */
-  new_tiles = tile_manager_new (gimp_item_get_width  (item),
-                                gimp_item_get_height (item),
+  new_tiles = tile_manager_new (gimp_item_get_width  (GIMP_ITEM (layer)),
+                                gimp_item_get_height (GIMP_ITEM (layer)),
                                 GIMP_IMAGE_TYPE_BYTES (new_type));
 
-  /*  Configure the pixel regions  */
-  pixel_region_init (&srcPR, gimp_drawable_get_tiles (drawable),
-                     0, 0,
-                     gimp_item_get_width  (item),
-                     gimp_item_get_height (item),
-                     FALSE);
-  pixel_region_init (&destPR, new_tiles,
-                     0, 0,
-                     gimp_item_get_width  (item),
-                     gimp_item_get_height (item),
-                     TRUE);
+  gimp_context_get_background (context, &background);
+  flatten = gimp_gegl_create_flatten_node (&background);
+
+  gimp_drawable_apply_operation_to_tiles (GIMP_DRAWABLE (layer), NULL, NULL,
+                                          flatten, TRUE, new_tiles);
 
-  /*  Remove alpha channel  */
-  flatten_region (&srcPR, &destPR, bg);
+  g_object_unref (flatten);
 
-  /*  Set the new tiles  */
   gimp_drawable_set_tiles (GIMP_DRAWABLE (layer),
                            gimp_item_is_attached (GIMP_ITEM (layer)),
                            C_("undo-type", "Remove Alpha Channel"),
diff --git a/app/gegl/Makefile.am b/app/gegl/Makefile.am
index ce4da5e..f562e3b 100644
--- a/app/gegl/Makefile.am
+++ b/app/gegl/Makefile.am
@@ -20,6 +20,8 @@ libappgegl_a_sources = \
 	gimp-gegl-types.h			\
 	gimp-gegl.c				\
 	gimp-gegl.h				\
+	gimp-gegl-nodes.c			\
+	gimp-gegl-nodes.h			\
 	gimp-gegl-utils.c			\
 	gimp-gegl-utils.h			\
 	gimptilebackendtilemanager.c		\
diff --git a/app/gegl/gimp-gegl-nodes.c b/app/gegl/gimp-gegl-nodes.c
new file mode 100644
index 0000000..9440667
--- /dev/null
+++ b/app/gegl/gimp-gegl-nodes.c
@@ -0,0 +1,69 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * gimp-gegl-nodes.h
+ * Copyright (C) 2012 Michael Natterer <mitch gimp org>
+ *
+ * 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 <gegl.h>
+
+#include "gimp-gegl-types.h"
+
+#include "gimp-gegl-nodes.h"
+#include "gimp-gegl-utils.h"
+
+
+GeglNode *
+gimp_gegl_create_flatten_node (const GimpRGB *background)
+{
+  GeglNode  *node;
+  GeglNode  *input;
+  GeglNode  *output;
+  GeglNode  *color;
+  GeglNode  *over;
+  GeglColor *c;
+
+  g_return_val_if_fail (background != NULL, NULL);
+
+  node = gegl_node_new ();
+
+  input  = gegl_node_get_input_proxy  (node, "input");
+  output = gegl_node_get_output_proxy (node, "output");
+
+  c = gegl_color_new (NULL);
+  gimp_gegl_color_set_rgba (c, background);
+
+  color = gegl_node_new_child (node,
+                               "operation", "gegl:color",
+                               "value",     c,
+                               NULL);
+  g_object_unref (c);
+
+  over = gegl_node_new_child (node,
+                              "operation", "gegl:over",
+                              NULL);
+
+  gegl_node_connect_to (input,  "output",
+                        over,   "aux");
+  gegl_node_connect_to (color,  "output",
+                        over,   "input");
+  gegl_node_connect_to (over,   "output",
+                        output, "input");
+
+  return node;
+}
diff --git a/app/gegl/gimp-gegl-nodes.h b/app/gegl/gimp-gegl-nodes.h
new file mode 100644
index 0000000..792c9dd
--- /dev/null
+++ b/app/gegl/gimp-gegl-nodes.h
@@ -0,0 +1,28 @@
+/* GIMP - The GNU Image Manipulation Program
+ * Copyright (C) 1995 Spencer Kimball and Peter Mattis
+ *
+ * gimp-gegl-nodes.h
+ * Copyright (C) 2012 Michael Natterer <mitch gimp org>
+ *
+ * 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/>.
+ */
+
+#ifndef __GIMP_GEGL_NODES_H__
+#define __GIMP_GEGL_NODES_H__
+
+
+GeglNode * gimp_gegl_create_flatten_node (const GimpRGB *background);
+
+
+#endif /* __GIMP_GEGL_NODES_H__ */
diff --git a/app/gegl/gimp-gegl-utils.c b/app/gegl/gimp-gegl-utils.c
index b01ea4d..163a53e 100644
--- a/app/gegl/gimp-gegl-utils.c
+++ b/app/gegl/gimp-gegl-utils.c
@@ -178,3 +178,13 @@ gimp_gegl_buffer_refetch_tiles (GeglBuffer *buffer)
 
   gegl_tile_source_reinit (GEGL_TILE_SOURCE (buffer));
 }
+
+void
+gimp_gegl_color_set_rgba (GeglColor     *color,
+                          const GimpRGB *rgb)
+{
+  g_return_if_fail (GEGL_IS_COLOR (color));
+  g_return_if_fail (rgb != NULL);
+
+  gegl_color_set_rgba (color, rgb->r, rgb->g, rgb->b, rgb->a);
+}
diff --git a/app/gegl/gimp-gegl-utils.h b/app/gegl/gimp-gegl-utils.h
index 01e5b8b..1482704 100644
--- a/app/gegl/gimp-gegl-utils.h
+++ b/app/gegl/gimp-gegl-utils.h
@@ -35,5 +35,8 @@ GeglBuffer  * gimp_tile_manager_create_buffer   (TileManager           *tm,
 
 void          gimp_gegl_buffer_refetch_tiles    (GeglBuffer            *buffer);
 
+void          gimp_gegl_color_set_rgba          (GeglColor     *color,
+                                                 const GimpRGB *rgb);
+
 
 #endif /* __GIMP_GEGL_UTILS_H__ */



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