[gegl/soc-2011-ops] Added plasma op, slow for big pictures



commit f4b49a68744bf696245d587c780315b11a008544
Author: Robert Sasu <sasu robert gmail com>
Date:   Mon Jun 27 14:48:14 2011 +0300

    Added plasma op, slow for big pictures

 operations/workshop/plasma.c |  294 +++---------------------------------------
 1 files changed, 21 insertions(+), 273 deletions(-)
---
diff --git a/operations/workshop/plasma.c b/operations/workshop/plasma.c
index 9ccfe1f..a476fc6 100644
--- a/operations/workshop/plasma.c
+++ b/operations/workshop/plasma.c
@@ -24,7 +24,7 @@
 
 #ifdef GEGL_CHANT_PROPERTIES
 
-gegl_chant_int (seed, _("Seed"), 0 , 10, 1,
+gegl_chant_int (seed, _("Seed"), 0 , G_MAXINT, 1,
    _("Random seed"))
 gegl_chant_double (turbulance, _("Turbulance"), 0.1, 7.0, 2,
    _("The value of the turbulance"))
@@ -74,31 +74,23 @@ add_random (GRand  *gr,
             gfloat *dest,
             gfloat  amount)
 {
-  gint i;
-  gfloat tmp;
+  gint    i;
+  gfloat  tmp;
+  gdouble rnd;
+
 
   amount /= 2;
-  amount = fmod(amount, 1);
+  rnd = fabs(fmod(amount, 1));
+
   if (amount > 0)
     for (i = 0; i < floats_per_pixel-1; i++)
        {
-          tmp = dest[i] + (gfloat) g_rand_double_range (gr, -amount, amount);
+          tmp = dest[i] + (gfloat) g_rand_double_range(gr, -rnd, rnd);
           dest[i] = CLAMP (tmp, 0, 1);
        }
 }
 
 static void
-put_pixel (gfloat *dst_buf,
-           gfloat *pixel,
-           gint    offset)
-{
-  gint i;
-  for (i = 0; i < floats_per_pixel; i++)
-     dst_buf[offset++] = pixel[i];
-}
-
-
-static void
 put_pixel_to_buffer (GeglBuffer *output,
                      gfloat     *pixel,
                      gint        x,
@@ -111,191 +103,8 @@ put_pixel_to_buffer (GeglBuffer *output,
                    GEGL_AUTO_ROWSTRIDE);
 }
 
-static void
-get_pixel (gfloat              *src_buf,
-           const GeglRectangle *src,
-           gint                 x,
-           gint                 y,
-           gfloat              *dst)
-{
-  gint i, offset;
-  offset = ((y - src->y) * src->width + x - src->x) * floats_per_pixel;
-
-  if (src_buf == NULL) printf("NULL \n");
-
-  for (i = 0; i < floats_per_pixel; i++)
-      dst[i] = src_buf[offset++];
-
-  dst[i] = dst[i];
-}
-
-
-static gboolean 
-do_plasma (gfloat              *src_buf,
-           const GeglRectangle *src,
-           gfloat              *dst_buf,
-           const GeglRectangle *dst,
-           gint                 x1,
-           gint                 y1,
-           gint                 x2,
-           gint                 y2,
-           gint                 depth,
-           gint                 scale_depth,
-           GRand               *gr,
-           GeglChantO          *op)
-{
-  gfloat *tl, *ml, *bl, *mt, *mm, *mb, *tr, *mr, *br;
-  gfloat *tmp;
-  gint    xm, ym;
-  gfloat  ran;
-  gint    offset;
-  static gint count = 0;
-
-  tl = ml = bl = mt = mm = mb = tr = mr = br = tmp
-     = g_new0 (gfloat, floats_per_pixel);  
-
-  xm = (x1 + x2) / 2;
-  ym = (y1 + y2) / 2;
-
-  if (depth == -1)
-    {
-    random_rgba (gr, tl);
-    offset = ((y1 - dst->y) * dst->width + x1 - dst->x) * 4;
-    put_pixel (dst_buf, tl, offset);
-    random_rgba (gr, tr);
-    offset = ((y1 - dst->y) * dst->width + x2 - dst->x) * 4;
-    put_pixel (dst_buf, tr, offset);
-    random_rgba (gr, bl);
-    offset = ((y2 - dst->y) * dst->width + x1 - dst->x) * 4;
-    put_pixel (dst_buf, bl, offset);
-    random_rgba (gr, br);
-    offset = ((y2 - dst->y) * dst->width + x2 - dst->x) * 4;
-    put_pixel (dst_buf, br, offset);
-    random_rgba (gr, mm);
-    offset = ((ym - dst->y) * dst->width + xm - dst->x) * 4;
-    put_pixel (dst_buf, mm, offset);
-    random_rgba (gr, ml);
-    offset = ((ym - dst->y) * dst->width + x1 - dst->x) * 4;
-    put_pixel (dst_buf, ml, offset);
-    random_rgba (gr, mr);
-    offset = ((ym - dst->y) * dst->width + x2 - dst->x) * 4;
-    put_pixel (dst_buf, mr, offset);
-    random_rgba (gr, mt);
-    offset = ((y1 - dst->y) * dst->width + xm - dst->x) * 4;
-    put_pixel (dst_buf, mt, offset);
-    random_rgba (gr, mb);
-    offset = ((y2 - dst->y) * dst->width + xm - dst->x) * 4;
-    put_pixel (dst_buf, mb, offset);
-   /*ugly but working*/
-    return FALSE;
-    }
-
-  if (!depth)
-    {
-    if (x1 == x2 && y1 == y2) return FALSE;
-
-    get_pixel (src_buf, src, x1, y1, tl);
-    get_pixel (src_buf, src, x1, y2, bl);
-    get_pixel (src_buf, src, x2, y1, tr);
-    get_pixel (src_buf, src, x2, y2, tr);
-
-    ran =  (1.0 / (2.0 * scale_depth)) * op->turbulance;
-
-    if (xm != x1 || xm != x2)
-      {
-      /*left*/
-      average_pixel (ml, tl, bl);
-      add_random (gr, ml, ran);
-      offset = ((ym - dst->y) * dst->width + x1 - dst->x) * 4;
-      put_pixel (dst_buf, ml, offset);
-
-      /*right*/      
-      if (x1 != x2)
-        {
-        average_pixel (mr, tr, br);
-        add_random (gr, mr, ran);
-        offset = ((ym - dst->y) * dst->width + x2 - dst->x) * 4;
-        put_pixel (dst_buf, mr, offset);
-        }
-      }
-
-
-    if (ym != y1 || ym != x2)
-      {
-      /*bottom*/
-      if (x1 != xm || ym != y2)
-        {
-         average_pixel (mb, bl, br);
-         add_random (gr, mb, ran);
-         offset = ((y2 - dst->y) * dst->width + xm - dst->x) *4;
-         put_pixel (dst_buf, mb, offset);
-        }
-
-      if (y1 != y2)
-        {
-         /*top*/
-         average_pixel (mt, tl, tr);
-         add_random (gr, mt, ran);
-         offset = ((y1 - dst->y)* dst->width + xm - dst->x) * 4;
-         put_pixel (dst_buf, mt, offset);
-        }
-      }
-
-    if (y1 != y2 || x1 != x2)
-      {
-       average_pixel (mm, tl, br);
-       average_pixel (tmp, bl, tr);
-       average_pixel (mm, mm, tmp);
-     
-       add_random (gr, mm, ran);
-       offset = ((ym - dst->y) * dst->width + xm - dst->x) * 4;
-       put_pixel (dst_buf, mm, offset);
-      }
-    count++;
-
-    return x2 - x1 < 3 && y2 - y1 < 3;
-    }
-
-  if (x1 < x2 || y1 < y2)
-    {
-     /*top-left*/
-     do_plasma (src_buf, src, dst_buf, dst, x1, y1, xm, ym, depth - 1,
-           scale_depth + 1, gr, op);
-     /*bottom-left*/
-     do_plasma (src_buf, src, dst_buf, dst, x1, ym, xm, y2, depth - 1,
-            scale_depth + 1, gr, op);
-     /*top-right*/
-     do_plasma (src_buf, src, dst_buf, dst, xm, y1, x2, ym, depth - 1,
-            scale_depth + 1, gr, op);
-     /*bottom-right*/
-     return do_plasma (src_buf, src, dst_buf, dst, xm, ym, x2, y2, depth - 1,
-                   scale_depth + 1, gr, op);
-    }
-  else return TRUE;
-
-  return TRUE;
-}
-
-
-static void
-init_rect_buf (GeglBuffer    *input,
-               gfloat        *src_buf,
-               GeglRectangle *roi,
-               gint           x1,
-               gint           y1,
-               gint           x2,
-               gint           y2)
-{
-   roi->x      = x1;
-   roi->y      = y1;
-   roi->width  = x2 - x1;
-   roi->height = y2 - y1;
-}
-
-
 static gboolean 
-do_plasma_big (GeglBuffer *input,
-               GeglBuffer *output,
+do_plasma_big (GeglBuffer *output,
                gint        x1,
                gint        y1,
                gint        x2,
@@ -310,11 +119,11 @@ do_plasma_big (GeglBuffer *input,
   gint    xm, ym;
   gfloat  ran;
   static gint count = 0;
-
+ /*
   GeglRectangle  roi = {0, 0, 0, 0};
   gfloat        *src_buf;
   gboolean       toreturn;
-
+  */
   tl = ml = bl = mt = mm = mb = tr = mr = br = tmp
      = g_new0 (gfloat, floats_per_pixel);  
 
@@ -357,15 +166,6 @@ do_plasma_big (GeglBuffer *input,
     {
     if (x1 == x2 && y1 == y2) return FALSE;
 
-    gegl_buffer_sample (input, x1, y1, 1.0, tl, babl_format ("RGBA float"),
-                       GEGL_INTERPOLATION_LINEAR);
-    gegl_buffer_sample (input, x1, y2, 1.0, bl, babl_format ("RGBA float"),
-                       GEGL_INTERPOLATION_LINEAR);
-    gegl_buffer_sample (input, x2, y1, 1.0, tr, babl_format ("RGBA float"),
-                       GEGL_INTERPOLATION_LINEAR);
-    gegl_buffer_sample (input, x2, y2, 1.0, br, babl_format ("RGBA float"),
-                       GEGL_INTERPOLATION_LINEAR);
-/*
     gegl_buffer_sample (output, x1, y1, 1.0, tl, babl_format ("RGBA float"),
                        GEGL_INTERPOLATION_LINEAR);
     gegl_buffer_sample (output, x1, y2, 1.0, bl, babl_format ("RGBA float"),
@@ -373,9 +173,9 @@ do_plasma_big (GeglBuffer *input,
     gegl_buffer_sample (output, x2, y1, 1.0, tr, babl_format ("RGBA float"),
                        GEGL_INTERPOLATION_LINEAR);
     gegl_buffer_sample (output, x2, y2, 1.0, br, babl_format ("RGBA float"),
-                       GEGL_INTERPOLATION_LINEAR);*/
+                       GEGL_INTERPOLATION_LINEAR);
 
-    ran =  (1.0 / (2.0 * scale_depth)) * op->turbulance;
+    ran =  ((gfloat) op->turbulance / (2.0 * scale_depth));
 
     if (xm != x1 || xm != x2)
       {
@@ -429,71 +229,18 @@ do_plasma_big (GeglBuffer *input,
 
   if (x1 < x2 || y1 < y2)
     {
-     if (x2 - x1 < MANUAL_ROI_VAL && y2 - y1 < MANUAL_ROI_VAL)
-        {
-        /*top-left*/
-        src_buf = NULL;
-        init_rect_buf (output, src_buf, &roi, x1, y1, xm, ym);
-     
-        src_buf = g_new0 (gfloat, (roi.width + 1) * (roi.height + 1) * floats_per_pixel);
-        gegl_buffer_get (input, 1.0, &roi, babl_format ("RGBA float"), src_buf, 
-                    GEGL_AUTO_ROWSTRIDE);
-
-        do_plasma (src_buf, &roi, src_buf, &roi, x1, y1, xm, ym, depth - 1,
-              scale_depth + 1, gr, op);
-        gegl_buffer_set (output, &roi, babl_format ("RGBA float"), src_buf,
-                        GEGL_AUTO_ROWSTRIDE);
-        /*bottom-left*/
-        init_rect_buf (output, src_buf, &roi, x1, ym, xm, y2);
-     
-        src_buf = g_new0 (gfloat, (roi.width + 1) * (roi.height + 1) * floats_per_pixel);
-        gegl_buffer_get (input, 1.0, &roi, babl_format ("RGBA float"), src_buf, 
-                    GEGL_AUTO_ROWSTRIDE);
-
-        do_plasma (src_buf, &roi, src_buf, &roi, x1, ym, xm, y2, depth - 1,
-               scale_depth + 1, gr, op);
-        gegl_buffer_set (output, &roi, babl_format ("RGBA float"), src_buf,
-                        GEGL_AUTO_ROWSTRIDE);
-        /*top-right*/
-        init_rect_buf (output, src_buf, &roi, xm, y1, x2, ym);
-     
-        src_buf = g_new0 (gfloat, (roi.width + 1) * (roi.height + 1) * floats_per_pixel);
-        gegl_buffer_get (input, 1.0, &roi, babl_format ("RGBA float"), src_buf, 
-                    GEGL_AUTO_ROWSTRIDE);
-
-        do_plasma (src_buf, &roi, src_buf, &roi, xm, y1, x2, ym, depth - 1,
-               scale_depth + 1, gr, op);
-        gegl_buffer_set (output, &roi, babl_format ("RGBA float"), src_buf,
-                        GEGL_AUTO_ROWSTRIDE);
-        /*bottom-right*/
-        init_rect_buf (output, src_buf, &roi, xm, ym, x2, y2); 
-     
-        src_buf = g_new0 (gfloat, (roi.width + 1) * (roi.height + 1) * floats_per_pixel);
-        gegl_buffer_get (input, 1.0, &roi, babl_format ("RGBA float"), src_buf, 
-                    GEGL_AUTO_ROWSTRIDE);
-
-        toreturn = do_plasma (src_buf, &roi, src_buf, &roi, xm, ym, x2, y2, depth - 1,
-                              scale_depth + 1, gr, op); 
-        gegl_buffer_set (output, &roi, babl_format ("RGBA float"), src_buf,
-                        GEGL_AUTO_ROWSTRIDE);
-   
-        return toreturn;
-        }
-     else 
-        {
         /*top-left*/
-        do_plasma_big (input, output, x1, y1, xm, ym, depth - 1,
+        do_plasma_big (output, x1, y1, xm, ym, depth - 1,
                   scale_depth + 1, gr, op);
         /*bottom-left*/
-        do_plasma_big (input, output, x1, ym, xm, y2, depth - 1,
+        do_plasma_big (output, x1, ym, xm, y2, depth - 1,
                   scale_depth + 1, gr, op);
         /*top-right*/
-        do_plasma_big (input, output, xm, y1, x2, ym, depth - 1,
+        do_plasma_big (output, xm, y1, x2, ym, depth - 1,
                   scale_depth + 1, gr, op);
         /*bottom-right*/
-        return do_plasma_big (input, output, xm, ym, x2, y2, depth - 1,
+        return do_plasma_big (output, xm, ym, x2, y2, depth - 1,
                          scale_depth + 1, gr, op); 
-        }
     }
   else return TRUE;
 
@@ -536,14 +283,15 @@ process (GeglOperation       *operation,
   x = boundary.x + boundary.width;
   y = boundary.y + boundary.height;
 
-  do_plasma_big (output, output, boundary.x, boundary.y, x-1, y-1, -1,
+  do_plasma_big (output, boundary.x, boundary.y, x-1, y-1, -1,
                  0, gr, o);
    /*
     * Now we recurse through the images, going deeper each time
     */
   depth = 1;
-  while (!do_plasma_big (output, output, boundary.x, boundary.y, x-1, 
-                         y-1, depth, 0, gr, o))
+  while (!do_plasma_big (output, boundary.x, boundary.y, x-1, 
+                       y-1, depth, 0, gr, o))
+   
           depth++;
 
   return TRUE;



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