[gegl] tile-paper: some improvements



commit 16a86f0581438806577255778a0d3c3d8ad50a04
Author: Thomas Manni <thomas manni free fr>
Date:   Tue Mar 14 13:34:26 2017 +0100

    tile-paper: some improvements
    
    Use FILTER base class instead of AREA_FILTER since the whole output
    buffer is processed independently of the roi.
    Use buffer iterator to fill the background instead
    of allocating memory for the whole output buffer.

 operations/common/tile-paper.c |  119 +++++++++++++++------------------------
 1 files changed, 46 insertions(+), 73 deletions(-)
---
diff --git a/operations/common/tile-paper.c b/operations/common/tile-paper.c
index b7fe192..02bfabe 100644
--- a/operations/common/tile-paper.c
+++ b/operations/common/tile-paper.c
@@ -21,7 +21,6 @@
 
 #include "config.h"
 #include <glib/gi18n-lib.h>
-#include <stdlib.h>
 
 
 #ifdef GEGL_PROPERTIES
@@ -83,13 +82,12 @@ property_seed (seed, _("Random seed"), rand)
 
 #else
 
-#define GEGL_OP_AREA_FILTER
+#define GEGL_OP_FILTER
 #define GEGL_OP_NAME     tile_paper
 #define GEGL_OP_C_SOURCE tile-paper.c
 
 #include "gegl-op.h"
 #include <math.h>
-#include <stdio.h>
 
 
 typedef struct _Tile
