[gegl] operations: A faster gegl:checkerboard



commit 27d9e45d226812d3f8fafdb4868c02ed9765f4c4
Author: Daniel Sabo <DanielSabo gmail com>
Date:   Sun Oct 13 21:04:42 2013 -0700

    operations: A faster gegl:checkerboard

 operations/common/checkerboard.c |   92 ++++++++++++++++++--------------------
 1 files changed, 44 insertions(+), 48 deletions(-)
---
diff --git a/operations/common/checkerboard.c b/operations/common/checkerboard.c
index 3e0ae56..0a21929 100644
--- a/operations/common/checkerboard.c
+++ b/operations/common/checkerboard.c
@@ -65,6 +65,11 @@ get_bounding_box (GeglOperation *operation)
   return gegl_rectangle_infinite_plane ();
 }
 
+#define TILE_INDEX(coordinate,stride) \
+  (((coordinate) >= 0)?\
+      (coordinate) / (stride):\
+      ((((coordinate) + 1) /(stride)) - 1))
+
 static gboolean
 process (GeglOperation       *operation,
          void                *out_buf,
@@ -73,71 +78,62 @@ process (GeglOperation       *operation,
          gint                 level)
 {
   GeglChantO *o = GEGL_CHANT_PROPERTIES (operation);
+  const Babl *out_format = babl_format ("RGBA float");
   gfloat     *out_pixel = out_buf;
   gfloat      color1[4];
   gfloat      color2[4];
-  gint        x = roi->x; /* initial x                   */
-  gint        y = roi->y; /*           and y coordinates */
-
-  gegl_color_get_pixel (o->color1, babl_format ("RGBA float"), color1);
-  gegl_color_get_pixel (o->color2, babl_format ("RGBA float"), color2);
+  gint        y;
+  gint        x;
+  const gint  x_min = roi->x - o->x_offset;
+  const gint  y_min = roi->y - o->y_offset;
+  const gint  x_max = roi->x + roi->width - o->x_offset;
+  const gint  y_max = roi->y + roi->height - o->y_offset;
 
-  while (n_pixels--)
-    {
-      gint nx,ny;
+  const gint  square_width  = o->x;
+  const gint  square_height = o->y;
 
-      if ((x - o->x_offset) < 0)
-        {
-          nx = div (x - o->x_offset + 1, o->x).quot;
-        }
-      else
-        {
-          nx = div (x - o->x_offset, o->x).quot;
-        }
+  gegl_color_get_pixel (o->color1, out_format, color1);
+  gegl_color_get_pixel (o->color2, out_format, color2);
 
-      if ((y - o->y_offset) < 0)
-        {
-          ny = div (y - o->y_offset + 1, o->y).quot;
-        }
-      else
-        {
-          ny = div (y - o->y_offset, o->y).quot;
-        }
+  for (y = y_min; y < y_max; y++)
+    {
+      x = x_min;
 
-      /* shift negative cell indices */
-      nx -= (x - o->x_offset) < 0 ? 1 : 0;
-      ny -= (y - o->y_offset) < 0 ? 1 : 0;
+      gfloat *cur_color;
 
-      if ( (nx+ny) % 2 == 0)
-        {
-          out_pixel[0]=color1[0];
-          out_pixel[1]=color1[1];
-          out_pixel[2]=color1[2];
-          out_pixel[3]=color1[3];
-        }
+      /* Figure out which box we're in */
+      gint tilex = TILE_INDEX (x, square_width);
+      gint tiley = TILE_INDEX (y, square_height);
+      if ((tilex + tiley) % 2 == 0)
+        cur_color = color1;
       else
-        {
-          out_pixel[0]=color2[0];
-          out_pixel[1]=color2[1];
-          out_pixel[2]=color2[2];
-          out_pixel[3]=color2[3];
-        }
+        cur_color = color2;
 
-      out_pixel += 4;
-
-      /* update x and y coordinates */
-      x++;
-      if (x>=roi->x + roi->width)
+      while (x < x_max)
         {
-          x=roi->x;
-          y++;
+          /* Figure out how long this stripe is */
+          gint stripe_end = (TILE_INDEX (x, square_width) + 1) * square_width;
+               stripe_end = stripe_end > x_max ? x_max : stripe_end;
+
+          while (x < stripe_end)
+            {
+              *out_pixel++ = cur_color[0];
+              *out_pixel++ = cur_color[1];
+              *out_pixel++ = cur_color[2];
+              *out_pixel++ = cur_color[3];
+              x++;
+            }
+
+          if (cur_color == color1)
+            cur_color = color2;
+          else
+            cur_color = color1;
         }
     }
 
   return  TRUE;
 }
 
-
 static void
 gegl_chant_class_init (GeglChantClass *klass)
 {


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