[gegl] ripple: actually allow m/n == 0, fix a division by zero issue.



commit 92643721312f7ec76f437a6cfc09cdb537903007
Author: Simon Budig <simon budig de>
Date:   Wed Apr 25 02:00:15 2018 +0200

    ripple: actually allow m/n == 0, fix a division by zero issue.

 operations/common-gpl3+/ripple.c |   22 +++++++++++++++-------
 1 files changed, 15 insertions(+), 7 deletions(-)
---
diff --git a/operations/common-gpl3+/ripple.c b/operations/common-gpl3+/ripple.c
index 09399d2..17dcb1f 100644
--- a/operations/common-gpl3+/ripple.c
+++ b/operations/common-gpl3+/ripple.c
@@ -121,20 +121,27 @@ process (GeglOperation       *operation,
     {
       gint w, h;
       gdouble n, m;
+      GeglRectangle *bbox;
 
-      w = gegl_buffer_get_width (input);
-      h = gegl_buffer_get_height (input);
+      bbox = gegl_operation_source_get_bounding_box (operation, "input");
+      w = bbox->width;
+      h = bbox->height;
 
       n = cos (angle_rad) * w / period;
       m = sin (angle_rad) * h / period;
 
-      /* round away from zero but ensure a nonzero result */
-      n = n < 0 ? MIN (round (n), -1.0) : MAX (round (n), 1.0);
-      m = m < 0 ? MIN (round (m), -1.0) : MAX (round (m), 1.0);
+      /* round away from zero */
+      n = round (n);
+      m = round (m);
+      if (n == 0 && m == 0)
+        {
+          n = 1;
+          amplitude = 0;
+        }
 
       /* magic! */
       angle_rad = atan2 (m * w, h * n);
-      period = cos (angle_rad) * w / n;
+      period = sqrt ((gdouble) h*h * w*w / (n*n * h*h + m*m * w*w));
 
       /* ok, not actually.
        *
@@ -155,7 +162,8 @@ process (GeglOperation       *operation,
        * slightly modified new angle/period that meets these criteria.
        *
        * We determine the angle by computing tan(), thereby eliminating
-       * the period, then determining the period.
+       * the period, then determining the period via a formula
+       * derived from the  sinĀ²(a)+cosĀ²(a) = 1  identity.
        */
     }
 


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