@@ -131,14 +129,6 @@ random_move (gint             tile_x,
 static void
 prepare (GeglOperation *operation)
 {
-  GeglOperationAreaFilter *op_area = GEGL_OPERATION_AREA_FILTER (operation);
-  GeglProperties          *o       = GEGL_PROPERTIES (operation);
-
-  op_area->left   = o->tile_width;
-  op_area->right  = o->tile_width;
-  op_area->top    = o->tile_height;
-  op_area->bottom = o->tile_height;
-
   gegl_operation_set_format (operation, "input",  babl_format ("RGBA float"));
   gegl_operation_set_format (operation, "output", babl_format ("RGBA float"));
 }
@@ -293,71 +283,57 @@ set_background (GeglProperties      *o,
                 gint                 offset_x,
                 gint                 offset_y)
 {
-  const Babl *format;
-  gfloat     *dest_buf;
-  gfloat     *buf;
-  gint        x;
-  gint        y;
-  gint        index;
-  gint        clear_x0;
-  gint        clear_y0;
-  gint        clear_x1;
-  gint        clear_y1;
+  const Babl *format = babl_format ("RGBA float");
 
-  format = babl_format ("RGBA float");
-  dest_buf = g_new0 (gfloat, 4 * rect->width * rect->height);
-
-  if (o->fractional_type == GEGL_FRACTIONAL_TYPE_IGNORE)
+  if (o->background_type == GEGL_BACKGROUND_TYPE_TRANSPARENT)
     {
-      clear_x0     = offset_x;
-      clear_y0     = offset_y;
-      clear_x1     = clear_x0 + o->tile_width * (rect->width / o->tile_width);
-      clear_y1     = clear_y0 + o->tile_height * (rect->height / o->tile_height);
+      GeglColor *color = gegl_color_new ("rgba(0.0,0.0,0.0,0.0)");
+      gegl_buffer_set_color (output, rect, color);
+      g_object_unref (color);
     }
-  else
+  else if (o->background_type == GEGL_BACKGROUND_TYPE_COLOR)
     {
-      clear_x0     = 0;
-      clear_y0     = 0;
-      clear_x1     = clear_x0 + rect->width;
-      clear_y1     = clear_y0 + rect->height;
+      gegl_buffer_set_color (output, rect, o->bg_color);
     }
-
-  switch (o->background_type)
+  else if (o->background_type == GEGL_BACKGROUND_TYPE_IMAGE)
+    {
+      gegl_buffer_copy (input, NULL, GEGL_ABYSS_NONE, output, NULL);
+    }
+  else
     {
-    case GEGL_BACKGROUND_TYPE_TRANSPARENT:
-      gegl_buffer_set_color (output, rect,
-                             gegl_color_new ("rgba(0.0,0.0,0.0,0.0)"));
-      break;
+      /* GEGL_BACKGROUND_TYPE_INVERT */
 
-    case GEGL_BACKGROUND_TYPE_COLOR:
-      gegl_buffer_set_color (output, rect, o->bg_color);
-      break;
+      GeglBufferIterator  *iter;
+      GeglRectangle        clear = *rect;
+
+      if (o->fractional_type == GEGL_FRACTIONAL_TYPE_IGNORE)
+        {
+          clear.x      = offset_x;
+          clear.y      = offset_y;
+          clear.width  = o->tile_width * (rect->width / o->tile_width);
+          clear.height = o->tile_height * (rect->height / o->tile_height);
+        }
+
+      iter = gegl_buffer_iterator_new (input, &clear, 0, format,
+                                       GEGL_ACCESS_READ, GEGL_ABYSS_NONE);
+      gegl_buffer_iterator_add (iter, output, &clear, 0, format,
+                                GEGL_ACCESS_WRITE, GEGL_ABYSS_NONE);
 
-    case GEGL_BACKGROUND_TYPE_INVERT:
-      gegl_buffer_get (input, rect, 1.0, format, dest_buf,
-                       GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
-      buf = dest_buf;
-      for (y = clear_y0; y < clear_y1; y++)
+      while (gegl_buffer_iterator_next (iter))
         {
-          for (x = clear_x0; x < clear_x1; x++)
+          gfloat  *src      = iter->data[0];
+          gfloat  *dst      = iter->data[1];
+          glong    n_pixels = iter->length;
+
+          while (n_pixels--)
             {
-              index         = 4 * (y * rect->width + x);
-              buf[index]    = 1 - buf[index];
-              buf[index+1]  = 1 - buf[index+1];
-              buf[index+2]  = 1 - buf[index+2];
+              *dst++ = 1.f - *src++;
+              *dst++ = 1.f - *src++;
+              *dst++ = 1.f - *src++;
+              *dst++ = *src++;
             }
         }
-      gegl_buffer_set (output, rect, 0, format, dest_buf, GEGL_AUTO_ROWSTRIDE);
-      break;
-
-    case GEGL_BACKGROUND_TYPE_IMAGE:
-      gegl_buffer_get (input, rect, 1.0, format, dest_buf,
-                       GEGL_AUTO_ROWSTRIDE, GEGL_ABYSS_NONE);
-      gegl_buffer_set (output, rect, 0, format, dest_buf, GEGL_AUTO_ROWSTRIDE);
-      break;
     }
-
-  g_free (dest_buf);
 }
 
 static gboolean
@@ -383,9 +359,6 @@ process (GeglOperation       *operation,
 
   if (o->fractional_type == GEGL_FRACTIONAL_TYPE_FORCE)
     {
-      if (0 < result->width  % o->tile_width) division_x++;
-      if (0 < result->height % o->tile_height) division_y++;
-
       if (o->centering)
         {
           if (1 < result->width % o->tile_width)
@@ -411,15 +384,18 @@ process (GeglOperation       *operation,
     }
 
   n_tiles = division_x * division_y;
-  tiles = g_new(Tile, n_tiles);
+  tiles   = g_new (Tile, n_tiles);
 
   randomize_tiles (o, result,  division_x, division_y,
                    offset_x, offset_y, n_tiles, tiles);
+
   set_background  (o, result, input, output, division_x, division_y,
                    offset_x, offset_y);
 
   draw_tiles (o, result, input, output, n_tiles, tiles);
 
+  g_free (tiles);
+
   return  TRUE;
 }
 
@@ -433,9 +409,7 @@ get_bounding_box (GeglOperation *operation)
   if (! in_rect)
     return result;
 
-  gegl_rectangle_copy (&result, in_rect);
-
-  return result;
+  return *in_rect;
 }
 
 /* Compute the input rectangle required to compute the specified
@@ -446,15 +420,14 @@ get_required_for_output (GeglOperation       *operation,
                          const gchar         *input_pad,
                          const GeglRectangle *roi)
 {
-  GeglRectangle  result = get_bounding_box (operation);
-  return result;
+  return get_bounding_box (operation);
 }
 
 static GeglRectangle
 get_cached_region (GeglOperation       *operation,
                    const GeglRectangle *roi)
 {
-  return *gegl_operation_source_get_bounding_box (operation, "input");
+  return get_bounding_box (operation);
 }
 
 static void


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